/*=========================================================================================
	Media Widget On Dom Load Function
==========================================================================================*/
var currentMediaIndex;
var currentMediaType;
var photoCount = 0;
var videoCount = 0;
var flashInstallCount = 0;
var viewVideoDetails = 0;
var slideshowDefaultSpeed = 5000;
var slideshowSpeedRatio = 1;
var slideshow;
var isSlideshowPlaying = false;
var firstTime = true;
var userAgent = navigator.userAgent;
var ofText = "of";
var sharePhotoText = "Share photo";
var shareVideoText = "Share video";
var imageCountFromPage = 0;
var videoCountFromPage = 0;
var language = "en";
var transitionEffect;
var mediaID;
var mediaType;
var documentTitle;
var theDivInFront = "mediaContainer";
var lastAction;
var targetedIndex = 0;
var basePath = "";
var share_page_media;
var social_bookmarks = "true";

function formatMediaWidgetThumbnails(){
//1. Rewrite all hrefs that contain the keyword mediaID.
	documentTitle = document.title;
	photoCount = 0;
	videoCount = 0;
	$$("a").each(function(item){
		var itemContents = item.readAttribute('href');
		if(itemContents != null && (itemContents.include(basePath + '/image-') || itemContents.include(basePath + '/video-'))){
			if(itemContents.include('image')){
				photoCount++; 
				item.writeAttribute('href', itemContents.replace(basePath + "/", "#"));
			}else if(itemContents.include('video')){
				videoCount++; 
				item.writeAttribute('href', itemContents.replace(basePath + "/", "#"));
			}else{ 
				item.writeAttribute('href','#');
			}
		}
	});
//2. Show filmstrip arrows
	if($("photo_scroll_left")){$("photo_scroll_left").style.display = "block";};
	if($("photo_scroll_right")){$("photo_scroll_right").style.display = "block";};
	if($("video_scroll_left")){$("video_scroll_left").style.display = "block";};
	if($("video_scroll_right")){$("video_scroll_right").style.display = "block";};

	if($("photoThumbsViewer")){
		$("photoThumbsViewer").style.overflow = "hidden";
		$("photoThumbsViewer").style.height = "72px";
	};
	if($("videoThumbsViewer")){
		$("videoThumbsViewer").style.overflow = "hidden";
		$("videoThumbsViewer").style.height = "72px";
	};
	
//Done updating elements - go ahead and update the local time on the page.
	document['using_dom_loaded'] = false;
	if($("local_time")){update_time();}

//3. Determine what media should be loaded based on the document URL
	var strURL = document.location.toString();
	if (strURL.match('#')) {
		var rawID = strURL.split('#')[1];
		if(rawID.match('image')){
			for (i=1;i<imageLookup.length;i++)
			{
				if(imageLookup[i] == rawID){
					mediaID = i;
					mediaType = 'photo';
				}
			}	
		}
		if(rawID.match('video')){
			for (i=1;i<videoLookup.length;i++)
			{
				if(videoLookup[i] == rawID){
					mediaID = i;
					mediaType = 'video';
				}
			}	
		}
	}else{
		//Check for CGI variable and go to that picture (Done in case of search engine linking).
		if(strURL.match(".html/")){
			var rawID = strURL.split(".html/")[1];
			if(rawID.match('image')){
				for (i=1;i<imageLookup.length;i++)
				{
					if(imageLookup[i] == rawID){
						mediaID = i;
						mediaType = 'photo';
					}
				}	
			}
			if(rawID.match('video')){
				for (i=1;i<videoLookup.length;i++)
				{
					if(videoLookup[i] == rawID){
						mediaID = i;
						mediaType = 'video';
					}
				}	
			}
		}else{
			//URL does not have CGI or Anchor present.  Search document for link to either a video or image and load first array item.
			$$("a").each(function(item){
				var itemContents = item.readAttribute('href');
				//I don't like searching just for media, let's find something more unique.
				if(itemContents != null && ((itemContents.include('#image-')) || (itemContents.include('#video-')))){
					if(itemContents.include('photo')){
						mediaType = "photo";
						mediaID = 1;
						throw $break;
					}else if(itemContents.include('video')){
						mediaType = "video";
						mediaID = 1;
						throw $break;
					}else{ 
						throw $break;
					}
				}
			});
		}
	}
	if(isNaN(mediaID)){
		mediaID = 1;
	}
	if((mediaType != "photo") && (mediaType != "video")){
		if(photoCount > 0){
			mediaType = "photo";
			mediaID = 1;
		}
		else if(videoCount > 0){
			mediaType = "video";
			mediaID = 1;
		}
		else{
			if(imageCountFromPage > 0){
				mediaType = "photo";
				mediaID = 1;
				photoCount = imageCountFromPage;
			}
			else{
				if(videoCountFromPage > 0){
					mediaType = "video";
					mediaID = 1;
					videoCount = videoCountFromPage;
				}
				else{
					mediaType = "photo";
					mediaID = 1;
					photoCount = imageCountFromPage;
				}
			}
			
		}
	}
	if($("slideshow_buttons")){$("slideshow_buttons").style.visibility = "visible";}
//4. Now that we have determined the media Type and Index. 
currentMediaIndex = mediaID;
currentMediaType = mediaType;
//5. See if the Flash Player is installed.

	if (swfobject.hasFlashPlayerVersion("9.0.115")) { 
		if(($("flashContainer")) && (photoCount > 0)){
			loadFlashElements("photo");
		}
	}else {  
		if(($("flashContainer")) && (photoCount > 0)){
			loadFlashElements("photo");
		}
		if($("nonFlashImageHolder")){$("nonFlashImageHolder").style.display = "block";}
		if($("flashContainer")){$("flashContainer").style.display = "none";}
	}
	
	//Show the thumbnail area because the switches have taken place.
	if($("photoThumbsContainer")){$("photoThumbsContainer").style.visibility = "visible";};
	if($("videoThumbsContainer")){$("videoThumbsContainer").style.visibility = "visible";};
	if($("slideshow_buttons")){$("slideshow_buttons").style.visibility = "visible";};
	if($("flashButton")){$("flashButton").style.visibility = "visible";};	
	if($("mediaToolbar") && (mode == "gallery")){$("mediaToolbar").style.paddingBottom = "2px";}


//6. Load contents into container.  Then scroll thumbnails into view.
	loadMediaContainer(currentMediaType, currentMediaIndex);	
	scrollIntoView(currentMediaType, currentMediaIndex);
	if((currentMediaType == "photo") && (videoCount > 0)){
		scrollIntoView("video", 1);
	}
	if((currentMediaType == "video") && (photoCount > 0)){
		scrollIntoView("photo", 1);	
	}
	if (userAgent.indexOf("MSIE") > -1) {
		if(language == "ar"){
			if($("slideshow_buttons")){
				$("slideshow_buttons").style.left == "100px";
			}
		}
	}
	firstTime = false;
	if($("mediaContainerNext")){
		$("mediaContainerNext").style.display = "block";
		/*
		if((social_bookmarks != "true") && (language != 'jp')){
			$("mediaContainerNext").style.top = "5px";
			if (userAgent.indexOf("MSIE") > -1) {
				if (userAgent.indexOf("MSIE 6") > -1) {
					$("mediaContainerNext").style.top = "5px";
				}
				else{
					$("mediaContainerNext").style.top = "5px";
				}
			}
		}
		*/
	}
	if(currentMediaType == "photo"){
		updatePhotoOrdinal(currentMediaIndex);
	}
	if(currentMediaType == "video"){
		updateVideoOrdinal(currentMediaIndex);
	}
}

