var numBlock = "scroller"; // ID блока для рисования цифр
var pref = "images/nums/num_"; // папка с картинками цифр
var numMarg = 19; // отступ между ЦИФРАМИ
var imgMarg = 2; // отступ между КАРТИНКАМИ в двух и более значных цифрах
var startNum = 1; // стартовое значение счетчика цифр

document.onload = function() {
	showHideText;
}

function init()
{
	var obj = getObj(numBlock);

	// вычисляем ширину блока с циферками
	var blockWid = (parseInt((document.body.clientWidth - 724) / 12) * 12) + 1;
	// ширина не может быть меньше 277
	blockWid = (blockWid > 0 && blockWid > 277)? blockWid : 277;

	obj.style.width = blockWid;

	// очищаем блок от старых циферок (нужен при resize'е)
	obj.innerHTML = "";

	var nowWid = 0;
	var i = startNum;

	nowWid += calcNowWid(i);
	while(nowWid <= blockWid)
	{
		// рисуем текущюю картинку цифры
		writeNumImg(i);
		// прибавляем к ширине блока размер(ширину) следующей цифры
		nowWid += calcNowWid(++i);
	}
}

function writeNumImg(num)
{
	// переводим int в string
	num = num + "";

	var blockObj = getObj(numBlock);

	for(var i = 0; i < num.length; i++)
	{
		// создаем картинку цифры
		var tmpImg = document.createElement("IMG");
		tmpImg.setAttribute("src", pref + num.charAt(i) + ".jpg");
		tmpImg.setAttribute("width", imgNumSize[num.charAt(i)]['width'] + "px");
		tmpImg.setAttribute("height", imgNumSize[num.charAt(i)]['height'] + "px");
		tmpImg.setAttribute("valign", "bottom");

		// устанавливаем отступ между картинками слева
		tmpImg.style.marginLeft = imgMarg + "px";
		if(i == 0)
			tmpImg.style.marginLeft = numMarg + "px"; // отступ между ЦИФРАМИ слева

		// устанавливаем отступ между картинками справа
		tmpImg.style.marginRight = imgMarg + "px";
		if(i == num.length - 1)
			tmpImg.style.marginRight = numMarg + "px";  // отступ между ЦИФРАМИ справа

		// впихиваем ЦИФРУ в нужный блок
		blockObj.appendChild(tmpImg);
	}
}

function calcNowWid(num)
{
	// переводим int в string
	num = num + "";

	// ширина цифры
	var wid = 0;
	for(var i = 0; i < num.length; i++) {
		wid += imgNumSize[num.charAt(i)]['width'];
	}

	// к ширине изображений прибавляем отступ между цифрами и отступ между картинками циферок
	return wid + (numMarg * 2) + ((num.length * (imgMarg * 2)) - (imgMarg * 2));
}

function getObj(objID)
{
	return document.getElementById(objID)? document.getElementById(objID): false;
}

/*
function show_full_img(img, width, height)
{
	window.open(img, '', 'width=' + width + ', height=' + height);
}
*/

function show_opt(optID, e)
{
	var obj = getObj("opt" + optID);
	if(!obj) return;

	var pagexoff = 0;
	var pageyoff = 0;

	if(window.event)
	{
		e = window.event;
		pagexoff = document.body.scrollLeft;
		pageyoff = document.body.scrollTop;
	}
	else
	{
		pagexoff = window.pageXOffset;
		pageyoff = window.pageYOffset;
	}

	mousex = e.clientX;
	mousey = e.clientY;

	obj.style.top = parseInt(mousey + pageyoff + 10) + "px";
	obj.style.left = parseInt(mousex + pagexoff + 10) + "px";

	obj.style.display = "block";
}

function hide_opt(optID)
{
	getObj("opt" + optID).style.display = "none";
}

function showHide(id){
    if (document.getElementById){
    obj = document.getElementById(id);
    if (obj.style.display == "none")
	{
		obj.style.display = "block";
		getObj('filter_ids').innerHTML = "Скрыть";
		readCookie(1);
	} else {
      obj.style.display = "none";
      getObj('filter_ids').innerHTML = "Показать";
	  readCookie(0);
    }
  }
}

function showHideText()
{
	if(getObj('filter_ids') && document.cookieEnabled)
	{
		var stat = document.cookie.split(";")[1].split("=")[1];
		if(getObj('filter_ids').style.display == "block" || stat == 1)
			getObj('filter_ids').innerHTML = "Скрыть";
		else
			getObj('filter_ids').innerHTML = "Показать";
	}
}

