var choice;

$(document).ready(function () {    $("a.noad").click(function(){
        $("#noad").html('<div id="noad_back" style="position:absolute;left:0;top:0;background-image:url(/sms/images/overlay.png);z-index:9999;width:100%;height:'+$(document).height()+'px;"></div>');
        $("#noad_back").click(function(){
            $("#noad").empty();
        }).html('<div style="position:fixed;width:785px;margin: 1% 20%;height:100%;"><iframe src="'+$(this).attr("href")+'" frameborder="0" width="785" height="550" scrolling="no" align="center"></firame></div>');
        return false;
    });

	$("input.text").focus(function () {
		$(this).prev("label").hide();
	});

	$("input.text").blur(function () {
		if ( !$(this).val() )
			$(this).prev("label").show();
	});

	$("input.text").each(function () {
		if ( $(this).val() ) {
			$(this).prev("label").hide();
		}
	});

	$("#login-expand").click(function () {
		var cut = $(this).parent().parent().find("div.cut");
		if ( cut.css("display") == 'none' ) cut.show();
		else if ( cut.css("display") == 'block' ) cut.hide();

		$("input.text").each(function () {
			if ( $(this).val() ) {
				$(this).prev("label").hide();
			}
		});

		return false;
	});

	$("div.menu>ul>li").mouseover(function () {
		$(this).addClass("act");
		if ( $(this).hasClass("expand") )
			$(this).find("p").addClass("expand");
	});

	/*$("div.menu>ul>li>a").click(function(){		return false;	});*/

	$("div.menu>ul>li").mouseout(function () {
		$(this).removeClass("act");
		$(this).find("p").removeClass("expand");
	});

	$("#header-menu ul ul").each(function () {		pos = $(this).parent().offset ();
		pos.top += 27;
		$(this).css(pos).find("li.overlay").css("width", $(this).parent().innerWidth()-2);
	});

	$(window).resize(function() {
		$("#header-menu ul ul").each(function () {
			pos = $(this).parent().offset ();
			pos.top += 27;
			$(this).css(pos).find("li.overlay").css("width", $(this).parent().innerWidth()-2);
		});
	});

	$('ul.categories').jScrollPane({
		scrollbarWidth:11,
        verticalDragMinHeight: 86,
        verticalDragMaxHeight: 86,
		scrollbarMargin:0
	});

	$('ul.hot20').jScrollPane({
		scrollbarWidth:11,
        verticalDragMinHeight: 86,
        verticalDragMaxHeight: 86,
		scrollbarMargin:0
	});

	$("#player-1-select").click(function () {		$("#player-1").show();
		$("#player-2").hide();
		$("#player-1-select").parent().addClass("act");
		$("#player-2-select").parent().removeClass("act");
		return false;
	});

	$("#player-2-select").click(function () {		$("#player-1").hide();
		$("#player-2").show();
		$("#player-1-select").parent().removeClass("act");
		$("#player-2-select").parent().addClass("act");
		return false;
	});

	var size = $("#popular li").size();
	var i = 0;
	$("#popular li:eq(0)").css("display","block");

	function sec() {
		$("#popular li:eq("+i+")").fadeOut("slow");
		i++;
		if ( i == size ) i = 0;
		$("#popular li:eq("+i+")").fadeIn("slow");
	}

	$(".prev a").click(function () {		$("#popular li:eq("+i+")").fadeOut("slow");
		i--;
		if ( i < 0 ) i = size-1;
		$("#popular li:eq("+i+")").fadeIn("slow");
		return false;	});

	$(".next a").click(function () {
		$("#popular li:eq("+i+")").fadeOut("slow");
		i++;
		if ( i == size ) i = 0;
		$("#popular li:eq("+i+")").fadeIn("slow");

		return false;
	});

	$("#hot20_expand").click(function () {
		$("ul.hot20 li.hide").show();
		$(this).hide();
		return false;
	});

	choice = Math.floor(Math.random()*($("ul.hot20_post li").size()))+1;
	$("ul.hot20_post li:nth-child("+choice+")").show();

	setInterval(sec, 30000);

	if ( $("#ajaxReviewsUpdateSpan").attr("rel","logged") ) {		if ( $.cookie("reviews") == "1" ) {			$("#buttonRecencyHide").text('скрыть');
			$("div#ajaxReviewsUpdateSpan").show();
		}
		else {			$("div#ajaxReviewsUpdateSpan").hide();
			$("#buttonRecencyHide").text('показать');
			$.cookie("reviews",0);
		}	}

	if ( $.cookie("related") == "1" ) {
		$("#related_block").hide();
		$("#buttonRelatedHide").text('показать');
		$.cookie("related",0);
	}
	else {
		$("#buttonRelatedHide").text('скрыть');
		$("#related_block").show();
	}

	$("#buttonRecencyHide").click( function () {		$("#ajaxReviewsUpdateSpan").toggle();
		if ( $.cookie("reviews") == "1" ) {			$.cookie("reviews","0");
			$("#buttonRecencyHide").text('показать');
		}
		else {			$.cookie("reviews","1");
			$("#buttonRecencyHide").text('скрыть');
		}

		return false;	});

	$("#buttonRelatedHide").click( function () {
		$("#related_block").toggle();
		if ( $.cookie("related") == "1" ) {
			$.cookie("related","1");
			$("#buttonRelatedHide").text('скрыть');
		}
		else {
			$.cookie("related","0");
			$("#buttonRelatedHide").text('показать');
		}

		return false;
	});

	$("#kp_help").dialog ({
		autoOpen: false,
		width: '50%',
		modal: true,
	});

	$(".rating_what a").click ( function () {		$("#kp_help").dialog ('open');
		return false;	});

	var text = '<p>Что бы вы не тратили своё драгоценное время на просмотр фильма, который не понравился большой массе зрителей, мы создали рейтинг просмотра, по которому вы сами сможете решить смотреть вам данную картину или нет.</p>';
	text += '<p>Рейтинг оценивается по <b>10</b> бальной шкале. Верхняя часть рейтинга (большими буквами) определяет  рейтинг по версии "<b>Кинопоиск</b>", а в нижней части рейтинг по версии сайта "<b>IMDB</b>"';
	text += '<p><b>Пример:</b><br/>';
	text += '<b>8.45</b> - оценка, данные значения для каждой киноленты разные.<br/>';
	text += '(45767) - количество зрителей которые проголосовали за данный фильм.</p><br/>';

	text += '<b>По мнению пользователей оценки можно распределить по следующей шкале:</b><br/>';

	text += '<p><b>1.1-1.9</b> - ужаснее некуда, стыдно смотреть такое.<br/>';

	text += '<b>2.0-2.9</b> - ужас, не советую<br/>';

	text += '<b>3.0-3.9</b> - Не понравился большой части аудитории, смотреть не стоит так считают многие киноманы.<br/>';

	text += '<b>4.0-4.9</b> - Обычный фильм, как многие говорят, ничего нового, но все, же смотреть можно.<br/>';

	text += '<b>5.0-6.5</b> - Хороший фильм, можно посмотреть, большой части аудитории данная лента понравилась.<br/>';

	text += '<b>6.6-7,9</b> - Очень хороший фильм, стоит обязательно посмотреть.<br/>';

	text += '<b>8.0-10.0</b> - Шедевр, в обязательном порядке посмотрите, уж точно не пожелеете! Зачастую такие фильмы получают награды, и являются прорывом в киноиндустрии!</p>';

	$("#kp_help").html (text);

	$("a.year-select").click(function(){		$(this).attr("href",$("select[name=year-select]").val());	});

    $('a[href=#top]').click(function(){
        $('html, body').animate({scrollTop:0}, 'fast');
        return false;
    });

    $(".showfull").live("click",function() {        $("ul.catalog").load($(this).attr("href")+"&ajax=true");        return false;    });
	
	$("#design").change(function() {
		$("#main_css").attr('href',$(this).val());
		$.cookie("main_css",$(this).val());
	});
	
	if($.cookie("main_css")!='') {
		$("#design").val($.cookie("main_css"));
	}
});
