$(document).ready(function(){

	$(".grad").click(function(){
		return false;
	});

	var top = 0;
	var left = 0;
	var x = 0;
	var y = 0;
	var grad;

	$(".grad").mouseover(function () {
		
		grad = $(this).attr("id");
		
		if ( $(".tooltip_in").text()=='' ) {
			$("."+grad).clone(true).appendTo(".tooltip_in");
		}
		
		$(".tooltip .kontakt").hide();
		$(".tooltip .kontakt:first").show();
		
		var tooltip_height = $(".tooltip").height();
		
		var offset = $(this).offset();
		top = offset.top - tooltip_height;
		left = offset.left;
		var tooltip_left = $(".tooltip").offset().left;
		
		
		//hide tooltip if move mouse to other item (prevent hiding if move mouse from tooltip to his item)
		if ($(".tooltip").is(":visible") && left!=tooltip_left)	{
			$(".tooltip").hide();
		}
		
		$(".tooltip").css("top", top+"px").css("left", left+"px").fadeIn("slow");
		
	});
	
	$().mousemove(function(e){
		if ($(".tooltip").is(":visible")) {
			
			x = e.pageX;
			y = e.pageY;
			var item_width = $("#"+grad).width();
			var item_height = $(".grad").height();
			var tooltip_height = $(".tooltip").height();
			
			//fadeOut if move mouse out of area of tooltip and item
			if (y<top || y>top+item_height+tooltip_height || x<left || x>left+220 || (y>top+tooltip_height && x>left+item_width)) {
				
				$(".tooltip").hide();
				$(".tooltip .tooltip_in").empty();
			}
		}
	});
	
	$("h4.filijala").click(function(){
		if (! $(this).next(".kontakt").is(":visible")) {
			$(".kontakt").slideUp("slow");
			$(this).next(".kontakt").slideDown("slow");
		}
		return false;
	});
	
	//lista filijala
	$("h3.mesto a").click(function(){
		if (!$(this).hasClass("active")) {
			$("h3.mesto a").removeClass("active");
			$(this).addClass("active");
			$(".expo:visible").slideUp("slow");
			$(this).parent().next('.expo').slideDown("slow");
		} else {
			$(this).removeClass("active");
			$(".expo:visible").slideUp("slow");
		}
		return false;
	});
	$(".expo").each(function(){
		$(this).find(".kont:gt(0)").hide();
	});
	$("h4.fil").click(function(){
		if (! $(this).next(".kont").is(":visible")) {
			$(this).parents(".expo").find(".kont").slideUp("slow");
			$(this).next(".kont").slideDown("slow");
		}
		return false;
	});

});