/*=========================================================================================
	END
==========================================================================================*/

/*=========================================================================================
	Media Container Swapping
==========================================================================================*/

function loadMediaContainer(mediaType, mediaID, container){
	var content;
	var captions;	
	var mediaCaptionHeight = 50;
	if(!container){
		container = "mediaContainer";
	}
	viewVideoDetails = 0;
	if(mediaID < 1){
		mediaID = 1;
	}
	switch (mediaType){
		case "photo":
			if(photoCount > 0){
				if(mediaID > photoCount){
					mediaID = photoCount;
				}
				if($("mediaContainerFlashPlayer")){
					$('mediaContainerFlashPlayer').remove();
				}
				content = '<img src="' + photoDataArray[mediaID].img.src + '"alt="'+photoDataArray[mediaID].caption+'" title="'+photoDataArray[mediaID].caption+'" width="'+photoDataArray[mediaID].width+'" height="'+photoDataArray[mediaID].height+'" border="0" />';
				if((photoDataArray[mediaID].caption.replace(/^\s*|\s*$/g, "")) && (photoDataArray[mediaID].caption != "&nbsp;")){
					var captionLength = getWidth(photoDataArray[mediaID].caption);
					var divWidth = photoDataArray[mediaID].width;
					//alert("Div Width: " + divWidth + " Caption Length: " + captionLength);
					if(captionLength >=  divWidth){
						setStyleTop = 10;
					}
					else{
						setStyleTop = 18;
					}
					content += '<div id="' + container + 'mediaCaption" style="width:' + photoDataArray[mediaID].width + 'px;height:' +mediaCaptionHeight+'px;display:none;top:' + photoDataArray[mediaID].height +'px;"></div>';
					content += '<div id="' + container + 'mediaCaptionContents" class="mediaContents" style="position:absolute;top:'+setStyleTop+'px;width:' + (photoDataArray[mediaID].width - 20) + 'px;height:' +mediaCaptionHeight+'px;display:none;">'+photoDataArray[mediaID].caption+'</div>';
				}
				//if($(container)){$(container).style.height = photoDataArray[mediaID].height + "px";}
				if($("photoThumbsViewerNoJavascript")){
					$("photoThumbsViewerNoJavascript").id = "photoThumbsViewer";
				}
				if($("videoThumbsViewerNoJavascript")){
					$("videoThumbsViewerNoJavascript").id = "videoThumbsViewer";
				}
			}
		break;		
		case "video":
			if(videoCount > 0){
				if($("mediaContainerNext")){$("mediaContainerNext").style.display = "none";}
				if($("mediaContainerContentsNext")){$("mediaContainerContentsNext").style.visibility = "hidden";}
				if($("mediaContainerContents")){$("mediaContainerContents").style.visibility = "hidden";}
				if(mediaID > videoCount){
					mediaID = videoCount;
				}
				if (swfobject.hasFlashPlayerVersion("9.0.115")) { 
					if($("mediaContainerFlashPlayer")){
						showVideo(mediaID);
					}else{
						loadFlashElements("video");
						if($("fullScreenBtn")){$("fullScreenBtn").style.visibility = "hidden";}
					}
				}else {  
					content =  '<object width="'+ videoDataArray[mediaID].width + '" height="' + videoDataArray[mediaID].height + '">';
					content += '<param name="movie" value="' + videoDataArray[mediaID].source + '">';
					content += '<embed src="' + videoDataArray[mediaID].source + '" width="' + videoDataArray[mediaID].width + '" height="' + videoDataArray[mediaID].height + '"></embed>';
					content += '</object>';
				}


				if($("photoThumbsViewerNoJavascript")){
					$("photoThumbsViewerNoJavascript").id = "photoThumbsViewer";
				}
				if($("videoThumbsViewerNoJavascript")){
					$("videoThumbsViewerNoJavascript").id = "videoThumbsViewer";
				}
			}
		break;
		default:
			//alert("Error Finding Photo or Video Data"); ----------  We should have the emailed to us. ------------
		break;
	}
	if(!$(container + "Contents") && (!$(container + "FlashPlayer"))){	
		if($(container)){$(container).insert( { top: '<div id="' + container + 'Contents">' } );}
	}

	//Update thumbnail class to show hover and then set global variables to have new values.
	updateMediaVariables(currentMediaType);
	if(container == theDivInFront){
		if($(currentMediaType+'_id_'+currentMediaIndex)){$(currentMediaType+'_id_'+currentMediaIndex).removeClassName("currentThumb");}
		if($(mediaType+'_id_'+mediaID)){$(mediaType+'_id_'+mediaID).className = "currentThumb";}
		currentMediaIndex = mediaID;
		currentMediaType = mediaType;
	}
	
	//Update the content of the media container and align caption to the bottom of the photo.		
	if($(container + "Contents")){ 	
	
		if(content){
			//$(container + "Contents").addClassName('media_invisible');
			$(container + "Contents").innerHTML = content;
		}
		if(currentMediaType == "photo"){
			$(container + "Contents").style.width = photoDataArray[mediaID].width + "px";
			$(container + "Contents").style.height = photoDataArray[mediaID].height + "px";
		}
		else{
			$(container + "Contents").style.width = playerWidth + "px";
			$(container + "Contents").style.height = playerHeight + "px";
		}
		if($(container + "mediaCaption")){
			$(container + "mediaCaption").style.top = (photoDataArray[mediaID].height - mediaCaptionHeight) + "px";	
			$(container + "mediaCaption").style.display = "block";	
		}
		if($(container + "mediaCaptionContents")){
			$(container + "mediaCaptionContents").style.top = (photoDataArray[mediaID].height - mediaCaptionHeight + setStyleTop) + "px";	
			$(container + "mediaCaptionContents").style.display = "block";	
		}
		//$(container + 'Contents').removeClassName('media_invisible');
		
	}
	if(mediaType == "photo"){
		if($("fullScreenBtn")){$("fullScreenBtn").style.visibility = "visible";}
		if($("mediaToolbar")){
			$("mediaToolbar").style.visibility = "visible";
			$("mediaToolbar").style.display = "block";
		}
		if($("slideshow_buttons")){$("slideshow_buttons").style.visibility = "visible";}
		if($("fsBookmarksTogglerVideo")){$("fsBookmarksTogglerVideo").style.visibility = "hidden";}
		if($("mediaContainerNext")){$("mediaContainerNext").style.display = "block";}
		if($("mediaContainerContentsNext")){$("mediaContainerContentsNext").style.visibility = "visible";}
		if($("mediaContainerContents")){$("mediaContainerContents").style.visibility = "visible";}
		updateFullScreenImageSrc();
	}
	
	if(mediaType == "video"){
		var width_offset;
		if($("mediaToolbar")){
			$("mediaToolbar").style.display = "none";
			$("mediaToolbar").style.visibility = "hidden";
		}
		if($("slideshow_buttons")){$("slideshow_buttons").style.visibility = "hidden";}
		if($("mediaContainer")){
			$("mediaContainer").style.height = playerHeight + "px";
			$("mediaContainer").style.width = playerWidth + "px";
		}
		if($("fsBookmarksTogglerVideo")){
			$("fsBookmarksTogglerVideo").style.width = $("video_toggler_content").scrollWidth + "px";
			$("fsBookmarksTogglerVideo").style.whiteSpace = "nowrap";
			$("fsBookmarksTogglerVideo").style.visibility = "visible";
			$("fsBookmarksTogglerVideo").style.position = "relative";
			$("fsBookmarksTogglerVideo").style.paddingLeft = "";
			$("fsBookmarksTogglerVideo").style.float = "right";
			width_offset = playerWidth - $("fsBookmarksTogglerVideo").offsetWidth + 4;
			$("fsBookmarksTogglerVideo").style.top = "-30px";
			if (userAgent.indexOf("MSIE") > -1) {
				if(language != "ar"){
					$("fsBookmarksTogglerVideo").style.left = (width_offset) + "px";
				}
				else{
					$("fsBookmarksTogglerVideo").style.paddingLeft = "45px";
				}
			}
			else{
				if(language != "ar"){
					if ((userAgent.indexOf("Safari") <= -1) && (userAgent.indexOf("Chrome") <= -1)) {
						$("fsBookmarksTogglerVideo").style.left = width_offset + "px";		
					}
				}
			}
		}
	}
	if(share_page_media){
		if(mediaType == "photo"){
			share_page_media.page_url = share_page_media.base_page_url + "/" + imageLookup[mediaID];
		}
		else{
			share_page_media.page_url = share_page_media.base_page_url + "/" + videoLookup[mediaID];	
		}
	}
	if(document.title.indexOf("#",0) > -1){
		document.title = document.title.substring(0,document.title.indexOf("#",0));
	}
}
function getWidth(text){
	var spanElement = document.createElement("span");
	spanElement.style.whiteSpace = "nowrap";
	spanElement.innerHTML = text;
	document.body.appendChild(spanElement);

	var width = spanElement.offsetWidth;
	document.body.removeChild(spanElement);

	return width;
}

