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;

window.onload = function() {
	if (document.getElementById("genres-menu")) {
		allElements = document.getElementById("genres-menu").getElementsByTagName("a");
		aLength = allElements.length;
		if (document.createElement)
			tooltip.create();
	}
	if (document.getElementById("genres-menu-top")) {
		allElements = document.getElementById("genres-menu-top").getElementsByTagName("a");
		aLength = allElements.length;
		if (document.createElement) 
			tooltip.create();
                
	}
	if (document.getElementById("main-menu")) {
		allElements = document.getElementById("main-menu").getElementsByTagName("a");
		aLength = allElements.length;
		if (document.createElement) 
			alt.create();
	}

	if (document.getElementById("genres-menu-search")) {
		allElements = document.getElementById("genres-menu-search").getElementsByTagName("a");
		aLength = allElements.length;
		if (document.createElement) 
			tooltip.create();
                
	}
	if (document.getElementById("view-contents")) {
		conLink = document.getElementById("view-contents");
		conLink.onclick = function() { overlayWindow("contents-tooltip"); }
	}
	if (document.getElementById("genre-list-link")) {
		genrelistLink = document.getElementById("genre-list-link");
		genrelistLink.onclick = function() { genrelist(); }
	}
		
	if (document.getElementById("footer")) {
		/* alert(document.getElementById("footer").offsetHeight); */
		document.getElementById("footer").style.height = "90px";	
	}
}

function AddArticle(){
	
	var id_article = document.getElementById('article_id').value;
	
	var list_id = document.getElementById('list_articles').value;

	var list_name = document.getElementById('list_articles_name').innerHTML;

	var MessAct = document.getElementById('mess_act');
	
	if(id_article=='')
		document.getElementById('mess_act').innerHTML = 'Не введено значение!';
	else if(isNaN(id_article))
		document.getElementById('mess_act').innerHTML = 'В поле введено не число!';
	else{
		document.getElementById('mess_act').innerHTML = '';
		
		JsHttpRequest.query('/catalog/addinadvise.ajax.php',
			     { "id_article"  : id_article,
			     	"list_id"    : list_id,
			     	"list_name"  : list_name},
				function (result, errors) {
					if(result.error!=''){
						document.getElementById('mess_act').innerHTML = result.error;
					}
					else{
						document.getElementById('list_articles').value = result.list_id;
						document.getElementById('list_articles_name').innerHTML = result.list_name;
						document.getElementById('articles_block').style.height = parseInt(result.height) + 'px';
					}
				},
				true
		);
	}
}

function DelArticle(id_article){
	
	var list_id = document.getElementById('list_articles').value;

	var list_name = document.getElementById('list_articles_name').innerHTML;
	
	JsHttpRequest.query('/catalog/addinadvise.ajax.php',
			     { "id_article"  : id_article,
			     	  "list_id"  : list_id,
			     	"list_name"  : list_name,
		     		      "del"  : 1 },
				function (result, errors) {
						document.getElementById('list_articles').value = result.list_id;
						document.getElementById('list_articles_name').innerHTML = result.list_name;
						document.getElementById('articles_block').style.height = parseInt(result.height) + 'px';
				},
				true
	);
}

function phone(numb, numbcoun, type){

	JsHttpRequest.query( '../catalog/phone_cab.ajax.php', 
			     { "numb"  : numb,
			       "type"  : type,
			    "numbcoun" : numbcoun},
				function (result, errors) {
					if(type==1) list = document.getElementById('textmesscon');
					else if(type==2)  list = document.getElementById('textmesscit');
						if(result.str){
							list.innerHTML = '';
							list.innerHTML = result.str;
						}
						else list.innerHTML = '';
				},
				true
	);
}

function PhonesList(id){

	JsHttpRequest.query( '../catalog/PhoneList.ajax.php',
			     { "id"  : id},
				function (result, errors) {
					list = document.getElementById('PhoneList');					
						if(result.listphone){
							list.innerHTML = '';
							list.innerHTML = result.listphone;
							document.getElementById('ListInfo').innerHTML = '';
						}
						else{
							list.innerHTML = '';
							document.getElementById('ListInfo').innerHTML = '';
						}
				},
				true
	);
}

function DeletePhone(id, idc){
	JsHttpRequest.query( '../catalog/delete_phone.ajax.php',
			     { "id"  : id,
			       "idc" : idc},
				function (result, errors) {					
					list = document.getElementById('ListInfo');					
						if(result.listinfo){
							list.innerHTML = '';
							list.innerHTML = result.listinfo;
							PhonesList(idc);
						}
				},
				true
	);
}

