﻿var _entryPop;
var entryPopup = function (params) {
	this._url = (!params.url) ? '' : params.url;
	this._width = (!params.width) ? 320 : params.width;
	this._height = (!params.height) ? 240 : params.height;
	this._cookie = (!params.cookie) ? false : params.cookie;
	this._cookieid = (!params.cookieid) ? 0 : params.cookieid;

	_entryPop = this;

	this.init = function () {
		if (_entryPop.getCookie() == 'true') return;
		if (_entryPop._cookie == true) _entryPop.setCookie();

		$('head').append('<style type="text/css">' +
			'#entryPopBody{font-family:verdana;font-size:10px;height:100%;width:100%;position:fixed;top:0px;left:0px;}' +
			'#entryPopBody #entryPopWashout{display:none;position:fixed;background:#000;opacity:0.8;height:100%;width:100%;top:0;left:0;z-index:0;filter:alpha(opacity=80);}' +
			'#entryPopBody #entryPopWindowOuter{position:absolute;top:50%;left:50%;z-index:99999}' +
			'#entryPopBody #entryPopWindow{display:none;width:1;height:1;position:relative;left:-50%;top:-50%;}' +
			'#entryPopBody #entryPopClose{position:absolute;right:-12px;top:-12px;width:24px;height:24px;background:transparent url(/images/exitpop/closeButton-nosprite.png) no-repeat;_background-image:none;_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(s.rc=\'/images/exitpop/closeButton-nosprite.png\',sizingMethod=\'scale\');cursor:pointer;z-index:2}' +
			'#entryPopBody #entryPopContent{position:relative;background:#EEE url(/images/exitpop/loader.gif) no-repeat center center;border:2px solid #FFF;z-index:1;overflow:hidden}' +
			'#entryPopBody #entryPopFrame{display:none;border:0;margin:0;padding:0}' +
			'</style>');

		$('body').append('<div id="entryPopBody">' +
				'<div id="entryPopWashout"></div>' +
				'<div id="entryPopWindowOuter">' +
					'<div id="entryPopWindow">' +
						'<div id="entryPopClose"></div>' +
						'<div id="entryPopContent">' +
							'<iframe id="entryPopFrame" height="' + _entryPop._height + '" width="' + _entryPop._width + '" src="' + _entryPop._url + '" frameborder="0" marginheight="0" marginwidth="0" scrolling="auto"></iframe>' +
						'</div>' +
					'</div>' +
				'</div>' +
			'</div>');

		$('#entryPopWindowOuter').height(_entryPop._height);
		$('#entryPopWindow').height(_entryPop._height);
		$('#entryPopWindow').width(_entryPop._width);
		$('#entryPopContent').height(_entryPop._height);
		$('#entryPopContent').width(_entryPop._width);

		$('#entryPopClose').click(function () { _entryPop.closeEntryPop(); });

		$('#entryPopFrame').load(function () {
			$(this).css('display', 'block'); $('#entryPopContent').css('background', '#EEE');
			$('#entryPopBody').css('z-index', '999999');
			$('#entryPopWashout').css('display', 'block');
			$('#entryPopWindow').fadeIn(500);
		});
	};

	//###############################################################################
	// close the popup
	//###############################################################################
	this.closeEntryPop = function () {
		$('#entryPopClose').fadeOut(150);
		$('#entryPopWindow').slideUp(250);
		$('#entryPopBody').delay(100).fadeOut(400, function () { $(this).remove(); });
	};

	//###############################################################################
	// set the cookie for the exit popup
	//###############################################################################
	this.setCookie = function () {
		var exdays = 360;
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var cValue = escape("true") + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
		document.cookie = 'entryPopFired_' + _entryPop._cookieid + '=' + cValue;
	};

	//###############################################################################
	// get the cookie for the exit popup
	//###############################################################################
	this.getCookie = function () {
		var i, x, y, ARRcookies = document.cookie.split(";");
		for (i = 0; i < ARRcookies.length; i++) {
			x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
			y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
			x = x.replace(/^\s+|\s+$/g, "");
			if (x == "entryPopFired_" + _entryPop._cookieid) return unescape(y);
		}
		return false;
	};

};

$(document).ready(function () { try { _entryPop.init(); } catch (err) { } });