function loadFlashElements(type){
	if (swfobject.hasFlashPlayerVersion("6")) { 
		var index = currentMediaIndex;
		switch(type){
		case "photo":
			if(currentMediaType == "video"){
				index = 1;	
			}
			if((currentMediaType == "photo") && (index > photoCount)){
				index = photoCount;
			}
			if((currentMediaType == "photo") && (index < 1)){
				index = 1;
			}
			var flashvars = {
				fvVideoRSS: (photoXML),
				fvShowCaptions: true,
				fvMode: mode,
				fvIndex: (currentMediaIndex	- 1),
				segment: segment,
				fvLanguage: language
			};
			var params = {
				allowfullscreen: "true",
				allowScriptAccess: "always",
				wmode: "transparent"
			};
			var attributes = {
				id: "flashFullScreenButton",  
				name: "flashFullScreenButton"
			};
			swfobject.embedSWF("/apps/media_widget/ImagePlayer.swf?r="+ new Date().getTime(), "flashContent", "127", "17", "9.0.115", "/js/swfobject_2_1/expressInstall.swf", flashvars, params, attributes);
		break;

		case "video":
			var offset = $("video_toggler_content").scrollWidth + 50;
			if((language == "ar") && (userAgent.indexOf("MSIE") > -1)){
				$("fsBookmarksTogglerVideo").style.paddingLeft = "45px";
				offset -= 20;
			}
			if($("fsBookmarksTogglerVideo").style.paddingLeft){
				offset += parseInt($("fsBookmarksTogglerVideo").style.paddingLeft);
			}
			if(currentMediaType == "photo"){
				index = 1;	
			}
			if((currentMediaType == "video") && (index > videoCount)){
				index = videoCount;
			}
			if((currentMediaType == "video") && (index < 1)){
				index = 1;
			}
			var flashvars = {
				fvShowCaptions: false,
				fvMode: mode,
				fvVideoRSS: (videoXML),
				fvIndex: (currentMediaIndex	- 1),
				segment: segment,
				fvFullScreenOffset: offset,
				fvLanguage: language
			};			
			var params = {
				allowfullscreen: "true",
				allowScriptAccess: "always",
				wmode: "transparent"
			};
			var attributes = {
				id: "mediaContainerFlashPlayer",  
				name: "mediaContainerFlashPlayer"
			};
			swfobject.embedSWF("/apps/media_widget/VideoPlayer.swf?r="+ new Date().getTime(), "mediaContainerContents", playerWidth, playerHeight, "9.0.115", "/js/swfobject_2_1/expressInstall.swf", flashvars, params, attributes);
		break;

		default:
		break;
		}
	}
	document.title = documentTitle;

}