function SavePhones(type, code_c, code_g, tel, exttel, id){
	JsHttpRequest.query('../catalog/SavePhone.ajax.php', 
			     {    "type" : type,
			        "code_c" : code_c,
			        "code_g" : code_g,
			           "tel" : tel,
			        "exttel" : exttel,
			     	    "id" : id},
				function (result, errors) {
					var list = document.getElementById('ResultPhone');
						if(result.str){
							list.innerHTML = '';
							list.innerHTML = result.str;
							PhonesList(id);
						}
						else list.innerHTML = '';
				},
				true
	);
}

function hasClass(elem,ClassName) {
	if(elem && elem.parentNode && elem.parentNode.className != undefined)
		return elem.parentNode.className.match('(^| )'+ClassName+'($| )');	
		
	return false;
}

function hasIDName(elem,IdName) {
	return elem.parentNode.id.match('(^| )'+IdName+'($| )');	
}

function genrelist() {
	if (document.getElementById('genre-list-tooltip').style.display == '' || document.getElementById('genre-list-tooltip').style.display == 'none') {
		var bounds = getBounds(document.getElementById("genre-list-link"));
		window.status = 'LEFT: ' + bounds.left + ' TOP: ' + bounds.top;

		document.getElementById('genre-list-tooltip').style.left = bounds.left + bounds.width - 500 + "px";
		document.getElementById('genre-list-tooltip').style.top = bounds.top + 20 + "px";
		document.getElementById('genre-list-tooltip').style.display = "block";

	}
	else
		document.getElementById('genre-list-tooltip').style.display = "none";
}

function toggle(id) {
	if (document.getElementById(id).style.display == '' || document.getElementById(id).style.display == 'none') {
		var bounds1 = getBounds(document.getElementById("product-info"));
		var bounds2 = getBounds(document.getElementById("view-contents"));
		document.getElementById(id).style.display = "block";
		var arrow = getBounds(document.getElementById("contents-arrow"));
		var bounds = getBounds(document.getElementById("contents-tooltip"));
		document.getElementById("contents-arrow").style.top = bounds.height/2 - arrow.height/2 + 2 + "px";
		document.getElementById("contents-tooltip").style.top = bounds2.top + bounds2.height/2 - bounds1.top - bounds.height/2 + "px";
		//if (document.getElementById("contents-tooltip").style.display == "block") document.getElementById("contents-tooltip").style.display = "none";
	}	
	else
		document.getElementById(id).style.display = "none";
}

tooltip = {
	tip: document.createElement("div"),
	top: document.createElement("div"),
	middle: document.createElement("div"),
	bottom: document.createElement("div"),
	arrow: document.createElement("div"),
	c: null,
	visible: false,
	
	move: function(e) {
		if (tooltip.visible) {
			tooltip.draw(curX, curY);
		}
	},
	
	create: function() {
		//tip create
		tooltip.tip.setAttribute("id", "tooltip");
		document.body.appendChild(tooltip.tip);
		
		//top create
		tooltip.top.setAttribute("id", "tooltip-top");
		tooltip.tip.appendChild(tooltip.top);
		
		//tip create
		tooltip.middle.setAttribute("id", "tooltip-middle");
		tooltip.tip.appendChild(tooltip.middle);
		
		//tip create
		tooltip.bottom.setAttribute("id", "tooltip-bottom");
		tooltip.tip.appendChild(tooltip.bottom);
		
		//tip create
		tooltip.arrow.setAttribute("id", "tooltip-arrow");
		tooltip.tip.appendChild(tooltip.arrow);

		for (var i = 0; i < aLength; i++) {
			if (allElements[i].getAttribute("title")) {
				allElements[i].setAttribute(tooltip_attr_name, allElements[i].getAttribute("title"));
				if (allElements[i].getAttribute(tooltip_attr_name)) {
					allElements[i].removeAttribute("title");
					tooltip.addListener(allElements[i], "mouseover", tooltip.show);
					tooltip.addListener(allElements[i], "mouseout", tooltip.hide);
				}
			}
		}
		
		//document.onmousemove = tooltip.move;
		tooltip.tip.style.display = "none";
		tooltip.visible = false;

		window.onscroll = tooltip.hide;
		window.onresize = tooltip.hide;
	},
	
	show: function(e) {
		d = window.event ? window.event.srcElement : e.currentTarget;
		currentElement = d;
		if (!d.getAttribute(tooltip_attr_name))
			return;
		if (tooltip.middle.firstChild)
			tooltip.middle.removeChild(tooltip.middle.firstChild);
		tooltip.middle.appendChild(document.createTextNode(d.getAttribute(tooltip_attr_name)));
		
		position = getBounds(d);

		curX = position.left - 290;
		curY = position.top + position.height - 13 - 8;
		tooltip.visible = true;
		tooltip.move();		
		tooltip.c = setTimeout("tooltip.tip.style.display = 'block';", 0);
	},
	
	hide: function(e) {
		tooltip.tip.style.display = "none";
		clearTimeout(tooltip.c);
		tooltip.visible = false;
		tooltip.draw(-200, -200);
	},
	
	addListener: function(o, e, a) {
		if (o.addEventListener)
			o.addEventListener(e, a, false);
		else if (o.attachEvent)
			o.attachEvent("on" + e, a);
		else
			return null;
	},
	
	draw: function(x, y) {
		tooltip.tip.style.left = x + "px";
		tooltip.tip.style.top = y + "px";
	}
}

