$(document).ready(function(){

	$("select").addClass("to_cb");

//container

	$("#container").append("<div id='bottom'></div>");

	$("#right .full img").each(function() {
		$(this).wrap("<a href='" + $(this).attr("src") + "' title='" + $(this).attr("alt") + "'></a>");
	});

	$(".ill a, #right .full a").fancybox({
		'padding'			:   0,
		'overlayOpacity'	:	0.9,
		'overlayColor'		:	'#ccc',
		'zoomSpeedIn'		:	500,
		'zoomSpeedOut'		:	500
	});

//tapeedid

	function imgResize(imput) {
		var maxwidth = 480;
		var maxheight = 360;
		
		if ($(imput).width() > 0) {
			var w = $(imput).width();
		} else {
			var w = $(imput).attr("width");
		}
		
		if ($(imput).height() > 0) {
			var h = $(imput).height()
		} else {
			var h = $(imput).attr("height");
		}

		var r = w / h;
		if (r > 1) {
			if (w > maxwidth) { 
//				if (h > maxheight) {
//					$(imput).height(maxheight).width(r*maxheight);
//				} else {
					$(imput).width(maxwidth).height(maxwidth/r);
//				}
			} else if (h > maxheight) {
				$(imput).height(maxheight).width(r*maxheight);
			}
		} else {
			if (h > maxheight) {
//				if (w > maxwidth) {
//					$(imput).width(maxwidth).height(maxwidth/r);
//				} else {
					$(imput).height(maxheight).width(r*maxheight);
//				}
			} else if (w > maxwidth) {
					$(imput).width(maxwidth).height(maxwidth/r);
			}				
		}
	}
	
	var stay = $.cookie('stay');
	if ( $("#" + stay).length > 0 ) {
		var fst = stay;
	} else {
		var fst = $("#found ul li:first").attr("class");
	} 
	
	imgResize("#" + fst + " img.yx");
	$("#" + fst).addClass("show");
	$("#right .noimage").addClass("show");
	$("#noimage").addClass("show");	
	$("#data_" + fst).addClass("show");

	$("div.full img.kax").hide();
	$("div.full").not(".show").hide();
	$(".wp_data").not(".show").hide();	

	$("img.tn").wrap("<a href='#' class='loe' title='Näita'></a>");

	$("h4, h5").prev("p").addClass("last");

	$(".loe").click(function(event) {
		var cl = $(this).parent("li").attr("class");
		$(".show").hide().removeClass("show");		
		$("#" + cl).show().addClass("show");
		imgResize("#" + cl + " img.yx");
		$("#data_" + cl).show().addClass("show");
		$(this).toggleClass("loe");
		return false;
	});
	
	$(".zoom").click(function(event) {
		var c = $(this).parents(".wp_data").prev(".full").attr("id");
		imgResize("#" + c + " img.kax");
		$("#" + c + " img").toggle();
	});
	
	
//clear	
	$(".pics, #nav, #chooseClass, #mail ul, #found ul, .pager, #right, #kolofon ul, #search ul, h1, #tootjad, div.ill").append("<div class='clear'></div>");
	

//kolofon
	$("#kolofon li:first").css({ border: "none", paddingLeft: "0", marginLeft: "0" });
	
//main
	$("#main p:last").css({ marginBottom: "0" });
	$("h3").prev("p").addClass("lastBeforeTitle");	
	
//nav
	$("#nav li:first").css({ border: "none", paddingLeft: "0" });
	var pm = 32;
	var containerWidth = 804;
	var width1 = $("a.meist").width();
	var width2 = $("a.tapeedid").width();
	var width3 = $("a.kleebised").width();
	var width4 = $("a.tootjad").width();
	var width5 = $("a.kontakt").width();
	var width6 = $("a.meeldib").width();
	var width7 = $("a.eripakkumised").width();
	var countLi = $("#nav").children().size();
	var navWidth = width1 + width2 + width3 + width4 + width5 + width6 + width7 + (countLi * pm);
	var divNavPaddingLeft = containerWidth - navWidth - 12;
	$("#nav").css({ paddingLeft: divNavPaddingLeft +"px", background: "url(http://tapeedid.ee/img/nav_bg.png) no-repeat -" + (320 - divNavPaddingLeft) + "px 8px" });
	$("#nav").prepend("<div class='feher'></div>");
	$(".feher").css({ width: "20px", height: "37px", backgroundColor: "white", position: "absolute", top: "0", left: "2px" });

//add classes to inputs

	$("input[@type=submit]").addClass("submit");
	$("input[@type=checkbox]").addClass("checkbox");    
	$("input[@type=text]").addClass("text");

//mail form 
	$("#mail li").addClass("styled");	
	$("#mail li:last").addClass("last");
	$("#mail label").addClass("overlabel");	
	$("#mail .text").hint();
	$("#mail .text").focus(function () {
         $(this).removeClass("error_input");
         $(this).addClass("focus");
         $(this).prev("label").css({ marginLeft: "0" });
    });
	$("#mail .text").blur(function () {         $(this).removeClass("focus");
         $(this).prev("label").css({ marginLeft: "-9999px" });             });
	
	$("#tootjad li a").addClass("ex");

	$('a.ex').click(function(){        
		window.open(this.href);        
		return false;    
	});
	
	$("#tootjad li.borastapeter a").attr({ title: "BoråsTapeter"});
	
	$("li.email").html("<a href='mailto:tapeedistuudio@tapeedid.ee'>tapeedistuudio@tapeedid.ee</a>");

// sidebar

	$("#sidebar > ul > li > a").addClass("head");
	$("#sidebar li.actual ul a").each(function() {
		$(this).attr({ href: "#" + $(this).parent("li").attr("class") });
	});
	$("#sidebar li:not('.actual') ul").hide();
	$("#sidebar .head").click(function() {
		$(this).toggleClass("open").next("ul").toggle();
		return false;
	});


//dekaart nicer positioning

	var dh = $(document).height();
	var wh = $(window).height();
	if (wh >= dh) {
		$("#dekaart").css({ position: "absolute", bottom: "0", right: "0" });
	}
});