function tellFlashOnLoadWhichVideo(){
	var flashMediaIndex = currentMediaIndex-1;
	return flashMediaIndex
}

function tellFlashToLoadViewDetails(){
	return viewVideoDetails;
	viewVideoDetails = 0;
	
}

// This is a pause function to fix an IE6 issue with display images in the media widget
function pausecomp(millis){
	var date = new Date();
	var curDate = null;
	do { curDate = new Date(); }
	while (curDate-date < millis);
}

function showExpressInstall(){
	if($("nonFlashImageHolder")){$("nonFlashImageHolder").style.display = "none";}
	if($("flashContainer")){$("flashContainer").style.display = "block";}
}
function updateFullScreenImageSrc(){
	if($("fullScreen_target")){
		$("fullScreen_target").href = photoDataArray[currentMediaIndex].fullScreen_src;
	}
	if($("flashFullScreenButton")){
		if($("flashFullScreenButton").loadImageSrc){
			$("flashFullScreenButton").loadImageSrc((currentMediaIndex-1));
		}
	}	
}

function launchViewDetails(){
	
	if(currentMediaType != "video"){
		slideshowPause();
		loadMediaContainer("video", 1)
	}
	if($("mediaContainerFlashPlayer")){
		if($("mediaContainerFlashPlayer").launchViewDetails){
			$("mediaContainerFlashPlayer").launchViewDetails();
		}
	}
	viewVideoDetails = 1;
}

function showVideo(videoIndex){
	flashInstallCount++;
	if((currentMediaType == "video") && (flashInstallCount < 200)){
		if($("mediaContainerFlashPlayer")){
			if($("mediaContainerFlashPlayer").playNextVideo){
				$("mediaContainerFlashPlayer").playNextVideo((videoIndex-1));			
				updateVideoOrdinal(videoIndex);
			}
			else{
			}
		}
		else{
			setTimeout('showVideo(currentMediaIndex)', 100);
			updateVideoOrdinal(currentMediaIndex);
		}
	}
}
/*=========================================================================================
	END
==========================================================================================*/

var photoDataArray = new Array();
var videoDataArray = new Array();

function newPhotoObject(which, src, caption, hover_text, full_screen_src, width, height) {
	photoDataArray[which] = new Object();
	photoDataArray[which].img = newImage(src);  //newImage() is a function found in _lib.js
	photoDataArray[which].caption = caption;
	photoDataArray[which].hover_text = hover_text;
	photoDataArray[which].fullScreen_src = full_screen_src;
	photoDataArray[which].width = width;
	photoDataArray[which].height = height;
}

function newVideoObject(which, source, width, height) {
	videoDataArray[which] = new Object();
	videoDataArray[which].source = source;
	videoDataArray[which].width = width;
	videoDataArray[which].height = height;
}

function newImage(arg) {
	rslt = new Image();
	rslt.src = arg;
	return rslt;
}
/*=========================================================================================
	Media Widget Thumbnail Scrolling
==========================================================================================*/
var mediaLeft = 0;
var scrollDirection = "none";
var mediaInterval;
var mediaScrollIncrement = 5;
var mediaType ="";
var mediaTable = "";
var mediaViewer = "";
var mediaArrowLeftId = "";
var mediaArrowRightId = "";