alt = {
	tip: document.createElement("div"),
	top: document.createElement("div"),
	middle: document.createElement("div"),
	bottom: document.createElement("div"),
	arrow: document.createElement("div"),
	c: null,
	visible: false,
	
	move: function(e) {
		if (alt.visible) {
			alt.draw(curX, curY);
		}
	},
	
	create: function() {

		//tip create
		alt.tip.setAttribute("id", "tooltip-right");
		document.body.appendChild(alt.tip);

		
		//top create
		alt.top.setAttribute("id", "tooltip-right-top");
		alt.tip.appendChild(alt.top);
		
		//tip create
		alt.middle.setAttribute("id", "tooltip-right-middle");
		alt.tip.appendChild(alt.middle);
		
		//tip create
		alt.bottom.setAttribute("id", "tooltip-right-bottom");
		alt.tip.appendChild(alt.bottom);
		
		//tip create
		alt.arrow.setAttribute("id", "tooltip-right-arrow");
		alt.tip.appendChild(alt.arrow);

		for (var i = 0; i < aLength; i++) {
			if (allElements[i].getAttribute("title")) {
				allElements[i].setAttribute(tooltip_attr_name_right, allElements[i].getAttribute("title"));
				if (allElements[i].getAttribute(tooltip_attr_name_right)) {
					allElements[i].removeAttribute("title");
					alt.addListener(allElements[i], "mouseover", alt.show);
					alt.addListener(allElements[i], "mouseout", alt.hide);
				}
			}
		}
		
		//document.onmousemove = alt.move;
		alt.tip.style.display = "none";
		alt.visible = false;

		window.onscroll = alt.hide;
		window.onresize = alt.hide;
	},
	
	show: function(e) {
		d = window.event ? window.event.srcElement : e.currentTarget;
		currentElement = d;
		if (!d.getAttribute(tooltip_attr_name_right))
			return;
		if (alt.middle.firstChild)
			alt.middle.removeChild(alt.middle.firstChild);
		alt.middle.appendChild(document.createTextNode(d.getAttribute(tooltip_attr_name_right)));
		
		position = getBounds(d);

		curX = position.left + position.width + 10;
		curY = position.top + position.height - 13 - 8;
		alt.visible = true;
		alt.c = setTimeout("alt.tip.style.display = 'block';", 0);
		setTimeout("alt.move()",0);
	},
	
	hide: function(e) {
		alt.tip.style.display = "none";
		clearTimeout(alt.c);
		alt.visible = false;
		alt.draw(-200, -200);
	},
	
	addListener: function(o, e, a) {
		if (o.addEventListener)
			o.addEventListener(e, a, false);
		else if (o.attachEvent)
			o.attachEvent("on" + e, a);
		else
			return null;
	},
	
	draw: function(x, y) {
		alt.tip.style.left = x + "px";
		alt.tip.style.top = y + "px";
	}
}

