$(document).ready(function() {
	
	if ($("#panel_Components_loga-na-hp, .panel_Components_loga-na-hp").length) {
			$("#panel_Components_loga-na-hp, .panel_Components_loga-na-hp").jCarouselLite({
			   	auto: 5000,
			    speed: 1000,
			    visible: 6
			  });
		}
	
	$("#work").hide();
	//$("#content .content-container-helper").hide();
	
	var hash = "";
	if (self.document.location.hash != "") {
		hash = self.document.location.hash;
	}
	
	//offer
	var __anchor = new Array();
	var _opened;
	
	$("#offer2")
		.hide()
		.find(".jshide").hide().end()
		.find("h2").each(function() {
			var $h2 = $(this);
			var _anchor = $h2.find("a").attr("name");
			$("#c-" + _anchor).hide();
			__anchor.push(_anchor);
			
			$h2.find("a").remove();
			//$h2.wrap("a").attr("href", "javascript:void(0)");
			
			$h2.css({"cursor":"pointer"}).append($(document.createElement("span")).addClass("closed").html("&nbsp;"));
						
			$h2.click(function(){
				if ($("#c-" + _anchor).is(":visible")) {
					$("#c-" + _anchor).slideUp("100");
					document.location.href = document.location.protocol + "//" + document.location.host + "" + document.location.pathname + "#";
					$h2.find("span").addClass("closed").removeClass("opened");
				} else {
					if (_opened && _opened != _anchor) {
						document.location.href = document.location.protocol + "//" + document.location.host + "" + document.location.pathname + "#";
						$("#c-" + _opened).slideUp("100");
						var $prevH2 = $("#c-" + _opened).prev();
						$prevH2.find("span").addClass("closed").removeClass("opened");
					}
					$("#c-" + _anchor).slideDown("300");
					_opened = _anchor;
					document.location.href = (document.location.href.indexOf("#") > 0) ? document.location.href + _anchor : document.location.href + "#" + _anchor;
					$h2.find("span").addClass("opened").removeClass("closed");
				}
			});
		}).end()
		.each(function() {
			if (hash) {
				hash = hash.substring(1);
				$("#c-" + hash).slideDown("300");
				_opened = hash;
				var $prevH2 = $("#c-" + hash).prev();
				$prevH2.find("span").addClass("opened").removeClass("closed");
			}
		}).show();
		
		$("#offer")
		.find(".jshide").hide().end()
		.find("h2").each(function() {
			var $h2 = $(this);
			var _anchor = $h2.find("a").attr("name");
			$("#c-" + _anchor).hide();
			__anchor.push(_anchor);
			
			//$h2.find("a").remove();
			//$h2.wrap("a").attr("href", "javascript:void(0)");
			
			$h2.css({"cursor":"pointer"}).append($(document.createElement("span")).addClass("closed").html("&nbsp;"));
						
			$h2.click(function(){
				if ($("#c-" + _anchor).is(":visible")) {
					$("#c-" + _anchor).slideUp("100");
					document.location.href = document.location.protocol + "//" + document.location.host + "" + document.location.pathname + "#";
					$h2.find("span").addClass("closed").removeClass("opened");
				} else {
					
					if (_opened && _opened != _anchor) {
						document.location.href = document.location.protocol + "//" + document.location.host + "" + document.location.pathname + "#";
						//$("#c-" + _opened).slideUp("100");
						//var $prevH2 = $("#c-" + _opened).prev();
						//$prevH2.find("span").addClass("closed").removeClass("opened");
					}
					
					$("#c-" + _anchor).slideDown("300");
					_opened = _anchor;
					document.location.href = (document.location.href.indexOf("#") > 0) ? document.location.href + _anchor : document.location.href + "#" + _anchor;
					$h2.find("span").addClass("opened").removeClass("closed");
				}
			});
		}).end()
		.each(function() {
			if (hash) {
				hash = hash.substring(1);
				$("#c-" + hash).slideDown("300");
				_opened = hash;
				var $prevH2 = $("#c-" + hash).prev();
				$prevH2.find("span").addClass("opened").removeClass("closed");
			}
		});
		
//		$("#work h3").hide();
		
		$("#work")
		.hide()
		.find(".jshide").hide().end()
		.find(".jshide2").css({"visibility" : "hidden"}).end()
		.find("h2").each(function() {
			var $h2 = $(this);
			var _anchor = $h2.find("a").attr("name");
			$("#c-" + _anchor).hide();
			
			$h2.find("a").remove();
			//$h2.wrap("a").attr("href", "javascript:void(0)");
			
			$h2.css({"cursor":"pointer"}).append($(document.createElement("span")).addClass("closed").html("&nbsp;"));
						
			$h2.click(function(){
				if ($("#c-" + _anchor).is(":visible")) {
					$("#c-" + _anchor).slideUp("100");
					document.location.href = document.location.protocol + "//" + document.location.host + "" + document.location.pathname + "#";
					$h2.find("span").addClass("closed").removeClass("opened");
				} else {
					
					if (_opened && _opened != _anchor) {
						document.location.href = document.location.protocol + "//" + document.location.host + "" + document.location.pathname + "#";
						$("#c-" + _opened).slideUp("100");
						var $prevH2 = $("#c-" + _opened).prev();
						$prevH2.find("span").addClass("closed").removeClass("opened");
					}
					
					$("#c-" + _anchor).slideDown("300");
					if ($.browser.msie) {
						$("#c-" + _anchor).css({
							"float" : "left"
						});
					}
					_opened = _anchor;
					document.location.href = (document.location.href.indexOf("#") > 0) ? document.location.href + _anchor : document.location.href + "#" + _anchor;
					$h2.find("span").addClass("opened").removeClass("closed");
				}
			});
			
			$("#c-" + _anchor).each(function(i) {
				$anchor = $(this);
				$anchor.find(".work-cat-cont").hide();
				//$anchor.find("div.work-cat-cont").attr("id", "work-cat-cont-" + i);
				$anchor.find("a.detail")
					.attr("id", "a-" + i)
					.attr("href", "javascript:void(0)")
					.click(function() {
						
						var $cont = $(this).next("div.work-cat-cont");
						if ($cont.is(":visible")) {
							$cont.slideUp("100");
						} else {
							$cont.slideDown("300");
						}
					});				
				
			});
			
		}).end()
		.each(function() {
			if (hash) {
				hash = hash.substring(1);
				$("#c-" + hash).slideDown("300");
				_opened = hash;
				var $prevH2 = $("#c-" + hash).prev();
				$prevH2.find("span").addClass("opened").removeClass("closed");
			}
		}).show();
		
		
		
		if ($.flash.hasVersion(8)) {
		$("#tree")
			.css({"background" : "none", "filter" : ""})
			.flash({
				swf: "/Flash/birds.swf",
				width: 230,
				height: 587,
				wmode: 'transparent',
				flashvars: {
        	'lang': $("#tree").attr("class")
    		}
			});
		}
		
		$("li.sound")
			.html("")
			.flash({
				swf : "/Flash/player.swf",
				width: 30,
				height: 16,
				wmode: 'transparent',
				flashvars: {
        	'autoplay': (($.cookie('the_cookie') === null) ? 1 : $.cookie('the_cookie'))
    		}
			});
		
		//$(".flora").css({"opacity" : "0.5"});
		
		//site min height
		
		resetContentHeitht();
		$(window).resize(function() {
			resetContentHeitht();
		});
		
		//.sub .content-container
		
		var clicked = false;
		
		$("body").append("<div class='wrk_content' id='wrk_content'><p></p></div>");
		var my_tooltip = $("#wrk_content");
		
			
		$("#work .logo").each(function(i) {
			var $logo = $(this);
						
			$logo	
				.mouseover(function() {
					
					clicked = false;
					
					$logo.addClass("logo_on");
					
					var _id = $logo.attr("id").substring(5);
					
					/*
					var _url = $("#url-1-" + _id).attr("href");
					
					if (_url) {
						$logo
							.click(function() {
								if (!clicked) {
									window.open(_url);
									clicked = true;
								}
							})
							.addClass("logo_href");
						
							//$logo.find("img").attr("title", "Otevřít odkaz v novém okně.");
							
					}
					*/
					
					var _html = "<h3>" + $("#title-" + _id).html()+ "</h3>";
					_html += (jQuery.trim($("#url-" + _id).html()) != "") ? "<p>" + $("#url-" + _id).html() + "</p>" : "";
					_html += $("#cont-" + _id).html();
					
					
					my_tooltip.find("p").html(_html);
					my_tooltip.css({opacity: 0.95, display:"none"}).fadeIn(400);
					
					
				})
				.mousemove(function(kmouse){
						var border_top = $(window).scrollTop();
						var border_right = $(window).width();
						var left_pos;
						var top_pos;
						var offset = 15;
						if(border_right - (offset *2) >= my_tooltip.width() + kmouse.pageX){
							left_pos = kmouse.pageX+offset;
							} else{
							left_pos = border_right-my_tooltip.width()-offset;
							}
		
						if(border_top + (offset *2)>= kmouse.pageY - my_tooltip.height()){
							top_pos = border_top +offset;
							} else{
							top_pos = kmouse.pageY-my_tooltip.height()-offset;
							}	
		
						my_tooltip.css({left:left_pos, top:top_pos});

				})
				.mouseout(function() {
					my_tooltip.css({left:"-9999px"});
					$logo.removeClass("logo_on");
				});
		});
		
		/*
		var logosTimer;
				
		if ($("#logos").is(":visible")) {
			logosTimer = window.setInterval(function() {
				var _lang = $("#logos").hasClass("en") ? 'en' : 'cs';
				$("#logos").fadeOut(1000, function() { logosReload(_lang);} );
			}, 8000);
		} else {
			clearInterval(logosTimer);
		}
		*/

		
		
	//$("#content .content-container-helper").show();	
});

