var showDarkGlassPaneInit = false;
var popupsFrequencyDays = 15;
var popups = [];
popups[0] = "/fragments/popups/clipboardText.jsp";
popups[1] = "/fragments/popups/noticeText.jsp";

function showWelcomePopup(){
	//1-clipboard
	var cookieValue = getRealCookie("welcomePopupViewed");

	if(!cookieValue){
		setPopupContent(0);
	} else {
		var values = cookieValue.split("-");
		if(values.length==1){
			//legacy clipboard cookie
			setPopupContent(1);
		} else {
			var viewedPopup = parseInt(values[0]);
			var viewedDateMsec = parseInt(values[1]);
			var nextViewDate = new Date(viewedDateMsec);
			nextViewDate.setDate(nextViewDate.getDate()+popupsFrequencyDays);
			var cdate = new Date();
			if(nextViewDate<cdate){
				if(viewedPopup<(popups.length-1)){		
					setPopupContent(viewedPopup+1);				
				} else {
					setPopupContent(0);
				}
			}
		}
	}

}

function viewPopup(uri){
	$j("#welcomePopup").load(uri,function(){
		showPopup("welcomePopup");
	});	
}

function setPopupContent(idx){
	var expdate = new Date();
	expdate.setDate(expdate.getDate()+(popupsFrequencyDays*2));
	var cookieValue = idx + "-" + new Date().getTime(); 
	setRealCookie('welcomePopupViewed',cookieValue,expdate,"/","ruszajwpolske.pl","");
	$j("#welcomePopup").load(popups[idx],function(){
		showPopup("welcomePopup");
	});	
}

function tpGet(id)
{
	return document.getElementById(id);
}

function showDarkGlassPane()
{
	var div = tpGet('tpGlassPaneDarkDiv');
	var ua = navigator.userAgent.toLowerCase();
	if(!showDarkGlassPaneInit && ua.indexOf('msie') != -1)
	{
		div.parentNode.removeChild(div);
		document.body.insertBefore(div, document.body.childNodes[0]);
		div = tpGet('tpGlassPaneDarkDiv');

		div.style.position = 'absolute';
		div.style.setExpression("width", "document.body.clientWidth");
		div.style.setExpression("height", "document.body.clientHeight");
		div.style.setExpression("top", "(document.body.scrollTop) + 'px'");
		div.style.setExpression("left", "(document.body.scrollLeft) + 'px'");
		document.body.onscroll = function() {
			tpGet('tpGlassPaneDarkDiv').style.top = (document.body.scrollTop) + 'px';
			tpGet('tpGlassPaneDarkDiv').style.left = (document.body.scrollLeft) + 'px';
		}
		div.style.display = 'block';
	} else {
		div.style.height=document.body.clientHeight;
		//div.style.position = 'fixed';
		div.style.display = 'block';
		document.body.onscroll = function() {
			tpGet('tpGlassPaneDarkDiv').style.top = (document.body.scrollTop) + 'px';
			tpGet('tpGlassPaneDarkDiv').style.left = (document.body.scrollLeft) + 'px';
		}
	}
	disableSelects();
	showDarkGlassPaneInit = true;
}

function hideDarkGlassPane()
{
	tpGet('tpGlassPaneDarkDiv').style.display = 'none';
	enableSelects();
}

function showPopup(id)
{
	showDarkGlassPane();
	var obj = tpGet(id);
	var ua = navigator.userAgent.toLowerCase();
	if(ua.indexOf('msie') == -1)
	{
		obj.style.position = 'fixed';
		obj.style.zIndex = 99998;
		obj.style.display = 'block';
		var w = obj.offsetWidth;
		var h = obj.offsetHeight;
		obj.style.left = parseInt((document.body.clientWidth-w)/2);
		obj.style.top = parseInt((document.body.clientHeight-h)/2);
	}
	else
	{
		obj.style.position = 'absolute';
		obj.style.zIndex = 99998;
		obj.style.display = 'block';
		var w = obj.offsetWidth;
		var h = obj.offsetHeight;		
		obj.style.left = parseInt((document.body.clientWidth-w)/2);
		obj.style.top = document.body.scrollTop + parseInt((document.body.clientHeight-h)/2);
	}
	try {
		var close = tpGet('tpPopupCloseButton');
		close.style.display="block";	
	} catch (e) { }
}

function hidePopup(id)
{
	hideDarkGlassPane();
	tpGet(id).style.display='none';
}

function getOffset(obj)
{
	var l=0,t=0;
	do
	{
		l += obj.offsetLeft || 0;
		t += obj.offsetTop  || 0;
		obj = obj.offsetParent;
	}
	while(obj);
	return [l,t];
}


function disableSelects()
{
	var ua = navigator.userAgent.toLowerCase();
	if(ua.indexOf('msie') != -1)
	{
		var tab = document.getElementsByTagName('select');
		for(var i=0;i<tab.length;i++)
		{
			tab[i].style.visibility = 'hidden';
		}
	}
}
function enableSelects()
{
	var ua = navigator.userAgent.toLowerCase();
	if(ua.indexOf('msie') != -1)
	{
		var tab = document.getElementsByTagName('select');
		for(var i=0;i<tab.length;i++)
		{
			tab[i].style.visibility = 'visible';
		}
	}
}
