var playState = "playing";
var slideShowTimer = 0;
var slideShowCounter = -1; // so that when incremented for the first time in runSlideShow, it will start at zero
var blendDuration = 2; // # of seconds for transition
var desiredWidth = 240;
var desiredHeight = 178;

var holderWidth = 242; //document.getElementById("slideShowHolder").style.width;
var holderHeight = 180; //document.getElementById("slideShowHolder").style.height;

//sadly, browser detection is needed for the cursor value to use

var isWindowsIE = false;
if(navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Win") != -1 && navigator.userAgent.indexOf("Opera") == -1)
	if(document.all)
		isWindowsIE = true;

function fixAspectRatios() {
	var currH, currW, oldAR;
	for (var i = 0; i < slideArray.length; i++) {
		currH = slideArray[i].image.height;
		currW = slideArray[i].image.width;
		oldAR = currW / currH;
		if (currH > currW) {
			//image is taller than it is wide
			slideArray[i].image.height = desiredHeight;
			slideArray[i].image.width = desiredHeight * oldAR;
			if (slideArray[i].image.width > desiredWidth) {
				slideArray[i].image.width = desiredWidth;
				slideArray[i].image.height = desiredWidth / oldAR;
			}
		} else {
			//image is wider than it is tall
			slideArray[i].image.width = desiredWidth;
			slideArray[i].image.height = desiredWidth / oldAR;
			if (slideArray[i].image.height > desiredHeight) {
				slideArray[i].image.height = desiredHeight;
				slideArray[i].image.width = desiredHeight * oldAR;
			}
		}
	}
}


function runSlideShow()
{
	slideShowCounter++;
	if (slideShowCounter > (slideArray.length-1))
		slideShowCounter=0;

	if(slideArray[slideShowCounter] && slideArray[slideShowCounter].image && slideArray[slideShowCounter].image.complete) {
		switchImage(null);
//	} else {
//		slideShowCounter--;
	}
		
	clearTimeout(slideShowTimer);
	slideShowTimer = setTimeout('runSlideShow()', slideShowSpeed);
}


function mouseEnterImage() {
	if (playState != "paused") { //if already paused, do nothing
		clearTimeout(slideShowTimer);
	}
}

function mouseLeaveImage() {
	if (playState != "paused") { //if already paused, do nothing
		clearTimeout(slideShowTimer);
		slideShowTimer = setTimeout('runSlideShow()', 1);	}
}


//pauses without 
function pauseSlideShow(toggle) {
	if (toggle == "start") {
		clearTimeout(slideShowTimer);
		slideShowTimer = setTimeout('runSlideShow()', 1);
		playState = "playing";
	} else {
		playState = "paused";
		clearTimeout(slideShowTimer);
	} 

}

function toggleSlideShow(){
	if (playState == "paused") {
		document.getElementById("slideshowplay").src = "/images/elements/slideshow/pause.gif";
		document.getElementById("slideshowplay").alt = "Pause Slide Show";
		playState = "playing";
		clearTimeout(slideShowTimer);
		slideShowTimer = setTimeout('runSlideShow()', 1);
	} else {
		document.getElementById("slideshowplay").src = "/images/elements/slideshow/play.gif";
		document.getElementById("slideshowplay").alt = "Play Slide Show";
		playState = "paused";
		clearTimeout(slideShowTimer);
	} 
}

function showBig(sB)
{
	switchImage(sB);
}

function clickThrough(cT) {
	clearTimeout(slideShowTimer);

	if (cT.name == "forward") {
		if (slideShowCounter >= (slideArray.length-1))
			slideShowCounter=0;
		else
			slideShowCounter++;
	}
	else if(cT.name == "back")
	{
		if (slideShowCounter == 0)
			slideShowCounter=slideArray.length-1;
		else
			slideShowCounter--;
	}
	switchImage(null);

	if (playState == "playing") {
		slideShowTimer = setTimeout('runSlideShow()', slideShowSpeed);
	}
}

function switchImage(directLink)
{
	var slideShow = document.getElementById("PropSlideShow");
	
	if(document.images.PropSlideShow.filters)
	{
		 document.images.PropSlideShow.style.filter="blendTrans(duration="+ blendDuration+")";
		 document.images.PropSlideShow.style.filter="blendTrans(duration=crossFadeDuration)";
		 document.images.PropSlideShow.filters.blendTrans.Apply();
	}
	
	if(directLink != null)
	{
		playState = "playing";
		toggleSlideShow();
//		pauseSlideShow("stop");
		slideShowCounter = directLink;
	}
	
	//if mozilla, the image will be set later
	if (!( document.images.PropSlideShow.style && document.images.PropSlideShow.style.MozOpacity == "")) {
		//slideShow.src = slideArray[slideShowCounter].image.src;
		var slideShowLast = document.getElementById("SlideShowLast");
		slideShowLast.width = slideArray[slideShowCounter].image.width;
		slideShowLast.height = slideArray[slideShowCounter].image.height;
		slideShowLast.src = slideArray[slideShowCounter].image.src;
		//slideShowLast.style.top = 1;
		slideShowLast.style.top = Math.round((holderHeight - slideShowLast.height) / 2);
		//slideShowLast.style.left = 1;
		slideShowLast.style.left = Math.round((holderWidth - slideShowLast.width) / 2);

		slideShow.src = "/images/elements/clear.gif";
	}

/*	//reset these since if they exist already and then another skew is attempted, bad things happen
	slideShow.style.height = null;
	slideShow.style.width = null;

	if(slideShow.height > 180)
	{		
		var newWidth = (180 * slideShow.width) / slideShow.height;
				
		slideShow.style.height = "180px";
		slideShow.style.width = newWidth + "px";
		
		if(!slideArray[slideShowCounter].bigImage)
			slideArray[slideShowCounter].bigImage = slideArray[slideShowCounter].image.src;
	}
	
	if(slideShow.width < 240 || slideShow.height < 180)
	{
		//get left offset
		var leftOffset = 240 - slideShow.width;
		leftOffset = leftOffset / 2;
		var topOffset = 180 - slideShow.height;
		topOffset = topOffset / 2;
		
		if(document.getElementById("slideShowTitle"))
			topOffset += document.getElementById("slideShowTitle").offsetHeight + 2;
		
		slideShow.style.position = "absolute";
		slideShow.style.left = leftOffset + "px";
		slideShow.style.top = topOffset + "px";
	}
	else
	{
		slideShow.style.position = "relative";
		slideShow.style.left = null;
		slideShow.style.top = null;
	}
*/	
	showSnipe();
	if(document.getElementById("photoCaption"))
		if (slideArray[slideShowCounter].url == "") {
			document.getElementById("photoCaption").innerHTML = slideArray[slideShowCounter].caption;
		} else {
			document.getElementById("photoCaption").innerHTML = slideArray[slideShowCounter].caption + "... <a href='" + slideArray[slideShowCounter].url + "'>More Details</a>";
		}
	if(document.getElementById("slideShowTitle"))
		document.getElementById("slideShowTitle").innerHTML = slideArray[slideShowCounter].title;

	if(document.images.PropSlideShow.filters)
	{
//alert("3");
		document.images.PropSlideShow.filters.blendTrans.Play();
//alert("4");
		//set a timeout to know it's finished -- copy new image src width and height into "front" slideshow image
		mozDissTimer = setTimeout('finishIESlide()', blendDuration * 1000);
	} else if (document.images.PropSlideShow.style && document.images.PropSlideShow.style.MozOpacity == "") {
		startMozDissolve();
	}/* else {
		alert("no dissolve for you");
	}*/
	
	hideBigImage();

}

function finishIESlide() {
	var slideShow = document.getElementById("PropSlideShow");
	var newSlideShow = document.getElementById("SlideShowLast");
//alert("slide finished. src=" + slideShow.src);
	slideShow.width = newSlideShow.width;
	slideShow.height = newSlideShow.height;
	slideShow.src = newSlideShow.src;
	document.images.PropSlideShow.style.filter="";
	//slideShow.style.top = 1;
	slideShow.style.top = Math.round((holderHeight - slideShow.height) / 2);
	//slideShow.style.left = 1;
	slideShow.style.left = Math.round((holderWidth - slideShow.width) / 2);

}


var mozDissTimer, mozDissNumSteps;

function startMozDissolve() {
//1 = full opacity, 0 = invisible

//put the old image in the background of the div
//document.getElementById("slideShowHolder").style.backgroundImage = "url(" + document.images.SlideShow.src + ")";
	var slideShowLast = document.getElementById("SlideShowLast");
	slideShowLast.src = document.images.PropSlideShow.src;
	slideShowLast.width = document.images.PropSlideShow.width;
	slideShowLast.height = document.images.PropSlideShow.height;
	//slideShowLast.style.top = 1;
	slideShowLast.style.top = Math.round((holderHeight - slideShowLast.height) / 2);
	//slideShowLast.style.left = 1;
	slideShowLast.style.left = Math.round((holderWidth - slideShowLast.width) / 2);

	//set the current image opacity to zero
	document.images.PropSlideShow.style.MozOpacity = "0";
	//put the new image in
	document.images.PropSlideShow.width = slideArray[slideShowCounter].image.width;
	document.images.PropSlideShow.height = slideArray[slideShowCounter].image.height;
	document.images.PropSlideShow.src = slideArray[slideShowCounter].image.src;
	//document.images.PropSlideShow.style.top = 1;
	document.images.PropSlideShow.style.top = Math.round((holderHeight - document.images.PropSlideShow.height) / 2);
	//document.images.PropSlideShow.style.left = 1;
	document.images.PropSlideShow.style.left = Math.round((holderWidth - document.images.PropSlideShow.width) / 2);


//iteratively increment it with timeouts
	mozDissNumSteps = 20; //this is the number of times to change the opacity during the dissolve
	timeStep = blendDuration * 1000 / mozDissNumSteps;
	mozDissTimer = setTimeout("mozDissolveStep("+ 1 +","+timeStep+")", timeStep);
}

function mozDissolveStep(currStepCt,timeStep) {
	if (currStepCt == mozDissNumSteps) {
//		document.getElementById("slideShowHolder").style.backgroundImage = "url(" + document.images.SlideShow.src + ")";
		slideShowLast = document.getElementById("SlideShowLast");

//the transition is smoother if width and height are set before *and* after
		slideShowLast.width = document.images.PropSlideShow.width;
		slideShowLast.height = document.images.PropSlideShow.height;
		slideShowLast.style.top = document.images.PropSlideShow.style.top;
		slideShowLast.style.left = document.images.PropSlideShow.style.left;
		slideShowLast.src = document.images.PropSlideShow.src;
		slideShowLast.width = document.images.PropSlideShow.width;
		slideShowLast.height = document.images.PropSlideShow.height;
		slideShowLast.style.top = document.images.PropSlideShow.style.top;
		slideShowLast.style.left = document.images.PropSlideShow.style.left;

		document.images.PropSlideShow.style.MozOpacity = null;
		mozDissTimer = null;
	} else {
		document.images.PropSlideShow.style.MozOpacity = "" + (currStepCt / mozDissNumSteps);
		mozDissTimer = setTimeout("mozDissolveStep("+ (currStepCt + 1) +","+timeStep+")", timeStep);
	}
}


function openDetailPage()
{
	if(slideArray[slideShowCounter].url)
		window.location = slideArray[slideShowCounter].url;
}

function onHover(eSrc)
{
	if(isWindowsIE)
		eSrc.style.cursor = "hand";
	else
		eSrc.style.cursor = "pointer";
		
	if(slideArray.length > 1)
	{
//		document.getElementById("controls").style.visibility = "visible";
		pauseSlideShow("stop");
	}
	
}
	
function offHover(eSrc)
{
	if(slideArray.length > 1)
	{
		//document.getElementById("controls").style.visibility = "hidden";
		pauseSlideShow("start");
	}
}

/*loay's versions */
function showThumbs(pageNum)
{
	if(pageNum == 2){
		document.getElementById("thumbsPage1").style.display = "none";
		document.getElementById("thumbsPage2").style.display = "";
		// Thumbs Control
		if(document.getElementById("morethumbs"))
			document.getElementById("morethumbs").style.display = "none";
		if(document.getElementById("previousthumbs"))
			document.getElementById("previousthumbs").style.display = "";
	}else{
		document.getElementById("thumbsPage1").style.display = "";
		document.getElementById("thumbsPage2").style.display = "none";
		if(document.getElementById("morethumbs"))
			document.getElementById("morethumbs").style.display = "";
		if(document.getElementById("previousthumbs"))
			document.getElementById("previousthumbs").style.display = "none";
	}
	if(document.getElementById("morephotos")) {
		document.getElementById("morephotos").style.visibility  = "hidden";
		document.getElementById("morephotosLabel").style.visibility  = "hidden";
	}
	document.getElementById("thumbsPanel").style.display = "block";
}

	
function hideThumbs()
{
	document.getElementById("morephotos").style.visibility  = "visible";
	document.getElementById("morephotosLabel").style.visibility  = "visible";
	document.getElementById("thumbsPanel").style.display = "none";
}

/*end loay's versions */

/* old ones
function showThumbs()
{
	document.getElementById("showThumbsButton").style.visibility = "hidden";
	document.getElementById("thumbsPanel").style.visibility = "visible";
}
	
function hideThumbs()
{
	document.getElementById("showThumbsButton").style.visibility = "visible";
	document.getElementById("thumbsPanel").style.visibility = "hidden";
}
*/
function showSnipe()
{
	var photoSnipe = document.getElementById("photoSnipe");
	var photoBigImage = document.getElementById("photoBigButton")
	if(slideArray[slideShowCounter].snipe)
		photoSnipe.src = slideArray[slideShowCounter].snipe;
	else
		photoSnipe.src = "/images/elements/clear.gif";
	photoSnipe.style.left = 0;
	photoSnipe.style.right = null;
	
	if(document.getElementById("slideShowTitle"))
		photoSnipe.style.top = document.getElementById("slideShowTitle").offsetHeight + 2;

	if(slideArray[slideShowCounter].bigImage)
	{
		photoBigImage.src = "/Websites/images/slideShow/bigImageIcon.gif";
/*was at top right:
		photoBigImage.style.left = null;
		photoBigImage.style.right = 0;
now at bottom right: */
		photoBigImage.style.left = null;
		photoBigImage.style.right = 0;
		photoBigImage.style.top = null;
		photoBigImage.style.bottom = 0;
				
		if(document.getElementById("slideShowTitle"))
			photoBigImage.style.top = document.getElementById("slideShowTitle").offsetHeight + 2;
	}
	else
	{
		photoBigImage.src = "/images/elements/clear.gif";
	}
}


function showBigImage()
{
	var centerSlideShowX = 0;
	var centerSlideShowY = 0;
	
	var bigImageHolder = document.getElementById("bigImageHolder");
	var slideShow = document.getElementById("PropSlideShow");
	var slideShowHolder = document.getElementById("slideShowHolder");
	
	document.getElementById("bigImage").src = slideArray[slideShowCounter].bigImage;
	bigImageHolder.style.position = "absolute";
	
	document.getElementById("bigImage").onmouseover = function()
	{
		onHover(this);
	}
	document.getElementById("bigImage").onmouseout = function()
	{
		offHover(this);
		hideBigImage();
	}
	
	var centerSlideShowX = (slideShowHolder.offsetWidth) / 2;
	var centerSlideShowY = (slideShowHolder.offsetHeight) / 2;

	if(document.all)
	{
		var browseWidth = document.body.clientWidth;
		var browseHeight = document.body.clientHeight;
	}
	else
	{
		var browseWidth = window.outerWidth;
		var browseHeight = window.outerHeight;
	}
	
	//pick top or bottom (whichever has more room)
	var onBottom = Math.abs(browseHeight - (get_true_top(slideShowHolder) - ((slideShowHolder.offsetHeight) / 2)));
	var onTop = browseHeight - onBottom;

	//pick left or right (whichever has more room)
	var onRight = browseWidth - (get_true_left(slideShowHolder) - ((slideShowHolder.offsetWidth) / 2));
	var onLeft = browseWidth - onRight;

	document.getElementById("bigImage").onload = function()
	{
		if(document.all)
			bigImageHolder.style.display = "block";
			
		if(onTop > onBottom)
			bigImageHolder.style.top = centerSlideShowY - document.getElementById("bigImage").offsetHeight;
		else
			bigImageHolder.style.top = centerSlideShowY;
			
		if(onLeft > onRight)
			bigImageHolder.style.left = centerSlideShowX - document.getElementById("bigImage").offsetWidth;
		else
			bigImageHolder.style.left = centerSlideShowX;
			
		//bigImageHolder.style.border = "solid thin black";
		bigImageHolder.style.visibility = "visible";
		bigImageHolder.style.zIndex = 99;
	}
}

function hideBigImage()
{
	var bigImageHolder = document.getElementById("bigImageHolder");
	bigImageHolder.style.visibility = "hidden";
	bigImageHolder.style.top = null;
	bigImageHolder.style.left = null;
	if(document.all)
		bigImageHolder.style.display = "none";
}

function get_true_left(element)
{
	yPos = element.offsetLeft;
	tempEl = element.offsetParent;
	while (tempEl != null)
	{
		yPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}
function get_true_top(element)
{
	yPos = element.offsetTop;
	tempEl = element.offsetParent;
	while (tempEl != null)
	{
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

var thumbView = 'right';	

function beginDelayed()
{
	clearTimeout(slideShowTimer);
	slideShowTimer = setTimeout('runSlideShow()', 300);
}


function addLoadEvent(fn) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = fn;
	} else {
		window.onload = function() {
			oldonload();
			fn();
		}
	}
}

function startSlidesProp() {
	beginDelayed();
	fixAspectRatios();
}

addLoadEvent(startSlidesProp);
//window.onload = new Function("beginDelayed();fixAspectRatios();");

