var ishov = true;
var ie = 0/*@cc_on+@_jscript_version*10@*/;

$(document).ready(function () {
    // двигаем футер если нет кнопки далее
    var class_ = $("#content").attr("class")
    if (class_ == null) return;
    if (class_.indexOf("statistic") == -1) {
        if ($("#content .next").length == 0)
            $("#foot #f_right").css("top", "-91px");
    }
    // сохраняем строчки таблицы на странице статистика
    var len = $("#content.statistic-post table .title").length;
    if (len > 0) {
        $("#index").append("<div style='display:none' id='stat-rows-tmp'></div>");
        var obj = null;
        for (i = 0; i < len; i++) {
            obj = $("#content.statistic-post table .title:eq(" + i + ")");
            txt = obj.text();
            $("#stat-rows-tmp").append("<div>" + txt + "</div>");
        }
    }

    $("#popup").animate({ opacity: 0 }, 1);
    declareEv(); // события
    // what is browser  ??
    if (($.browser.msie) && (jQuery.browser.version == "6.0")) { ie = 56 }
    //alert($.browser.opera);

    // поиск
    $("#header .search .a").click(function () {
        if ($(this).get(0).value == "поиск в топах и топблогеров") {
            $(this).attr("value", "");
            //$(this).parent().addClass("search-active");
            $(this).css("color", "#464646");
            $("#ctl00_HeadContent_ctl00_searchtext").css("border-color", "#088FE5");
        }
    });
    $("#header .search .a").blur(function () {
        if ($(this).get(0).value == "") {
            $(this).attr("value", "поиск в топах и топблогеров");
            //$(this).parent().removeClass("search-active");
            $(this).css("color", "#d0d0d0");
            $("#ctl00_HeadContent_ctl00_searchtext").css("border-color", "#B5B5B5");
        }
    });

    // [дальше]
    $("#content .next a").hover(function () {
        $(this).parent().parent().parent().addClass("next-hovered");
    }, function () {
        $(this).parent().parent().parent().removeClass("next-hovered");
    });

    $("div.elem").mouseenter(function (e) {
        $(this).find("span.copy").show();
    });

    $("div.elem").mouseleave(function (e) {
        $(this).find("span.copy").hide();
    });
}
);

function declareEv()
{
	spaceCtrl();
	imgpr();
	$(window).resize(function()
	{
		spaceCtrl();
		imgpr();
	});
	// превьюшки
	$("#content:not(.micro-top) .elem .wrapper").hover(function()
	{		
		var obj = $(this);
				
		$("#popup").css("width",$(this).children(".i1").get(0).value+"px");
		$("#popup").css("height",$(this).children(".i2").get(0).value+"px");
		$("#popup .img").css("width",$(this).children(".i1").get(0).value+"px");
		$("#popup .img").css("height",$(this).children(".i2").get(0).value-8+"px");
		
		$("#popup .u1").width($(this).children(".i1").get(0).value-4); // 8
				
		$("#popup .u2").width($(this).children(".i1").get(0).value-2);
		
		//$("#popup .u3").width($(this).children(".i1").get(0).value);
		$("#popup .u3,#popup .u4").css("width",$(this).children(".i1").get(0).value+"px");
		
		//$("#popup .u4").width($(this).children(".i1").get(0).value);
		
		$("#popup .b4").width($(this).children(".i1").get(0).value-4);
		$("#popup .b4").css("background-position","0 -"+($(this).children(".i2").get(0).value-1)+"px");
				
		$("#popup .b3").width($(this).children(".i1").get(0).value-2);
		$("#popup .b3").css("background-position","0 -"+($(this).children(".i2").get(0).value-2)+"px");
		
		//$("#popup .b2").width($(this).children(".i1").get(0).value);
		$("#popup .b2").css("background-position","0 -"+($(this).children(".i2").get(0).value-3)+"px");
		
		//$("#popup .b1").width($(this).children(".i1").get(0).value);
		$("#popup .b1,#popup .b2").css("width",$(this).children(".i1").get(0).value+"px");
		$("#popup .b1").css("background-position","0 -"+($(this).children(".i2").get(0).value-4)+"px");
		
		$("#popup div").css("background-image","url("+obj.children("img").attr("src")+")");		

		$("#popup").css("left",$(this).offset().left+35-$(this).children(".i1").get(0).value/2+"px");
		$("#popup").css("top",$(this).offset().top+35-$(this).children(".i2").get(0).value/2+"px");
		if ((jQuery.browser.version=="9.24") && $.browser.opera)
		{
			$("#popup").css("top",$(this).offset().top+150+"px");
		}

		$("#popup").stop().animate({opacity: 1},500);
	},function()
	{});
	$("#popup").mouseout(function()
	{
			$("#popup").stop().animate({opacity: 0},500,function()
			{
				$("#popup").css("left","-11000px");
			});	
	});
	
	//
	$("#header .ico p a,#content.statistic .th a").hover(function()
	{
		if (ie!=56)
		{
			$(this).stop().animate({opacity: 0},100);
			$(this).animate({opacity: 1},150);
		}
	},function()
	{});	
}