function getBounds(element) {
  var left = element.offsetLeft;
  var top = element.offsetTop;
  for (var parent = element.offsetParent; parent; parent = parent.offsetParent) {
    left += parent.offsetLeft;
    top += parent.offsetTop;
  }
  return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function setCookie(name, value) {
	cswitch = getCookie("cswitch");
	if (!cswitch) {
		var today = new Date();d = new Date(today.getTime() + 7790000000); 
		var newCookie1 = name + "=" + escape(value) + ";expires=" + d.toGMTString() + ";path=/ ;";
		document.cookie = newCookie1;
	}
}

function delshoping(id_tov, check) { 
	if (check=="") {check=0;}
	
	if (check==1) {
		var urlnavi="/buy.php?check=1&id=" + id_tov
		window.location=urlnavi;
	}
	else if (check==2) {
		var urlnavi="/buy.php?check=1&step=1&id=" + id_tov
		window.location=urlnavi;
	}
	else {
		var urlnavi="/buy.php?id=" + id_tov
		window.open(urlnavi,'',"toolbar=0,location=0,menubar=0,scrollbars=0,resizable=0,width=10,height=10,top=20000,left=20000");	
	}
}

function delshoping2(id_tov, check) { 
	if (check=="") {check=0;}
	
	if (check==1) {
		var urlnavi="/buy.php?check=1&ido=" + id_tov
		window.location=urlnavi;
	}
	else {
		var urlnavi="/buy.php?ido=" + id_tov
		window.open(urlnavi,'',"toolbar=0,location=0,menubar=0,scrollbars=0,resizable=0,width=10,height=10,top=20000,left=20000");
	}
}

function screenshot(idbook, scrn) { 
	var urlnavi="/screenshot/" + idbook + "/" + scrn + "/";
	window.open(urlnavi, "Screenshot","width=872,height=612,scrollbars=yes,resizable=yes"); 
}
function autotarif() { 
	var urlnavi="/catalog/popuptarif.php";
	window.open(urlnavi, "calculator", "width=643,height=762,top=100, left=200, menubar=no,status=no,location=no,toolbar=no,scrollbars=yes,resizable=yes"); 
}

function fragment(idbook) { 
	var urlnavi="/fragment/" + idbook + "/";
	this.window.open(urlnavi, "Fragment","width=872,height=612,scrollbars=yes,resizable=yes"); 
}

function commentpic(idcomment, pic) { 
	var urlnavi="/commentpic/" + idcomment + "/" + pic + "/";
	window.open(urlnavi, "Screenshot","width=872,height=612,scrollbars=yes,resizable=yes"); 
}

function advisepic(idadvise, type, pic) { 
	var urlnavi="/advisepic/" + type + "/" + idadvise + "/" + pic + "/";
	window.open(urlnavi, "Screenshot","width=872,height=612,scrollbars=yes,resizable=yes"); 
}

function alreadyInBasket(idbook, retail) {
	var buy="buy"+idbook;
	for (i=0;i<document.getElementsByTagName('span').length; i++) {
		var qq=document.getElementsByTagName('span')[i];
			
		if (qq.id==buy) {qq.innerHTML=((retail ==1 )  ? '<a href=\"/retail/basket/\">В корзине</a>' : '<a href=\"/basket/\">В корзине</a>');}
	}
}

function alreadyInBasketOffer(idbook) {
	var buy="buyof"+idbook;
	for (i=0;i<document.getElementsByTagName('span').length; i++) {
		var qq=document.getElementsByTagName('span')[i];
			
		if (qq.id==buy) {qq.innerHTML='<a href=\"/basket/\">В корзине</a>';}
	}
}

function alreadyInBasketCharity(idbook) {
	var buy="buychar"+idbook;
	for (i=0;i<document.getElementsByTagName('span').length; i++) {
		var qq=document.getElementsByTagName('span')[i];
			
		if (qq.id==buy) {qq.innerHTML='<a href=\"/basket/\">В корзине</a>';}
	}
}

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=' shoping("+id_books+","+isbasket+"); hidepicture();'><img src='/images/design/kupit.gif'></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='http://img.labirint.ru/images/books"+books_num+"/"+id_books+"/small.jpg'></a></div>"+str_buy_batton+"<div id='loading-pic' onmouseout='hidepicture()' onmousemove='show=1'>Загружается...<img src='/images/design/upload.gif'></div><div id='noshow' onmouseout='hidepicture()' onmousemove='show=1'><input type='checkbox' onChange='dontshowbig(this.checked)' 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 = "http://img.labirint.ru/images/books"+books_num+"/"+id_books+"/big.jpg";
		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+"<BR>");
	}
}
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() + "<br />");
	$(label.parentNode).empty().css("display", "none");
}

