/***************************************************************************
*
*	Pseudo Popup - version 1.0
*	Author: Michael Turnwall
*
*	Copyright 2007 Michael Turnwall - Some Rights Reserved
*	http://creativecommons.org/licenses/GPL/2.0/
*
*	This program is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation (GPLv2)
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*	See http://creativecommons.org/licenses/GPL/2.0/ for more information.
*
***************************************************************************/

// possible values: width, height, pageOpacity, fadeSpeed, popupID, url
function PopUp(values)
{
	this.width = values.width;
	this.height = values.height;
	this.pageOpacity = values.pageOpacity;
	this.fadeSpeed = values.fadeSpeed;
	this.popupID = values.popupID;
	this.objName = "pseudoPop";
	this.opacityStart = this.pageOpacity/2;
}
/*----------------------*/

PopUp.prototype.createElement = function()
{
	this.div = document.createElement("div");
	if(!this.popupID)
		this.div.setAttribute("id","pseudoPopup");
	else
		this.div.setAttribute("id",this.popupID);
	this.div.style.display = "none";
	var linkage = document.createElement("a");
	linkage.setAttribute("href","#");
	linkage.setAttribute("id","closeLink");
	linkage.onclick = function(){popupObj.hideOverlay(); return false;};
	
	var text =  document.createTextNode("Close X");
	linkage.appendChild(text);
	this.div.appendChild(linkage);
	var htmlBody = document.getElementsByTagName("body").item(0);
	htmlBody.insertBefore(this.div,htmlBody.firstChild.nextSibling);

	this.getContent();
}
/*----------------------*/

PopUp.prototype.getContent = function()
{
	var regexp = new RegExp("(.gif|.jpg|.png)$");
	if(this.url.match(regexp))
	{
		var img = document.createElement("img");
		var imgObj = new Image();
		imgObj.onload = function()
		{
			img.setAttribute("src",imgObj.src);
			img.setAttribute("id","responseContent");
			popupObj.div.appendChild(img);
			if(img.width == 0 && img.height==0){var IEfix = true;} // this was added cuz IE wasnt seeing H and W of img, so if theyre empty we just set a default size later....no time to find the problem, so i patched it :(
			//popupObj.centerBox(((img.width)+56),((img.height)+70), IEfix);
			popupObj.centerBox(this);
			popupObj.showBox();
		}
		imgObj.src = this.url;
	}
	else
	{
		initRequest("GET",this.url,true,popupObj.writeContent);
	}
}
/*----------------------*/

PopUp.prototype.fadePage = function(url)
{
	this.pageSize = getDocumentSize();
	this.url = url.href;
	if(url.getAttribute("rel"))
	{
		var dimensions = url.getAttribute("rel").split("x");
		this.width = dimensions[0];
		this.height = dimensions[1];
	}
	this.pageOverlay = document.createElement("div");
	this.pageOverlay.setAttribute("id","pageOverlay");
	this.pageOverlay.style.height = this.pageSize[3] + "px";
	popupObj = this;
	this.pageOverlay.onclick = function(){popupObj.hideOverlay()};	// hides the overlay and pop if it's clicked on
	setOpacity(this.pageOverlay,this.opacityStart);
	var htmlBody = document.getElementsByTagName("body").item(0);
	htmlBody.insertBefore(this.pageOverlay,htmlBody.firstChild);
	if(document.getElementById("artThumbs"))
	{
		this.artThumbs = document.getElementById("artThumbs");
		this.artThumbs.style.overflow = "hidden";
	}
	fadeIn("pageOverlay",this.opacityStart,this.pageOpacity,this.fadeSpeed,function(){popupObj.createElement();});
}
/*----------------------*/

PopUp.prototype.writeContent = function()
{
	if(ajax.readyState == 4 && ajax.status == 200)
	{
		popupObj.div.style.width = popupObj.width + "px";
		popupObj.div.style.height = popupObj.height + "px";
		popupObj.centerBox();
		var div = document.createElement("div");
		div.setAttribute("id","responseContent");
		div.innerHTML = ajax.responseText;
		// popupObj.div.innerHTML = ajax.responseText;
		var linkage = document.createElement("a");
		linkage.setAttribute("href","#");
		linkage.setAttribute("id","closeLink");
		linkage.onclick = function(){popupObj.hideOverlay(); return false;};

		var text =  document.createTextNode("Close X");
		linkage.appendChild(text);
		// popupObj.div.insertBefore(linkage,popupObj.div.firstChild);
		//popupObj.div.appendChild(linkage);
		popupObj.div.appendChild(div);
		popupObj.div.style.display = "block";
	}
}
/*----------------------*/
//PopUp.prototype.centerBox = function(boxWidth,boxHeight, IEfix)
PopUp.prototype.centerBox = function(oldThis)
{
//if(boxWidth == null && boxHeight == null)
	if(oldThis == null)	{
		var boxWidth = this.width;
		var boxHeight = this.height;
	} else {
		var boxWidth = oldThis.width;
		var boxHeight = oldThis.height;
		
		this.div.style.width = boxWidth + 70 + "px";
		this.div.style.height = boxHeight + 70 + "px";
	}
	
	/*
	if (IEfix) {
		var boxWidth = 225;
		var boxHeight = 164;
	}*/
 	var pageScroll = getPageScroll();
//	var boxTop = Math.round(pageScroll[1] + (this.pageSize[1] - boxHeight - 35) / 2);
	var boxTop = Math.round(pageScroll[1] + (this.pageSize[1] - boxHeight - 35) / 2);
//	var boxLeft = Math.round((this.pageSize[0] - boxWidth) / 2);
	
	var boxLeft = Math.round((this.pageSize[0] - boxWidth) / 2);
	this.div.style.top = (boxTop < 0) ? "35px" : boxTop +"px";
	this.div.style.left = (boxLeft < 0) ? "35px" : boxLeft +"px";

}

PopUp.prototype.showBox = function()
{
	this.div.style.display = "block";
	this.pageOverlay.style.height = this.pageSize[3] + "px";
}
/*----------------------*/

PopUp.prototype.hideOverlay = function()
{
	removeElement(this.popupID);
	if(this.artThumbs)
	{
		this.artThumbs.style.overflow = "auto";
	}
	fadeOut("pageOverlay",this.opacityStart,0,this.fadeSpeed,"delete");
}
/*----------------------*/


