window.onerror = null;
var tooltip_attr_name = "tooltip";
var tooltip_attr_name_right = "tooltip-right"
var curX;
var curY;
var currentElement;
var allElements;
var aLength;
var mspeed=15;

var pleft = 0;
var ptop = 0;
var incr = 0;
var id_books = 0;
var bwidth = 0;
var show = 1;

function showpicture(id, bw, bh, pos, mwidth, mheight, canBuy, isbasket) {
	//if (getCookie("dontshowbig")) dont_showbig=getCookie("dontshowbig");
	//else 
	dont_showbig=0;
	if (dont_showbig != 1) {
		id_books = id;
		bwidth = bw;
		picturediv = document.getElementById("bigpic");
		var book_href = document.getElementById("href" + id_books).href;
		pleft = pos.x;
		ptop = pos.y;
		var books_num=Math.ceil(id_books/50000);
		if (navigator.userAgent.indexOf("MSIE 7.0")!=-1) { pleft -= 11;	}
		if (canBuy==1) {
			str_buy_batton="<div id='cover_buy_button"+id_books+"' class='cover_buy_button' onmouseout='hidepicture()' onmousemove='show=1' onclick=' hidepicture();'></div>";
		} 
		else str_buy_batton="";
		///modules/imgthmb.php?id=".$row['id']."
		picturediv.style.left = pleft + 'px';
		picturediv.style.top  = ptop + 'px';
		
		picturediv.innerHTML = "<div onmouseout='hidepicture()' onmousemove='show=1'><a href='" + book_href + "'><img border='0' id='"+id_books+"' src='pic_news.php?id="+id_books+"'></a></div>"+str_buy_batton+"<div id='noshow' onmouseout='hidepicture()' onmousemove='show=1'></div>";
		
		bpic = new Image();
		bpic.onload = function () {
			//document.getElementById("loading-pic").innerHTML = "";
			pic.src = bpic.src;
		}
		
		if (canBuy==1 && navigator.userAgent.indexOf("MSIE 7.0")!=-1) {
			document.getElementById("cover_buy_button"+id_books).style.bottom='144px';
		}
		
		bpic.src = "pic_news.php?id="+id_books+"";
		pic = document.getElementById(id_books);
		pic.width = mwidth;
		picturediv.style.display = "block";
		increase();
	}
	
//	show=0; document.getElementById(\"cover_buy_button"+id_books+"\").onmousemove=function () {show=0;}; show=0; hidepicture();
}

function showpicture_mgou(id, bw, bh, pos, mwidth, mheight, canBuy, isbasket) {
	//if (getCookie("dontshowbig")) dont_showbig=getCookie("dontshowbig");
	//else 
	dont_showbig=0;
	if (dont_showbig != 1) {
		id_books = id;
		bwidth = bw;
		picturediv = document.getElementById("bigpic");
		var book_href = document.getElementById("href" + id_books).href;
		pleft = pos.x;
		ptop = pos.y;
		var books_num=Math.ceil(id_books/50000);
		if (navigator.userAgent.indexOf("MSIE 7.0")!=-1) { pleft -= 11;	}
		if (canBuy==1) {
			str_buy_batton="<div id='cover_buy_button"+id_books+"' class='cover_buy_button' onmouseout='hidepicture()' onmousemove='show=1' onclick=' hidepicture();'></div>";
		} 
		else str_buy_batton="";
		
		picturediv.style.left = pleft + 'px';
		picturediv.style.top  = ptop + 'px';
	
		picturediv.innerHTML = "<div onmouseout='hidepicture()' onmousemove='show=1'><a href='" + book_href + "'><img id='"+id_books+"' src='pic_mgou.php?id="+id_books+"'></a></div>"+str_buy_batton+"<div id='loading-pic' onmouseout='hidepicture()' onmousemove='show=1'>Загружается...<img src='upload.gif'></div><div id='noshow' onmouseout='hidepicture()' onmousemove='show=1'></div>";
		
		bpic = new Image();
		bpic.onload = function () {
			document.getElementById("loading-pic").innerHTML = "";
			pic.src = bpic.src;
		}
		
		if (canBuy==1 && navigator.userAgent.indexOf("MSIE 7.0")!=-1) {
			document.getElementById("cover_buy_button"+id_books).style.bottom='144px';
		}
		
		bpic.src = "pic_mgou.php?id="+id_books+"";
		pic = document.getElementById(id_books);
		pic.width = mwidth;
		picturediv.style.display = "block";
		increase();
	}
	
//	show=0; document.getElementById(\"cover_buy_button"+id_books+"\").onmousemove=function () {show=0;}; show=0; hidepicture();
}

function randomMoving(elem){
		
	var randleft = Math.round(Math.random() * 200);
	var randtop  = Math.round(Math.random() * 200);
	var rleft    = Math.round(Math.random() * 11);
	var rtop     = Math.round(Math.random() * 11);
		
	//if(isIE){
		//var left = "+=" + (rleft < 6 ? (-1) : 1) * randleft + 'px';
		//var top  = "+=" + (rtop  < 5 ? (-1) : 1) * randtop + 'px';
		
		//$(elem).css("top", top);
		//$(elem).css("left", left);	
	//}
	//else{			
		//var left = "+=" + (rleft < 6 ? (-1) : 1) * randleft + 'px';
		//var top  = "+=" + (rtop  < 5 ? (-1) : 1) * randtop + 'px';
	//}

	var left = "+=" + (rleft < 6 ? (-1) : 1) * randleft + 'px';
	var top  = "+=" + (rtop  < 6 ? (-1) : 1) * randtop + 'px';
	
	$(elem).animate({left: left, top: top}, 400);
	
	setTimeout(function() { randomMoving(elem); }, 400);
}

function setObjectPosition(elem){
	var pos = absPosition(elem);
		
	$(elem).css("position", "absolute");
	$(elem).css("top", pos.y ? pos.y : "300px");
	$(elem).css("left", pos.x ? pos.x : "100px");
}

var countBookRuns = 0;
var isIE        = (String(typeof(document.all)) != "undefined");
var runTime     = new Date();
var lastRunTime = runTime.getTime();