function copyToBuffer(id) {
	var content = document.getElementById(id).value;

	if (window.clipboardData && window.clipboardData.setData) {
		window.clipboardData.setData("Text", content);
		return true;
	}
	else {
		try { 
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
		} 
		catch (e) { 
			alert('Ваш браузер не работает с буфером обмена. Придется копировать текст вручную.');
			return false;
		}
		
		var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(); 
		if (clipboard) { 
			clipboard = clipboard.QueryInterface(Components.interfaces.nsIClipboard); 
		} 
		
		var transferable = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(); 
		if (transferable) { 
			transferable = transferable.QueryInterface(Components.interfaces.nsITransferable); 
		}
		
		if (clipboard && transferable) { 
			// transferable.addDataFlavor("text/unicode"); 
			var textObj = new Object(); 
			var textObj = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); 
			if (textObj) { 
				textObj.data = content; 
				transferable.setTransferData("text/unicode", textObj, content.length*2); 
				var clipid=Components.interfaces.nsIClipboard; 
				clipboard.setData(transferable,null,clipid.kGlobalClipboard); 
				
				return true;
			} 
		}
		
		return false;
	}
}

function setBackColor(to, color) {
	$("div." + to).each(
		function () {
			$(this).css("background-color", color);
		}
	);
	$("div.preview_cont").css("background-color", color);
}

function setInnerColor(to, color) {
	$("." + to).each(
		function () {
			$(this).css("color", color);
			$("a", this).each( function () { $(this).css("color", color); } );
		}
	);
}

function showColor(name) {
	$("div.color_picker").each( function(){ $(this).css("display", "none"); } );
	$("#" + name).css("display", "block");
}

function hideColor(close_but) {
	$(close_but.parentNode).css("display", "none");
}

var gallery_timer = 0;

function slideRight() {
	var cards = document.getElementById("gallery_preview").childNodes;
	var k = 0;
	for (var i = 0; i < cards.length; i++) {
		if (cards[i].tagName != undefined && cards[i].style != undefined && cards[i].className == "card_container") {
			if (cards[i].style.display == "block") {
				var old_card = cards[i];
				var next = 1;
				k++;
			}
			else {
				if (next) {
					var new_card = cards[i];
					next = 0;
				}
				if (!first_div) { var first_div = i; }
			}
		}
	}
	if (new_card == undefined) {
		var new_card = cards[first_div];
	}
	
	if (k < 2) {
		old_card.style.marginLeft = 0;
		new_card.style.marginLeft = old_card.offsetWidth + 30 + "px";
		new_card.style.display = "block";
		document.getElementById("gallery_preview").style.height = new_card.offsetHeight + "px";
		//document.getElementById("gallery_preview").style.width = new_card.offsetWidth + "px";
		moveRight(old_card, new_card, old_card.offsetWidth + 30);
	}
	
	stopSlide();
}

function moveRight(old_card, new_card, w) {
	var margin = parseInt(new_card.style.marginLeft);
	var margin1 = parseInt(old_card.style.marginLeft);
	var op = 1 - (margin / w);
	if (margin > 0) {
		margin -= 20;
		if (margin < 0) { margin = 0; }
		new_card.style.marginLeft = margin + "px";
		old_card.style.marginLeft = margin1 - 20 + "px";
		new_card.style.opacity = op;
		old_card.style.opacity = 1 - op;

		setTimeout(function() { moveRight(old_card, new_card, w); }, 20);
	}
	else {
		old_card.style.opacity = 1;
		old_card.style.display = "none";
	}
}

function slideLeft() {
	var cards = document.getElementById("gallery_preview").childNodes;
	var k = 0;
	for (var i = cards.length - 1; i >= 0; i--) {
		if (cards[i].tagName != undefined && cards[i].style != undefined && cards[i].className == "card_container") {
			if (cards[i].style.display == "block") {
				var old_card = cards[i];
				var next = 1;
				k++;
			}
			else {
				if (next) {
					var new_card = cards[i];
					next = 0;
				}
				if (!first_div) { var first_div = i; }
			}
		}
	}
	if (new_card == undefined) {
		var new_card = cards[first_div];
	}
	
	if (k < 2) {
		old_card.style.marginLeft = 0;
		new_card.style.marginLeft =  -(old_card.offsetWidth + 30) + "px";
		new_card.style.display = "block";
		document.getElementById("gallery_preview").style.height = new_card.offsetHeight + "px";
		//document.getElementById("gallery_preview").style.width = new_card.offsetWidth + "px";
		moveLeft(old_card, new_card, old_card.offsetWidth + 30);
	}
	
	stopSlide();
}

function moveLeft(old_card, new_card, w) {
	var margin = parseInt(new_card.style.marginLeft);
	var margin1 = parseInt(old_card.style.marginLeft);
	var op = 1 - (-margin / w);
	if (margin < 0) {
		margin += 20;
		if (margin > 0) { margin = 0; }
		new_card.style.marginLeft = margin + "px";
		old_card.style.marginLeft = margin1 + 20 + "px";
		new_card.style.opacity = op;
		old_card.style.opacity = 1 - op;
		
		setTimeout(function() { moveLeft(old_card, new_card, w); }, 20);
	}
	else {
		old_card.style.opacity = 1;
		old_card.style.display = "none";
	}
}