function readCookie(stat)
{
	if(document.cookieEnabled)
		document.cookie = "cookie_show=" + stat;
}

function verify_search()
{
	if(event.keyCode == 13)
		document.getElementById("form_search_id").submit();
}

function tab_color()
{
	var tables = document.getElementsByTagName("TABLE");
	for(var t = 0; t < tables.length; t++)
	{
//		if (tables[t].border > 1 && tables[t].className != "detail") tables[t].className += " detail";
		if(tables[t].className == "detail")
		{
			var k = 0;
			var tr = tables[t].getElementsByTagName("TR");
			for(var tr_nums = 0; tr_nums < tr.length; tr_nums++)
			{
				if(k % 2 == 0)
				{
					if(tr[tr_nums].className == "items") continue;
					else tr[tr_nums].className = "gray";
				}
				k++;
			}
		}

		if(tables[t].className == "c_img")
		{
			var img = tables[t].getElementsByTagName("IMG");
			for(var img_s = 0; img_s < img.length; img_s++)
			{
				img[img_s].style.cursor = "hand";
				img[img_s].style.cursor = "pointer";
			}
		}
	}
}

function check_img(select_value)
{
	if(select_value == 2)
	{
		document.getElementById("image_div").style.display = "none";
		document.getElementById("image_div_name").style.display = "none";
	}
	else
	{
		document.getElementById("image_div").style.display = "block";
		document.getElementById("image_div_name").style.display = "block";
	}
}

function check_img_onload()
{
	if(getObj('filter_cols'))
	{
		if(getObj('filter_cols').value == 2)
			getObj('image_div').style.display = "none";
	}
}

// Images;

function show_full_img(obj)
{
	var img_id;
	if(obj.img_id == undefined)
	{
		img_id = obj.getAttribute('img_id');
		alt_text = obj.getAttribute('alt_data');
	}
	else
	{
		img_id = obj.img_id;
		alt_text = obj.alt_data;
	}

	window.open('/cat_images/img.php?img_id=' + img_id + '&alt=' + alt_text, '', 'width=' + parseInt(imgSizes[img_id][0]) + ', height=' + parseInt(imgSizes[img_id][1]));
}

function show_full_img2(img_id, size_w, size_h)
{
	window.open('/cat_hit/' + img_id + ', ', 'width=' + parseInt(size_w + 25) + ', height=' + parseInt(size_h + 25));
}

function tabs_set_active(obj)
{
	var active_id = obj.id.split("_")[2]

	var par = obj.parentNode;
	var tabs_id = par.id.split("_")[2]

	var tabs_tabs = par.childNodes
	for (var i = 0; i < tabs_tabs.length; i++)
	{
		if (tabs_tabs[i].tagName != undefined) tabs_tabs[i].className = "tab"
	}

	obj.className = "tab active"

	var tabs_bocks = document.getElementById("tabs_blocks_" + tabs_id).childNodes
	for (var i = 0; i < tabs_bocks.length; i++)
	{
		if (tabs_tabs[i].tagName != undefined) tabs_bocks[i].style.display = "none"
	}

	document.getElementById("tabs_block_" + active_id).style.display = ""
}



var $cookie_fix = $.cookie;
var state = $cookie_fix("close");

