/*	Javascript for a pop-up image display

	Dependence: value added to popUpDeltaW/H includes an integer equal to double the border.
	
	100128 RBracco Initial Version
	100209 RBracco Added left/right, comments, cover, and functions popUpShow, popUpIncrement, popUpShowComment, popUpHideComment
	(c)2010, Red Beagle Web Development, All Rights Reserved */
	var popupOStep=15;					//	Opacity scale is 1-100, this is the step size
	var popupOMax=75;					//	Max opacity of the faded-in background
	var popupODelay=25;					//	Speed of the actions
	var popUpBeginW=0;
	var popUpBeginH=0;
	var popUpDeltaW=0;
	var popUpDeltaH=0;
	var popUpStep=0;
	var popUpStage=0;
	var popUpRatio=0;
	var windowWidth=0;					//	Dimensions of screen for scaling the image
	var windowHeight=0;
	var imageScale=0;					//	Ratio of image size to screen size for scaling
	var imageMaxRatio=0.8;
	var imageScaleFlag=false;
	var finalPopUpHeight=0;
	var finalPopUpWidth=0;
	if (typeof popUpImageFolder=="undefined") var popUpImageFolder="./images/photos/";
	var popUpImageFile="";
	if (typeof popupDisplayOtherGals=="undefined") var popupDisplayOtherGals=true;
	if (typeof popupDisplayHeading=="undefined") var popupDisplayHeading=true;
	var popUpButtonsCloseFlag=true;
	var popUpButtonsLeftFlag=true;
	var popUpButtonsRightFlag=true;
	var popUpButtonsCommentFlag=true;
	var popUpButtonsLeftNow=true;
	var popUpButtonsRightNow=true;
	var popUpCurrentlyDisplayed=false;
	var popUpCurrentComment="";
	var popupCurrentImageIndex=0;
	var popUpGalleryIndex=-1;
	var popUpGalleryLength=0;
	var popUpCaptionAtBottom=true;
	var popUpButtonsActive=false;
