/*****************************************************************************
*  File: Popup.js                                                            *
*  Author: Victor Andales                                                    *
*  Copyright: Copyright 2005 Victor Andales, vandalesm@gmail.com			 *
*  Licence: Free for personal and commercial use as long as copyright notice *
*           remains in place. Distribution prohibited without the express    *
*           permission of the owner (vandalesm@gmail.com)			         *
******************************************************************************/

var constFontMetrics=8;
var globalPopupZIndex=100;

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		popup_Shape_2_over = newImage("images/popup2/popup_Shape-2-over.gif");
		popup_Shape_2_down = newImage("images/popup2/popup_Shape-2-down.gif");
		preloadFlag = true;
	}
}

if(typeof(window.addEventListener) != "undefined") {
	window.addEventListener("load", preloadImages, false);
} else {
	window.attachEvent("onload", preloadImages);
}

function Popup(NAME, left, top, width, height) {
	this.name=NAME;
	this.IFRAME_ID = NAME + "_IFRAME_ID";
	this.DIV_ID = NAME + "_DIV_ID";
	this.DIVCL_ID = NAME + "_DIVCL_ID";
	this.CONTENT_DIV_ID = NAME + "_CONTENT_DIV_ID";
	this.TABLE_ID = NAME + "_TABLE_ID";
	this.TITLE_DIV_ID = NAME + "_TITLE_DIV_ID";
	this.left=left | 0;
	this.top=top | 0;
	this.width=width | 0;
	this.height=height | 0;
	this.title="&nbsp;";
	this.content="&nbsp;";
	this.mainContent="";
	this.xButton="";

	this.show=function() { showPopup(this); };
	this.hide=function() { hidePopup(this); };
	this.setup=function() { setPopup(this); };
	initialize(this);
}

function initialize(obj) {
	var vBody = document.getElementsByTagName("BODY").item(0);
	var vIFrame = document.createElement("IFRAME");
	vIFrame.setAttribute("id",obj.IFRAME_ID);
	vIFrame.width=obj.width+"px";
	vIFrame.height=obj.height+"px";
	vIFrame.style.left=obj.left+"px";
	vIFrame.style.top=obj.top+"px";
	vIFrame.style.border=0;
	vIFrame.frameBorder=0;
	vIFrame.style.position="absolute";
	vIFrame.style.display='none';
	var vDiv = document.createElement("DIV");
	vDiv.setAttribute("id", obj.DIV_ID);
	vDiv.style.position="absolute";
	vDiv.style.width=obj.width+"px";
	vDiv.style.height=obj.height+"px";
	vDiv.style.left=obj.left+"px";
	vDiv.style.top=obj.top+"px";
	vDiv.style.display='none';
	var vDivCl = document.createElement("DIV");
	vDivCl.setAttribute("id", obj.DIVCL_ID);
	vDivCl.style.position="absolute";
	vDivCl.style.width="240px";
	vDivCl.style.height=18+"px";
//	vDivCl.style.left=obj.left+5+"px";
//	vDivCl.style.left="10px";
//	vDivCl.style.border="1px solid #ff0000";

	vDivCl.style.top=obj.top+4+"px";
	vDivCl.style.display='none';
	vBody.appendChild(vIFrame);
	vBody.appendChild(vDiv);
	vBody.appendChild(vDivCl);

	setPopup(obj);
	vDiv.innerHTML=obj.mainContent;
vDivCl.innerHTML=obj.xButton;
}