function updateMediaVariables(mediaType){
	switch(mediaType){
		case "photo":
			mediaTable = "photoThumbsTable";
			mediaViewer = "photoThumbsViewer";
			mediaArrowLeftId = "photoArrowLeft";
			mediaArrowRightId = "photoArrowRight";
			if($("photoThumbsTable")){
				mediaLeft = ($("photoThumbsTable").style.left == "") ? "0" : parseInt($("photoThumbsTable").style.left);
			}
			if(currentMediaType == "photo"){
				if(currentMediaIndex > photoCount){
					currentMediaIndex = photoCount;
				}
				if(currentMediaIndex < 1){
					currentMediaIndex = 1;
				}
			}
		break;
		case "video":
			mediaTable = "videoThumbsTable";
			mediaViewer = "videoThumbsViewer";
			mediaArrowLeftId = "videoArrowLeft";
			mediaArrowRightId = "videoArrowRight";
			if($("videoThumbsTable")){
				mediaLeft = parseInt($("videoThumbsTable").style.left);
			}
			if(currentMediaType == "video"){
				if(currentMediaIndex > videoCount){
					currentMediaIndex = videoCount;
				}
				if(currentMediaIndex < 1){
					currentMediaIndex = 1;
				}
			}
		break;
		default:
		break;
	}
	
	mediaType = mediaType;
}

function scrollForward(mediaType) {
	updateMediaVariables(mediaType);
	scrollDirection = "right";
	scrollThumbnails();
}
function scrollBack(mediaType) {
	updateMediaVariables(mediaType);
	scrollDirection = "left";	
	scrollThumbnails();	
}
function scrollIntoView(type, index) {
	updateMediaVariables(type);
	scrollDirection = "viewable";
	scrollThumbnails(type,index);
}
function scrollNone() {
	mediaType = "";
	scrollDirection = "none";
	return false;
}

function scrollThumbnails(currentType,currentIndex) {
	if($(mediaTable)){
		var mediaTableWidth = $(mediaTable).offsetWidth;
		var mediaViewerWidth = $(mediaViewer).offsetWidth;
		switch (scrollDirection) {
			case "right":			
				if (mediaLeft > (mediaViewerWidth - mediaTableWidth)) {
					mediaLeft -= mediaScrollIncrement;
					if (mediaLeft < (mediaViewerWidth - mediaTableWidth)) mediaLeft = (mediaViewerWidth - mediaTableWidth);
					$(mediaTable).style.left = mediaLeft + "px";
					mediaInterval = setTimeout("scrollThumbnails()", 10);
				} else {
					clearTimeout(mediaInterval);
				}
				break;
			case "left":
				if (mediaLeft < 0) {
					mediaLeft += mediaScrollIncrement;
					if (mediaLeft > 0) mediaLeft = 0;
					$(mediaTable).style.left = mediaLeft + "px";
					mediaInterval = setTimeout("scrollThumbnails()", 10);
				} else {
					clearTimeout(mediaInterval);
				}
				break;
			case "viewable":
				if(currentType == "photo"){var mediaArrayLength = photoCount}else{var mediaArrayLength = videoCount};
				updateMediaVariables(currentType);
				if($(currentType +"_id_1")){
					var imgOffset = ($(currentType +"_id_1").cumulativeOffset().left - $(currentType +"_id_"+currentIndex).cumulativeOffset().left) + 74;
					var imgSrc = currentType +"_id_"+currentIndex;

					var page_language = $$('html')[0].readAttribute('dir');
					if(page_language == "rtl"){
						if(imgSrc == currentType +"_id_1"){
							var imgOffset = ($(currentType +"_id_"+currentIndex).cumulativeOffset().left - $(imgSrc).cumulativeOffset().left) + 50;
						} else {
							var imgOffset = $(currentType +"_id_"+currentIndex).cumulativeOffset().left - $(imgSrc).cumulativeOffset().left ;
						}
					}else{
						if(imgSrc == currentType +"_id_"+ mediaArrayLength){
							var imgOffset = ($(currentType +"_id_1").cumulativeOffset().left - $(imgSrc).cumulativeOffset().left) + 74;
						} else {
							var imgOffset = $(currentType +"_id_1").cumulativeOffset().left - $(imgSrc).cumulativeOffset().left ;
						}
					}

					if(currentType == "video"){
						if(videoCount < 2){
							$(mediaTable).style.left = "0px";
							mediaLeft = 0;	
						}
						else
						{
							$(mediaTable).style.left = imgOffset +"px";
							mediaLeft = imgOffset;	
						}
					}
					else{
						if(photoCount < 2){
							$(mediaTable).style.left = "0px";
							mediaLeft = 0;	
						}
						else
						{
							$(mediaTable).style.left = imgOffset +"px";
							mediaLeft = imgOffset;	
						}					
					}
				}
				break;
			default:
				clearTimeout(mediaInterval);
		}
		checkArrows();
	}
}


function checkArrows() {
	var arrowLeft = $(mediaArrowLeftId);
	var arrowRight = $(mediaArrowRightId);
	
	if ($(mediaTable) && arrowLeft && arrowRight) {
		var galleryWidth = $(mediaTable).offsetWidth;
		var viewerWidth = $(mediaViewer).offsetWidth;

		//alert(mediaTable + ": " + "galleryWidth: " + galleryWidth + "   viewerWidth: " + viewerWidth);
		// if gallery is wider than viewer window, check the arrows
		if (galleryWidth > viewerWidth) {
			//if left edge is hidden, enable left arrow
			arrowLeft.className = (mediaLeft < 0 ) ? "enabled" : "";
			arrowLeft.title = (mediaLeft < 0 ) ? "Scroll left" : "";
			
			// if right edge is hidden, enable right arrow
			arrowRight.className = (mediaLeft > (viewerWidth - galleryWidth)) ? "enabled" : "";
			arrowRight.title = (mediaLeft > (viewerWidth - galleryWidth)) ? "Scroll right" : "";
	
		// gallery isn't wide enough to scroll - disable both arrows
		} else {
			arrowLeft.className = "";
			arrowRight.className = "";
		}
	}
}