function startSlide() {
	slideRight();
	gallery_timer = setTimeout("startSlide()", mspeed*200);
}

function stopSlide() {
	clearTimeout(gallery_timer);
}

function changeDesignType(radio) {
	if (radio.checked) {
		$("div.preview_cont").attr("id", radio.id + "_preview");
	}
	
	jQuery.trackbar.getObject('width_val').onMove();
	jQuery.trackbar.getObject('lmargin_val').onMove();
	jQuery.trackbar.getObject('rmargin_val').onMove();
	
	var first_div = $("div.preview_cont > div.card_container:first");
	if (radio.id == "gallery") 
	{
		document.getElementById('div_no_arrows').style.display="block";
		document.getElementById('move_speed').style.display="block";
		document.getElementById('msp').style.display="block";
		$('#gallery_preview > div.card_container').each( function() { $(this).css("display", "none"); } );
		$('#gallery_preview > div.card_container:first').css("display", "block");
		startSlide();
	}
	else 
	{
		document.getElementById('div_no_arrows').style.display="none";
		document.getElementById('move_speed').style.display="none";
		document.getElementById('msp').style.display="none";
		stopSlide();
		$("#" + radio.id + "_preview")
			.css("width", "auto")
			.css("height", "auto");
		var lwidth = 0;
		$("#" + radio.id + "_preview > div.card_container").each(
			function() { 
				$(this)
					.css("display", "block")
					.css("opacity", "1")
					.css("margin", 0)
					.css("position","relative");
				lwidth += $(this).width();
			}
		);
		$("#" + radio.id + "_preview > div.cleaner").each(
			function() { $(this).remove(); }
		);
		
		if (radio.id == "vertical") 
		{ 
			//fitOnCard(); 
			var widthc = $("[@name='width_val']").attr("value");
			$("#" + radio.id + "_preview > div.card_container").each(
				function() { 
					$(this).css("width", widthc + "px");
					$('<div class="cleaner"></div>').insertAfter($(this));
				}
			);
		}
		else if (radio.id == "horizontal") 
		{
			/*$("#" + radio.id + "_preview").css("width", (first_div.width() + parseInt(first_div.css("padding-left")) * 2) * parseInt($("#cards_in_row").attr("value")));*/
			$("#" + radio.id + "_preview").css("width","auto");	
		}
		else if (radio.id == "table") 
		{
			var x = 1;
			$("div.cards_in_row").css("display", "block");
			$("#" + radio.id + "_preview").css("width", (first_div.width() + parseInt(first_div.css("padding-left")) * 2) * parseInt($("#cards_in_row").attr("value")));
			$("#" + radio.id + "_preview > div.card_container").each(
				function() { 
					if (!(x++ % parseInt($("#cards_in_row").attr("value")))) { $('<div class="cleaner"></div>').insertAfter($(this)); } 
				}
			);
		}
	}

	$("#tr_cards_in_row").css("display", radio.id == "table" ? "table-row" : "none");

	setCopyWidth();
}

function fitOnCard () {
	var first_div = $("div.preview_cont > div.card_container:first");
	$("div.preview_cont").css("width", parseInt(first_div.css("width")) + parseInt(first_div.css("margin-right")) * 2);
}

function setBlank (set) {
	$("div.preview_cont a").each(
		function() { $(this).attr("target", set ? "_blank" : ""); }
	);
}

function withHeight(set)
{
	if(set==1)
	{
		$("#height_span").css("display","none");
		$("#height_val").css("display","none");
		var divs = $("div.card_container");
		divs.each( 
			function() { 
				$(this).css("height","auto");
				$("div.card_body", this).css("height","auto"); 
				} 
		);
		$("#vertical_preview").css("height", "auto"); 
	}
	else
	{
		$("#height_span").css("display","inline");
		$("#height_val").css("display","block");
		var tmargin = $("input[@name=tmargin_val]").attr("value");
		var bmargin = $("input[@name=bmargin_val]").attr("value");
		var height_val = $("input[@name=height_val]").attr("value");
		var divs = $("div.card_container");
		divs.each( 
			function() { 
				$(this).css("height",height_val+"px");
				$("div.card_body", this).css("height", (height_val-tmargin-bmargin) + "px"); 
			} 
		);
		$("#vertical_preview").css("height", "auto"); 
		$("#gallery_preview").css("height",height_val + "px"); 
	}
}