function setPopup(obj) {
	var title=obj.title;
	var trimmed=false;

	obj.xButton =   "<A HREF='javascript:void(0)' style='cursor:default;'  onClick=\"hideByID('" + obj.name + "');\"" +
					"ONMOUSEOVER=\"changeImages('"+obj.name+"_popup_Shape_2', 'images/popup2/popup_Shape-2-over.gif'); return true;\" " +
					"ONMOUSEOUT=\"changeImages('"+obj.name+"_popup_Shape_2', 'images/popup2/popup_Shape-2-down.gif'); return true;\" " +
					"ONMOUSEDOWN=\"changeImages('"+obj.name+"_popup_Shape_2', 'images/popup2/popup_Shape-2-down.gif'); return true;\" " +
					"ONMOUSEUP=\"changeImages('"+obj.name+"_popup_Shape_2', 'images/popup2/popup_Shape-2-over.gif'); return true;\"> " +
					"<IMG NAME=\""+obj.name+"_popup_Shape_2\" SRC=\"images/popup2/popup_Shape-2-down.gif\" WIDTH=18 HEIGHT=18 BORDER=0 ALT='' align='right' style='padding-left:20px;'></A>";
	obj.mainContent="<TABLE id='"+obj.TABLE_ID+"' BORDER='0' CELLPADDING='0' CELLSPACING='0' width='"+obj.width+"' height='"+obj.height+"'>" + 
					"<TR><TD><IMG SRC='images/popup2/popup_07.gif' WIDTH='4' HEIGHT='3' ALT=''></TD>" +
					"	<TD background='images/popup2/popup_08.gif' height='3'></TD>" +
					"	<TD><IMG SRC='images/popup2/popup_09.gif' WIDTH='5' HEIGHT='3' ALT=''></TD>" +
					"</TR>" +
					"<TR><TD><IMG SRC='images/popup2/popup_11.gif' WIDTH='4' HEIGHT='20' ALT=''></TD>" +
					"	<TD background='images/popup2/popup_12.gif' height='20' nowrap><div id='"+obj.TITLE_DIV_ID+"' style=\"font-size:14px; font-family:'Trebuchet MS'; color:#2F4F4F; padding-left:22px; font-weight:bold;\">"+title+"</div></TD>" +
					"	<TD><IMG SRC='images/popup2/popup_13.gif' WIDTH='5' HEIGHT='20' ALT=''></TD>" +
					"</TR>" +
					"<TR><TD><IMG SRC='images/popup2/popup_14.gif' WIDTH='4' HEIGHT='7' ALT=''></TD>" +
					"	<TD background='images/popup2/popup_15.gif' height='7'></TD>" +
					"	<TD><IMG SRC='images/popup2/popup_16.gif' WIDTH='5' HEIGHT='7' ALT=''></TD>" +
					"</TR>" +
					"<TR><TD background='images/popup2/popup_17.gif' width='4'></TD>" +
					"	<TD background='images/popup2/popup_18.gif' height='100%' weight='100%' valign='baseline'><div id='"+obj.CONTENT_DIV_ID+"'></div></TD>" +
					"	<TD background='images/popup2/popup_19.gif' width='5'></TD>" +
					"</TR>" +
					"<TR><TD><IMG SRC='images/popup2/popup_20.gif' WIDTH='4' HEIGHT='5' ALT=''></TD>" +
					"	<TD background='images/popup2/popup_21.gif' height='5'></TD>" +
					"	<TD><IMG SRC='images/popup2/popup_22.gif' WIDTH='5' HEIGHT='5' ALT=''></TD>" +
					"</TR>" + 
					"</TABLE>";
}

function showPopup(obj) {
	var vIFrame = getObject(obj, obj.IFRAME_ID);
	var vDiv = getObject(obj, obj.DIV_ID);
	var vDivCl = getObject(obj, obj.DIVCL_ID);
	vIFrame.style.display='block';
	vDiv.style.display='block';
	vDivCl.style.display='block';
	setMemberZIndexToTop(obj);
	vIFrame.style.height=vDiv.offsetHeight;
	vIFrame.style.width=vDiv.offsetWidth;
}

function hidePopup(obj) {
	var vIFrame = getObject(obj, obj.IFRAME_ID);
	var vDiv = getObject(obj, obj.DIV_ID);
	var vDivCl = getObject(obj, obj.DIVCL_ID);
	setMemberZIndexToNormal(obj);
	vIFrame.style.display='none';
	vDiv.style.display='none';
	vDivCl.style.display='none';
}

function getObject(obj, id) {
	switch(id) {
		case obj.IFRAME_ID:
			return document.getElementById(obj.IFRAME_ID);
		case obj.DIV_ID:
			return document.getElementById(obj.DIV_ID);
		case obj.DIVCL_ID:
			return document.getElementById(obj.DIVCL_ID);
	}
}

function hideByID(name) {
	var IFrameID = name + "_IFRAME_ID";
	var DivID = name + "_DIV_ID";
	var DivClID = name + "_DIVCL_ID";

	document.getElementById(DivClID).style.display='none';
	document.getElementById(DivID).style.display='none';
	document.getElementById(IFrameID).style.display='none';
}

function setMemberZIndexToTop(obj) {
	var vIFrame = getObject(obj, obj.IFRAME_ID);
	var vDiv = getObject(obj, obj.DIV_ID);
	var vDivCl = getObject(obj, obj.DIVCL_ID);
	globalPopupZIndex++;
	vIFrame.style.zIndex=globalPopupZIndex;
	vDiv.style.zIndex=globalPopupZIndex;
	vDivCl.style.zIndex=globalPopupZIndex;
}

function setMemberZIndexToNormal(obj) {
	var vIFrame = getObject(obj, obj.IFRAME_ID);
	var vDiv = getObject(obj, obj.DIV_ID);
	var vDivCl = getObject(obj, obj.DIVCL_ID);
	vIFrame.style.zIndex=0;
	vDiv.style.zIndex=0;
	vDivCl.style.zIndex=0;
}

function setPopupTitle(obj, title) {
	obj.title=title;
	document.getElementById(obj.TITLE_DIV_ID).innerHTML=title;
}

function movePopup(obj, left, top) {
	var vIFrame = getObject(obj, obj.IFRAME_ID);
	var vDiv = getObject(obj, obj.DIV_ID);
	var vDivCl = getObject(obj, obj.DIVCL_ID);
	vIFrame.style.left=left+"px";
	vIFrame.style.top=top+"px";
	vDiv.style.left=left+"px";
	vDiv.style.top=top+"px";
	vDivCl.style.left=left+5+"px";
	vDivCl.style.top=top+4+"px";
	obj.left=left;
	obj.top=top;
}