/*=========================================================================================
	END
==========================================================================================*/

function togglePlayPause(){
	if($("play")){
		slideshowPlay();
	}
	else{
		slideshowPause();
	}
}

function gotoVideo(videoNumber){
	document.title = documentTitle;
	if(!transitionEffect){
		if(videoNumber > videoCount){
			videoNumber = 1;
		}
		if(videoNumber < 1){
			videoNumber = videoCount;
		}
	
		if (typeof createITT=="function"){
			// ROI tracking code to track the specific photo or video that was clicked
			var tracking_object = createITT();
			if (tracking_object)
			{
				tracking_object.ACTION = '91';
				tracking_object._s_cf40= "video_" + videoDataArray[videoNumber].source.slice(videoDataArray[videoNumber].source.lastIndexOf("/")+1);
				// alert(tracking_object._s_cf40);
				tracking_object.submit_action();
			}
		}
		slideshowPause(true);	
		for (i=1;i<photoDataArray.length;i++){
			if($('photo_id_'+i)){$('photo_id_'+i).removeClassName("currentThumb");}
		}
		for (i=1;i<videoDataArray.length;i++){
			if($('video_id_'+i)){$('video_id_'+i).removeClassName("currentThumb");}
		}
		if($('video_id_'+videoNumber)){$('video_id_'+videoNumber).className = "currentThumb";}

		currentMediaIndex = videoNumber;
		currentMediaType = "video";
		loadMediaContainer("video", videoNumber);
		updateVideoOrdinal(videoNumber);
	}
	else{
		//transitionEffect.cancel();
		
	}
	document.title = documentTitle;
}
function gotoVideoScrollThumbnail(videoNumber){
	if(videoNumber > videoCount){
		videoNumber = 1;
	}
	if(videoNumber < 1){
		videoNumber = videoCount;
	}
	currentMediaType = "video";
	loadMediaContainer("video", videoNumber);
	registerUrlChange();
	scrollIntoView("video", videoNumber);	
	
}

function gotoPhoto(photoNumber,noTransition){
	document.title = documentTitle;
	if(!transitionEffect){
		if(photoNumber > photoCount){
			photoNumber = 1;
		}
		if(photoNumber < 1){
			photoNumber = photoCount;
		}

		if (typeof createITT=="function"){
			// ROI tracking code to track the specific photo or video that was clicked
			var tracking_object = createITT();
			if (tracking_object)
			{
				tracking_object.ACTION = '90';
				tracking_object._s_cf40= "photo_" + photoDataArray[photoNumber].img.src.slice(photoDataArray[photoNumber].img.src.lastIndexOf("/")+1);
				// alert(tracking_object._s_cf40);
				tracking_object.submit_action();
			}
		}
		slideshowPause(true);
		for (i=1;i<photoDataArray.length;i++){
			if($(currentMediaType+'_id_'+i)){$(currentMediaType+'_id_'+i).removeClassName("currentThumb");}
		}
		for (i=1;i<videoDataArray.length;i++){
			if($(currentMediaType+'_id_'+i)){$(currentMediaType+'_id_'+i).removeClassName("currentThumb");}
		}
		if($('photo_id_'+photoNumber)){$('photo_id_'+photoNumber).className = "currentThumb";}
		if((currentMediaType == "video") || (noTransition == true)){
			loadMediaContainer("photo", photoNumber);
			registerUrlChange();
			updatePhotoOrdinal(photoNumber);
		}
		else{
			transition(photoNumber,true,true);
		}
		currentMediaIndex = photoNumber;
		currentMediaType = "photo";
	}
	document.title = documentTitle;
}
function transition(photoIndex,doNotScroll,doNotRegisterURL){
	if(!transitionEffect){
		if(photoIndex > photoCount){
			photoIndex = 1;
		}
		if(photoIndex < 1){
			photoIndex = photoCount;
		}
		if(theDivInFront == "mediaContainer"){
			loadMediaContainer("photo", photoIndex, "mediaContainerNext");
		}
		else{
			loadMediaContainer("photo", photoIndex, "mediaContainer");
		}
		transitionEffect = new Effect.Opacity(theDivInFront,
			{duration:1,
			     from:1,
			       to:0,
			afterFinish:function () {gotoPhotoTransitionScrollThumbnail(photoIndex, doNotScroll, doNotRegisterURL);}
		});
	}

}
function gotoPhotoScrollThumbnail(photoNumber){
	if(isSlideshowPlaying == true){
		setSlideshowInterval()
	}
	if(photoNumber > photoCount){
		photoNumber = 1;
	}
	if(photoNumber < 1){
		photoNumber = photoCount;
	}
	loadMediaContainer("photo", photoNumber);
	scrollIntoView("photo", photoNumber);	
	document.title = documentTitle;	
}
function gotoPhotoTransitionScrollThumbnail(photoNumber, doNotScroll, doNotRegisterURL){
	var photoNumberNext = photoNumber+1;
	if(isSlideshowPlaying == true){
		setSlideshowInterval()
	}
	if(photoNumber > photoCount){
		photoNumber = 1;
	}
	if(photoNumber < 1){
		photoNumber = photoCount;
	}
	if(photoNumberNext > photoCount){
		photoNumberNext	= 1;
	}
	if($(currentMediaType+'_id_'+currentMediaIndex)){$(currentMediaType+'_id_'+currentMediaIndex).removeClassName("currentThumb");}
	if($('photo_id_'+photoNumber)){$('photo_id_'+photoNumber).className = "currentThumb";}
	currentMediaType = "photo";
	currentMediaIndex = photoNumber;
	if(theDivInFront == "mediaContainer"){
		theDivInFront = "mediaContainerNext";
		$("mediaContainerNext").style.zIndex = 100;
		$("mediaContainer").style.zIndex = 99;
		$("mediaContainer").appear();
		loadMediaContainer("photo", photoNumberNext, "mediaContainer");
	}
	else{
		theDivInFront = "mediaContainer";
		$("mediaContainerNext").style.zIndex = 99;
		$("mediaContainer").style.zIndex = 100;
		$("mediaContainerNext").appear();
		loadMediaContainer("photo", photoNumberNext, "mediaContainerNext");
	}
	if(doNotScroll == false){
		scrollIntoView("photo", photoNumber);	
	}
	if(doNotRegisterURL == false){
		registerUrlChange();
	}
	updatePhotoOrdinal(photoNumber);
	transitionEffect = "";
}
function registerUrlChange(){
	if($(currentMediaType + "_count")){
		$(currentMediaType + "_count").innerHTML = currentMediaIndex;
		if(currentMediaType == "photo"){
			if(document.location.hash != imageLookup[currentMediaIndex]){document.location.hash = imageLookup[currentMediaIndex];}
		}
		else{
			if(document.location.hash != videoLookup[currentMediaIndex]){document.location.hash = videoLookup[currentMediaIndex];}
		}
	}
	document.title = documentTitle;
}
function nextPhoto(){
	lastAction = "next";
	slideshowPause(true);
	//if($(currentMediaType+'_id_'+currentMediaIndex)){$(currentMediaType+'_id_'+currentMediaIndex).removeClassName("currentThumb");}
	transition(parseInt(currentMediaIndex) + 1,false,false);
}

