//********************
// Popup Constructor *
//********************
function Popup(objName)
{	
	// get ids
	this.body_node	= document.getElementsByTagName('body').item(0);
	
	// set strings
	this.path			= 'components/multishop/http/';
	this.show_class		= 'show';
	this.hide_class		= 'hide';
	this.visible_class	= 'visible';
	this.hidden_class	= 'hidden';
	
	// properties
	this.popup_max_height	= 500;
	
	// params
	this.objName	= objName;
}

//************************
// Function create popup *
//************************
Popup.prototype.create = function(popup_id, location, iid)
{
	// create filter div
	this.filter_div	= document.createElement('div');
	this.filter_div.setAttribute('id', 'popup_filter');
	
	// create shadow div
	this.shadow_div	= document.createElement('div');
	this.shadow_div.setAttribute('id', 'popup_shadow');
	
	// create popup div
	this.popup_div			= document.createElement('div');
	this.popup_div.setAttribute('id', 'popup');
	
	this.popup_inner_div	= document.createElement('div');
	this.popup_inner_div.setAttribute('id', 'popup_inner');
	
	// hide filter and popup node
	this.hide();
	
	// append children
	this.popup_div.appendChild(this.popup_inner_div);
	this.body_node.appendChild(this.filter_div);
	this.body_node.appendChild(this.shadow_div);
	this.body_node.appendChild(this.popup_div);
	
	// get the right data for this popup
	this.getData(popup_id, location, iid);
}

//******************************
// Function get data for popup *
//******************************
Popup.prototype.getData = function(popup_id, location, iid)
{
	// set object to var
	var _this = this;
	
	var myConn = new XHConn();
	
	if (!myConn) alert("XMLHTTP not available. Please try a newer/better browser.");
	
	// return result when done
	var fnWhenDone = function (oXML)
	{
		// put response in the wrapper
		_this.popup_inner_div.innerHTML = oXML.responseText;
		
		// set position of popup and shadow
		_this.position();
		
		// display filter and popup
		_this.show();
	}
	
	// make the connection
	myConn.connect(this.path + 'getPopupData.php', 'POST', 'did=' + popup_id + '&obj=' + this.objName + '&loc=' + location + '&id=' + iid, fnWhenDone);
}

//************************
// Function remove popup *
//************************
Popup.prototype.remove = function()
{
	// get the filter and popup nodes
	var popup_filter	= document.getElementById('popup_filter');
	var popup_shadow	= document.getElementById('popup_shadow');
	var popup			= document.getElementById('popup');
	
	// remove the filter, shadow and popup nodes
	popup_filter.parentNode.removeChild(popup_filter);
	popup_shadow.parentNode.removeChild(popup_shadow);
	popup.parentNode.removeChild(popup);
}

//**************************
// Function position popup *
//**************************
Popup.prototype.position = function()
{
	// set properties
	var popup_height	= parseInt(this.popup_div.offsetHeight);
	
	// check for maximum height
	if (popup_height > this.popup_max_height)
	{
		popup_height = this.popup_max_height;

		// internet explorer
		if (document.all)
		{
			this.popup_div.style.height			= parseInt(popup_height) + 'px';
			this.popup_inner_div.style.height	= parseInt((popup_height - 6)) + 'px';
		}
		
		// other browsers
		else
		{
			this.popup_div.style.height			= parseInt((popup_height - 6)) + 'px';
			this.popup_inner_div.style.height	= parseInt((popup_height - 6 - 40)) + 'px';
		}
	}
	
	var position = parseInt(popup_height / 2);
	
	// set height of shadow popup
	this.shadow_div.style.height	= popup_height + 'px';
	
	// position shadow
	this.shadow_div.style.left		= '50%';
	this.shadow_div.style.top		= '50%';
	this.shadow_div.style.marginTop	= '-' + (position - 4) + 'px';
	
	// position popup
	this.popup_div.style.left		= '50%';
	this.popup_div.style.top		= '50%';
	this.popup_div.style.marginTop	= '-' + position + 'px';
}

//**********************
// Function hide popup *
//**********************
Popup.prototype.hide = function()
{
	// filter
	this.filter_div.style.visibility = this.hidden_class;
	
	// shadow
	this.shadow_div.style.visibility = this.hidden_class;
	
	// popup
	this.popup_div.style.visibility = this.hidden_class;
}

//**********************
// Function show popup *
//**********************
Popup.prototype.show = function()
{
	// filter
	this.filter_div.style.visibility = this.visible_class;
	
	// shadow
	this.shadow_div.style.visibility = this.visible_class;
	
	// popup
	this.popup_div.style.visibility = this.visible_class;
}