//-- custom "lightbox" popup --//
// 

// need utility library

//--------------

// set global vars
var fadeId;
var boxOpacity = 0;
var boxDir;

var resizeWidthId;
var resizeHeightId;
var popupSize; // array
var tempWidth;
var tempHeight;

var statusId;
statusHeight = 0;

var visLoadId;

var popUrl;

//--
 
var frameIndex = 0;

//--
var popupRelativePaths = ['html/images/nopage.htm', 'html/images/pop_close.png', 'html/images/pop_loading.gif'];


function createDynPop() {
    createDynPop('');
}

function createDynPop(root) {

    if (document.getElementById('popupdiv') == null) {

	    var din0 = document.createElement("div");
	    din0.id = "popupdiv";
	    din0.style.display = "none";

	    // set w/h of popup back to previous size
	    if (tempWidth == undefined) {
	    din0.style.width = "200px";
	    } else {
	    din0.style.width = parseInt(tempWidth)+"px";
	    }

	    if (tempHeight == undefined) {
	    din0.style.height = "200px";
	    } else {
	    din0.style.height = parseInt(tempHeight)+"px";
	    }

	    var newiframe = document.createElement("iframe");
	    newiframe.id = "popupiframe";
	    newiframe.name = "popupiframe";
	    newiframe.src = root + popupRelativePaths[0];   //"images/nopage.htm";
	    newiframe.frameBorder = "no";
	    din0.appendChild(newiframe,null);

	    var din1 = document.createElement("div");
	    din1.id = "popupstatus";
	    din1.style.height = "0px";
	    din0.appendChild(din1,null);

	    var din2 = document.createElement("div");
	    din2.id = "popupcaption";
	    din1.appendChild(din2,null);

	    var din3 = document.createElement("div");
	    din3.id = "popupclose";
	    din1.appendChild(din3,null);

	    var img1 = document.createElement("img");
	    img1.id = "popupcloser";
	    img1.src = root + popupRelativePaths[1];   //"images/pop_close.png";
	    img1.width = "67";
	    img1.height = "17";
	    img1.alt = "";
	    img1.onclick = removePop;
	    din3.appendChild(img1,null);

	    var img2 = document.createElement("img");
	    img2.id = "visimage";
	    img2.src = root + popupRelativePaths[2];   //"images/pop_loading.gif";
	    img2.width = "32";
	    img2.height = "32";
	    img2.alt = "";
	    img2.style.display = "none";
	    din0.appendChild(img2,null);

	    document.getElementById('popholder').appendChild(din0,null);
    }
}

function removeDynPop() {

    if (document.getElementById('popupdiv')) {

    var pHolder = document.getElementById('popholder');
    var oldDiv = document.getElementById('popupdiv');
    pHolder.removeChild(oldDiv);

    //alert(document.getElementById('popholder').innerHTML);
    }

}




function showPop(url,w,h,caption) {

    showRootPop('', url, w, h, caption);
}

function showRootPop(root, url, w, h, caption) {

    // dynamically creates popup | obviously
    createDynPop(root);

    document.getElementById('coverdiv').style.display = "block";
    document.getElementById('coverdiv').style.height = getBodyHeight() + "px";

    //if (isIE7()) {
    //document.getElementById('coverdiv').style.height=document.documentElement.scrollHeight+"px";
    //}

    if (vIE() != -1) {
        document.getElementById('coverdiv').style.height = document.documentElement.scrollHeight + "px";
    }


    popupSize = new Array(w, h);
    setTxt('popupcaption', caption);

    openPop(root+url, w, h);
    startFade('in');
}


function adjustPop(url,w,h,caption) {

	if (window.frames[frameIndex].location == null) {
		// firefox
	    //			window.frames[frameIndex].location = "images/membersblank.php";
	} else {
	//	window.frames[frameIndex].location = "images/membersblank.php";
	}


	popupSize = new Array(w, h);
	setTxt('popupcaption',caption);

	openPop(url,w,h);
}


function removePop() {
	closePop();
	startFade('out');
}



function startVisLoad() {
	document.getElementById('visimage').style.display = "";
	clearInterval(visLoadId);
	visLoadId = setInterval(doVisLoad,1000);
}


function doVisLoad() {
	var popupVisObj = document.getElementById('visimage');

	try {
		var popIframe = window.frames[0];
		var iframePage = popIframe.location.href.substr(popIframe.location.href.lastIndexOf("/")+1);


		if (iframePage != "membersblank") {
			clearInterval(visLoadId);
			popupVisObj.style.display = "none";
		}

	} catch (err) {
		clearInterval(visLoadId);
		popupVisObj.style.display = "none";
	}
}



function startStatus() {

	// only use padding in ie
	var agt = navigator.userAgent.toLowerCase();
	if (agt.indexOf("msie") != -1) {
		//document.getElementById('popupstatus').style.padding = "0px 10px 0px 10px";
	}

	clearInterval(statusId);
	statusId = setInterval(doStatusOpen,100);
}


function doStatusOpen() {

	var statusObj = document.getElementById('popupstatus');

	if (getHeight(statusObj)<32) {
		statusHeight += (32-getHeight(statusObj))/3;
		statusObj.style.height = statusHeight+"px";
	} else {
		clearInterval(statusId);
	}


}



function startResizeWidth() {
	clearInterval(resizeWidthId);
	tempWidth = getWidth(document.getElementById('popupdiv'));
	resizeWidthId = setInterval(doResizeWidth,100);
}