function setWithP (p, set) {
	$("div.preview_cont a").each(
		function() 
		{ 
			//alert('');
			var id=$(this).attr("id");
			var link = $(this).attr("href");
			if ((id[0]+id[1])=="ab") 
			{
				if (set)
				{
					if (document.getElementById('books').checked==1)  link=link+'?p='+p;
					else link=link+'&p='+p;
				}
				else 
				{
					if (document.getElementById('books').checked==1) link = link.replace('/?p='+p, '/');
					else link = link.replace('&p='+p, '');
				}
			}
			else 
			{
				if (set)
				{
					link=link+'?p='+p;
				}
				else 
				{
					link = link.replace('?p='+p, '');
				}
			}
			$(this).attr("href",link); 
		}
	);
}

function changeBuyTo(radio) 
{
	$("div.preview_cont a").each(
		function() 
		{ 
			var id=$(this).attr("id");
			if ((id[0]+id[1])=="ab") 
			{
				var link = $(this).attr("href");
				if (document.getElementById('with_p').checked==0)
				{
					
					if (radio.id == "basket") 
					{
						link = link.substring(0,link.length-1);
						$(this).attr("href",link.replace("/books/","/basket/?w=")); 	
					}
					else $(this).attr("href",link.replace("/basket/?w=","/books/")+'/'); 	
				}
				else 
				{
					if (radio.id == "basket") 
					{
						link = link.replace("/?p","&p");
						$(this).attr("href",link.replace("/books/","/basket/?w=")); 	
					}
					else 
					{
						link = link.replace("&p","/?p");
						$(this).attr("href",link.replace("/basket/?w=","/books/")); 						
					}
				}
			}
		}
	);	
}	

function displayArrows(set) 
{
	var cardwidth = $("input[@name=width_val]").attr("value");
	var rmargin = $("input[@name=rmargin_val]").attr("value");
	var lmargin = $("input[@name=lmargin_val]").attr("value");
	var display_type="none";
	if (set==0)
	{
		lmargin = (lmargin*1) + 38;
		rmargin = (rmargin*1) + 38;
		display_type="block";
	}
	$("#left_arrow").css("display", display_type);
	$("#right_arrow").css("display", display_type);	
	$("div.card_container").each( 
		function() { 
			//$(this).css("width",cardwidth+"px");
			$("div.card_body", this).css("margin-left", lmargin+"px")
				.css("margin-right", rmargin+"px")
				.css("width", (cardwidth - rmargin - lmargin)+"px");
		} 
	);
}	

function displayCards() {
	var cards_num = parseInt($("#view_count").attr("value"));
	if (cards_num) {
		if ($('#gallery_preview').length) { stopSlide(); }
		
		if (cards_num < 1) {
			cards_num = 1;
			if ($('#gallery_preview').length) { cards_num = 2; }
		}
		else if (cards_num > 10) {
			cards_num = 10;
		}
		var i = 0;
		$("div.preview_cont div[@class^=card_container]").each(
			function () {
				i++;
				if (i <= cards_num) {
					$(this).attr("class", "card_container");
				}
				else {
					$(this)
						.attr("class", "card_container_hidden")
						.css("display", "none");
				}
			}
		);
		$("input[@name=design_type]:checked").click();
		
		if ($('#gallery_preview').length) { startSlide(); }
	}
}

function displayCopyRight(set)
{
	//if (set) $('#copylink').css("display", "none"); else $('#copylink').css("display", "block");
	var copyplace = parseInt($("select[@name=copy_place]").attr("value"));
	changeCopyPlace(copyplace);
}

function getCopyLink()
{
	var font, color,txtval, txt;
	font = $("input[@name=copy_size]").attr("value");
	color = $("#copy_color").attr("value");
	txt = document.getElementById("copytxt").options[parseInt($("#copytxt").attr("value"))-1].innerHTML;
	var str = "<a href='http://www.labirint.ru' target='_blank' id='copylink' class='clc' style='text-decoration:none;font-size:"+font+"px;color:"+color+"'>"+txt+"</a>";
	
	return str;
}


