$(function(){
	z = 100;
	$("#top .menu li").each(function(){
		$(this).css("z-index",z--);
	})
	
	if($.support.opacity) fadetime = 200
	else fadetime = 0;
	
	$(".js_link").click(function(event){
		event.preventDefault();
		block = $("#"+ this.id.substr(0,this.id.length-4) +"block");
		if(block.is(":hidden")){
			$(".popup").not(block).fadeOut(fadetime);
			block.find(".sh_middle").css("height", block.outerHeight()-116)
			block.fadeIn(fadetime);
			if($(this).parent().hasClass("adv") && ! block.find(".jslider").length){
				$("#slider").slider({ from: 1930, to: 2020, scale: [1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010, 2020], limits:false, step: 1, dimension: '' });
			}
		}else{
			block.fadeOut(fadetime);
		}
	})
	
	$("body").click(function(event) {
		if(!($(event.target).parents(".popup").length) && !($(event.target).hasClass("js_link")) || $(event.target).parents(".shadow").length){
			$(".popup").fadeOut(fadetime);
		}
		
		if(! $("#wrap").hasClass("index_page") && ! $("#wrap").hasClass("movie_reg_page") && ! $(event.target).parents("#signup_login").length){
			$("#signup_login .tabs").hide();
		}
	});
	
	$(".sel_change").sSelect();
	
	$("a#show_res").click(function() {
		var year1 = $("div.jslider-value > span:eq(0)").text().replace(" ", "");
		var year2 = $("div.jslider-value > span:eq(1)").text();
		if(year2.indexOf("–") > 0) {
			year2 = year2.substr(8, 5);
		}
		year2 = year2.replace(" ", "");
		window.location = '/years/' + year1 + "-" + year2;
		return false;
    });

	$("a.adv_search_href").click(function() {
		var year1 = $("div.jslider-value > span:eq(0)").text().replace(" ", "");
		var year2 = $("div.jslider-value > span:eq(1)").text();
		if(year2.indexOf("–") > 0) {
			year2 = year2.substr(8, 5);
		}
		year2 = year2.replace(" ", "");
		window.location = this + '/' + year1 + "-" + year2;
		return false;
	});

	$("#down_types .tab_links a").click(function(event){
		event.preventDefault();
		nr = $("#down_types .tab_links a").index($(this));
		$("#down_types .tab_links li").removeClass("active");
		$(this).parent().addClass("active");
		$("#down_types .tab").hide();
		$("#down_types .tab:eq("+ nr +")").show();
	})
	
	$("#signup_login .tab_links a").click(function(event){
		event.preventDefault();
		if(! $("#wrap").hasClass("index_page") && ! $("#wrap").hasClass("movie_reg_page")){
			if($("#signup_login .tabs").is(":visible") && $(this).parent().hasClass("active")){
				$("#signup_login .tabs").hide();
			}else{
				$("#signup_login .tabs").show();
			}
		}
		
		nr = $("#signup_login .tab_links a").index($(this));
		$("#signup_login .tab_links li").removeClass("active");
		$(this).parent().addClass("active");
		$("#signup_login .tab").hide();
		$("#signup_login .tab:eq("+ nr +")").show();
	})
	
	$("#cvv2").focus(function(){
		$("#cvv2_img").fadeIn(100);
	}).blur(function(){
		$("#cvv2_img").fadeOut(100);
	})

	$(window).resize(function(){
		$(".movies .items").each(function(){
			nr_row = Math.floor(($(this).width()) / ($(".item",this).width() + 15));
			item_marg = Math.floor((($(this).width()-8) - (nr_row * $(".item",this).width())) / nr_row)
			$(".item",this).css("marginRight",Math.floor(item_marg));
			if($(this).parent().hasClass("remove_two")){
				$(".item", this).css("visibility","visible");
				$(".item:gt("+ (nr_row-3) +"):lt(2)", this).css("visibility","hidden");
			}
		})
	})
	$(window).resize();
	
	$("#top li:not(.active) > a, #f_menu li > a, .search_but > a").hover(function(){
		clr = $(this).css("color");
		$(this).stop().animate({"color":"#8fff79"}, 300);
	}, function(){
		$(this).stop().animate({"color":clr}, 300);
	})
	
	first_slide_nr = Math.floor(Math.random() * $("#slides .slide").length);
	$("#slides .slide:not(:eq("+ first_slide_nr +"))").hide();
	function change_slide(){
		if($("#slides .slide:visible").next().length){
			$("#slides .slide:visible").fadeOut().next().fadeIn();
		}else{
			$("#slides .slide:visible").fadeOut();
			$("#slides .slide:eq(0)").fadeIn();
		}
		setTimeout(change_slide,5000);
	}
	setTimeout(change_slide,5000);
})
