var animMax = 12;
var intervalMin = 900;
var intervalMax = 1100;
var showTime = 10000;
var containers = Array('#kategorie', '#podstranky', '#kontakt', '#kosik', '#dotaz');
var activeObj = null;
var activeCont = null;
var canHide = false;

function showAnim() {
	var i = Math.round(Math.random() * (animMax - 1)) + 1;
	var id = containers[Math.round(Math.random() * (containers.length-1))];
	var newCont = $(id);
	var newObj = $('#anim' + i);
	if (activeCont != null && activeObj != null) {
		if (newObj.get(0) == activeObj.get(0) || newCont.get(0) == activeCont.get(0)) {
			showAnim();
			return;
		}
	}
	activeObj = newObj;
	activeCont = newCont;
	for (var n = 0; n < containers.length; n++) $(containers[n]).css('z-index', 1);
	var pos = activeCont.css('z-index', 12).css('position', 'relative').offset();
	if (pos) {
		activeObj.css('top', pos.top).css('left', pos.left).css('height', 0).css('display', 'block');
		activeObj.stop().animate({ top: pos.top-140, height: 140}, 500, 'swing', function() {
			canHide = true;
			timeout = setTimeout('hideAnim()', showTime);
		});
	}
}

function hideAnim(obj) {
	if (canHide) {
		canHide = false;	
		clearTimeout(timeout);
		var pos = activeCont.offset();
		activeObj.stop().animate({ top: pos.top, height: 1 }, 200, 'swing', function() {
			$('.animImage').css('display', 'none').css('height', 1);
			var t = intervalMin + Math.round(Math.random()*(intervalMax-intervalMin));
			activeObj.css('display', 'none');
			setTimeout('showAnim()', t);
		});
	}
}

function hidePopup(a) {
	var p = $('#popup').offset();
	$('#popup2').animate({
		left: p.left,
		top: p.top
	}, 'normal', 'swing', function() {
		$('#popup2').hide();
	});
	$('#popup2 img').animate({
		width: 208,
		height: 288
	}, 'normal', 'swing');
	return false;
}

function showPopup() {
	$('#popup2').animate({
		top: 100,
		left: $('body').width() / 2 - 230
	}, 'slow', 'swing');
	$('#popup2 img').animate({
		width: 434,
		height: 600
	}, 'slow', 'swing');
}

function setCookie(c_name, value, expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+";path=/";
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

$(document).ready(function () {	

	for (var i = 1; i <= animMax; i++) 
		$('body').append('<div id="anim' + i + '" style="display: none; position: absolute; z-index: 10; width: 210px; height: 1px; overflow: hidden;" class="animImage" ><img onmouseover="hideAnim(); return false" src="/images/anim' + i + '.png" /></div>');
	var t = intervalMin + Math.round(Math.random() * (intervalMax - intervalMin));
	setTimeout('showAnim()', t);
/*
	if (getCookie('babyvisit') != 1) {
		$('body').append('<a id="popup2" href="#" onclick="return hidePopup(this)"><img src="/images/popup.gif" /></a>');
		var p = $('#popup').offset();
		$('#popup2').css('left', p.left).css('top', p.top);
		setTimeout('showPopup()', 1000);
			
	}
	setCookie('babyvisit', 1, 1000);
*/
	$(document).pngFix(); 
});