//	window.onresize=getWindowSizeScale;
	
	whichie=document.URL.split("?");
	if (whichie.length>1) {
	arr=whichie[1].split("&");
	if (arr[0].length>0) {
		if (isNaN(arr[0])) { // if not passed a number, then see if it's the name of a gallery
			arr[0]=unescape(arr[0]);
			for (i=0; i<galleries.length; i++) {
				if (arr[0]==galleries[i].toLowerCase()) {
					popUpGalleryIndex=i;
					i=galleries.length;
				}
			}
		}
		else if (arr[0]<popupGalText.length) popUpGalleryIndex=arr[0];
	}
//	if (arr.length==2) if (arr[1]<10) firstPhoto=arr[1];
}

	function getWindowSize() {
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			windowWidth = window.innerWidth;
			windowHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
	}

	function getWindowSizeScale() {
		getWindowSize();
	}
	
	function popUpInit() {
		getWindowSize();
		popUp=document.getElementById("popUpPlate");
		popupRevealBkgd=document.getElementById("popUpBkgd");
		popUpI=document.getElementById("popUpImage");
		popUpComment=document.getElementById("popUpCommentBox");
		popUpCoverObj=document.getElementById("popUpCover");
//		popUpCoverImg=document.getElementById("popUpCoverImage");
		popUpCoverLImg=document.getElementById("popUpCoverLeftImage");
		popUpCoverRImg=document.getElementById("popUpCoverRightImage");
		popUpBClose=document.getElementById("popUpButtonClose");
		if (popUpButtonsLeftFlag) popUpBLeft=document.getElementById("popUpButtonLeft");
		if (popUpButtonsRightFlag) popUpBRight=document.getElementById("popUpButtonRight");
		if (popUpButtonsCommentFlag) popUpBComment=document.getElementById("popUpButtonComment");
		for (i=0; i<popupImageArray.length; i++) {
			if (popupImageArray[i][5]==popUpGalleryIndex && document.images) {
				rslt = new Image();
				rslt.src = popUpImageFolder+popupImageArray[i][0];
			}
		}
	}

	function popUpCreateGallery(ii) {
		if (isNaN(ii)) {
			for (i=0; i<popupGalNames.length; i++) if (ii==popupGalNames[i]) { popUpGalleryIndex=i; i=popupGalNames.length; }
		} else if (popUpGalleryIndex==-1) popUpGalleryIndex=ii;
		if (popupDisplayHeading) document.write("<div id='popupGalleryHeading'><h1>"+popupGalMainName+popupGalNames[popUpGalleryIndex]+"</h1></div>");
		if (popupGalText[popUpGalleryIndex].length>0) document.write("<div id='popupGalleryCopy'>"+popupGalText[popUpGalleryIndex]+"</div>");
		document.write("<div id='popupGalleryThumbs'>");
		for (i=0; i<popupImageArray.length; i++) if (popupImageArray[i][5]==popUpGalleryIndex) {
			document.write("<div id=\"popUpThumb"+popupImageArray[i][6]+"\"><img class=\"popupThumb\" onclick=\"popUpShow("+i+"); return false;\" src=\""+popUpImageFolder+popupImageArray[i][4]+"\" alt=\"Thumb #"+i+"\" /></div>");
			popUpGalleryLength++;
		}
		if (popupGalNames.length>1 && popupDisplayOtherGals) document.write (popupGalList);
		document.write("</div>");
		if (popupDisplayHeading) document.title=popupGalMainName+popupGalNames[popUpGalleryIndex];
	}

	function popUpFriend(popUpImageFileN, popUpWidth, popUpHeight, popUpComment) {
		getWindowSize();
		popUpButtonsActive=false;
		finalPopUpWidth=popUpWidth;
		finalPopUpHeight=popUpHeight;
		imageScale=Math.max(popUpWidth/windowWidth, popUpHeight/windowHeight);
		imageScaleFlag=(imageScale>imageMaxRatio);
		if (imageScaleFlag) {
			// alert("Scale by "+Math.floor(100*imageMaxRatio/imageScale)+"%");
			finalPopUpWidth=Math.floor(popUpWidth*imageMaxRatio/imageScale);
			finalPopUpHeight=Math.floor(popUpHeight*imageMaxRatio/imageScale);
		}
		popUpDeltaW=finalPopUpWidth+20-popUpBeginW;
		popUpDeltaH=finalPopUpHeight+20-popUpBeginH;
		popUpImageFile=popUpImageFileN;
		popUpCurrentComment=popUpComment;
		popUpButtonsCommentFlag=(popUpCurrentComment.length>0);
		popupRevealBkgd.style.display="block";	// Set the display from none to block
		popUpI.style.opacity = 0;
		popUpI.style.filter = 'alpha(opacity=0)';
		if (!popUpCurrentlyDisplayed) {
			popUpI.src=popUpImageFolder+popUpImageFile;
			popUpI.setAttribute('height', finalPopUpHeight);
			popUpI.setAttribute('width', finalPopUpWidth);
			popUpSequence(1, 0);
		} else popUpSequence(5, 0);
	}
	
	function popUpSequence(popUpStage, popUpStep) {
		switch(popUpStage) {
			case 1:	// Fade in the background
				popUpCurrentlyDisplayed=true;
				popupRevealBkgd.style.opacity = popUpStep/100;
				popupRevealBkgd.style.filter = 'alpha(opacity=' + popUpStep + ')';
				if (popUpStep<popupOMax) setTimeout("popUpSequence(1, "+Math.min((popUpStep+popupOStep), popupOMax)+")", popupODelay);
				else {	// Get ready for step 2
					setTimeout("popUpSequence(2, 0)", popupODelay);
				}
				break;
			case 2: // Expand the box for the image
				popUpRatio=popUpStep/100; //alert (popUpBeginH+" "+popUpDeltaH+" "+popUpRatio);
				popUp.style.top="-"+Math.floor((popUpBeginH+popUpDeltaH*popUpRatio)/2)+"px";
				popUp.style.left="-"+Math.floor((popUpBeginW+popUpDeltaW*popUpRatio)/2)+"px";
				popUp.style.height=Math.floor(popUpBeginH+popUpDeltaH*popUpRatio)+"px";
				popUp.style.width=Math.floor(popUpBeginW+popUpDeltaW*popUpRatio)+"px";
				if (popUpStep==0) {
					popUp.style.display="block";
					popUp.style.opacity = 1;
					popUp.style.filter = 'alpha(opacity=100)';
				}
				if (popUpStep<100) setTimeout("popUpSequence(2, "+Math.min((popUpStep+popupOStep), 100)+")", popupODelay);
				else {	// Get ready for step 3
					popUpI.style.display="block";
					if (popUpCaptionAtBottom) {
						popUpComment.style.top=Math.floor((popUpBeginH+popUpDeltaH)/2)+"px";
						popUpComment.style.left="-"+Math.floor((popUpBeginW+popUpDeltaW)/2)+"px";
//						popUpComment.style.height="50px";
						popUpComment.style.width=Math.floor(popUpBeginW+popUpDeltaW)+"px";
					} else {
						popUpComment.style.top="-"+Math.floor((popUpBeginH+popUpDeltaH)/2-15)+"px";
						popUpComment.style.left="-"+Math.floor((popUpBeginW+popUpDeltaW)/2-15)+"px";
						popUpComment.style.height=Math.floor(popUpBeginH+popUpDeltaH-40)+"px";
						popUpComment.style.width=Math.floor(popUpBeginW+popUpDeltaW-40)+"px";
					}
					popUpCoverObj.style.top="-"+Math.floor((popUpBeginH+popUpDeltaH)/2)+"px";
					popUpCoverObj.style.left="-"+Math.floor((popUpBeginW+popUpDeltaW)/2)+"px";
					popUpCoverObj.style.height=Math.floor(popUpBeginH+popUpDeltaH)+"px";
					popUpCoverObj.style.width=Math.floor(popUpBeginW+popUpDeltaW)+"px";
//					popUpCoverImg.style.height=Math.floor(popUpBeginH+popUpDeltaH)+"px";
//					popUpCoverImg.style.width=Math.floor(popUpBeginW+popUpDeltaW)+"px";
					popUpCoverLImg.style.height=Math.floor(popUpBeginH+popUpDeltaH)+"px";
					popUpCoverLImg.style.width=Math.floor((popUpBeginW+popUpDeltaW)/2+1)+"px";
					popUpCoverRImg.style.height=Math.floor(popUpBeginH+popUpDeltaH)+"px";
					popUpCoverRImg.style.width=Math.floor((popUpBeginW+popUpDeltaW)/2)+"px";
					popUpCoverObj.style.display="block";
					setTimeout("popUpSequence(3, 0)", popupODelay);
				}
				break;
			case 3:	// Fade in the image
				popUpI.style.opacity = popUpStep/100;
				popUpI.style.filter = 'alpha(opacity=' + popUpStep + ')';
				if (popUpStep<100) setTimeout("popUpSequence(3, "+Math.min((popUpStep+popupOStep), 100)+")", popupODelay);
				else {
					popUpBeginH=popUpBeginH+popUpDeltaH;		// Set up for the next time
					popUpBeginW=popUpBeginW+popUpDeltaW;		// Set up for the next time
					popUpButtonsLeftNow=(popUpButtonsLeftFlag && popupImageArray[popupCurrentImageIndex][7]!=-1);
					popUpButtonsRightNow=(popUpButtonsRightFlag && popupImageArray[popupCurrentImageIndex][8]!=-1);
					setTimeout("popUpSequence(4, 0)", popupODelay);
				}
				break;
			case 4: // Position the buttons
				if (popUpButtonsCloseFlag) {
					popUpBClose.style.opacity = popUpStep/100;
					popUpBClose.style.filter = 'alpha(opacity=' + popUpStep + ')';
					if (popUpStep==0) popUpBClose.style.display="block";
				}
				if (popUpButtonsLeftNow) {
					popUpBLeft.style.opacity = popUpStep/100;
					popUpBLeft.style.filter = 'alpha(opacity=' + popUpStep + ')';
					// if (popUpStep==0) popUpBLeft.style.display="block";
				}
				if (popUpButtonsRightNow) {
					popUpBRight.style.opacity = popUpStep/100;
					popUpBRight.style.filter = 'alpha(opacity=' + popUpStep + ')';
					// if (popUpStep==0) popUpBRight.style.display="block";
				}
				if (!popUpCaptionAtBottom && popUpButtonsCommentFlag) {
					popUpBComment.style.opacity = popUpStep/100;
					popUpBComment.style.filter = 'alpha(opacity=' + popUpStep + ')';
					if (popUpStep==0) popUpBComment.style.display="block";
				}
				if (popUpStep<100) setTimeout("popUpSequence(4, "+Math.min((popUpStep+popupOStep), 100)+")", popupODelay);
				else {
					popUpButtonsActive=true;
					if (popUpCaptionAtBottom) popUpSequence(8, 0);
				}
				break;
			case 5: // Fade out image
				if (popUpStep==0) {	// Some things to do only the first time
					//	Drop the buttons
					if (popUpButtonsCloseFlag) popUpBClose.style.display="none";
					if (popUpButtonsLeftFlag) popUpBLeft.style.display="none";
					if (popUpButtonsRightFlag) popUpBRight.style.display="none";
					popUpBComment.style.display="none";
					popUpComment.style.display="none";
				}
				popUpI.style.opacity = (100-popUpStep)/100;
				popUpI.style.filter = 'alpha(opacity=' + (100-popUpStep) + ')';
				if (popUpStep<100) setTimeout("popUpSequence(5, "+Math.min((popUpStep+popupOStep), 100)+")", popupODelay);
				else {
					popUpI.style.display="none";
					popUpI.src=popUpImageFolder+popUpImageFile;
					popUpI.setAttribute('height', finalPopUpHeight);
					popUpI.setAttribute('width', finalPopUpWidth);
					popUpCoverObj.style.display="none";
					setTimeout("popUpSequence(2, 0)", popupODelay);
				}
				break;
			case 6:	// Fade in the comment box
				popUpComment.style.opacity = popUpStep/100;
				popUpComment.style.filter = 'alpha(opacity=' + popUpStep + ')';
				if (popUpStep==0)  { popUpComment.innerHTML=popUpCurrentComment+"<p><a href=\"#\" onclick=\"popUpHideComment(); return false;\">Close</a></p>"; popUpComment.style.display="block";  }
				if (popUpStep<93) setTimeout("popUpSequence(6, "+Math.min((popUpStep+popupOStep), 93)+")", popupODelay);
				break;
			case 7:	// Fade out the comment box
				popUpComment.style.opacity = (100-popUpStep)/100;
				popUpComment.style.filter = 'alpha(opacity=' + (100-popUpStep) + ')';
				if (popUpStep<100) setTimeout("popUpSequence(7, "+Math.min((popUpStep+popupOStep), 100)+")", popupODelay);
				else popUpComment.style.display="none";
				break;
			case 8: // Turn on the comment box at the bottom
				if (popUpCurrentComment.length>0) {
					popUpComment.innerHTML=popUpCurrentComment;
					popUpComment.style.display="block"; 
					popUpComment.style.opacity = 1;
					popUpComment.style.filter = 'alpha(opacity=100)';
//					popUpComment.style.height="0px";
				}
				break;
		}
	}
	function dropUp(popUpStage, popUpStep) {	// Fade out the image and the box.
		switch(popUpStage) {
			case 1: // Fade out image
				if (popUpStep==0) {	// Some things to do only the first time
					popUpButtonsActive=false;
					//	Drop the buttons
					if (popUpButtonsCloseFlag) popUpBClose.style.display="none";
					if (popUpButtonsLeftFlag) popUpBLeft.style.display="none";
					if (popUpButtonsRightFlag) popUpBRight.style.display="none";
					popUpComment.style.display="none";
					popUpBComment.style.display="none";
					popUpBeginW=0;		// Set up for the next time
					popUpBeginH=0;		// Set up for the next time
					popUpCurrentlyDisplayed=false;
				}
				popUp.style.opacity = (100-popUpStep)/100;
				popUp.style.filter = 'alpha(opacity=' + (100-popUpStep) + ')';
				if (popUpStep<100) setTimeout("dropUp(1, "+Math.min((popUpStep+popupOStep), 100)+")", popupODelay);
				else {
					popUpCoverObj.style.display="none";
					setTimeout("dropUp(2, 0)", popupODelay);
				}
				break;
			case 2: // Fade out background
				popupRevealBkgd.style.opacity = (popupOMax-popUpStep)/100;
				popupRevealBkgd.style.filter = 'alpha(opacity=' + (popupOMax-popUpStep) + ')';
				if (popUpStep<popupOMax) setTimeout("dropUp(2, "+Math.min((popUpStep+popupOStep), popupOMax)+")", popupODelay);
				else {
					popupRevealBkgd.style.display="none";
					popUp.style.display="none";
				}
				break;
		}
	}

	function popUpShow(i) {
		popupCurrentImageIndex=i;
		popUpFriend(popupImageArray[i][0], popupImageArray[i][1], popupImageArray[i][2], popupImageArray[i][3]);
	}

	function popUpIncrement(i) {
		if (popupImageArray[popupCurrentImageIndex][7+i]!=-1)
			popUpShow(popupImageArray[popupCurrentImageIndex][7+i]);
	}
	
	function popUpShowComment() {
		popUpSequence(6, 0);
	}
	function popUpHideComment() {
		popUpSequence(7, 0);
	}
