// Popup shows for first n_count visits within n_expire days since last visit
// denis@softcomplex.com 11/29/2011

function f_clientWidth() {
	if (typeof(window.innerWidth) == 'number')
		return window.innerWidth;
	if (document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	if (document.body && document.body.clientWidth)
		return document.body.clientWidth;
	return null;
}
function f_clientHeight() {
	if (typeof(window.innerHeight) == 'number')
		return window.innerHeight;
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	if (document.body && document.body.clientHeight)
		return document.body.clientHeight;
	return null;
}

function f_popupInit () {
	
	var n_count = 5;
	var n_expire = 365;
	
	if (String(document.cookie).match(/popupcount\=(\d+)/))
		n_count = Number(RegExp.$1);
	if (n_count > 0) {
		f_popupShow();
		n_count--;
	}
	var d_expires = new Date();
	d_expires.setDate(d_expires.getDate() + n_expire);
	document.cookie='popupcount=' + n_count + ';expires=' + d_expires.toUTCString();
}

function f_popupReset () {
	var d_expires = new Date();
	d_expires.setDate(d_expires.getDate() - 1);
	document.cookie='popupcount=;expires=' + d_expires.toUTCString();
}

function f_popupShow () {
	var e_popup = document.getElementById('popup');
	if (!e_popup) return;
	
	var n_width  = e_popup.offsetWidth;
	var n_height = e_popup.offsetHeight;
	var n_left = (f_clientWidth() - n_width) / 2;
	if (n_left < 10) n_left = 10;
	var n_top = (f_clientHeight() - n_height) / 2;
	if (n_top < 10) n_top = 10;
		
	e_popup.style.left = n_left + 'px';
	e_popup.style.top  = n_top  + 'px';
	e_popup.style.visibility = 'visible';
}

function f_popupHide () {
	var e_popup = document.getElementById('popup');
	if (!e_popup) return;
	e_popup.style.visibility = 'hidden';
}

function f_popupAddOnload (f_func) {
	if (document.addEventListener) {
		window.addEventListener('load', f_func, false);
	}
	else if (window.attachEvent) {
		window.attachEvent('onload', f_func);
	}
	else {
		var f_onLoad = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = f_func;
		}
		else {
			window.onload = function() {
				f_onLoad();
				f_func();
			}
		}
	}
}

f_popupAddOnload (f_popupInit);