function previousPhoto(){
	lastAction = "previous";
	slideshowPause(true);
	//if($(currentMediaType+'_id_'+currentMediaIndex)){$(currentMediaType+'_id_'+currentMediaIndex).removeClassName("currentThumb");}
	transition(parseInt(currentMediaIndex) - 1,false,false);
}

function autoNext(){
	transition(parseInt(currentMediaIndex) + 1,false,true);
}

function clearSlideshowInterval(){
	clearInterval(slideshow);
}

function setSlideshowInterval(){
	if(slideshow){
		clearInterval(slideshow);
	}
	if(currentMediaType != "video"){
		slideshow = setInterval("autoNext()",(slideshowDefaultSpeed*slideshowSpeedRatio));	
	}
}

function slideshowFaster(){
	if(slideshowSpeedRatio > .5){
		switch (slideshowSpeedRatio) {
			case 0.75:
				slideshowSpeedRatio = 0.5;
				break;
			case 1:
				slideshowSpeedRatio = 0.75;
				break;
			case 1.5:
				slideshowSpeedRatio = 1;
				break;
			case 2:
				slideshowSpeedRatio = 1.5;
				break;
		}
		displaySlideshowRatio();
		if(isSlideshowPlaying == true){
			setSlideshowInterval();
		}
		if($("slower")){$("slower").src="/images/photo_gallery/" + segment + "/slower.gif";}
		if($("slower_button")){$("slower_button").style.cursor="pointer";}
	}
}

function slideshowSlower(){
	if(slideshowSpeedRatio < 2){
		switch (slideshowSpeedRatio) {
			case 0.5:
				slideshowSpeedRatio = 0.75;
				break;
			case 0.75:
				slideshowSpeedRatio = 1;
				break;
			case 1:
				slideshowSpeedRatio = 1.5;
				break;
			case 1.5:
				slideshowSpeedRatio = 2;
				break;
		}
		displaySlideshowRatio();
		if(isSlideshowPlaying == true){
			setSlideshowInterval();
		}
		if($("faster")){$("faster").src="/images/photo_gallery/" + segment + "/faster.gif";}
		if($("faster_button")){$("faster_button").style.cursor="pointer";}
	}
}

function getSlideshowRatio(){
	return slideshowSpeedRatio;
}
function setSlideshowRatio(ratio){
	slideshowSpeedRatio = ratio;
	displaySlideshowRatio();
}
function getIsSlideshowPlaying(){
	return isSlideshowPlaying;
}
function setIsSlideshowPlaying(isPlaying){
	isSlideshowPlaying = isPlaying;
	if(isSlideshowPlaying == true){
		slideshowPlay();
	}
	else{
		slideshowPause();
	}
}
function getCurrentMediaIndex(){
	return currentMediaIndex-1;
}