function setCopyWidth(val)
{
	var copywidth, copyplace;
	copywidth = "100%";
	if ($("#gallery").attr("checked") || $("#table").attr("checked")) 
	{
		if ($("#table").attr("checked")) $rows = parseInt($("input[@name=cards_in_row]").attr("value")); else $rows = 1;
		copywidth = parseInt($("input[@name=width_val]").attr("value")) * 1 * $rows;
		copywidth +="px";
	}
	copyplace = ( val ? val : parseInt($("select[@name=copy_place]").attr("value")));
	if (copyplace<2) copyplace=""; else copyplace="2";
	$('#copydiv'+copyplace).css("width", copywidth);
	
}

function changeCopyPlace(val)
{
	var copyplace, talign;
	var link = getCopyLink();
	var width = "100%";

	if (val>=2) copyplace=""; else copyplace="2";
	if (val==1 || val==3) talign="left"; else  talign="right";

	$('#copydiv').attr("innerHTML", "");
	$('#copydiv2').attr("innerHTML", "");
	$('#copydiv'+copyplace).attr("innerHTML", link);
	$('#copydiv'+copyplace).css("text-align", talign);
	setCopyWidth(val);
	if($('#no_copy').attr("checked")) $('#copylink').css("display", "none"); else $('#copylink').css("display", "block");
}

function del_carousel() {
	return confirm('Вы точно хотите удалить карусель?');
}

function clickElem(id) {
	$("#" + id).click();
}

function displayAnnotations(checked) {
	$("div.preview_cont span.card_annot").each(
		function() { $(this).css("display", checked ? "inline" : "none"); }
	);
}

function displayPubhouse(checked)
{
	$("div.preview_cont div.hdiv3").each(
		function() { $(this).css("display", checked ? "block" : "none"); }
	);	
}
function displayAuthor(checked)
{
	$("div.preview_cont div.hdiva").each(
		function() { $(this).css("display", checked ? "block" : "none"); }
	);
}
function displaySeries(checked)
{
	$("div.preview_cont div.hdiv4").each(
		function() { $(this).css("display", checked ? "block" : "none"); }
	);
}
function displayRang(checked)
{
	$("div.preview_cont div.hdiv5").each(
		function() { $(this).css("display", checked ? "block" : "none"); }
	);
}
function displayPrice(checked)
{
	$("div.preview_cont div.card_price").each(
		function() { $(this).css("display", checked ? "block" : "none"); }
	);
	if (checked) $("#where_price_selector").css("display","block");	else $("#where_price_selector").css("display","none");
}

function changePriceLocation(radio)
{
	if (radio.id=='pright')
	{
		/*$("div.preview_cont div.card_price").each(
			function() { 
				$(this).css("margin-left", "90px")
				.css("margin-top", "-30px")
				.css("padding-left", "8px"); 
			}
		);*/
		$("div.card_container").each( 
			function() { 
				$("#hdiv71", this).css("display","block");
				/*$("#hdiv2", this).attr("_float:","left")
								.attr("_margin:","0 0 0 8px");*/
				/*$("div.dimg", this).css("position","relative")
									.css("margin-bottom:","5px");		*/		
			}
		);		
		$("div.card_container").each( 
			function() { 
				$("#hdiv72", this).css("display","none");
				/*$("div.dimg", this).css("position","absolute")
									.css("margin-bottom:","0px");		*/
				/*$("#hdiv2", this).attr("_float:",null)
								.attr("_margin:",null);		*/								
			}
		);			
	}
	else
	{
		/*$("div.preview_cont div.card_price").each(
			function() { 
				$(this).css("margin-left", "0px")
				.css("margin-top", "0px")
				.css("padding-left", "0px"); 
			}
		);*/
		$("div.card_container").each( 
			function() { 
				$("#hdiv71", this).css("display","none");
			}
		);		
		$("div.card_container").each( 
			function() { 
				$("#hdiv72", this).css("display","block");
			}
		);			
	}
}

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 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-500;
	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();

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)
{
	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)
{
	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)
{
	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(cm)
{
	var dhaf = document.getElementById('dhcena'+cm);
	var dhbf = document.getElementById('dhmassa'+cm);
	var dha = document.getElementById('dhcena'+cm).value;
	var dhb = document.getElementById('dhmassa'+cm).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.keyCode;
	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') {
    	
		   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 {
					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';
    }
	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 increaseBooksCount(id_book)
{
	var count = parseInt($("#q" + id_book).val()) + 1;
	$("#q" + id_book).val(count);
	
	sumPM(id_book, 0, 0, count);
}

function lessenBooksCount(id_book)
{
	var count = parseInt($("#q" + id_book).val());
	
	if(count > 0)
	{
		$("#q" + id_book).val(count - 1);
		sumPM(id_book, 0, 0, count - 1);
	}
}

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);
}	