$(document).ready(function() {
	var ItemCache = [];
	
	/* IE Check */
    var ieversion = 0;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		ieversion = new Number(RegExp.$1); // capture x.x portion and store as a number
	}

	function initSubMenu() {
		/* Menu */
		$("#menu li.sub").addClass("subMenu").append("<div><span></span></div>");
		$("#menu li.last a").each(function(){
			offsMenu = $("#menu").offset();
			offs = $(this).offset();
			if ( (ieversion < 9) && (offs.top == offsMenu.top)) {
				x = offs.left - offsMenu.left;
				x = 980 - x -16;
				l = Math.round(x/2);
				r = x - l;
				$(this).css({ paddingLeft: l, paddingRight: r });
			}
			
		});
		$("#menuSub div.subMenuBody").prepend("<div class='top'><span></span></div>").append("<div class='bottom'><span></span></div>");
		$("#menu li.subMenu div").hover(function(){
				$(this).addClass("hovered");
			}, function() {
				$(this).removeClass("hovered");
		}).add("#menu li#lFind a, #header p.menu a, a#lCityAlt").click(function(){
			$("#menuSub div.subMenu").fadeOut(200);
			var par;
			if ($(this).attr("id") == "lCityAlt") {
				par = $("#lCity");
			} else {
				par = $(this).parent();
			}

			if (par.hasClass("opened")) {
				par.removeClass("opened");
			} else {
				$("#menu li.opened, #header p.menu").removeClass("opened");
				par.addClass("opened");
				$("#menuSub div.subMenu[class*='"+par.attr("id")+"']").fadeIn(200).find("input#searchText").each(function(){
					this.focus();
				});
			}
			return false;
		});
		$("#menuSub div.subMenuBottom, #menuSub div.subMenuBody div.bottom span").click(function(){
			$("#menu li.opened, #header p.menu").removeClass("opened");
			$("#menuSub div.subMenu").fadeOut(200);
		});
		
		/** Regions */
		$("a.region").click( function() {
			var link = ROOT + "int/controls/main-menu/region/" + $(this).attr("id").substr(6);
			$.get( link, {}, function(data){
				$("#regionColumns").html( data ); 
			});
			
			return false;
		});		
	}

    initSubMenu();

    /*
	if ( WithMenu ) {
        $.get(ROOT+"int/controls/main-menu/", function(data) {
            $("#menu").after(data);
            initSubMenu();
        });
    }
    */
	
	/* Promo and menuSub IE fix */
	$("div.promo span.header").css("opacity",0.8);
	if (ieversion == 6) {
		$("div.promo a.nav img").attr("src","/i/0.gif");
	}
	var lineBlock = $("div.promo div.line");
	var lineBlockWidth = ($("div.promo a.block").length+1)*155;
	lineBlock.css("width",lineBlockWidth);
	$("div.promo a.toRight").click(function(){
		if (lineBlock.queue().length == 0) {
			var lPos = parseInt(lineBlock.css("left"));
			if (lPos <= -(lineBlockWidth-1220)) {
				$("div.promo a.block:first").clone().appendTo(lineBlock);
				lineBlock.animate({ left: '-=155px' }, 450, function(){
					$("div.promo a.block:first").remove();
					lineBlock.css("left",-(lineBlockWidth-1220));
				});
			} else {
				lineBlock.animate({ left: '-=155px' }, 450 );
			}
		}
		return false;
	});
	$("div.promo a.toLeft").click(function(){
		if (lineBlock.queue().length == 0) {
			var lPos = parseInt(lineBlock.css("left"));
			if (lPos >= 0) {
				lineBlock.css("left",lPos-155);
				$("div.promo a.block:last").clone().prependTo(lineBlock);
				lineBlock.animate({ left: '+=155px' }, 450, function(){
					$("div.promo a.block:last").remove();
				});
			} else {
				lineBlock.animate({ left: '+=155px' }, 450 );
			}
		}
		return false;
	});
	
	/* Viewer */
	$("div.viewer div.sidebar dl").css("cursor","pointer").hover(function(){
			$(this).addClass("hovered");
		}, function() {
			$(this).removeClass("hovered");
	}).add("div.viewer div.sidebar dd a").click(function(){
		if ($(this).is("a")) { elem = $(this).parent().parent("dl"); } else { elem = $(this); }
		if (!elem.is(".active")) {
			$("div.viewer div.sidebar dl").removeClass("active");
			elem.addClass("active");
			var idstr = elem.children().children("a").attr("id");
			var type  = idstr.substr(0, idstr.indexOf('-')); 
			var id    = idstr.substr(idstr.indexOf('-') + 1, idstr.length - type.length );
			var link  = ROOT + "int/controls/item/" + type + "/" + id;
	
			if ( !ItemCache[id] )  {
				ItemCache[id] = "<h3>Загрузка...</h3>"
				$.get( link, {}, function(data){
					ItemCache[id] = data;
					$("div.viewer div.info").html( data ) 
				});
			} 
				
			$("div.viewer div.info").html( ItemCache[id] );
		}
		return false;
	});
	/* TV Prog Viewer */
	$("div.tvprog div.sidebar ul.tvList li").hover(function(){
			$(this).addClass("hovered");
		}, function() {
			$(this).removeClass("hovered");
	}).add("div.tvprog div.sidebar ul.tvList li a").click(function(){
		if ($(this).is("a")) { elem = $(this).parent(); } else { elem = $(this); }
		if (!elem.is(".active")) {
			$("div.tvprog div.sidebar ul.tvList li").removeClass("active");
			elem.addClass("active");
			
			var idstr = elem.children("a").attr("id");
			if ( !idstr ) {
				$("div.tvprog div.info").html( "<h3>&nbsp;</h3>");
			} else {
				var type  = idstr.substr(0, idstr.indexOf('-')); 
				var id    = idstr.substr(idstr.indexOf('-') + 1, idstr.length - type.length );
				var link  = ROOT + "int/controls/item/" + type + "/" + id;
		
				if ( !ItemCache[id] )  {
					ItemCache[id] = "<h3>Загрузка...</h3>"
					$.get( link, {}, function(data){
						ItemCache[id] = data;
						$("div.tvprog div.info").html( data ) 
					});
				} 
					
				$("div.tvprog div.info").html( ItemCache[id] );
			}
		}
		return false;
	});

	/* Comments */
	$("form#comForm:not(.active)").hide();
	$("div.comments h5 a").click(function(){
		$(this).parents("h5").toggleClass("active");
		$("form#comForm").toggle();
		return false;
	});
	
	/* textHider */
	$("div.textHider").hide().after("<p class='textHiderButton'><em>Раскрыть весь текст</em></p>");
	$("p.textHiderButton em").click(function(){
		$(this).parent("p.textHiderButton").hide().prev("div.textHider").show();
	});


    /* Voting */
    $(".vote-link").one( "click", function() {
        $(this).removeAttr("href");
        var idstr = $(this).attr("id");
        var votingId  = idstr.substr(1, idstr.indexOf('-')-1);        
        var variantId = idstr.substr(idstr.indexOf('-') + 1, idstr.length - votingId.length );        
        var link  = ROOT + "int/controls/votings/" + votingId + "/";
        $("#v" + votingId ).load( link, {"vote" : variantId } );
        return false;
    });

    $(".vote-submit").one( "click", function() {
        var votingId  = $(this).attr("id").substr(2);
        var variantId = $("#v" + votingId + ' input[type=radio]:checked' ).val();
        if ( variantId ) {
            $(this).attr('value', 'Пожалуйста, подождите...').attr('disabled', 'disabled' );
            var link  = ROOT + "int/controls/votings/" + votingId + "/";
            $("#v" + votingId ).load( link, {"vote" : variantId } );
        }

        return false;
    });
});
