(function ($) {

	function createCookie(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else var expires = "";
		document.cookie = name + "=" + value + expires + "; path=/";
	};

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		return null;
	};


	function hideFlash() {
		var embeds = document.getElementsByTagName('embed');
		for (i = 0; i < embeds.length; i++) {
		embeds[i].style.visibility = 'hidden';
	}
		var objects = document.getElementsByTagName('object');
		for (i = 0; i < objects.length; i++) {
		objects[i].style.visibility = 'hidden';
	}
		var iframes = document.getElementsByTagName('iframe');
		for (i = 0; i < iframes.length; i++) {
		iframes[i].style.visibility = 'hidden';
		}
	};
	
	function showFlash() {
		var embeds = document.getElementsByTagName('embed');
		for (i = 0; i < embeds.length; i++) {
		embeds[i].style.visibility = 'visible';
	}
		var objects = document.getElementsByTagName('object');
		for (i = 0; i < objects.length; i++) {
		objects[i].style.visibility = 'visible';
	}
		var iframes = document.getElementsByTagName('iframe');
		for (i = 0; i < iframes.length; i++) {
		iframes[i].style.visibility = 'visible';
		}
	};

	/**
	* When the open event is called, this function will be used to 'open'
	* the overlay, container and data portions of the modal dialog.
	*
	* onOpen callbacks need to handle 'opening' the overlay, container
	* and data.
	*/
	function modalOpen(dialog) {
		$.fn.exitpopup.modelDlgShown++;
		hideFlash();
		dialog.overlay.fadeIn('fast', function () {
			dialog.container.fadeIn('fast', function () {
				dialog.data.hide().slideDown('fast');
			});
		});
	};
	/**
	* When the close event is called, this function will be used to 'close'
	* the overlay, container and data portions of the modal dialog.
	*
	* The SimpleModal close function will still perform some actions that
	* don't need to be handled here.
	*
	* onClose callbacks need to handle 'closing' the overlay, container
	* and data.
	*/
	function simplemodal_close(dialog) {
		dialog.data.fadeOut('fast', function () {
			dialog.container.hide('fast', function () {
				dialog.overlay.slideUp('fast', function () {
					$.modal.close();
					showFlash();
					if($.fn.exitpopup.options.cookieOnClose){ createCookie($.fn.exitpopup.options.cookieName, "1", "3"); }
					//$.fn.exitpopup.modelDlgShown--;
				});
			});
		});
	};

	jQuery.checkSubmitted = function () {
		return readCookie($.fn.exitpopup.options.cookieName) == "1";
	};

	jQuery.trackSubmit = function () {
		createCookie($.fn.exitpopup.options.cookieName, "1", "100"); /* name, value, days */
	};


	$.fn.exitpopup = function (options) {
		if (this.length > 0) {
			var opts = $.extend({}, $.fn.exitpopup.defaults, options);
			$.fn.exitpopup.options = opts;
			$.fn.exitpopup.modelDlgShown = 0;
			var item = $(this[0]);
			$(document).mouseout(function (e) {
				if (/*$.fn.exitpopup.modelDlgShown < 1 &&*/ (e.pageY - $(document).scrollTop() <= 5) && (!opts.checkCookie || !$.checkSubmitted())) {
					// Launch MODAL BOX
					item.modal({ onOpen: modalOpen, onClose: simplemodal_close });
				}
			});

			if (opts.showOnDelay) {
				setTimeout(function () {
					if (!$.checkSubmitted() && $.fn.exitpopup.modelDlgShown < 1) {
						// we check the modelDlgShown variable value so that if the user triggered the dialog going out the window, it won't be shown second time on timer event
						item.modal({ onOpen: modalOpen, onClose: simplemodal_close });
					}}, opts.delay * 1000);
			}
		}
		return this;

	};

	$.fn.exitpopup.defaults = {
		checkCookie: true,  // if true, the dialog won't be shown when the cookie is set, if false - the dialog is shown always when the user goes up the browser window
		cookieName: "pp_msg",
		cookieOnClose: true, // if true, the dialog is shown to the user once and a cookie is set to prevent second popup
		showOnDelay: false, // if true, the dialog shows up after the specified amont of time
		delay: 100
	};

})(jQuery);


