var Popup_Engine_List = new Array ();

function Popup_Engine() {

	this.detect = null;
	this.AutoCenter = true;

	this.CookieUse = false;
	this.CookieTime = 0;

	this.AutoClose = false;

	this.timeOutSec = 30;
	this.timeOutID = null;
	this.timeInSec = 1;
	this.timeInID = null;

	this.popupType = null;

	this.url = null;
	
	this.left = null;
	this.top = null;
	
	this.width = null;
	this.height = null;

	this.scrollbars = null;

	this.popuphwnd = null;

	this.layerID = 'plContainer';

	this.engine = null;
};

Popup_Engine.prototype = {


	init: function(param, engine) {

		Popup_Engine_List[Popup_Engine_List.length] = engine;

		if ( typeof(param) != 'object' )
		{
			return false;
		}

		if ( param.cookie > 0)
		{
			this.CookieUse = true;
			this.CookieTime = param.cookie;
		}

		if ( param.leadout > 0)
		{
			this.AutoClose = true; 
			this.timeOutSec = param.leadout;
		}

		if ( param.leadin > 0)
		{
			this.timeInSec = param.leadin;
		}

		if ( param.layer != '' )
		{
			this.layerID = param.layer;
		}

        this.popupType = param.type;

		if ( param.AutoCenter == false || param.AutoCenter == true )
		{
			this.AutoCenter = param.AutoCenter;
		}


		this.url = param.url;

		this.top = param.top;
		this.left = param.left;
		
		this.width = param.width;
		this.height = param.height;
		
		this.scrollbars = param.scrollbars;

		this.fnStartAutoOpen = function () {engine.StartAutoOpen();}
		this.fnStartAutoClose =  function () {engine.StartAutoClose();}

		// event handler
		if (typeof window.addEventListener != "undefined") {
			window.addEventListener("load", this.fnStartAutoOpen, false);

		} else if (typeof window.attachEvent != "undefined") {
			window.attachEvent("onload", this.fnStartAutoOpen );

		} else {
			window.onload = this.fnStartAutoOpen;

		}

	},

	StartAutoOpen: function() {
		if ( this.timeInSec == 0 ) {
			this.StopAutoOpen();
			this.DoLoad();
		
		} else {
			this.timeInSec = this.timeInSec - 1;
			this.timeInID = setTimeout(this.fnStartAutoOpen, 1000);
		}
	},

	StopAutoOpen: function() {
		clearTimeout(this.timeInID);
	},

	StartAutoClose: function() {
		if ( this.timeOutSec == 0 ) {
			this.StopAutoClose();
			this.dismiss();
		
		} else {
			this.timeOutSec = this.timeOutSec - 1;
			this.timeOutID = self.setTimeout(this.fnStartAutoClose, 1000);
		}
	},

	StopAutoClose: function() {
		clearTimeout(this.timeOutID);
	},

	DoLoad: function() {

		if ( (this.readCookie("plContainer") != 'yes') || !this.CookieUse ) {

			if ( this.AutoClose ) {
				this.StartAutoClose();
			}

			if ( this.AutoCenter && this.popupType == 'layer' ) {
				this.CenterPopup(this.layerID);
			}

            if ( this.popupType == 'layer' )
			{
				this.showHideLayers(this.layerID, '', 'show');
			}

			if ( this.popupType == 'popup' || this.popupType == 'popunder' )
			{
				if (this.left == 0 && this.top == 0)
				{
					this.popuphwnd = popup(this.url, '', this.width, this.height, {'scrollbars': this.scrollbars, 'center': 1});
				} else {
					this.popuphwnd = popup(this.url, '', this.width, this.height, {'scrollbars': this.scrollbars, 'left': this.left, 'top': this.top});
				}

				if ( this.popuphwnd == null ) {
					alert("Sajnálom, de az Ön böngészője letiltja a popup ablakokat!\nWe are sorry, but Your browser is blocking pop-up windows!\nEs tut uns leid, aber Ihr Browser blockiert Pop-up-Fenster!");
				} else {
					if ( this.popupType == 'popup' ) {
						this.popuphwnd.focus();
					}
					if ( this.popupType == 'popunder' ) {
						self.focus();
					}
				}
			}

			if ( this.CookieUse ) {
				this.writeCookie("plContainer", "yes", this.CookieTime);
			}
		} else {
			this.dismiss()
		}
	},

	dismiss: function() {

		this.StopAutoClose();

		if ( this.popupType == 'layer' )
		{
			this.showHideLayers(this.layerID, '', 'hide');
            //changeWMode(false);
		}

		if ( (this.popupType == 'popup' || this.popupType == 'popunder') && this.popuphwnd != null )
		{
			this.popuphwnd.close();
		}

	},

	readCookie: function(name) {
		var cookieValue = "";
		var search = name + "=";
		
		if(document.cookie.length <= 0) { 
			return;
		}

		offset = document.cookie.indexOf(search);
		if (offset == -1) { 
			return;
		}

		offset += search.length;
		end = document.cookie.indexOf(";", offset);

		if (end == -1) {
			end = document.cookie.length;
		}
		
		cookieValue = unescape(document.cookie.substring(offset, end))

		return cookieValue;
	},


    getWindowWidth: function() {
	    return (document.layers||(document.getElementById&&!document.all)) ? window.outerWidth : (document.all ? document.body.clientWidth : 0);
	},

	getWindowHeight: function() {
	    return window.innerHeight ? window.innerHeight :(document.getBoxObjectFor ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : ((document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : (document.body ? document.body.clientHeight : 0)));
	},

	CenterPopup: function(theObj) {
		var height, width, obj,ttop,lleft;
		width = this.getWindowWidth();
		height = this.getWindowHeight();

		obj = this.findObj(theObj);

		var w = (obj.style.width == '') ? 0 : parseInt(obj.style.width);
		lleft = (width / 2) - (w / 2);

		obj.style.left = lleft + 'px';

		var h = (obj.style.height == '') ? 0 : parseInt(obj.style.height);
		ttop = (height / 2) - (h / 2);
		if (ttop < 228) {
			ttop = 228;
		}
		obj.style.top = ttop + 'px';
	},

	writeCookie: function(name, value, hours) {
		var expire = "";

		if ( hours != null ) {
			expire = new Date((new Date()).getTime() + hours * 3600000);
			expire = "; expires=" + expire.toGMTString();
		}
		document.cookie = name + "=" + escape(value) + expire;
	},

	showHideLayers: function() { 
		var i, visStr, obj, args = this.showHideLayers.arguments;
		
		for (i=0; i<(args.length-2); i+=3) {

			if ((obj = this.findObj(args[i])) != null) {
				visStr = args[i+2];

                if (obj.style) {
                    obj = obj.style;
                    if ( visStr == 'show' ) visStr = 'visible';
                    else if ( visStr == 'hide' ) visStr = 'hidden';
                }

                obj.visibility = visStr;
			}
		}
	},

	findObj: function(theObj, theDoc) {
		var p, i, foundObj;
	  
		if ( !theDoc ) {
			theDoc = document;
		}

		if( (p = theObj.indexOf("?")) > 0 && parent.frames.length) {
			theDoc = parent.frames[theObj.substring(p+1)].document;
			theObj = theObj.substring(0,p);
		}
		
		if ( !(foundObj = theDoc[theObj]) && theDoc.all) {
			foundObj = theDoc.all[theObj];
		}

		for (i=0; !foundObj && i < theDoc.forms.length; i++) {
			foundObj = theDoc.forms[i][theObj];
		}

		for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) {
			foundObj = findObj(theObj,theDoc.layers[i].document);
		}
		
		if (!foundObj && document.getElementById) {
			foundObj = document.getElementById(theObj);
		}
		
		return foundObj;
	}

}

function plClose() {
	for (i=0; i<Popup_Engine_List.length; i++)
	{
		if ( Popup_Engine_List[i].popupType == 'layer' )
		{
			Popup_Engine_List[i].dismiss();
		}
	}
}
