
function Popup(sizeX, sizeY) {
	this.sizeX = sizeX;
	this.sizeY = sizeY;
	this.opacity = 0;
	this.activeContentId = 0;
	this.activeDomId = 0;
}

Popup.prototype.Show = function() {
	// stop image fading first
	if(document.getElementById("moodImage")) {
		stopImageFader();
	}

	this.darkener = document.getElementById("mainDarkenerDiv");

	this.popupContainer = document.getElementById("popupContainer");

	this.popupCloser = document.getElementById("popupCloser");
	this.edgeTopLeft = document.getElementById("popupEdgeTopLeft");
	this.edgeTop = document.getElementById("popupEdgeTop");
	this.edgeTopRight = document.getElementById("popupEdgeTopRight");
	this.edgeLeft = document.getElementById("popupEdgeLeft");
	this.edgeRight = document.getElementById("popupEdgeRight");
	this.edgeBottomLeft = document.getElementById("popupEdgeBottomLeft");
	this.edgeBottom = document.getElementById("popupEdgeBottom");
	this.edgeBottomRight = document.getElementById("popupEdgeBottomRight");
	this.contentContainer = document.getElementById("popupContentContainer");

	this.darkener.style.visibility = "visible";

	if(document.all) {
		this.popupContainer.style.left = (document.body.clientWidth / 2) - (this.sizeX/2) + "px";
		this.popupContainer.style.top = (document.getElementById("mainDarkenerDiv").clientHeight / 2) - (this.sizeY/2) + getScrollXY()[1] + "px";
		if(Number(this.popupContainer.style.top.split("px")[0]) < 0) {
			this.popupContainer.style.top = "0px";
		}
	} else {
		this.popupContainer.style.left = (window.innerWidth / 2) - (this.sizeX/2) + "px";
		this.popupContainer.style.top = (window.innerHeight / 2) - (this.sizeY/2) + getScrollXY()[1] + "px";
		if(Number(this.popupContainer.style.top.split("px")[0]) < 0) {
			this.popupContainer.style.top = "0px";
		}
	}


	activePopup = this;


	this.FadeIn();
}

Popup.prototype.Hide = function() {

	// clear content
	activePopup.contentContainer.innerHTML = "";

	this.darkener = document.getElementById("mainDarkenerDiv");

	this.darkener.style.filter = "Alpha(opacity=0)";
	this.darkener.style.opacity = 0.0;
	this.darkener.style.MozOpacity = 0.0;

	this.darkener.style.visibility = "hidden";

	this.opacity = 0;

	this.popupCloser.style.visibility = "hidden";
	this.edgeTopLeft.style.visibility = "hidden";
	this.edgeLeft.style.visibility = "hidden";
	this.edgeBottomLeft.style.visibility = "hidden";
	this.edgeTop.style.visibility = "hidden";
	this.edgeTopRight.style.visibility = "hidden";
	this.contentContainer.style.visibility = "hidden";
	this.edgeRight.style.visibility = "hidden";
	this.edgeBottom.style.visibility = "hidden";
	this.edgeBottomRight.style.visibility = "hidden";

	activePopup.ResetPopupSize();

	// restart image fading
	if(document.getElementById("moodImage")) {
		startImageFader();
	}
}

Popup.prototype.FadeIn = function() {

	activePopup.contentContainer.style.paddingTop = 0 + "px";

	activePopup.darkener.style.filter = "Alpha(opacity="+activePopup.opacity+")";
	activePopup.darkener.style.opacity = activePopup.opacity / 100;
	activePopup.darkener.style.MozOpacity = activePopup.opacity / 100;

	if(activePopup.opacity < 60) {
		activePopup.opacity += 10;
		window.setTimeout(activePopup.FadeIn, 30);
	} else {

		activePopup.edgeTopLeft.style.visibility = "visible";
		activePopup.edgeLeft.style.visibility = "visible";
		activePopup.edgeBottomLeft.style.visibility = "visible";
		activePopup.edgeTop.style.visibility = "visible";
		activePopup.edgeTopRight.style.visibility = "visible";
		activePopup.contentContainer.style.visibility = "visible";
		activePopup.edgeRight.style.visibility = "visible";
		activePopup.edgeBottom.style.visibility = "visible";
		activePopup.edgeBottomRight.style.visibility = "visible";

		activePopup.makeSizeX();
	}
}

Popup.prototype.makeSizeX = function() {
	activePopup.currentXSize = Number(activePopup.edgeTop.style.width.split("px")[0]);
	activePopup.xDiff = activePopup.sizeX - activePopup.currentXSize;
	activePopup.currentXSize += (activePopup.xDiff/3);

	activePopup.popupCloser.style.left = (activePopup.currentXSize - 83) + "px";
	activePopup.edgeTop.style.width = activePopup.currentXSize + "px";
	activePopup.edgeTopRight.style.left = (8 + activePopup.currentXSize) + "px";
	activePopup.contentContainer.style.width = (8 + activePopup.currentXSize) + "px";
	activePopup.edgeRight.style.left = (8 + activePopup.currentXSize) + "px";
	activePopup.edgeBottom.style.width = activePopup.currentXSize + "px";
	activePopup.edgeBottomRight.style.left = (8 + activePopup.currentXSize) + "px";

	if(activePopup.xDiff > 2) {
		window.setTimeout(activePopup.makeSizeX, 30);
	} else {
		activePopup.makeSizeY();
	}
}

