/*
Module Name: wwPopPhoto.js
Module URI: http://highseasdrifter.net/blog/
Description: Utility for creating popup photos, styled with backgrounds, etc. Can be used in conjunction with Postie plugin.
Version: 1.1.1
Author: Jack Webb, WebbWebs Consulting
Author URI: http://highseasdrifter.net/blog/
*/

var skipFade=1; // Turn off FadeIn and FadeOut

function getScrollXY(which) {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	if(which=="x") { return scrOfX; }
	if(which=="y") { return scrOfY; }
	return [ scrOfX, scrOfY ];
}

function getWinW() {
	var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	// window.alert( 'Width = ' + myWidth );
	return myWidth;
}

function getWinH() {
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	// window.alert( 'Height = ' + myHeight );
	return myHeight;
}

function getWinHgt() {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}
	//alert( "Window width = "+winW+"\n" +"Window height = "+winH );
	return winH;
}
function getDocHgt() {
	var docH = (document.height !== undefined) ? document.height : document.body.offsetHeight;
	return docH;
}

function wwPopPhoto(imgPath,wid,hgt) {
	var cvrObj = document.getElementById('wwPopPhotoPageCover');
	if(cvrObj) { cvrObj.style.display = "block"; }
	var divObj = document.getElementById('wwPopPhotoDiv');
	var imgObj = document.getElementById('wwPopPhotoImg');
	var adjustH=0, adjustV=0;
	if( typeof( window.innerWidth ) == 'number' ) { // (Non-IE)
		// Compensate for differences in how IE handles dimensions
		adjustV = -10; adjustH = -10;
	}
	var scrlH = document.body.scrollHeight; //alert(scrlH);
	var scrlT = document.body.scrollTop; //alert(scrlT);
	if(divObj && imgObj) {
		imgObj.style.visibility = 'hidden';
		imgObj.src = "";
		var winVCtr = Math.round(getWinH()/2) + adjustV;
		var winHCtr = Math.round(getWinW()/2) + adjustH - 10; // -10 to Allow for scroll
		var newTop = Math.round( winVCtr - (hgt/2) );
		var newLft = Math.round( winHCtr - (wid/2) );
		var scrollY = getScrollXY("y");

		var hasMenu=0;
		var menuDiv = top.document.getElementById('headerMasterDiv');
		// This has been modified to handle images larger than the iframe or viewport height

		if(menuDiv) {
			if(top.iframeHgt>0) {
				if( document.body && document.body.clientHeight ) { //alert('isIE');
					divObj.style.top = (newTop+scrollY+100) + 'px';
				} else if(hgt>(top.iframeHgt+40)) {
					var myTop = document.body.scrollTop + 20;
					divObj.style.top = myTop + 'px';
				} else {
					divObj.style.top = (newTop+scrollY) + 'px';
				}
			} else {
				divObj.style.top = (newTop+scrollY) + 'px';
			}
		} else {
			var myHeight = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				myHeight = window.innerHeight;
			} else if( document.documentElement && document.documentElement.clientHeight ) {
				//IE 6+ in 'standards compliant mode'
				myHeight = document.documentElement.clientHeight;
			} else if( document.body && document.body.clientHeight ) {
				//IE 4 compatible
				myHeight = document.body.clientHeight;
			}

			if(hgt>(myHeight+40)) {
				var myTop = document.body.scrollTop + 20;
				divObj.style.top = myTop + 'px';
			} else {
				divObj.style.top = (newTop+scrollY) + 'px';
			}
		}
		
		divObj.style.left = (newLft-0) + 'px';
		imgObj.src = imgPath;
		imgObj.style.width = wid + 'px';
		imgObj.style.height = hgt + 'px';

		divObj.style.opacity = 0.1;
		imgObj.style.visibility = 'visible';
		divObj.style.visibility = 'visible';

		if(skipFade) {
			show_wwPopPhoto();
		} else {
			fadeIn_wwPopPhoto();
		}
		// alert(" imgPath="+imgPath+"\n wid="+wid+"\n hgt="+hgt+"\n winVCtr="+winVCtr+"\n winHCtr="+winHCtr+"\n newTop="+newTop+"\n newLft="+newLft+"\n scrollY="+scrollY+"\n");
	}
}

function hideDiv(id) {
	var obj = document.getElementById(id);
	if(obj) {
		obj.style.display = "none";
	}
}

function showDiv(id) {
	var obj = document.getElementById(id);
	if(obj) {
		obj.style.display = "block";
	}
}

function wwPopPhotoClose() {
		if(skipFade) {
			hide_wwPopPhoto();
		} else {
			fadeOut_wwPopPhoto()
		}
}

var glbInc, glbDec, delay = 0; /* PREV delay = 64 */

function fadeIn_wwPopPhoto() {
	if(glbInc != null) {
		clearTimeout(glbInc);
		glbInc = null;
	}
	var NewOpacity;
	var divObj = document.getElementById("wwPopPhotoDiv");
	if(divObj) {
		divObj.style.display = "block";
		/* CSS3 compliant; Mozilla 1.7alpha and higher, Safari 1.2, Opera 9 */		
		if(typeof divObj.style.opacity == "string") {
			if(parseFloat(divObj.style.opacity) < 1) {
				NewOpacity = roundNumber((parseFloat(divObj.style.opacity) + 0.1),1);
				divObj.style.opacity = (NewOpacity > 1.0) ? "1.0" : + NewOpacity;
				glbInc = setTimeout("fadeIn_wwPopPhoto()", delay);
			} else {
				return true;
			}
		}
	}
	return true;
}
function show_wwPopPhoto() {
	var cvrObj = document.getElementById("wwPopPhotoPageCover");
	if(cvrObj) { cvrObj.style.display = "block"; }
	var divObj = document.getElementById("wwPopPhotoDiv");
	divObj.style.display = "block";
	divObj.style.opacity = 1;
}
function hide_wwPopPhoto() {
	var cvrObj = document.getElementById("wwPopPhotoPageCover");
	if(cvrObj) { cvrObj.style.display = "none"; }
	var divObj = document.getElementById("wwPopPhotoDiv");
	divObj.style.display = "none";
	divObj.style.opacity = 0;
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function fadeOut_wwPopPhoto() {
	if(glbInc != null) {
		clearTimeout(glbInc);
		glbInc = null;
	}
	var NewOpacity;
	var divObj = document.getElementById("wwPopPhotoDiv");
	/* CSS3 compliant; Mozilla 1.7alpha and higher, Safari 1.2, Opera 9 */		
	if(typeof divObj.style.opacity == "string") {
		if(parseFloat(divObj.style.opacity) > 0) {
			NewOpacity = roundNumber((parseFloat(divObj.style.opacity) - 0.1),1);
			divObj.style.opacity = (NewOpacity < 0.0) ? "0.0" : + NewOpacity;
			glbDec = setTimeout("fadeOut_wwPopPhoto()", delay);
		} else {
			if(glbInc != null) {
				clearTimeout(glbInc);
				glbInc = null;
			}
			var cvrObj = document.getElementById("wwPopPhotoPageCover");
			if(cvrObj) { cvrObj.style.display = "none"; } else { alert('wwPopPhotoPageCover not found'); }
			var divObj = document.getElementById("wwPopPhotoDiv");
			if(divObj) { divObj.style.display = "none"; } else { alert('wwPopPhotoDiv not found'); }
			return true;
		}
	}
}

var appVersion=navigator.appVersion;
if(appVersion.indexOf("Safari")) { var isSafari=1; } else { var isSafari=0; }