function displaySlideshowRatio(){
var speedLabel = "Normal";
	switch (slideshowSpeedRatio) {
		case 0.5:
			speedLabel = "Fastest";
			break;
		case 0.75:
			speedLabel = "Fast";
			break;
		case 1:
			speedLabel = "Normal";
			break;
		case 1.5:
			speedLabel = "Slow";
			break;
		case 2.0:
			speedLabel = "Slowest";
			break;
	}
	if($("slower_button")){
		//$("slower_button").title = "Current Speed: " + speedLabel;
		if(speedLabel == "Slowest"){
			if($("slower")){$("slower").src="/images/photo_gallery/" + segment + "/slower_over.gif";}
			if($("slower_button")){$("slower_button").style.cursor="default";}
			if($("faster")){$("faster").src="/images/photo_gallery/" + segment + "/faster.gif";}
			if($("faster_button")){$("faster_button").style.cursor="pointer";}
		}
	}
	if($("faster_button")){
		//$("faster_button").title = "Current Speed: " + speedLabel;
		if(speedLabel == "Fastest"){
			if($("faster")){$("faster").src="/images/photo_gallery/" + segment + "/faster_over.gif";}
			$("faster_button").style.cursor="default";
			if($("slower")){$("slower").src="/images/photo_gallery/" + segment + "/slower.gif";}
			if($("slower_button")){$("slower_button").style.cursor="pointer";}
		}
	}
}
function slideshowPlay(){
	if(currentMediaType != "video"){
		if($("play")){
			$("play").visibility = "hidden";
			$("play").src="/images/photo_gallery/" + segment + "/pause.gif";
			//$("playpause_label").innerHTML=pause_string;
			$("play").onMouseOver = "buttonOver('pause')";
			$("play").onMouseOut = "buttonOver('pause')";
			$("play").id="pause";
			if($("pause")){$("pause").visibility = "visible";}
		}
		setSlideshowInterval();
		displaySlideshowRatio();
		isSlideshowPlaying = true;
		if(document.location.hash){document.location.hash = "slideshow";}
	}
}
function slideshowPause(silent){
	if($("pause")){
		$("pause").visibility = "hidden";
		$("pause").src="/images/photo_gallery/" + segment + "/play.gif";
		//$("playpause_label").innerHTML=play_string;
		$("pause").onMouseOver = "buttonOver('play')";
		$("pause").onMouseOut = "buttonOver('play')";
		$("pause").id="play";
		if($("play")){$("play").visibility = "visible";}
	}
	if(silent != true){
		registerUrlChange();
	}
	clearSlideshowInterval();
	isSlideshowPlaying = false;
}

function buttonOver(id,segment){
	if($(id)){
		if(((id == "slower") && (slideshowSpeedRatio >= 2.0)) || ((id == "faster") && (slideshowSpeedRatio <= 0.5))){
			
		}
		else{
			$(id).src="/images/photo_gallery/" + segment + "/" + id + "_over.gif";
		}
	}
}
function buttonOut(id,segment){
	if($(id)){
		if(((id == "slower") && (slideshowSpeedRatio >= 2.0)) || ((id == "faster") && (slideshowSpeedRatio <= 0.5))){
			
		}
		else{
			$(id).src="/images/photo_gallery/" + segment + "/" + id + ".gif";
		}
	}
}

function autoSlideshowPlay(){

	var obj = getObj("soundimg");
  	var cookie = readCookie("execsound");
  	var cond = cookie ? cookie : 'play';

	if(cond == 'play'){
		getObj("msound").play();
		isplay = true;
		obj.src = "images/sound-on.png";
	}else{
		getObj("msound").stop();
		isplay = false;
		obj.src = "images/sound-off.png";
	}


	if(currentMediaType != "video"){
		var strURL = document.location.toString();
		var playSlideshow;

		if ((((document.location.hash) && !(strURL.match('#slideshow')))) || (strURL.match("mediaID="))) {
			playSlideshow = false;
		}else{
			if($("photoThumbsContainer")){
				playSlideshow = true;
			}
		}
		if(($("slideshow_buttons")) && (playSlideshow == true)){
			setTimeout('slideshowPlay()', 1000);
			if(document.location.hash){document.location.hash = "slideshow";}
		}
		if($("slideshow_buttons")){
			displaySlideshowRatio();
		}
	}
}

function isPlaying(){
	return isSlideshowPlaying;
}

function updatePhotoOrdinal(ordinalIndex){
	if($("fsBookmarksTogglerTop")){
		var togglerContent = "";
		if (social_bookmarks == "true"){
			togglerContent += '<span style="text-decoration: underline;">' + sharePhotoText + '</span>';
		}
		if(photoCount > 1){
			togglerContent += ' (<span id="photo_count">' + ordinalIndex + '</span> ' + ofText + ' ' + photoCount + ')';
		}
		$("fsBookmarksTogglerTop").innerHTML = togglerContent;
		if(($("fsBookmarksTogglerTop").className == "") && (social_bookmarks == "true")){
			$("fsBookmarksTogglerTop").className = "fsCollapsedTop";
		}
		if (userAgent.indexOf("MSIE") > -1) {
		}
		else{
			$("fsBookmarksTogglerTop").style.top = "1px";
		}
	}
	if($("fsBookmarksTogglerTop")){
		$("fsBookmarksTogglerTop").style.width = "";
		$("fsBookmarksTogglerTop").style.marginLeft = "0px";
		$("fsBookmarksTogglerTop").style.padding = "0px";
		$("fsBookmarksTogglerTop").style.paddingBottom = "5px";
		$("fsBookmarksTogglerTop").style.paddingTop = "6px";
		$("fsBookmarksTogglerTop").style.paddingLeft = "24px";
		if(mode == "widget"){
			$("fsBookmarksTogglerTop").style.top = "-1px";
		}
		width_offset = $("fsBookmarksTogglerTop").offsetWidth;
		if($("fullScreenBtn")){width_offset += $("fullScreenBtn").scrollWidth;}
		if($("slideshow_buttons")){width_offset += $("slideshow_buttons").scrollWidth;}
		if($("slideshow_buttons")){width_offset += $("slideshow_buttons").style.left;}
		width_offset = playerWidth - width_offset;
		$("fsBookmarksTogglerTop").style.left = width_offset + "px";		
	}

}

function updateVideoOrdinal(ordinalIndex){
	if($("video_count")){
		$("video_count").innerHTML = ordinalIndex;
	}

}

function resetTitle(){
	document.title = documentTitle;
}