Popup.prototype.makeSizeY = function() {
	activePopup.currentYSize = Number(activePopup.edgeLeft.style.height.split("px")[0]);
	activePopup.yDiff = activePopup.sizeY - activePopup.currentYSize;
	activePopup.currentYSize += (activePopup.yDiff/3);

	activePopup.edgeLeft.style.height = activePopup.currentYSize + "px";
	activePopup.contentContainer.style.height = activePopup.currentYSize + "px";
	activePopup.edgeRight.style.height = activePopup.currentYSize + "px";
	activePopup.edgeBottomLeft.style.top = (8 + activePopup.currentYSize) + "px";
	activePopup.edgeBottom.style.top = (8 + activePopup.currentYSize) + "px";
	activePopup.edgeBottomRight.style.top = (8 + activePopup.currentYSize) + "px";

	if(activePopup.yDiff > 2) {
		window.setTimeout(activePopup.makeSizeY, 30);

	}  else {
		activePopup.contentContainer.style.height = (activePopup.currentYSize-24) + "px";
		activePopup.contentContainer.style.paddingTop = 24 + "px";
		activePopup.popupCloser.style.visibility = "visible";
		getPopupContent(activePopup.activeContentId, activePopup.activeDomId);
	}
}

Popup.prototype.ResetPopupSize = function() {
	activePopup.edgeTop.style.width = "10px";
	activePopup.edgeTopRight.style.left = "18px";
	activePopup.edgeLeft.style.height = "10px";
	activePopup.contentContainer.style.width = "10px";
	activePopup.contentContainer.style.height = "10px";
	activePopup.edgeRight.style.height = "10px";
	activePopup.edgeRight.style.left = "18px";
	activePopup.edgeBottomLeft.style.top = "18px";
	activePopup.edgeBottom.style.width = "10px";
	activePopup.edgeBottom.style.top = "18px";
	activePopup.edgeBottomRight.style.left = "18px";
	activePopup.edgeBottomRight.style.top = "18px";
}

function getScrollXY() {
    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;
    }
    return [ scrOfX, scrOfY ];
}

function changePopupContent(id) {
	activePopup.contentContainer.innerHTML = "";
	getPopupContent(id, activePopup.activeDomId);
}

var activePopupContent;

function getPopupContent(id, dom) {
	activePopupContent = id;

	if((""+id).split("-")[0] == 'list') {
		http_request.open('POST', '/dyn/GetFrontNewsList.cfm', true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		if(id.split("-")[1]){
			http_request.send('&dom='+dom+'&page='+id.split("-")[1]);
		}else{
			http_request.send('&dom='+dom);
		}
		http_request.onreadystatechange = handlePopupContent;
	} else if(id == -99) {
		if (window.ActiveXObject) {
			http_request.open('POST', '/rpc/LoadShowtimesContent_ie.cfm', true);
		} else {
			http_request.open('POST', '/rpc/LoadShowtimesContent.cfm', true);
		}
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send('&dom='+dom);
		http_request.onreadystatechange = handlePopupContent;
	} else if(id.toString().indexOf('img') != -1) {
		/*if (window.ActiveXObject) {
			http_request.open('POST', '/rpc/LoadPopupContent_ie.cfm', true);
		} else {
			http_request.open('POST', '/rpc/LoadPopupContent.cfm', true);
		}
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send('&id='+id);
		http_request.onreadystatechange = handlePopupContent;*/

		http_request.open('POST', '/rpc/LoadPopupImage.cfm', true);

		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send('&id='+id);
		http_request.onreadystatechange = handlePopupContent;
		
		//alert(getScrollXY());
	} else {
		if (window.ActiveXObject) {
			http_request.open('POST', '/rpc/LoadPopupContent_ie.cfm', true);
		} else {
			http_request.open('POST', '/rpc/LoadPopupContent.cfm', true);
		}
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send('&id='+id);
		http_request.onreadystatechange = handlePopupContent;
	}
}

function handlePopupContent() {

	if(http_request.readyState == 4)	{
		if(http_request.status == 200) {

			//req.responseText represents the returning text from the server
			var response = http_request.responseText;
			var xmlDoc;
			var value;
			response = unescape(response);
			//alert(response);   
			//
			//for IE
			if (window.ActiveXObject) {

				//crate xml docuemnt object in IE
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = "false";
				
				if(response.split("|")[1]){
					xmlDoc.loadXML(response.split("|")[1]);
				}else{
					xmlDoc.loadXML(response);
				}
				var x = xmlDoc.documentElement;
				value = x.childNodes[0].nodeValue;

			}
			//for Firefox
			else {
				if(http_request.responseText.split("|")[1]){
					xmlDoc = http_request.responseText.split("|")[1];			
					xmlDoc = (new DOMParser()).parseFromString(xmlDoc, "text/xml");
				}else{
					xmlDoc = http_request.responseXML;
				}
				
				var element = xmlDoc.getElementsByTagName('resp').item(0);
                         
				value = element.childNodes[0].nodeValue; 
			}

			activePopup.contentContainer.innerHTML = value;

			if(activePopupContent == -99) {
				initCalendar();
			}

		} else {
			alert("Error: returned status code " + http_request.status + " " + http_request.statusText);
		}
	}
}


var p = new Popup(10,10);

function showPopup(id, sX,sY, dom) {
	p.sizeX = sX
	p.sizeY = sY;
	p.activeContentId = id;
	p.activeDomId = dom;
	p.Show();
}

function hidePopup() {
	p.Hide();
}