function resetContentHeitht() {
	var is_depr = ($("#work").length || $("#offer").length);
	if ($(".sub .content-container").is(":visible")) {
		var contentBeginsAt = $(".sub .content-container").offset();
		//alert(contentBeginsAt.top + " " + $(window).height() + " " + $("#footer").height() + " " + $(".sub .content-container").height());
		var usablePlace = $(window).height() - contentBeginsAt.top - $("#footer").height() - 40 - 40 - 35 - 20 ;
		//$(".footer-copy").html(usablePlace + " " + $(".sub .content-container-helper").height());
		if ($(".sub .content-container-helper").height() < usablePlace) {
			if ($.browser.msie && parseInt($.browser.version) < 7) {
				$(".sub .content-container").css({"height": usablePlace});
			} else {
				//if (!is_depr)
					$(".sub .content-container").css({"min-height" : usablePlace});
			}
		}
		
		
		
	}
}

function setAutoplay(autoplay) {
	$.cookie('the_cookie', autoplay, {path: '/'});
}

function logosReload(_lang) {
	$.ajax({
		  url: "/Components/loga-na-hp/Contents/0/resource.php?lang=" + _lang,
		  cache: true,
		  success: function(html){
		    $("#logos").html(html);
		    $("#logos").fadeIn(2000);
		  }
		});
}
