
var ishov = true;
var ie = 0/*@cc_on+@_jscript_version*10@*/; 
var flag_=true;

$(document).ready(function()
{	
    // переключалка табов
	$(".menu a.topblogers").click(function()
	{
		$(".bottom-m .text-input input:first").attr("value",'Поиск по блогерам');
		$(".bottom-m .text-input input:first").attr("rel",'Поиск по блогерам');
		$("input[name='where-search']").attr("value",0);
		$(".menu ul.a").hide();
		$(".menu ul.a:eq(1)").show();
		//
		$("#content .menu .bottom-m .footer div span").show();
	    $("#addons").hide();
	    $(".bottom-m .text-input input:last").attr("value","Найти");
		return false;
	});
	$(".menu a.topposts").click(function()
	{
		$(".bottom-m .text-input input:first").attr("value",'Поиск по постам');
		$(".bottom-m .text-input input:first").attr("rel",'Поиск по постам');
		$("input[name='where-search']").attr("value",1);
		$(".menu ul.a").hide();
		$(".menu ul.a:first").show();		
		//
		$("#content .menu .bottom-m .footer div span").show();
	    $("#addons").hide();
	    $(".bottom-m .text-input input:last").attr("value","Найти");		
		return false;
	});
	$(".menu a.topother").click(function()
	{
		$(".bottom-m .text-input input:first").attr("value",'Введите адрес поста');
		$(".bottom-m .text-input input:first").attr("rel",'Введите адрес поста');
		$("input[name='where-search']").attr("value",2);
		$(".menu ul.a").hide();
		$(".menu ul.a:last").show();
		//показ доптекста вместо стандарта.
		$(".ext-search").hide();
		$("#content .menu .bottom-m").css("padding-bottom","30px");
	    $("#content .menu .bottom-m .footer div span").hide();	
	    $("#addons").show();
	    $(".bottom-m .text-input input:last").attr("value","Открыть");		
		return false;
	});
	
	$(".text-input span input").focus(function()
	{
		$(".text-input span:first").addClass("active");
		if ($(this).get(0).value==$(this).attr("rel"))
			$(this).attr("value","");
	});
	$(".text-input span input").blur(function()
	{
		$(".text-input span:first").removeClass("active");
		if ($(this).get(0).value=='')
			$(this).attr("value",$(this).attr("rel"));
	});
	$(".date-border input").focus(function()
	{
		$(this).addClass("date-input-active");
		$(this).parent().addClass("date-border-active");
		var txt_ = $(this).get(0).value;
		if ((txt_=='дд') || (txt_=='мм') || (txt_="год"))
			$(this).attr("value","");
	});
	$(".date-border input").blur(function()
	{
		if ($(this).get(0).value=='')
		{
			$(this).attr("value",$(this).attr("rel"));
			$(this).removeClass("date-input-active");
			$(this).removeClass("date-input-activeb");
			$(this).parent().removeClass("date-border-active");
		}
		else
		{
			$(this).removeClass("date-input-active");
			$(this).addClass("date-input-activeb");
			$(this).parent().removeClass("date-border-active");
		}
	});
	$(".text-input .button").hover(function()
	{
		$(this).css("background-position","bottom");
	},
	function()
	{
		$(this).css("background-position","top");
	});
	
	$("#index").click(function(e)
	{		
		if ($("#index .select-blogs-wrapper ul.select-blog-type.hidden").length==0)
		{
			if ($(e.target).attr("class")!='select-blog-type')
				$("#index .select-blogs-wrapper ul.select-blog-type").addClass("hidden");
		}
		
		if (($("#index .selest-year.hidden").length==0) || 
			(($("#index .selest-month.hidden").length==0)) ||
			(($("#index .selest-days.hidden").length==0)))
		{
			//alert("!");
			if (($(e.target).attr("class").indexOf('ul-select-list')==-1) && ($(e.target).attr("class").indexOf('date-input')==-1))
			{
				$("#index .ul-select-list").addClass("hidden");
				//$(".listdropactive").removeClass("listdropactive");
			}
		}		
	});
	
	$(".ext-search p.a .input54").keypress(function(e)
	{
		var cond = ((e.which > 48) && (e.which  < 57)) || (e.which<9);
		
		 if ($.browser.opera)
												   {
													   cond = cond || ((e.which<9) || (e.which==46) || (e.which==37) || (e.which==39)
																	|| (e.which==35) || (e.which==36)
																)
												   }

		
		if (!cond) return false;
	});
	
	// переключалка платформ (источник постов)
	$(".select-blogs-wrapper input").attr("value",1);
	$("input[name='type-of-blog']").attr("value",$("#index .select-blogs-wrapper .select-blog-type li:first").attr("rel"));
	
	$(".select-blogs-wrapper .select-blogs b").click(function()
	{
		var id_ = $(this).attr("rel");
		$(this).toggleClass("active");		
		$(".select-blogs-wrapper input[name='blogs-"+id_+"']").attr("value",1-$(".select-blogs-wrapper input[name='blogs-"+id_+"']").get(0).value);
	});
	// список типов блогов
	$("#index .select-blogs-wrapper span.select-blog-type").click(function()
	{
		if ($("#index .select-blogs-wrapper ul.select-blog-type").attr("class")=='select-blog-type')
		{
			$("#index .select-blogs-wrapper ul.select-blog-type").addClass("hidden");
		}
		else
		{
			$("#index .select-blogs-wrapper ul.select-blog-type").removeClass("hidden");
		}
	});	
	$("#index .select-blogs-wrapper .select-blog-type li").click(function()
	{		
		$("#index .select-blogs-wrapper .select-blog-type li").show();
		$(this).hide();
		//$(this).get(0).focus();
		$("input[name='type-of-blog']").attr("value",$(this).attr("rel"));	
		$("#index .select-blogs-wrapper span.select-blog-type").html($(this).html());
		$("#index .select-blogs-wrapper ul.select-blog-type").addClass("hidden");
		
	});
	// выпадающие списки выбора дня, месяца, года
		// год
		$(".ext-search p input:eq(2),.ext-search p input:eq(5)").focus(function()
		{
			$(".ul-select-list").addClass("hidden");
			$(".listdropactive").removeClass("listdropactive");
			$(this).addClass("listdropactive");
			var ofss=0;
			if ($.browser.msie && ($.browser.version==7)) ofss = -7;			
			
			var x_ = $(this).parent().offset().left;
			var y_ = $(this).parent().offset().top+$(this).innerHeight()+ofss+4;
			$(".ul-select-list.selest-year").css("top",y_+'px');
			$(".ul-select-list.selest-year").css("left",x_+'px');
			$(".ul-select-list.selest-year").width($(this).width()+2);
			$(".ul-select-list.selest-year").removeClass("hidden");		
		});	
		$(".ul-select-list.selest-year li").click(function()
		{		
			$(".listdropactive").attr("value",$(this).html());
			$(".listdropactive").addClass("date-input-activeb");
			$(".listdropactive").removeClass("listdropactive");
			$(".ul-select-list").addClass("hidden");
		});
		// месяц
		$(".ext-search p input:eq(1),.ext-search p input:eq(4)").focus(function()
		{
			$(".ul-select-list").addClass("hidden");
			$(".listdropactive").removeClass("listdropactive");
			$(this).addClass("listdropactive");
			var ofss=0;
			if ($.browser.msie && ($.browser.version==7)) ofss = -7;
			var x_ = $(this).parent().offset().left;
			var y_ = $(this).parent().offset().top+$(this).innerHeight()+ofss+4;
			$(".ul-select-list.selest-month").css("top",y_+'px');
			$(".ul-select-list.selest-month").css("left",x_+'px');
			$(".ul-select-list.selest-month").width($(this).width()+2);
			$(".ul-select-list.selest-month").removeClass("hidden");		
		});	
		$(".ul-select-list.selest-month li").click(function()
		{		
			$(".listdropactive").attr("value",$(this).html());
			$(".listdropactive").addClass("date-input-activeb");
			$(".listdropactive").removeClass("listdropactive");
			$(".ul-select-list").addClass("hidden");
		});
		// день
		$(".ext-search p input:eq(0),.ext-search p input:eq(3)").focus(function()
		{
			$(".ul-select-list").addClass("hidden");
			$(".listdropactive").removeClass("listdropactive");
			$(this).addClass("listdropactive");
			
			if ($(".ext-search p input:eq(0)").get(0)==$(this).get(0))
			{
				MakeDaysList($(".ext-search p input:eq(1)").get(0).value,$(".ext-search p input:eq(2)").get(0).value);
			}
			else
			{
				MakeDaysList($(".ext-search p input:eq(4)").get(0).value,$(".ext-search p input:eq(5)").get(0).value);
			}
			
			var ofss=0;
			if ($.browser.msie && ($.browser.version==7)) ofss = -7;
			var x_ = $(this).parent().offset().left;
			var y_ = $(this).parent().offset().top+$(this).innerHeight()+ofss+4;
			$(".ul-select-list.selest-days").css("top",y_+'px');
			$(".ul-select-list.selest-days").css("left",x_+'px');
			$(".ul-select-list.selest-days").width($(this).width()+2);
			$(".ul-select-list.selest-days").removeClass("hidden");		
		});	
		$(".ul-select-list.selest-days li").live("click",function()
		{
			$(".listdropactive").attr("value",$(this).html());
			$(".listdropactive").addClass("date-input-activeb");
			//$(".listdropactive").removeClass("listdropactive");			
		});


	// расширенный поиск/обычный
	$("#content .menu .bottom-m .footer div span").click(function()
	{
		$("#content .menu .bottom-m .footer div span").hide();
		$("#content .menu .bottom-m .collapse-esh").show();
		$("#content .ext-search").show();
		$("#content .menu .bottom-m").css("padding-bottom","16px");
	});
	$("#content .menu .bottom-m .collapse-esh").click(function()
	{
		$("#content .menu .bottom-m .footer div span").show();
		$("#content .ext-search").hide();
		$(this).hide();
		$("#content .menu .bottom-m").css("padding-bottom","30px");
	});
	
	declareEv()
});

function declareEv()
{
	spaceCtrl();
	$(window).resize(function()
	{
		spaceCtrl();
	});
	
}

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 MakeDaysList(month,year) // на входе - строка номер месяца (или 0) на выходе верска списка дней
{
	var m_ = 1*month;	
	var y_ = 1*year;

	if (y_!=y_) y_ = new Date().getFullYear();

	if (m_==m_)
		var dayCount = new Date(y_, m_, 0).getDate();
	else
		var dayCount = 31;	

	var html_='';
	var d_ = '';
	
		for (i=1;i<=dayCount;i++)
		{
			d_ = i;
			if (i<10) d_ = '0'+d_;
			html_=html_+'<li>'+d_+'</li>';
		}
	$(".ul-select-list.selest-days").html(html_);
}

