/*************************************************
 File			: Popup.js
 Description: Javascript to manage popup windows
 Date			: 4/30/2008
 Project		: kajames.ca/kajames.com
 ================================================
 Updates:
*************************************************/

function popup_window(puID, puWidth, puHeight, puTop, puLeft, puIsModal, puTitleText, puContent, puIframeSrc, puAnimate) {
	if (puIsModal) {
		var modalLayer = document.createElement("div");
		modalLayer.id = puID + "Modal";
		modalLayer.className = "popupModalLayer";
		modalLayer.style.zIndex = 10;
		var arrPageSize = getPageSizeWithScroll();
		var layerHeight = arrPageSize[1];
		if ((window.innerHeight) && (layerHeight < window.innerHeight)) layerHeight = window.innerHeight;
		if ((document.documentElement) && (document.documentElement.clientHeight) && (layerHeight < document.documentElement.clientHeight)) layerHeight = document.documentElement.clientHeight;
		modalLayer.style.height = layerHeight + "px";
		
		document.body.appendChild(modalLayer);
	} 
	
	var newPopup = document.createElement("div");
	newPopup.id = puID;
	newPopup.className = "popup";
	newPopup.style.zIndex = 20;
	
	var topOuter = document.createElement("div");
	topOuter.className = "PopupTopOuter";
	topOuter.style.width = puWidth;
	newPopup.appendChild(topOuter);
	
	// The rounded top corners
	var popupTop = document.createElement("b");
	popupTop.className = "PopupTop";
	var tempNode = document.createElement("b"); tempNode.className = "PopupTopR1"; popupTop.appendChild(tempNode);	
	tempNode = document.createElement("b"); tempNode.className = "PopupTopR2"; popupTop.appendChild(tempNode);	
	tempNode = document.createElement("b"); tempNode.className = "PopupTopR3"; popupTop.appendChild(tempNode);	
	tempNode = document.createElement("b"); tempNode.className = "PopupTopR4"; popupTop.appendChild(tempNode);	
	tempNode = document.createElement("b"); tempNode.className = "PopupTopR5"; popupTop.appendChild(tempNode);	
	topOuter.appendChild(popupTop);
	
	// The title
	if ((puTitleText!=null) && (puTitleText!="")) {
   	var titleDiv = document.createElement("div");
   	titleDiv.className = "PopupTitle";
   	titleDiv.onmousedown = function() { SetToDrag(puID,'BodyNavDiv'); };
   	var closeAnchor = document.createElement("a");
   	closeAnchor.className = "PopupCloseBtn";
   	closeAnchor.href = "javascript:close_popup('" + puID + "');";
   	closeAnchor.title = "Close window";
   	closeAnchor.style.border = "none";
   	closeAnchor.onmousedown = function(event) { if (event) event.cancelBubble=true; };
   	var closeImg = document.createElement("img");
   	closeImg.src = "http://kajames.ca/images/PopupClose2.gif";
   	closeImg.alt = "[X]";
   	closeImg.title = "Close window";
   	closeImg.style.border = "none";
   	closeAnchor.appendChild(closeImg);
   	titleDiv.appendChild(closeAnchor);
   	var titleText = document.createTextNode(puTitleText);
   	titleDiv.appendChild(titleText);
   	
   	topOuter.appendChild(titleDiv);
	}
	
	// Content div
	var contentDiv = document.createElement("div");
	contentDiv.className = "PopupTopInner";
	contentDiv.style.height = puHeight;
	if ((puContent!=null) && (puContent!="")) contentDiv.innerHTML = puContent;
	
	// See if we're using an iframe as source
	var ifrmContent = null;
	if ((puIframeSrc!=null) && (puIframeSrc!="")) {
		ifrmContent = document.createElement("iframe");
		ifrmContent.src = puIframeSrc;
		ifrmContent.width = "100%";
		ifrmContent.height = puHeight;
		ifrmContent.style.overflow = "auto";
		ifrmContent.style.border = "none";
		contentDiv.appendChild(ifrmContent);
	}
	
	topOuter.appendChild(contentDiv);	
	
	// Rounded bottom corners
	var popupBottom = document.createElement("b");
	popupBottom.className = "PopupBottom";
	tempNode = document.createElement("b"); tempNode.className = "PopupTopR5"; popupBottom.appendChild(tempNode);	
	tempNode = document.createElement("b"); tempNode.className = "PopupTopR4"; popupBottom.appendChild(tempNode);	
	tempNode = document.createElement("b"); tempNode.className = "PopupTopR3"; popupBottom.appendChild(tempNode);	
	tempNode = document.createElement("b"); tempNode.className = "PopupTopR2"; popupBottom.appendChild(tempNode);	
	tempNode = document.createElement("b"); tempNode.className = "PopupTopR1"; popupBottom.appendChild(tempNode);	
	topOuter.appendChild(popupBottom);
	
	if ((puTop=="center") || (puLeft=="center")) {
   	var winSize = GetWindowSize();
   	var winScroll = getScrollXY();
   	var newTop = puTop;
   	if (puTop == "center") {
   		newTop = ((winSize[1]-parseInt(puHeight))/2 + winScroll[1]) + "px"; 
   	}
   	var newLeft = puLeft;
   	if (puLeft == "center") {
   		newLeft = ((winSize[0]-parseInt(puWidth))/2 + winScroll[0]) + "px"; 
   	}
   	newPopup.style.top = newTop;
   	newPopup.style.left = newLeft;
	} else {
   	newPopup.style.top = puTop;
   	newPopup.style.left = puLeft;	
	}
	
	if (((puAnimate=="Vertical") || (puAnimate=="Both")) &&
	 	 ((navigator.appName!="Microsoft Internet Explorer") || (window.XMLHttpRequest))) {
		contentDiv.style.height = "10px";
		if (ifrmContent!=null) ifrmContent.style.display = "none";
	}
	
	document.body.appendChild(newPopup);
	
	if ((puAnimate=="Vertical") && ((navigator.appName!="Microsoft Internet Explorer") || (window.XMLHttpRequest))) {
		animate_popup(newPopup, contentDiv, ifrmContent, puHeight);
	} else {	
		shadowItem(newPopup);
	}	
}

