$(document).ready(function() {
	//inicializace prázdného formulářového políčka pokud je předdefinované

	// tooltips
	$("#sidebar #tooltip .blogeri li").bind("mouseover", function() {
		$(".tooltip").hide();
		var actualItem = $("#sidebar #tooltip .blogeri li").index(this)+1;
		$("#tooltip"+actualItem).show();
		//alert(actualItem);
	}).bind("mouseout", function() {
		$(".tooltip").hide();
	});

	//menu
	$('#menu>li').click(function(event) {
		$("ul", $(this)).toggle();
		$("div.form", $(this)).toggle();
		//alert($("#menu>li").index(this));
		if($("#menu>li").index(this) == 0) {
			setCookie("filterBox","categories");
		} else if($("#menu>li").index(this) == 1) {
			setCookie("filterBox","date");
		}
		
		event.stopPropagation();
	});/*
	$('#menu>li').click(function(event) {
		
		
		event.stopPropagation();
	});*/

	$('#menu>li div.form').click(function(event) {
		event.stopPropagation();
	});
	
	//datapicker
	$(function() {
		$('#dateFrom, #dateTo').datepicker({
			changeMonth: true,
			changeYear: true,
			altFormat: 'yy-mm-dd'
		});
	});
	// Obsluha fulltextového pole
	$("#query").attr("value",localSearchQuery);
	$("#query").focus(function(){
		if($("#query").attr("value") == localSearchQuery){
			$("#query").attr("value","");
		}
	}).blur(function(){
		if($("#query").attr("value") == "") {
			$("#query").attr("value",localSearchQuery);
		}
	})

    /* skrytí funkčních odkazů na user formy */
    $("#loginBottom").remove();

    /*
	$('#menu li').click(function() {
		var actualItem = $("#menu li").index(this)+1;
		alert(actualItem);
		$('#menu li ul').toggle(1);
		return false;
	});
*/
	if(!$("#banner-leaderboard .in div").height()) {
		$("#banner-leaderboard").remove();
		$("#page").css("margin-top","0");
		$("#side-content .wrapper").css("margin-top","0");
	}
	if(!$("#banner-skyscraper div").height()) {
		$("#banner-skyscraper").remove();
	}
	if(!$(".banner-square .banner-content").height()) {
		$(".banner-square").remove();
	}

	$("tr:odd").addClass('zebra');
	$(".prihlaseni tr:odd").removeClass('zebra');

})

var localSearchQuery = 'Hledaný výraz:';

// Objekt pro lepsi spravu stringu
function StringBuffer() {
	this.buffer = "";
	this.append = new Function("str", "this.buffer += str;");
	this.getString = new Function("return this.buffer");
}

// Hide/Show widgets
function manageWidget(idWidget) {
	$("#widget-" + idWidget).show("fast");
	$(".hide-widget").each(
		function(i){
			if (this.style.display != 'none' && this.style.id != "#" + idWidget) $(this).hide();
		}
	);
	return false;
}

// Save cookie
function setCookie(name,value,days) {
	if (days != null || days != 0) {
		var today = new Date();
		var expire = new Date();
		expire.setTime(today.getTime() + 3600000*24*days);
		expire = "; expires=" + expire.toGMTString();
	} else { var expire = ""; }
	document.cookie = name + "=" + escape(value) + expire + "; path=/";
}
// Read cookie
function getCookie(Name) {
	var search = Name + "=";
	if (document.cookie.length > 0) { // if there are any cookies
		offset = document.cookie.indexOf(search);
		if (offset != -1) { // if cookie exists
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset)
			// set index of end of cookie value
			if (end == -1)
				end = document.cookie.length
				return unescape(document.cookie.substring(offset, end))
		}
	}
}

// Set iframe height
function setIframeHeight(h,f) {
	$("#"+f).attr({ 
		height: h
	});
}

function countdown() {
	chatRest --;
	hours = chatRest % 86400;
	days = (chatRest - hours) / 86400;
	mins = hours % 3600;
	hours = (hours - mins) / 3600;
	secs = mins % 60;
	mins = (mins - secs) / 60;

	if(days > 0) {
		if(days == 1) {
			$("#chatTimer").text("již jen za "+days+" den");
		} else if(days < 5) {
			$("#chatTimer").text("již jen za "+days+" dny");
		} else {
			$("#chatTimer").text("již jen za "+days+" dní");
		}
	} else if(hours > 0) {
		if(hours == 1) {
			$("#chatTimer").text("již jen za "+hours+" hodina");
		} else if(hours < 5) {
			$("#chatTimer").text("již jen za "+hours+" hodiny");
		} else {
			$("#chatTimer").text("již jen za "+hours+" hodin");
		}
	} else if(mins > 0) {
		if(mins == 1) {
			$("#chatTimer").text("již jen za "+mins+" minuta");
		} else if(mins < 5) {
			$("#chatTimer").text("již jen za "+mins+" minuty");
		} else {
			$("#chatTimer").text("již jen za "+mins+" minut");
		}
	} else if(now > chatStart && now < chatStop) {
		$("#chatTimer").text("probíhající chat");
	} else if(now > chatStop) {
		$("#chatTimer").text("chat již proběhl");
	}
	
	
	if (chatRest > 0) {
		window.setTimeout("countdown()", 1000*60);
	}
}