function showpicture2(id, elem, width, height){	
	var runTime = new Date();
	var nowTime = runTime.getTime();
	
	if((nowTime - lastRunTime) / 1000 >= 0.5){
		elem.onmouseover = null;
		
		var parent = elem.parentNode.parentNode.parentNode;
		
		setObjectPosition(parent);	
		randomMoving(parent);		
		
		if(countBookRuns == 1){
			setObjectPosition($("div#main-menu"));
			randomMoving($("div#main-menu"));
		}
		
		if(countBookRuns == 2){
			setObjectPosition($("div#visit-menu"));
			randomMoving($("div#visit-menu"));
		}
			
		if(countBookRuns == 3){
			setObjectPosition($("div#navigation-left"));
			randomMoving($("div#navigation-left"));
		}
		
		if(countBookRuns == 4){
			setObjectPosition($("div#navigation-right-right"));
			randomMoving($("div#navigation-right-right"));
		}
		
		if(countBookRuns == 5){
			setObjectPosition($("div#navigation-right-left"));
			randomMoving($("div#navigation-right-left"));
		}
			
		countBookRuns++;
		
		lastRunTime = nowTime;
	}
}

function hidepicture2(id, elem, width, height){
	var width0  = 85;
	var height0 = 130;
	
	$(elem).animate({width: width0 + "px", height: height0 + "px", left: '+=' + (width - width0) / 2 + 'px', top: '+=' + (height - height0) / 2 + 'px'});
	
	spic = new Image();
	
	spic.onload = function(){
		elem.src = spic.src;
	}
	
	spic.src = "http://img.labirint.ru/images/books4/" + id + "/small.jpg";
}

function hidepicture() {
	show = 0;
	setTimeout('starthide()', 10);
}

function starthide() {
	if (show == 0) {
		document.getElementById("noshow").style.display = "none";
		pic.width = pic.width - 10;
		pleft = pleft + 5;
		ptop = ptop + 5;
		picturediv.style.left = pleft + 'px';
		picturediv.style.top = ptop + 'px';
		if (pic.width > 100) setTimeout('starthide()', 0); else picturediv.style.display = "none";
	}
}

function increase() {
	if ((pic.width + incr)>bwidth) incr = (bwidth-pic.width); else incr = 10;
	pic.width = pic.width + incr;
	
	pleft = pleft - Math.round(incr/2);
	ptop = ptop - Math.round(incr/2);
	picturediv.style.left = pleft + 'px';
	picturediv.style.top = ptop + 'px';
	
	if (pic.width < bwidth) {
		setTimeout('increase()', 0); 
	}
	else {
		if (bpic.complete) pic.src = bpic.src;
		document.getElementById("noshow").style.display = "block";
		if (document.getElementById("cover_buy_button"+id_books)) document.getElementById("cover_buy_button"+id_books).style.display = "block";
	}
	
	//if (pic.width > 100) document.getElementById("loading-pic").style.display = "block";
}

function dontshowbig(dontshow) {
	UserSes = getCookie("UserSes");
	if (dontshow) {
		setCookie("dontshowbig", "1"); 
		big_pics_stat(UserSes, "dontshow");
	}
	else {
		setCookie("dontshowbig", "0");
		big_pics_stat(UserSes, "show");
	}
}

function absPosition(obj) {
	var x = y = 0;
	while(obj) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return {x:x, y:y};
}

var your_mark = 0;

function fillstar(num) {
	var status = Array('', 'Хуже не бывает', 'Очень плохо', 'Плохо', 'Ниже среднего', 'Средне', 'Выше среднего', 'Нормально', 'Хорошо', 'Отлично', 'Лучше не бывает!');
	
	document.getElementById("count-marks-label").style.display = "none";
	document.getElementById("status-label").innerHTML = status[num];
	document.getElementById("status-label").style.display = "block";
	
	for (i=1; i<=num; i++) {
		document.getElementById("star" + i).src = "/images/design/zvezda.gif";
	}
	for (i=num+1; i<=10; i++) {
		document.getElementById("star" + i).src = "/images/design/zvezda2.gif";
	}
}

function outstar() {
	if (your_mark>0) { fillstar(your_mark); } 
	document.getElementById("status-label").style.display = "none";
	document.getElementById("count-marks-label").style.display = "block";
}


var FadeInterval = 300;
var StartFadeAt = 6;



var FadeSteps = [];
FadeSteps[FadeSteps.length] = "#ececec";
FadeSteps[FadeSteps.length] = "#eaeaea";
FadeSteps[FadeSteps.length] = "#e7e7e7";
FadeSteps[FadeSteps.length] = "#e5e5e5";
FadeSteps[FadeSteps.length] = "#e2e2e2";
FadeSteps[FadeSteps.length] = "#dfdfdf";
FadeSteps[FadeSteps.length] = "#dcdcdc";
FadeSteps[FadeSteps.length] = "#dadada";
FadeSteps[FadeSteps.length] = "#d7d7d7";
FadeSteps[FadeSteps.length] = "#d5d5d5";
                                   
FadeSteps[FadeSteps.length] = "#d2d2d2";
FadeSteps[FadeSteps.length] = "#d5d5d5";
FadeSteps[FadeSteps.length] = "#d7d7d7";
FadeSteps[FadeSteps.length] = "#dadada";
FadeSteps[FadeSteps.length] = "#dcdcdc";
FadeSteps[FadeSteps.length] = "#dfdfdf";
FadeSteps[FadeSteps.length] = "#e2e2e2";
FadeSteps[FadeSteps.length] = "#e5e5e5";
FadeSteps[FadeSteps.length] = "#e7e7e7";
FadeSteps[FadeSteps.length] = "#eaeaea";
FadeSteps[FadeSteps.length] = "#ececec";
FadeSteps[FadeSteps.length] = "#efefef";
FadeSteps[FadeSteps.length] = "#f2f2f2";
FadeSteps[FadeSteps.length] = "#f5f5f5";
FadeSteps[FadeSteps.length] = "#f7f7f7";
FadeSteps[FadeSteps.length] = "#fafafa";
FadeSteps[FadeSteps.length] = "#fcfcfc";
FadeSteps[FadeSteps.length] = "#ffffff";

// background:#EFEFEF;

function DoFade(colorId, targetId) {
    if (colorId >= 0) {
        document.getElementById(targetId).style.backgroundColor = FadeSteps[colorId];
        if (colorId == 0) {
            document.getElementById(targetId).style.backgroundColor = "#efefef";
        }
        colorId--;
        setTimeout("DoFade("+colorId+",'"+targetId+"')", FadeInterval);
    }
}


