$(document).ready(function(){

	var footerheight = $("div#footer").innerHeight();
	$("div#footer").css({"margin-top":-footerheight+"px"});
	$("div#main").css({"padding-bottom":footerheight+"px"});
	
	var speedfade_start = 5000;
	var speedfade = 700;
	var speedshowitem = 700;
	var speedhideitem = 300;
	
	$("ul.products_list a.parent").click(function(){
		if( ! $(this).hasClass("selected") ) {
			$("ul.products_list a").removeClass("selected");
			$(this).addClass("selected");
			$("ul.products_list").find("ul").slideUp();
			$(this).parent().find("ul").slideDown();
			return false;
		}
		return;
	});
	
	$("div.qa_cat b").click(function(){
		if( ! $(this).hasClass("qa_cat_selected") ) {
			$("ul.qa_list").slideUp();
			$("div.qa_cat b").removeClass("qa_cat_selected");
			$(this).parent().find("ul.qa_list").slideDown();
			$(this).addClass("qa_cat_selected");
		}
	});
	
	$("a.single_item").click(function(e){
		e.preventDefault();
		var id = $(this).attr('rel');
		//var popupText = $('#'+id).find("div.product_text_popup");
		var popupTextWidth = $('#'+id).width();
		var maskHeight = $(document).height();
		var maskWidth = $(document).width();
		var scrollT = $(window).scrollTop();
		var winH = $(window).height();
		var winW = $(window).width();
		$('div#background_layer').css({'width':winW,'height':maskHeight}).fadeTo("slow",0.8);
		//popupText.css({'width':popupTextWidth-20,'display':'block'});
		$('#'+id).css('top',  scrollT+(winH/2-$('#'+id).height()/2));
		$('#'+id).css('left', winW/2-$('#'+id).width()/2);
		$('#'+id).fadeIn(speedshowitem);
	});

	$('div.item_dialog .close,div#background_layer').click(function(e){
		e.preventDefault();
		$('div.item_dialog').hide(speedhideitem);
		$('div#background_layer').fadeOut(speedhideitem);
	});
	
	$("ul.gallery_lab a").click(function(){
		gal_id = $(this).attr("rel");
		gal_img = $(this).attr("href");
		gal_height = $("div#"+gal_id).height();
		
		$("div#"+gal_id).css({"height":gal_height}).animate({"opacity":0},speedfade,function(){
			$(this).html('<img src="'+gal_img+'" alt="" />').animate({"height":gal_height,"opacity":1},speedfade);
		});
		
		return false;
	});
	
	$("ul.calc_fields input").keyup(function(){
		$.get("/engine/ajax/katran_calc.php",{calc_w: $("#calc_w").val(), calc_l: $("#calc_l").val(), calc_h: $("#calc_h").val()}, function(data){
			$("#calc_result").html(data);
		});
	});
	
	function cat_items_rotate(num) {
		if( num > 2 ) num = 0;
		$("a#gallery_labyrinth").delay(200).fadeOut(speedfade,function(){$(this).css("background-image","url(/templates/katran/images/gallery_labyrinth/"+num+".jpg)").fadeIn(speedfade);});
		$("a#gallery_balls").delay(400).fadeOut(speedfade,function(){$(this).css("background-image","url(/templates/katran/images/gallery_balls/"+num+".jpg)").fadeIn(speedfade);});
		$("a#gallery_playgrounds").delay(600).fadeOut(speedfade,function(){$(this).css("background-image","url(/templates/katran/images/gallery_playgrounds/"+num+".jpg)").fadeIn(speedfade);});
		//$("a#gallery_slides").delay(800).fadeOut(speedfade,function(){$(this).css("background-image","url(/templates/katran/images/gallery_slides/"+num+".jpg)").fadeIn(speedfade);});
		//$("a#gallery_attractions").delay(800).fadeOut(speedfade,function(){$(this).css("background-image","url(/templates/katran/images/gallery_attractions/"+num+".jpg)").fadeIn(speedfade);});
		$("a#gallery_trampolines").delay(800).fadeOut(speedfade,function(){$(this).css("background-image","url(/templates/katran/images/gallery_trampolines/"+num+".jpg)").fadeIn(speedfade);});
		setTimeout(function(){cat_items_rotate(num+1)}, speedfade_start);
	}
	setTimeout(function(){cat_items_rotate(1)}, speedfade_start);
	
	window.onerror = function (msg, url, line) {
		alert(msg + "\n" + url + "\n" + "\n" + line);
		return true;
	}
	
});