function startResizeHeight() {
	clearInterval(resizeHeightId);
	tempHeight = getHeight(document.getElementById('popupdiv'));
	resizeHeightId = setInterval(doResizeHeight,100);
}



function doResizeWidth() {
	var popObj = document.getElementById('popupdiv');
	if (getWidth(popObj) != popupSize[0]) {
		var objWidth = getWidth(popObj);

		tempWidth += ((popupSize[0]-objWidth)/1.5);
		popObj.style.width = tempWidth+"px";

		// ie6 only | width 100% doesnt work / have to set width it as well
		document.getElementById('popupstatus').style.width = tempWidth + "px";// (tempWidth + 20) + "px";

		var screenWidth = getScreenSizeArr()[0];
		var newX = (screenWidth-getWidth(popObj))/2;
		popObj.style.left = newX+"px";
	} else {

		//window.frames[frameIndex].location.replace(popUrl);



		//alert(typeof window.frames[frameIndex].location);


		// check if iframe location object exists first



		//if (typeof window.frames[frameIndex].location == "object") {
		//alert('o 1');
		if (window.frames[frameIndex].location == null) {
		//alert('innerfox');
			// firefox
				window.frames[frameIndex].location.replace(popUrl);
		} else {
		//alert('innerie');
				window.frames[frameIndex].location.replace(popUrl);
			//window.frames['popupiframe'].location.replace(popUrl);
		}

		//} else {

		//alert('location doesnt exist');
		
		//}





		startStatus();
		clearInterval(resizeWidthId);
	}
}




function doResizeHeight() {
	var popObj = document.getElementById('popupdiv');
	if (getHeight(popObj) != popupSize[1]) {
		var objHeight = getHeight(popObj);

		tempHeight += ((popupSize[1]-objHeight)/1.5);
		popObj.style.height = tempHeight+"px";

		var screenHeight = getScreenSizeArr()[1];

		var newY = ((screenHeight-getHeight(popObj))/2) - 11;
		popObj.style.top = (newY+ getScrollTop())+"px";



		if ((popupSize[1] - getHeight(popObj)) < 1) {
			//startResizeWidth();
		}

	} else {
		startResizeWidth();
		clearInterval(resizeHeightId);
	}
}




function openPop(url, w, h) {
	var popObj = document.getElementById('popupdiv');

	var objWidth = getWidth(popObj);
	var objHeight = getHeight(popObj);

	var screenWidth = getScreenSizeArr()[0];
	var screenHeight = getScreenSizeArr()[1];

	var newX = (screenWidth-objWidth)/2;
	var newY = ((screenHeight-objHeight)/2) - 11;

	startVisLoad();

	if (objWidth == w && objHeight == h) {

		// same dimensions / no need to resize
		popObj.style.left = newX+"px";
		popObj.style.top = (newY + getScrollTop()) + "px"; 
		popObj.style.display = "";


		// firefox breaks when using name of frame - instead of index
		//alert(typeof window.frames['popupiframe'].location);

		// check if iframe location object exists first

		//if (typeof window.frames[frameIndex].location == "object") {
		//alert('o 2');
		if ( window.frames[frameIndex].location == null) {
			// firefox
				window.frames[frameIndex].location.replace(url);
		} else {
			window.frames[frameIndex].location.replace(url);
		}

		//} else {

		//alert('location doesnt exist');
		
		//}




		startStatus();

	} else {

		popObj.style.left = newX+"px";
		popObj.style.top = (newY + getScrollTop()) + "px";

		popObj.style.display = "";

		//window.frames['popupiframe'].location = url;

		popUrl = url;

		startResizeHeight();
	}

}




function closePop() {

	var popObj = document.getElementById('popupdiv');
	var statusObj = document.getElementById('popupstatus');


	//window.frames['popupiframe'].location.replace("/images/membersblank.php");
	// opera breaks here if window.location is changed / breaks sandbox
	
	statusHeight = 0;
	clearInterval(statusId);
	statusObj.style.height = "0px";

	clearInterval(resizeWidthId);
	clearInterval(resizeHeightId);
	clearInterval(visLoadId);
	
	popObj.style.display = "none";
	setTxt('popupcaption','');
}



function startFade(dir) {
	boxDir = dir;
	clearInterval(fadeId);
	fadeId = setInterval(doFade,100);
}



function doFade() {

var fadeObj = document.getElementById('coverdiv');

	if (boxDir == 'in') {

		if (boxOpacity != 80) {
			boxOpacity += 20;
			fadeObj.style.filter = "alpha(opacity="+boxOpacity+")";

			
			if (boxOpacity != 80) {
				//fadeObj.style.MozOpacity = "."+boxOpacity;
				fadeObj.style.opacity = "."+boxOpacity;
			} else {
				//fadeObj.style.MozOpacity = "."+parseInt(boxOpacity-1);
				fadeObj.style.opacity = "."+parseInt(boxOpacity-1);
			}

		} else {

			clearInterval(fadeId);
		}
		
	} else if (boxDir == 'out') {

		if (boxOpacity != 0) {
				boxOpacity -= 20;
				fadeObj.style.filter = "alpha(opacity="+boxOpacity+")";
				//fadeObj.style.MozOpacity = "."+boxOpacity;
				fadeObj.style.opacity = "."+boxOpacity;
		} else {

			clearInterval(fadeId);
			fadeObj.style.display = "none";
			// remove popup
			removeDynPop();

		}
		
	}
}