/*Helper*/
/*$(function(){
	$("#helper .change_panel img, #helper .change_panel a").click(		
		function(){
			var mood = $("#helper .inner").css("display");
			if (mood == 'none')
			{
				$("#helper .change_panel img").attr("src", "/images/helper/close.gif");
				$("#helper").css("background", "url('/images/helper/bg.png') top left repeat");
				$("#helper .change_panel a").css({"color": "#5b6380", "border-bottom" : "1px dashed #5b6380", "font-weight" : "normal", "font-size" : "13px" });
				$("#helper .change_panel a").text("Скрыть");
				$("#helper .change_panel").css("text-align", "right");
				$("#helper .inner").show("1000");					
				$cookie_fix("close", "", {expires: 1});			
				
			} else {
				$("#helper .change_panel img").attr("src", "/images/helper/open.gif");
				$("#helper .change_panel a").css({color: "#ffffff", "border-bottom" : "none", "font-weight" : "bold", "font-size" : "15px" }).text("Помощь в навигации");				
				$("#helper .change_panel").css("text-align", "left");
				$("#helper .inner").hide("1000");	
				$("#helper").css("background", "#a9a9ad");
				$cookie_fix("close", "on", {expires: 1});
			}
		}	
	)

	if ($cookie_fix("close")){
		$("#helper .inner").css("display", "none");
		$("#helper .change_panel img").attr("src", "/images/helper/open.gif");	
	}

	if (!$cookie_fix("close") && state == ""){
		$("#helper .inner").css("display", "block");
		$("#helper").css("background", "url('/images/helper/bg.png') top left repeat");
		$("#helper .change_panel a").css({"color" : "#a9a9ad", "border-bottom" : "1px dashed #5b6380", "font-weight" : "normal", "font-size" : "13px" });
		$("#helper .change_panel a").text("Скрыть");
		$("#helper .change_panel").css("text-align", "right");		
	}
});


*/
$(function(){
	$("#helper .change_panel img, #helper .change_panel a").click(		
		function(){
			var mood = $("#helper .inner").css("display");
			if (mood == 'none')
			{
				$("#helper .change_panel img").attr("src", "/images/helper/close.gif");
				$("#helper").css("background", "url('/images/helper/bg.png') top left repeat");
				$("#helper .change_panel a").css({"color": "#5b6380", "border-bottom" : "1px dashed #5b6380", "font-weight" : "normal", "font-size" : "13px" });
				if (helplang=="eng")
				{
					$("#helper .change_panel a").text("Hide");
				} else {
					$("#helper .change_panel a").text("Скрыть");
				}
				$("#helper .change_panel").css("text-align", "right");
				$("#helper .inner").show("1000");					
				$cookie_fix("close", "", {expires: 1});			
				
			} else {
				$("#helper .change_panel img").attr("src", "/images/helper/open.gif");
				$("#helper .change_panel a").css({color: "#ffffff", "border-bottom" : "none", "font-weight" : "bold", "font-size" : "15px" });
				if (helplang=="eng") {
					$("#helper .change_panel a").text("Navigation");				
				} else {
					$("#helper .change_panel a").text("Помощь в навигации");				
				}
				$("#helper .change_panel").css("text-align", "left");
				$("#helper .inner").hide("1000");	
				$("#helper").css("background", "#a9a9ad");
				$cookie_fix("close", "on", {expires: 1});
			}
		}	
	)

	if ($cookie_fix("close")){
		$("#helper .inner").css("display", "none");
		$("#helper .change_panel img").attr("src", "/images/helper/open.gif");	
	}

	if (!$cookie_fix("close") && state == ""){
		$("#helper .inner").css("display", "block");
		$("#helper").css("background", "url('/images/helper/bg.png') top left repeat");
		$("#helper .change_panel a").css({"color" : "#a9a9ad", "border-bottom" : "1px dashed #5b6380", "font-weight" : "normal", "font-size" : "13px" });
		if (helplang=="eng") {
			$("#helper .change_panel a").text("Hide");
		} else {
			$("#helper .change_panel a").text("Скрыть");
		}
		$("#helper .change_panel").css("text-align", "right");		
	}

	
	$("label[for='opinion2'], input#opinion2").click(function(){
		$(".hidetr").css("display", "block");
		$(".hidetr2").css("display", "none");
	})	

	$("label[for='opinion3'], input#opinion3").click(function(){
		$(".hidetr2").css("display", "block");
		$(".hidetr").css("display", "none");
	})	
	

	$("label[for='opinion1'], input#opinion1").click(function(){
		$(".hidetr").css("display", "none");
		$(".hidetr2").css("display", "none");
	})


	$("form#tryrui").submit(
		function (){
			if ($("input:radio#opinion2").attr("checked"))
			{
				var len1 = $("textarea[name='addition']").val().length;
				if (len1 < 1)
				{
					alert("Пожалуйста, заполните поле 'Что нужно добавить'")
					return false;
				}
			}	

			if ($("input:radio#opinion3").attr("checked"))
			{
				var len1 = $("textarea[name='addition1']").val().length;
				if (len1 < 1)
				{
					alert("Пожалуйста, заполните поле 'Что по Вашему мнению нужно изменить?'")
					return false;
				}
			}
		}
	)

	function iframe() {
		$("iframe").css("display", "none")
	}
	
});