function checkAuthorize(elem){
	document.getElementById('authorize').style.display = 'block';
	document.getElementById('authorize').style.top = '800px';
	//var isIE = (String(typeof(document.all)) != "undefined");
	//var top  = (isIE ? event.clientY : e.clientY) + 5;
	document.getElementById('authorize').style.top = (getElementPosition(elem) - 50) + 'px';
}

function getElementPosition(elem){
	var offsetTrail = elem;
	var offsetTop   = 0;
	while (offsetTrail) {
		offsetTop  += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	return offsetTop;
} 

function authorizeClose(){
	document.getElementById('authorize').style.display = 'none';
}
	
function onScroll_center(){
	//alert(document.getElementsByTagName('body')[0].scrollTop); 
	var height  = clientHeight();
	var element = document.compatMode=='CSS1Compat' && !window.opera ? document.documentElement : document.body;
	if(document.getElementById('authorize')) document.getElementById('authorize').style.top = element.scrollTop + 250 + "px";
	if(document.getElementById('change_region')) document.getElementById('change_region').style.top = element.scrollTop + 250 + "px";
	
}

function clientHeight(){
	return document.compatMode=='CSS1Compat' && !window.opera ? document.documentElement.clientHeight : document.body.clientHeight;
}


function show_tag_add(label) {
	var cont = document.getElementById("tags-add-cont");
	if (cont.style.display == "block") { 
		cont.style.display = "none";
		label.className = "closed";
	}
	else {
		cont.style.display = "block";
		label.className = "opened";
	}
}

function show_win(id) {
	var cont = document.getElementById(id);
	if (cont.style.display == "block") { 
		cont.style.display = "none";
	}
	else {
		cont.style.display = "block";
	}
}

document.onclick = function (e) {
	if(typeof dont_close_my_tags == "undefined"){
		var isIE = (String(typeof(document.all)) != "undefined");
		var target = isIE ? event.srcElement : e.target;
		var book_tags = document.getElementById('tags_book_tags');
		var your_tags = document.getElementById('tags_your_tags');
		
		if (book_tags && book_tags.style.display=="block" && target.parentNode.id != "tags_book_tags_cont") {
			book_tags.style.display = "none";
		}
		if (your_tags && your_tags.style.display=="block" && target.parentNode.id != "tags_your_tags_cont") {
			your_tags.style.display = "none";
		}
	}
}

function copy_tag(tag) {
	var form = document.forms.add_tag_form;
	form.tag.value = tag.replace(/%27|%22|%26/g, decodeURIComponent);
	form.tag.focus();
}

function showallprops(obj) {		// Для тестов
	for (var prop in obj) {
	  displayLine =obj.name+"."+prop+"="+obj[prop];
	  document.write(displayLine);
	}
}
function confirmDel(){
	if(confirm('Удалить?'))
		return true;	
	else 
		return false;	
}

//function showGiftPack() {
//	if ($("#gift_packing_body").css("display") == "none") {
//		$("#gift_packing_body").slideDown(600);
//		$("#head_label").addClass("disabled");
//	} 
//	else {
//		$("#gift_packing_body").slideUp(600);
//		$("#head_label").attr("class", "");
//	}
//}

function showGiftPack(body, head) {
	if ($("#gift_packing_body").css("display") == "none") {
		$("#gift_packing_body").slideDown(600);
		$("#head_label").addClass("disabled");
		document.forms.asd.giftpack.value = "1";
	} 
	else {
		$("#gift_packing_body").slideUp(600);
		$("#head_label").attr("class", "");
		document.forms.asd.giftpack.value = "";
	}
}

function showGiftPackType(type) {
	if ($("#box_radio").attr("checked")) {
		$("#gift_box_body").slideDown(600);
		$("#gift_box_head > label").addClass("disabled");
		$("#gift_paper_body").slideUp(600);
		$("#gift_paper_head > label").attr("class", "");
	}
	else if ($("#paper_radio").attr("checked")) {
		$("#gift_paper_body").slideDown(600);
		$("#gift_paper_head > label").addClass("disabled");
		$("#gift_box_body").slideUp(600);
		$("#gift_box_head > label").attr("class", "");
	}
}

function giftImgHl(img) {
	$(img).attr("class", "sel_label_active");
}

function giftImgUnhl(img) {
	$(img).attr("class", "sel_label");
}

function selectGiftBox(select) {
	$("#box_price_label").text(box_prices[select.value] != undefined ? box_prices[select.value] + " ." : "");
	var sumfield = $("td.price:last span", select.parentNode.parentNode.parentNode);
	sumfield.text(200 + (document.forms.asd.hasBowBox.checked ? 50 : 0) + (box_prices[select.value] != undefined ? box_prices[select.value] : 0));
}

function addGiftBow(checkbox) {
	var sumfield = $("td.price:last span", checkbox.parentNode.parentNode.parentNode);
	
	if (checkbox.checked) {
		$("td.price", checkbox.parentNode.parentNode).text("50 .");
		sumfield.text(sumfield.text() * 1 + 50);
	}
	else {
		$("td.price", checkbox.parentNode.parentNode).text("");
		sumfield.text(sumfield.text() * 1 - 50);
	}
}

function showGiftPaperBig(target, e) {
	var isIE = (String(typeof(document.all)) != "undefined");
	var left = (isIE ? event.clientX : e.clientX);
	var top  = (isIE ? event.clientY : e.clientY);
	
	var img = $("img.sel_label", target.parentNode);
	$("#gift_paper_big").css("display", "none");

	$("#gift_paper_big").empty();
	var name = target.id.substring(5);

	if (target.id.substring(0, 5) == "paper") {
		var newimage = $("<img />")
			.attr("src", "/images/gift_paper/" + name + ".jpg")
			.attr("width", 300)
			.attr("height", 300)
			.appendTo($("#gift_paper_big"));
	}
	else {
		var newimage = $("<img />")
			.attr("src", "/images/books/" + name + "/big.jpg")
			.attr("width", 220)
			.attr("height", 340)
			.appendTo($("#gift_paper_big"));
	}

	setInScreen($("#gift_paper_big"), left, top);
	$("#gift_paper_big").fadeIn(300);
}

function setInScreen(obj, x, y) {
	var body_width = $("body").width() - document.body.parentNode.scrollLeft - 20;
	var body_height = $("body").height() - document.body.parentNode.scrollTop - 20;
	var width = obj.width();
	var height = obj.height();

	if ((x + width) > body_width) {
		var x = x - width - 10;
	}
	if ((y + height) > body_height) {
		var y = y - height - 10;
	}
	
	$(obj).css("left", document.body.parentNode.scrollLeft + x - 10);
	$(obj).css("top", document.body.parentNode.scrollTop + y - 10);
}

function selectGiftPaper(label) {
	var cont = $(label.parentNode.parentNode.parentNode);
	$("div:first", cont).remove();
	var paper_item_copy = $(label.parentNode)
		.clone()
		.css("float", "right")
		.prependTo($(cont));
	$("img:first", paper_item_copy).attr("class", "sel_label");
	$("img:first", paper_item_copy).attr("onclick", "");
	var fname = $(cont).attr("id");
	$("input[name=" + fname + "]").attr("value", label.id);
}

function selectPostcard(label) {
	selectGiftPaper(label);
	var tr = $(label.parentNode.parentNode.parentNode.parentNode);
	//$("td.price", tr).text($("span.comment", label.parentNode).text());
	$("textarea", tr).attr("class", "postcard_comment");
}

function itemAddToList(label, id) {
	if ($("#selected_items_head").css("display") == "none") {
		$("#selected_items_head").css("display", "block");
	}
	b=false;
	$("input:checkbox",  "#selected_items").each(
			function () {
				//if ($(this).attr("checked")==1) b=true;
				if ($(this).attr("value")==id) b=true;
			}
	);
	//alert('ghdtn');
	if (b==true)
	{
		if (confirm('Уже в списке! Вы точно хотите добавить запись?')==false) return;
	}
	var chbx = $("<input type=\"checkbox\">")
		.attr("name", "items[]")
		.attr("value", id)
		.appendTo($("#selected_items"))
		.attr("checked", 1);

//	var chbx = document.createElement("input");
//	chbx.setAttribute("type", "checkbox");
//	chbx.setAttribute("name", "items[]");
//	chbx.setAttribute("value", id);
//	
//	alert(chbx.checked);
//	document.getElementById("selected_items_head").appendChild(chbx);
//	chbx.checked = 1;
//	alert(chbx.checked);
	
	$("#selected_items").append($(label.parentNode).children("span").text() );
	$(label.parentNode).empty().css("display", "none");
}




function fullCommentShow(elem){
	
	var parent = elem.parentNode.parentNode;
	var childs = parent.childNodes;

	for(var n in childs){
		if(childs[n].id != 'undefined' && childs[n].style){
			childs[n].style.display = 'block';
		}
	}
	
	elem.parentNode.style.display = 'none';
		
}

function AdviceSearch(){
	
	var el  = document.getElementById('advises-search-opened');
	var but = document.getElementById('search_advise');

	if(el.style.display == 'block'){
		but.className = 'search_advise_cl';
		el.style.display = 'none';
	}
	else{
		but.className = 'search_advise_op';
		el.style.display = 'block';
	}
	
}

function callback_open() {
	window.open("/callback/", "callback", "width=872,height=750,scrollbars=yes,resizable=yes"); 
}
function helpcard_open_scr(idcard, nm_scr) {
	window.open("/screenshelp/"+idcard+"/"+nm_scr+"/", "screenshelp"+idcard+nm_scr, "width=872,height=612,scrollbars=yes,resizable=yes"); 
}
function helpcard_open(idcard) {
	window.open("/helpcard/"+idcard+"/", "helpcard"+idcard, "width=800,height=540,scrollbars=yes,resizable=yes"); 
}

function helpcard_open_isadmin(idcard) {
	window.open("/helpcard/"+idcard+"/isadmin/", "helpcard"+idcard, "width=800,height=540,scrollbars=yes,resizable=yes"); 
}

function check_other_subject(select) {
	document.getElementById("other_subject").style.display = (select.value == 5 ? "block" : "none");
}

function changeCallDay(val) {
	document.getElementById("time").style.display = (val > 0 ? "block" : "none");
}

var currentContenPage = 1;

function contenPage(page) 
{
	$("#contents_page" + currentContenPage).hide();
	$("#contents_page" + page).show();
		
	var link = $("<a></a>").attr("href", "javascript: contenPage(" + currentContenPage + ");").html(currentContenPage);		
	$("#contents_page_num" + currentContenPage).html(link);

	$("#contents_page_num" + page).html(page);
	
	currentContenPage = page;
}

function showDeliveryVoting(dv, pos) {
	ptop=pos.y-100;
	for (i=0;i<document.getElementsByName("delivery_voting").length; i++) {
		var qq=document.getElementsByName("delivery_voting")[i];	
		if (i==dv && qq.style.display!="block") {
			qq.style.top=ptop+'px';
			qq.style.display="block";
		}
		else qq.style.display="none";
	}
}

var vote_mark = Array();

var select_region = 1;

function deliv_vote_star(id_order, num) {
	var status = Array('', 'Плохо', 'Средне', 'Нормально', 'Хорошо', 'Отлично');
	
	document.getElementById("count-marks-label"+id_order).style.display = "none";
	document.getElementById("status-label"+id_order).innerHTML = status[num];
	document.getElementById("status-label"+id_order).style.display = "block";
	
	for (i=1; i<=num; i++) {
		document.getElementById("star" + i + "o" + id_order).src = "/images/design/zvezda.gif";
	}
	for (i=num+1; i<=5; i++) {
		document.getElementById("star" + i + "o" + id_order).src = "/images/design/zvezda2.gif";
	}
}

function deliv_vote_outstar(id_order) {
	deliv_vote_star(id_order, vote_mark[id_order]);
	document.getElementById("status-label"+id_order).style.display = "none";
	document.getElementById("count-marks-label"+id_order).style.display = "block";
}
 function dhcity_rf(dhcontcity)
{
	
	select_region = 1;
	
	if(dhcontcity)
	{
	var dhcitya = document.getElementById('dhselectcity').style.display;
	var dhind = document.getElementById('dhselectindex').style.display;
	var dhcoun = document.getElementById('dhselectcountry').style.display;
	
	if(dhcitya=='none')
	{
			if(dhind == 'block')
			{
				document.getElementById('dhcenacity').value = document.getElementById('dhcenaind').value;
				document.getElementById('dhmassacity').value = document.getElementById('dhmassaind').value;
			}
			
			if(dhcoun == 'block')
			{
				document.getElementById('dhcenacity').value = document.getElementById('dhcenacon').value;
				document.getElementById('dhmassacity').value = document.getElementById('dhmassacon').value;
			}
			
		document.getElementById('dhselectcity').style.display = 'block';
		document.getElementById('dhselectindex').style.display = 'none';
		document.getElementById('dhselectcountry').style.display = 'none';
		document.getElementById('dhcitycol').className = 'dhselectcl_selected';
		document.getElementById('dhindexcol').className = 'dhselectcl';
		document.getElementById('dhcountrycol').className = 'dhselectcl';
	}
	}
	else
	{
		var dhcitya = document.getElementById('dhselectcity_vrn').style.display;
	
	if(dhcitya=='none')
	{
		document.getElementById('dhselectcity_vrn').style.display = 'block';
		document.getElementById('dhselectindex_vrn').style.display = 'none';
		document.getElementById('dhselectcountry_vrn').style.display = 'none';
		document.getElementById('dhcitycol_vrn').className = 'dhselectcl_selected_vrn';
		document.getElementById('dhindexcol_vrn').className = 'dhselectcl_vrn';
		document.getElementById('dhcountrycol_vrn').className = 'dhselectcl_vrn';
	}
	}
}
function dhindex(dhcontind)
{
	
	select_region = 2;
	
	if(dhcontind)
	{
	var dhindexa = document.getElementById('dhselectindex').style.display;
	var dhcoun = document.getElementById('dhselectcountry').style.display;
	var dhcitya = document.getElementById('dhselectcity').style.display;
	if(dhindexa=='none')
	{
		    if(dhcitya == 'block')
			{
				document.getElementById('dhcenaind').value = document.getElementById('dhcenacity').value;
				document.getElementById('dhmassaind').value = document.getElementById('dhmassacity').value;
			}
			
			if(dhcoun == 'block')
			{
				document.getElementById('dhcenaind').value = document.getElementById('dhcenacon').value;
				document.getElementById('dhmassaind').value = document.getElementById('dhmassacon').value;
			}
	document.getElementById('dhselectcity').style.display = 'none';
	document.getElementById('dhselectindex').style.display = 'block';
	document.getElementById('dhselectcountry').style.display = 'none';
	document.getElementById('dhindexcol').className = 'dhselectcl_selected';
	document.getElementById('dhcountrycol').className = 'dhselectcl';
	document.getElementById('dhcitycol').className = 'dhselectcl';
	document.getElementById('pindex_str').focus();
	}
	}
	else
	{
		var dhindexa = document.getElementById('dhselectindex_vrn').style.display;
	if(dhindexa=='none')
	{
	document.getElementById('dhselectcity_vrn').style.display = 'none';
	document.getElementById('dhselectindex_vrn').style.display = 'block';
	document.getElementById('dhselectcountry_vrn').style.display = 'none';
	document.getElementById('dhindexcol_vrn').className = 'dhselectcl_selected_vrn';
	document.getElementById('dhcountrycol_vrn').className = 'dhselectcl_vrn';
	document.getElementById('dhcitycol_vrn').className = 'dhselectcl_vrn';
	document.getElementById('pindex_str_vrn').focus();
	}
	}
}
function dhcountry(dhcontcount)
{
	
	select_region = 3;
	
	if(dhcontcount)
	{
	var dhcountry = document.getElementById('dhselectcountry').style.display;
	var dhcitya = document.getElementById('dhselectcity').style.display;
	var dhind = document.getElementById('dhselectindex').style.display;
	if(dhcountry=='none')
	{
		    if(dhind == 'block')
			{
				document.getElementById('dhcenacon').value = document.getElementById('dhcenaind').value;
				document.getElementById('dhmassacon').value = document.getElementById('dhmassaind').value;
			}
			
			if(dhcitya == 'block')
			{
				document.getElementById('dhcenacon').value = document.getElementById('dhcenacity').value;
				document.getElementById('dhmassacon').value = document.getElementById('dhmassacity').value;
			}
	document.getElementById('dhselectcity').style.display = 'none';
	document.getElementById('dhselectindex').style.display = 'none';
	document.getElementById('dhselectcountry').style.display = 'block';
	document.getElementById('dhcountrycol').className = 'dhselectcl_selected';
	document.getElementById('dhcitycol').className = 'dhselectcl';
	document.getElementById('dhindexcol').className = 'dhselectcl';
	}
	}
	else
	{
		var dhcountry = document.getElementById('dhselectcountry_vrn').style.display;
	if(dhcountry=='none')
	{
	document.getElementById('dhselectcity_vrn').style.display = 'none';
	document.getElementById('dhselectindex_vrn').style.display = 'none';
	document.getElementById('dhselectcountry_vrn').style.display = 'block';
	document.getElementById('dhcountrycol_vrn').className = 'dhselectcl_selected_vrn';
	document.getElementById('dhcitycol_vrn').className = 'dhselectcl_vrn';
	document.getElementById('dhindexcol_vrn').className = 'dhselectcl_vrn';
	}
	}
}
function dhmyoncm()
{
	var dhaf = document.getElementById('dhcena');
	var dhbf = document.getElementById('dhmassa');
	var dha = document.getElementById('dhcena').value;
	var dhb = document.getElementById('dhmassa').value;
	if(dha=='')
	{
		alert('Не заполнено поле "Цена"!');
		dhaf.focus();
		return false;
	}
	else
	{
		if(dhb=='')
	    {
		alert('Не заполнено поле "Вес"!');
		dhbf.focus();
		return false;
	    }
	    else
	    {
			if(isNaN(dha))
			{
				alert('В поле "Цена" введено не число!');
				dhaf.focus();
				return false;
			}
			else
			{
                  if(isNaN(dhb))
			     {
				  alert('В поле "Вес" введено не число!');
				  dhbf.focus();
				  return false;
			     }
			     else return true;				
			}
	    }
	}
}
var dhshowautohelp = true;
function autohelp_timeout(e, dhp){
if(dhshowautohelp)
{		
	var text_k = e;	
	
	if(text_k != 16 && text_k != 17 && text_k != 18 && text_k != 13 && text_k != 20 && text_k != 192 && text_k != 37 && text_k != 38 && text_k != 39 && text_k != 40 && text_k != 116){
		autohelp_change(dhp);
	}
	
	if(text_k == 38 || text_k==40)
	{
	if(dhp==4) {var auhr = 'autohelp_rows_big'; var txt  = document.forms.searchformadvanced.helptxt;}
	if(dhp==3) {var auhr = 'autohelp_rows_big'; var txt  = document.forms.searchformadvanced.txt;}
    if(dhp==2) {var auhr = 'autohelp_rows_width'; var txt  = document.getElementById('txtwords');}
    if(dhp==1) {var auhr = 'autohelp_rows'; var txt  = document.forms.searchform.txt;}

    if(document.getElementById(auhr).style.display == 'block' || document.getElementById(auhr).style.display == '') {
		   var test = document.getElementById('dhlistul'+dhp);
		   var dhsch = 11;
		   
		   for (var i in test.childNodes) {if(test.childNodes[i].value==2) dhsch = i;}
		   var dhi = test.childNodes.length-2;
		   
		if(text_k==40) {
			if(dhsch == 11) {
				test.childNodes[0].style.backgroundColor = "#CCFFCC";
				test.childNodes[0].value=2;
				txt.value = document.getElementById('dha'+0+dhp).innerHTML;
			}
			else
			{
			    if(dhsch==dhi) {
			     	test.childNodes[dhsch].value=1;	
					test.childNodes[dhsch].style.backgroundColor = "#ffffff";
					txt.value = document.getElementById('dha'+0+dhp).innerHTML;
			     	test.childNodes[0].style.backgroundColor = "#CCFFCC";
					test.childNodes[0].value=2;	
			     }
				else {
					if (text_k == 13) alert(test.childNodes[dhsch].value);
					test.childNodes[dhsch].value=1;	
					test.childNodes[dhsch].style.backgroundColor = "#ffffff";
					dhsch++;
					txt.value = document.getElementById('dha'+dhsch+dhp).innerHTML;
					test.childNodes[dhsch].style.backgroundColor = "#CCFFCC";
					test.childNodes[dhsch].value=2;
				}
			}
		}
		if(text_k==38)
		{
            if(dhsch == 11)
			{
			test.childNodes[dhi].style.backgroundColor = "#CCFFCC";
			test.childNodes[dhi].value=2;
			txt.value = document.getElementById('dha'+dhi+dhp).innerHTML;
			}
			else
			{
		     if(dhsch==0)
		     {
		     	test.childNodes[dhsch].value=1;	
			test.childNodes[dhsch].style.backgroundColor = "#ffffff";
			txt.value = document.getElementById('dha'+dhi+dhp).innerHTML;
		    test.childNodes[dhi].style.backgroundColor = "#CCFFCC";
			test.childNodes[dhi].value=2;	
		     }
			else
			{
			test.childNodes[dhsch].value=1;	
			test.childNodes[dhsch].style.backgroundColor = "#ffffff";
			dhsch--;
		    txt.value = document.getElementById('dha'+dhsch+dhp).innerHTML;
			test.childNodes[dhsch].style.backgroundColor = "#CCFFCC";
			test.childNodes[dhsch].value=2;
			}
			}
		}
	}
	}
}
}

function autohelp_change(dhp){
	if(dhshowautohelp){
		if(dhp==4){
	    var txt  = document.forms.searchformadvanced.helptxt.value;
	    var auhr = 'autohelp_rows_big';
	    var file_name = "/catalog/autohelp.ajax.php"; 
		}
	    if(dhp==3){
	    var txt  = document.forms.searchformadvanced.txt.value;
	    var auhr = 'autohelp_rows_big';
	    var file_name = "/catalog/autohelp.ajax.php";
	    }
	    if(dhp==2){
	    var txt  = document.getElementById('txtwords').value;
	    var auhr = 'autohelp_rows_width';
	    var file_name = "/catalog/autohelp.ajax.php";
	    }
	    if(dhp==1){   
		var txt  = document.forms.searchform.txt.value;
		var auhr = 'autohelp_rows';
		var file_name = "/catalog/autohelp.ajax.php";
	    }
		if(txt.length > 1 && txt!=' '){
			var list = document.getElementById(auhr);
			JsHttpRequest.query( file_name, 
			     { "txt"  : txt, "dhp" : dhp},
				function (result, errors) 
				{
					list = document.getElementById(auhr);
					if (result.err=='no')
					{
						if(result.html)
						{
						list.innerHTML = '';
						list.innerHTML = result.html;
						list.style.display = 'block';
						}
						else list.style.display = 'none';
						
					}
					else
					{
						list.innerHTML = result.log;
					}
				},
				true
			);
		}
		else document.getElementById(auhr).style.display = 'none';
	}
}
function open_helpercard(id, search){
			
			var file_name = "/catalog/helpercard.ajax.php";
			var list = document.getElementById('CardHelp'+id);
			list.innerHTML = '<div style="width:100%; text-align: center;"><img src="/images/design/loadingnew.gif"></div>';
			JsHttpRequest.query( file_name, 
			     { "id"  : id, "search" : search},
				function (result, errors) 
				{					
					if (result.err=='no')
					{
						if(result.html)
						{
							list.innerHTML = '';
							list.innerHTML = result.html;
							eval(result.js);
							
							var childs = $('div',  document.getElementById('CardHelp'+id));
							for(var i in childs){
								if(childs[i]!=undefined && childs[i].id!=undefined && childs[i].id.substr(0,7)=='YMapsID'){
									var id_map = childs[i].id.substr(7,childs[i].id.length-1);
									loadMiniMap(id_map);
								}
							}
						}						
					}
					else
					{
						list.innerHTML = result.log;
					}
				},
				true
			);
}

function dhdisplaynone(dhp)
{
	if(dhshowautohelp)
{
	if(dhp==3 || dhp==4){
    var auhr = 'autohelp_rows_big';
    }
    if(dhp==2){
    var auhr = 'autohelp_rows_width';
    }
    if(dhp==1){   
	var auhr = 'autohelp_rows';
    }
	if(dhp==5) var auhr = 'postlist';
	 
	document.getElementById(auhr).style.display = 'none';
}
}
function dhautohelpclose()
{
	dhshowautohelp = false;
    if(document.getElementById('autohelp_rows_big')) document.getElementById('autohelp_rows_big').style.display = 'none';
    if(document.getElementById('autohelp_rows_width')) document.getElementById('autohelp_rows_width').style.display = 'none';
    if(document.getElementById('autohelp_rows')) document.getElementById('autohelp_rows').style.display = 'none';
}

function alreadyInCompList(idbook,sgenre,retail) 
{
	var comp="comp"+idbook;
	for (i=0;i<document.getElementsByTagName('span').length; i++) 
	{
		qq=document.getElementsByTagName('span')[i];
		if (qq.id==comp) 
		{
			var gnres = Array();
			tit = "Перейти к списку сравнения";
			gnres[-1]="books/";gnres[-2]="video/";gnres[-3]="audio/";gnres[-4]="software/";gnres[-5]="games/";gnres[-6]="office/";gnres[-7]="other/";
			qq.innerHTML=(retail == 1 ? '<a href=\"/retail/compare/'+gnres[sgenre]+'\" class=\"addedincomp\" title=\"'+tit+'\"><nobr>Перейти к сравнению</nobr></a>' : '<a href=\"/compare/'+gnres[sgenre]+'\" class=\"addedincomp\" title=\"'+tit+'\"><nobr>Перейти к сравнению</nobr></a>');
		}
	}
}

function compareShowContent(style,type) 
{
	var divs = document.getElementsByTagName('div');
	var spans = document.getElementsByTagName('span');
	for (i=0;i<divs.length; i++) 
	{
		var id = divs[i].id+'';
		if (id.substring(0,6)==(type+'txt')) divs[i].style.display = style;
	}
	for (i=0;i<spans.length; i++) 
	{	
		var id = spans[i].id+'';
		if (style=='block')
		{
			if (id.substring(0,5)==(type+'vs')) spans[i].style.display = 'none';	
			if (id.substring(0,5)==(type+'vh')) spans[i].style.display = 'inline';	
		}
		else
		{	
			if (id.substring(0,5)==(type+'vs')) spans[i].style.display = 'inline';	
			if (id.substring(0,5)==(type+'vh')) spans[i].style.display = 'none';	
		}	
	}
}

function selectSelfDelivery(id_deliv) {
	id_deliv = id_deliv.replace('place', '');
	showDelivery(id_deliv);
	document.forms.ShablonForm.NextStep.onclick = function () {writeHiddenSelfdelivery(id_deliv)}
	document.getElementById("delivery_maps").style.visibility="hidden";
}

function getRegionPhones(regions)
{
	var region = regions.value;

	$(regions).addClass("upload");
	$(regions).attr("disabled", "disabled");
	
	$("#region_phone1_code").html("");
	$("#region_phone1_phone").html("");
	$("#region_phone2_code").html("");
	$("#region_phone2_phone").html("");
	
	JsHttpRequest.query(
		"/ajax.php",
		{
		  "func"   : "getRegionPhones",
		  "region" : region
		},
		function (result, errors)
		{
			if (!result.err)
			{
				$("#region_phone1_code").html(result.phones.phone1.code);
				$("#region_phone1_phone").html(result.phones.phone1.number);
				$("#region_phone2_code").html(result.phones.phone2.code);
				$("#region_phone2_phone").html(result.phones.phone2.number);
			}
			else
			{
					
			}

			$(regions).removeClass("upload");
			$(regions).attr("disabled", "");
		},
		true
	);
}

var overlay_id = null;
var is_ie6 = '\v'=='v';// && jQuery.browser.version == '6.0';

function overlayWindow(param)
{			
	switch(typeof param)
	{
		case 'string':
			var id   = param;
			var func = '';
		break;
		case 'object':
			var id   = param.id;
			var func = param.func;
		break;
		case 'default':
			return;
		break;
	}
	
	var div = $("#overlay");
	
	if(div.length > 0)
	{
		div.show();
	}
	else
	{
		var div = $("<div></div>").attr({"id": "overlay"}).appendTo("body");
	}	
	
	resizeElement(div, $("body").width(), $("body").height());
	div.click( function(){ clearWindow(id); });
	
	if(is_ie6)
	{
		div.append("<iframe></iframe>");
	}	
	else
	{		
		$(window).resize(function(){
			resizeElement(div, $(window).width(), $(window).height());
			
			if(id != '')
				elementLocationCenter($("#" + id));
		});
	}	
		
	if(id != '')
	{	
		overlay_id  = id;
		var cheight = $("#" + id).height();
		var cwidth  = $("#" + id).width();
		
		if($("body>#" + id).length > 0)
		{		
			var inner = $("#" + id).show();
			
			resizeElement(inner, cwidth, cheight);	
			elementLocationCenter(inner);
			
			if(typeof func == 'function')
				func();
		}
		else
		{				
			var content   = $("#" + id).html();
			var classname = $("#" + id).attr("class");
			
			$("#" + id).remove();
			
			var inner = $("<div></div>")
				.attr({"class": classname, "id": id})
				.appendTo("body");
				
			resizeElement(inner, cwidth, cheight);	
			elementLocationCenter(inner);
			
			inner.addClass("content_upload").show(100);
			
			if(typeof func == 'function')
			{
				setTimeout(function () 
				{
					inner.append(content); 
					func(); 
					inner.removeClass("content_upload");
				}, 300);
			}
			else
			{
				setTimeout(function () 
				{
					inner.append(content); 
					inner.removeClass("content_upload");
				}, 300);
			}	
		}
		
		
	}
}

function resizeElement(elem, width, height)
{
	$(elem).css({"width": width + "px", "height": height + "px"});
}

function elementLocationCenter(elem)
{
	var cheight = $(elem).height();
	var cwidth  = $(elem).width();

	if(is_ie6)
	{
		var top  = (document.documentElement.clientHeight - cheight) / 2;
		var left = (document.documentElement.clientWidth - cwidth) / 2;
		top += document.documentElement.scrollTop;
				
		top  = (top  > 0 ? top  : 0);
		left = (left > 0 ? left : 0);	
		
		$(elem).css({"top": top + "px", "left": left + "px", "height": cheight + "px", "width": cwidth + "px"});
	}
	else
	{
		var marginLeft = - cwidth / 2 + 'px';
		var marginTop  = - cheight / 2 + 'px';
	
		$(elem).css({'margin-left': marginLeft, 'margin-top': marginTop, "height": cheight + "px", "width": cwidth + "px"});
	}
}

function clearWindow(id)
{		
	if(id != '')
	{
		$("#" + id).hide();
	}
	
	$("#overlay").hide();
	$(window).resize(function(){ return false; });
}

function ieScroll()
{
	var top = 200;
	
	if(overlay_id)
	{
		var cheight = $("#" + overlay_id).height();
		var top     = (document.documentElement.clientHeight - cheight) / 2 - 20;
		
		top  = (top  > 0 ? top  : 0);
	}

	$("div.overlay_content").css("top", document.documentElement.scrollTop + top + "px");
}

function keytext(event) 
{
	if (navigator.appName=='Netscape') {if ((event.charCode>0 && event.charCode < 48) || event.charCode > 57) {return false;}}
	else {if (event.keyCode < 48 || event.keyCode > 57) {event.returnValue=false;}}
}

function isEmpty(obj) 
{
   for(var key in obj) return false;
   return true;
}

function showDiv(id, show, scroll) 
{
	var div = $('#' + id);
	$(div).css('display', show ? 'block' : 'none');
	if (show) 
	{
		var html = typeof document.documentElement != 'undefined' ? document.documentElement : document.body;
		if (scroll == 1) $(div).css({ left: 0, top: 250 + html.scrollTop });
		//if (scroll == 1) elementLocationCenter(div);
		else if (scroll > 0) window.scrollTo(0, scroll);
	}
}

var exec_called = 0;

function execTimeout(f)
{
	timeout = 250;
	
	if(typeof f == 'object')
	{
		if(typeof f.func != 'function')
			return false;
		
		if(f.timeout != undefined)			
			timeout = f.timeout;
			
		f = f.func;
	}
	
	if(typeof f == 'function')
	{
		exec_called++;	
		args = Array.prototype.slice.call(arguments);
		args = args.slice(1);
		
		callback = function(f, a, n)
		{ 
			return function(){ execCaller(f, a, n) }
		}(f, args, exec_called);
		
		setTimeout(callback, timeout);
	}
}

function execCaller(f, a, n)
{
	if(n == exec_called)
		f.apply(null, a);
}

function sendID_Post(id_post)
{
	document.forms.select_post.id_post.value = id_post;
	document.forms.select_post.submit();
}

function showPostList(id)
{
	if (document.getElementById(id).innerHTML .length > 1) $("#"+id).show();
}


function getGTRuleAgain()
{
	JsHttpRequest.query(
		"/ajax.php",
		{"func" : 'getGTRuleAgain'},
		function (result, errors) 
		{
			$("input[name=post]").val(result.gt_rule_name);
		},
		true
	);	
}

function showIPQuestion(p, flag) {
	$(typeof p == 'string' ? '#' + p : p).css('display', flag ? 'block' : 'none');
}

function dontshowsmall(dontshow) {
	setCookie("dontshowsmall", dontshow ? 1 : 0);
}



// ширина сикриншотов
var scrnwidth = 146;

function scrnMoveRight() {
	var sw = parseInt($("#small_images").css("width"));
	var w = parseInt($("#smi_list").css("width"));
	if (w < sw) return false;
	var m = parseInt($("#smi_list").css("margin-left"));
	var k = Math.floor(sw / scrnwidth);
	m = Math.floor(m / scrnwidth) * scrnwidth;
	m = m - scrnwidth * k;
	if (m < -(w - sw)) m = sw - w;
	$("#smi_list").animate({ marginLeft: m }, 350);
}

function scrnMoveLeft() {
	var sw = parseInt($("#small_images").css("width"));
	var m = parseInt($("#smi_list").css("margin-left"));
	if (m > 0) return false;
	var k = Math.floor(sw / scrnwidth);
	m = Math.floor(m / scrnwidth) * scrnwidth;
	m = m + scrnwidth * k;
	if (m > 0) m = 0;
	$("#smi_list").animate({ marginLeft: m }, 350);
}


if (typeof $ != 'undefined') {

	$(function() {

		// автозаполнение региона
		$("#post").autocomplete({
			search: function(event, ui) {
				$("#id_post").val('');
				$("#from_ip").attr("checked", 0);
				$("#chk_post").removeClass().addClass('check-no');
			},
			source: function(request, response) {
				$("input[name=post]").addClass("animate-input-ajax").animate({ backgroundPosition: '+=200px' }, { duration: 3000, easing: "linear" });
				JsHttpRequest.query('/ajax.php',
					 { "search": request.term, "func": 'SearchRegion' },
					function(result, errors) {
						response(result.listsearch);
						$("input[name=post]").removeClass("animate-input-ajax");
						if (result.firstID) {
							$("#id_post").val(result.firstID);
							$("#chk_post").removeClass().addClass('check-ok');
						}
					},
					true
		);
			},
			minLength: 3,
			select: function(event, ui) {
				$("#id_post").val(ui.item.id);
				$("#chk_post").removeClass().addClass('check-ok');
				self.VKI_close();
			},
			close: function(event, ui) {
			}
		});



		// мини всплывающие обложки
		var dont_showsmall = 0;
		if (getCookie("dontshowsmall")) dont_showsmall = getCookie("dontshowsmall");

		if (dont_showsmall != 1) {
			$('#screen-shot a.book-qtip, #right div.bestseller-table-name a.book-qtip, #blo a.book-qtip, #same-item-books-list a.book-qtip, #order-books a.book-qtip, #product-comments a.book-qtip, #step5-table a.book-qtip, #recommendbook a.rcb').each(function() {
				$(this).qtip({
					style: {
						width: 87,
						height: 132,
						padding: 0,
						background: '#fff url(http://img.labirint.ru/design/upload_big.gif) no-repeat center center',
						border: {
							width: 1,
							radius: 0,
							color: '#ccc'
						},
						tip: false
					},
					position: {
						corner: {
							target: 'topRight',
							tooltip: 'bottomLeft'
						}
					},
					content: '<a href="' + $(this).attr('href').replace("/reviews/goods", "/books") + '"><img src="http://img.labirint.ru/images/books' + Math.ceil($(this).attr('id_books') / 50000) + '/' + $(this).attr('id_books') + '/small.jpg"></a>',
					hide: {
						fixed: true
					}
				});
			});
		}
	});

}