function close_popup(puID) {
	var puWindow = document.getElementById(puID);
	if (puWindow) document.body.removeChild(puWindow);
	var puModal = document.getElementById(puID + "Modal");
	if (puModal) document.body.removeChild(puModal);
}

function shadowItem(itm) {
	if ((navigator.appName!="Microsoft Internet Explorer") || (window.XMLHttpRequest)) {
   	var intWidth = itm.offsetWidth;
   	var intHeight = itm.offsetHeight;
   	itm.style.width = (itm.offsetWidth + 6) + "px";
   	itm.style.height = (itm.offsetHeight + 6) + "px";
   	itm.style.backgroundPosition = "bottom right";
   	itm.style.backgroundRepeat = "no-repeat";
   	itm.style.backgroundImage = "url(http://kajames.ca/imageTools/DropShadow.php?size=10&margin=3&width=" + intWidth + "&height=" + intHeight + ")";
	}
}

var PopupItems = [];
var ContentDivs = [];
var IFrameContents = [];
var MaxHeights = [];

function animate_popup(popup, contentDiv, ifrmContent, puHeight) {
	var idxPopup = PopupItems.length;
	PopupItems[idxPopup] = popup;
	var idxCDiv = ContentDivs.length;
	ContentDivs[idxCDiv] = contentDiv;
	var idxIFrm = IFrameContents.length;
	IFrameContents[idxIFrm] = ifrmContent;
	var idxMaxH = MaxHeights.length;
	MaxHeights[idxMaxH] = puHeight;
	contentDiv.style.height = "10px";
	if (ifrmContent!=null) ifrmContent.style.display = "none";
	setTimeout("expand_popup(20, " + idxPopup + ", " + idxCDiv + ", " + idxIFrm + ", " + idxMaxH + ")", 1);
}

function expand_popup(intNewHeight, idxPopup, idxCDiv, idxIFrm, idxMaxH) {
	var objContentDiv = ContentDivs[idxCDiv];
	objContentDiv.style.height = intNewHeight + "px";
	intNewHeight += 15;
	var intMaxHeight = parseInt(MaxHeights[idxMaxH]);
	if (intNewHeight < intMaxHeight) {
		setTimeout("expand_popup(" + intNewHeight + ", " + idxPopup + ", " + idxCDiv + ", " + idxIFrm + ", " + idxMaxH + ")", 1);
	} else {
		objContentDiv.style.height = MaxHeights[idxMaxH];
		var objIFrm = IFrameContents[idxIFrm];
		if (objIFrm!=null) objIFrm.style.display = "";
		shadowItem(PopupItems[idxPopup]);
		
		PopupItems.remove(idxPopup);
		ContentDivs.remove(idxCDiv);
		IFrameContents.remove(idxIFrm);
		MaxHeights.remove(idxMaxH);
	}
}