// JavaScript Document

function showPhotoGalleryPage(n)
{
	$$('.photo_gallery_thumbs').each(function(e){
		e.hide();
	});
	$('photo_gallery_thumbs_'+n).show();
	$$('.photo_gallery_page').each(function(e){
		e.removeClassName('selected');
		if (e.readAttribute('title') == n) { e.addClassName('selected'); }
	});
}

function showPreviousImage()
{
	if (photoGalleryImageCount == 0) return false;
	currentImage--;
	if (currentImage < 0) { currentImage = photoGalleryImageCount - 1; }
	showImage(currentImage);
}

function showNextImage()
{
	if (photoGalleryImageCount == 0) return false;
	currentImage++;
	if (currentImage >= photoGalleryImageCount) { currentImage = 0; }
	showImage(currentImage);
}

function showImage(n)
{
	$$('.photo_gallery_thumb').each(function(i){
		i.removeClassName('selected');
	});

	var e = $('photo_gallery_thumb_'+n);
	e.addClassName('selected');
	var id = e.readAttribute('rel');
	var content = galleryContent[id];

	//Update Image and Copy
	if (content.type == 'youtube' || content.type=='video')
	{
	  if(content.subtype == 'aspect'){
		var p = content;
		var file=p.filename.replace(/\/$/,"");
		if(file.substr(file.length-3,file.length)=="swf"){
		  var aspectVideo='<embed id="swfplayer" width="550" height="310" src="'+file+'"></embed>';
			$('photo_gallery_image').update(aspectVideo);
		} else {
		  var fileNames=p.image.split(".");
			var fileName="";
			for(var it=0; it<fileNames.length; it++){
			  if(it==fileNames.length-1){
				  fileName+="_lg."+fileNames[it];
				} else {
				  if(it>0){
					  fileName+=".";
					}
				  fileName+=fileNames[it];
				}
			}
			var share=document.location.href;
			var shareUrl=document.location.href.split("/");
			if(shareUrl[shareUrl.length-1]!=""){
			  if(p.vanity!=shareUrl[shareUrl.length-1]){
				  share+="/"+p.vanity;
				}
			} else {
			  if(p.vanity!=shareUrl[shareUrl.length-2]){
				  share+=p.vanity;
				}
			}
		  var aspectVideo = '<object id="swfplayer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="550" height="310">';
		  aspectVideo += '<param name="movie" value="assets/no-index/Starwood-YTplayer.swf" />';
		  aspectVideo += '<param name="quality" value="high" /><param name="wmode" value="opaque" /><param name="swfversion" value="8.0.35.0" />';
		  aspectVideo += '<param name="expressinstall" value="assets/no-index/expressInstall.swf" />';
		  aspectVideo += '<param name="flashvars" value="MediaLink=http://www.youtube.com/watch?v='+p.filename+'&amp;MediaLink2=http://www.youtube.com/watch?v='+p.filename+'&amp;hd=1&amp;image=assets/gallery/'+fileName+'&amp;htmlpath='+share+'&amp;CTR='+share+'&amp;backgroundBarColor=0x'+aspectPlayerColor+'&amp;outputBkgColor=0x'+aspectPlayerColor+'&amp;TwitterCTR=gallery/?share=twitter_'+share+'&amp;FacebookCTR=gallery/?share=facebook_'+share+'" />';
		  aspectVideo += '<param name="allowfullscreen" value="true" />';
		  aspectVideo += '<!--[if !IE]>-->';
		  aspectVideo += '<object type="application/x-shockwave-flash" data="assets/no-index/Starwood-YTplayer.swf" width="550" height="310">';
		  aspectVideo += '<!--<![endif]-->';
		  aspectVideo += '<param name="quality" value="high" /><param name="wmode" value="opaque" /><param name="swfversion" value="8.0.35.0" /><param name="expressinstall" value="assets/no-index/expressInstall.swf" />';
		  aspectVideo += '<param name="flashvars" value="MediaLink=http://www.youtube.com/watch?v='+p.filename+'&amp;MediaLink2=http://www.youtube.com/watch?v='+p.filename+'&amp;hd=1&amp;image=assets/gallery/'+fileName+'&amp;htmlpath='+share+'&amp;CTR='+share+'&amp;backgroundBarColor=0x'+aspectPlayerColor+'&amp;outputBkgColor=0x'+aspectPlayerColor+'&amp;TwitterCTR=gallery/?share=twitter_'+share+'&amp;FacebookCTR=gallery/?share=facebook_'+share+'" />';
		  aspectVideo += '<param name="allowfullscreen" value="true" />';
		  aspectVideo += '<div><h4>Content on this page requires a newer version of Adobe Flash Player.</h4><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" width="112" height="33" /></a></p></div>';
		  aspectVideo += '<!--[if !IE]>-->';
		  aspectVideo += '</object>';
		  aspectVideo += '<!--<![endif]-->';
		  aspectVideo += '</object>';
		  $('photo_gallery_image').update(aspectVideo);
		  swfobject.registerObject('swfplayer');
		  $('swfplayer').setStyle({'visibility':'visible'});
		}
                }
	  else {
		$('photo_gallery_image').update('<embed src="http://www.youtube.com/v/'+content.filename+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="550" height="369"></embed>');
	  }
	} else {
		$('photo_gallery_image').update('<img width="550" height="369" src="'+galleryRoot+content.filename+'_lg.jpg" alt="'+content.title+'" title="'+content.title+'" />');
	}
	$('photo_gallery_title').update(content.title);
	$('photo_gallery_copy').update(content.copy);

	//Update CTA Button
	$('photo_cta_button').update(content.cta_text);
	$('photo_cta_button').writeAttribute('href', content.cta_url);
	if ((!content.cta_text) || (content.cta_text.blank()))
	{
		$('photo_cta_button').hide();
	} else {
		$('photo_cta_button').show();
	}

	clearTags(); //Clear existing tags

	//Create the tags
	content.tags.each(function(t){
		var tag = createTag(t);

		if (!$('photo_gallery_showtags').checked)
		{
			tag.hide();
		}

		$('photo_gallery_image').insert(tag);

		var h = tag.getHeight();
		var w = tag.getWidth();

                t.top = (t.y - h);

		if (t.x > ($('photo_gallery_image').getWidth() / 2))
		{
			t.left = (t.x - w + galleryTagOffsetLeft);
			tag.addClassName('right');
		} else {
			t.left = (t.x - galleryTagOffsetLeft);
			tag.addClassName('left');
		}

		tag.setStyle({
			'top': t.top+'px',
			'left': t.left+'px'
		});
	});

	//Change page if required
	showPhotoGalleryPage(Math.floor(n / photoGallerPerPage) + 1);
}

function toggleTags()
{
	$$('#photo_gallery_image .photo_tag').each(function(e){
		if (!$('photo_gallery_showtags').checked)
		{
			e.hide();
		} else {
			e.show();
		}
	});

}

function clearTags()
{
	$$('#photo_gallery_image .photo_tag').each(function(e){
		e.remove();
	});
}

function createTag(t)
{
	var c = new Element('div', {
		'class': 'photo_tag',
		'id': 'photo_tag_'+t.id
	});

	var i = new Element('div', {
		'class': 'photo_tag_copy'
	}).update(t.copy);

	var a = new Element('div', {
		'class':'photo_tag_arrow'
	}).update('<img alt="tag" width="44" height="44" src="assets/no-index/photo_tag_arrow.png" alt="" title="" onload="fixPNG(this);" />');

	c.insert(i);
	c.insert(a);

	return c;
}

function initGallery()
{
	$$('.photo_gallery_page').each(function(e){
		e.observe('click', function(){
			showPhotoGalleryPage(e.readAttribute('title'));
		});
	});

	$$('.photo_gallery_thumb').each(function(e){
		e.observe('click', function(){
			currentImage = e.readAttribute('id').replace('photo_gallery_thumb_','');
			showImage(currentImage);
		});
	});
}

document.observe('dom:loaded', function(){
	initGallery();
});

