var bannerImage;

function bannerInit() {	
	setTimeout(showPopup, 5000);

	bannerImage = new Image();
	bannerImage.onload = function() {fixBannerPosition(this);}
	bannerImage.src = document.getElementById('popup_banner').src;
	

	document.getElementById('bottom_popup_content').getElementsByTagName('a')[0].onclick = function() {
		setPopupCookie();
	}

	document.getElementById('disable_popup').onclick = function() {
			setPopupCookie();
			fadeOut(document.getElementById('bottom_popup'), 1);

			return false;
		}
/*
	document.getElementById('disable_popup_permanent').onclick = function() {
			var date;
			date = new Date();
			date.setFullYear(date.getFullYear() + 5);

			document.cookie = 'cbf_page_popup=1; expires=' + date.toUTCString() + '; path=/';
			fadeOut(document.getElementById('bottom_popup'), 1);

			return false;
		}*/
};


function setPopupCookie() {
	var date;
	document.getElementById('bottom_popup').className;
	date = new Date();
	date.setMilliseconds(date.getUTCMilliseconds() + 1800000);
	document.cookie = 'cbf_popup=1; expires=' + date.toUTCString() + '; path=/';	
}


function showPopup(){
	document.getElementById('bottom_popup').className;
	if(document.cookie.indexOf('cbf_popup=1') === -1) {
		bg = document.getElementById('bottom_popup_background');
		bg.style.width = getWindowWidth() + 'px';
		bg.style.height = getWindowHeight() + 'px';
		fadeIn(document.getElementById('bottom_popup'), 0);
	}
}


function fadeIn(elm, opacity) {	
	elm.style.opacity = opacity;

	if(opacity == 0) {
		elm.style.display = 'block';
	}

	if(opacity < 1) {
		opacity += 0.05;
		setTimeout(function() {fadeIn(elm, opacity);}, 50);
	}
}


function fadeOut(elm, opacity) {
	elm.style.opacity = opacity;
	
	if(opacity > 0) {
		opacity -= 0.05;
		setTimeout(function() {fadeOut(elm, opacity);}, 50);
	}
	else {
		elm.style.display = 'none';
	}
}


/* *****************************************************************************
vrati sirku okna prohlizece
function by pixy http://www.pixy.cz

@param    [win = window]
@return   vrati sirku okna prohlizece nebo null pokud sirka nebyla zjistena
***************************************************************************** */
function getWindowWidth(win) {
	 win = win || window;

	 if (win.innerWidth)
			/* NN4 a kompatibilni prohlizece */
			return win.innerWidth;
	 else if(win.document.documentElement && win.document.documentElement.clientWidth)
			/* MSIE6 v std. rezimu - Opera a Mozilla jiz uspely s window.innerHeight */
			return win.document.documentElement.clientWidth;
	 else if(win.document.body && win.document.body.clientWidth)
			/* stare MSIE + MSIE6 v quirk rezimu */
			return win.document.body.clientWidth;
	 else
			return null;
}


/**
 * Get window height
 * function by pixy http://www.pixy.cz
 * @param  win window object. If not set, actual window is used. Optional
 * @return int heigth of the window
 */
function getWindowHeight(win) {
	win = win || window;

	 if (win.innerHeight)
			/* NN4 a kompatibilni prohlizece */
			return win.innerHeight;
	 else if(win.document.documentElement && win.document.documentElement.clientHeight)
			/* MSIE6 v std. rezimu - Opera a Mozilla jiz uspely s window.innerHeight */
			return win.document.documentElement.clientHeight;
	 else if(win.document.body && win.document.body.clientHeight)
			/* stare MSIE + MSIE6 v quirk rezimu */
			return win.document.body.clientHeight;
	 else
			return null;
}


function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "ext_link") 
     anchor.target = "_blank"; 
 } 
}

function setHeight() {
		divLen = document.getElementById("floating").offsetHeight;
		document.getElementById("obsah").style.height = divLen+"px";
	}


function messageAnswer(messageId, messageTitle, parentMessageInfo){
	document.getElementById('input_message').innerHTML = messageTitle + ": <br />" + parentMessageInfo;
	document.getElementById('parent_message_id').value = messageId;
	return true;
}

if(window.addEventListener) {
	window.addEventListener("load",function() {setHeight();},false);
}
else if(window.attachEvent) {
	window.attachEvent("onload",function() {setHeight();});
}
else {
	window.onload=function() {setHeight();};
}

function fixBannerPosition(img) {
	if(img.width && img.height) {
		content = document.getElementById('bottom_popup_content');
		content.style.width = img.width + 'px';
		content.style.height = img.height + 'px';
		content.style.left = '-' + Math.round(img.width / 2) + 'px';
		content.style.top = '-' + Math.round(img.height / 2) + 'px';
	}
}