function spaceCtrl() // контроль ширины заголовка в таблице на страницах статистики
{
	var len = $("#content.statistic-post table .title").length;
	var obj = null;
	for (i=0;i<len;i++)
	{
		obj = $("#content.statistic-post table .title:eq("+i+")");
		txt = $("#stat-rows-tmp div:eq("+i+")").text(); // текст из сохраненного буфера
		if (txt.length*7>=obj.width()) // строка не влезает в ячейку таблице так чтобы поместиться в 1 строчку
		{
			cnt = Math.round(obj.width()/7-2);
			txt = txt.substr(0,cnt); // отрежем невлезающую часть
			chr32 = txt.lastIndexOf(" "); // находим последний пробел c конца
			txt = txt.substr(0,chr32)+"..."; // формируем уменьшенный вариант
			obj.children("a").html(txt);
		}
	}
}


function imgpr()
{
	// в фототопе ныкаем картинки
	var ptw = $("#content.photo-top .elem:first .right-p").width();
	var cntph = Math.round(ptw/87)-2; // число фоток в элементе
	var lng = $("#content.photo-top .elem").length;
	for (i=0;i<lng;i++)
	{
		$("#content.photo-top .elem:eq("+i+") div.a:gt("+cntph+")").addClass("hidden");
		$("#content.photo-top .elem:eq("+i+") div.a:lt("+(cntph+1)+")").removeClass("hidden");
	}
	// в микротопе хитро центруем
	var lng = $("#content.micro-top .elem").length;
	for (i=0;i<lng;i++)
	{
		var obj = $(".elem div.a:eq("+i+") img");
		if (obj.width()<80)
		{
			obj.css("left",0);
			obj.css("top",0);
		}
	}

	// везде хитро центруем по высоте
	var lng = $("#content .elem").length;
	for (i=0;i<lng;i++)
	{
		var obj = $(".elem div.a:eq("+i+") img");
		if (obj.height()<80)
		{			
			obj.css("top",40-obj.height()/2);
		}
	}

	
	// подгоняем ширину,высоту	
	/*lng = $(".elem div.a").length;
	for (i=0;i<lng;i++)
	{		
		if ($(".elem div.a:eq("+i+")").attr("class")=="a")
		{			
			var obj = $(".elem div.a:eq("+i+") img");
			_w = obj.width();
			_h = obj.height();
			obj.after("<input type='hidden' class='i1' value='"+_w+"'/><input type='hidden' class='i2' value='"+_h+"'/>");
			if (_w<_h)
			{
				obj.attr("width","70");
				obj.attr("height",_h/(_w/70));
			}
			else
			{
				obj.attr("height","70");
				obj.attr("width",_w/(_h/70));
			}

			if (obj.attr("width")>90)
			{
				// центрируем по ширине
				obj.css("left",35-obj.attr("width")/2+"px");
			}
			$(".elem div.a:eq("+i+")").addClass("dummy");
		}
	}*/
}

