function GetAbsolutePos(obj)
{
	var x = 0;
	var y = 0;
	var p = obj;
	while (p)
	{
		x += p.offsetLeft;
		y += p.offsetTop;
		p = p.offsetParent;
	}
	return {"x":x,"y":y};
}

var mouseX;
var mouseY;

var menu = null;

function ShowMenu(blockName, btn)
{
	if (menu)
	{
		menu.style.display = 'none';
		menu = null;
	}

	var block = document.getElementById(blockName);
	if (block && btn)
	{
		var p = GetAbsolutePos(btn);
		block.style.display = '';
		block.style.left = p.x;
		block.style.top = p.y + btn.offsetHeight;
		menu = block;
	}
}

function CheckMenu()
{
	if (!menu) return;

	var p = GetAbsolutePos(menu);
	var d = 50;
	if ((mouseX < p.x - d) || (mouseX > p.x + menu.offsetWidth + d) || (mouseY < p.y - d) || (mouseY > p.y + menu.offsetHeight + d))
	{
		menu.style.display = 'none';
		menu = null;
	}
}

function selectProductType(search_form_name, product_type)
{
	var search_form;

	for(var i=0; i<5; i++)
	{
		if (i == product_type)
			ShowBlock(search_form_name+"search_form_"+i);
		else
			HideBlock(search_form_name+"search_form_"+i);
	}

	var search_product_type = document.getElementById(search_form_name+"search_product_type_"+product_type);
	if (search_product_type)
	{
		search_product_type.selectedIndex = product_type;
	}
}

function searchReset(search_form_name, product_type)
{
	var search_param;

	search_param = document.getElementById(search_form_name+"search_category_"+product_type);
	if (search_param != null)
	{
		search_param.value = "";
	}

	search_param = document.getElementById(search_form_name+"search_text_"+product_type);
	if (search_param != null)
	{
		search_param.value = "";
	}

	search_param = document.getElementById(search_form_name+"search_stock_"+product_type);
	if (search_param != null)
	{
		search_param.checked = "";
	}

	search_param = document.getElementById(search_form_name+"search_min_price_"+product_type);
	if (search_param != null)
	{
		search_param.value = "";
	}

	search_param = document.getElementById(search_form_name+"search_max_price_"+product_type);
	if (search_param != null)
	{
		search_param.value = "";
	}

	for(var i=0; i<50; i++)
	{
		search_param = document.getElementById(search_form_name+"search_param_"+product_type+"_var0_"+i+"_1");
		if (search_param != null)
		{
			search_param.value = "";
		}
		search_param = document.getElementById(search_form_name+"search_param_"+product_type+"_var0_"+i+"_2");
		if (search_param != null)
		{
			search_param.value = "";
		}		
		search_param = document.getElementById(search_form_name+"search_param_"+product_type+"_var1_"+i+"_1");
		if (search_param != null)
		{
			search_param.value = "";
		}
		search_param = document.getElementById(search_form_name+"search_param_"+product_type+"_var1_"+i+"_2");
		if (search_param != null)
		{
			search_param.value = "";
		}		
	}
}

var PreloadedImages = new Array();

function PreloadImages()
{
	for(i=0; i<PreloadImages.arguments.length; i++)
	{
		k = PreloadedImages.length-1;
		PreloadedImages[k] = new Image();
		PreloadedImages[k].src = PreloadImages.arguments[i];
	}	
}

function NoAccess()
{
	alert('Извините, у Вас нет доступа к этому разделу');
}

function PlayMP3(mp3_num, name, default_name)
{
	var player = document.getElementById("mp3_player");
	var status = document.getElementById("mp3_status");
	if (mp3_num)
	{
		if (status)
		{
			status.innerHTML = 'Воспроизведение: '+name+' <a href="#" onclick="PlayMP3(0,\'\',\''+default_name+'\'); return false;"><b>СТОП</b></a>';
		}
		player.innerHTML = '<embed allowScriptAccess="never" src="/userfiles/mp3player/mp3player.swf" align="middle" menu="false" quality="high" bgcolor="Transparent" width="2" height="2" name="index" allowScriptAccess="never" type="application/x-shockwave-flash"  wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="playlist=/xml/'+mp3_num+'" />';
	}
	else
	{
		if (status)
		{
			status.innerHTML = default_name;
		}
		player.innerHTML = '&nbsp;';
	}
}

function dynamicSelect(id1, id2)
{
	if (document.getElementById && document.getElementsByTagName) {
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		var clone = sel2.cloneNode(true);
		var clonedOptions = clone.getElementsByTagName("option");
		refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		sel1.onchange = function() {
			refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		}
	}
}

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions)
{
	while (sel2.options.length)
	{
		sel2.remove(0);
	}
	var pattern1 = /( |^)(select)( |$)/;
	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");

	for (var i = 0; i < clonedOptions.length; i++)
	{
		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2))
		{
			sel2.appendChild(clonedOptions[i].cloneNode(true));
		}
	}
}


function ShowIfChecked(cb, what)
{
	qq = document.getElementById(what);
	if (cb.checked)
	{
		qq.style.opacity = '1';
		qq.style.filter = 'alpha(opacity: 100)';
	}
	else
	{
		qq.style.opacity = '0.3';
		qq.style.filter = 'alpha(opacity: 30)';
	}
}

function setOpacity(element, opacity)
{
	var e = document.getElementById(element);
	if (e)
	{
		if (opacity <= 0)
		{
			e.style.display = 'none';
		}
		else
		{
			e.style.opacity = opacity/100;
			e.style.filter = 'alpha(opacity: '+opacity+')';
			e.style.display = '';
		}
	}
}

function photo_change(input_name, info_name, cb_name)
{
	var fileInput = document.getElementById(input_name);
	var info = document.getElementById(info_name);
	var cb = document.getElementById(cb_name);
	var i = fileInput.value.lastIndexOf('\\');
	if (i < 0) { i = fileInput.value.lastIndexOf('/'); }
	var n;
	if (i >= 0) { n = fileInput.value.substring(i+1); } else { n = fileInput.value; }
	info.innerHTML = 'Выбран файл<br><b>'+n+'</b>';
	cb.checked = true;
	cb.onchange();
}

function showTime()
{
	var block = document.getElementById("time");
	vr=new Date();
	block.innerText=vr.getHours()+":"+vr.getMinutes()+":"+vr.getSeconds();
	setTimeout("showTime()",1000)
}

function HideBlock(blockName)
{
	var block = document.getElementById(blockName);
	if (block)
	{
		block.style.display = 'none';	
	}
}

function ShowBlock(blockName)
{
	var block = document.getElementById(blockName);
	if (block)
	{
		block.style.display = '';
	}
}

function ShowHideBlock(blockName, plus, selectName)
{
	var block = document.getElementById(blockName);
	if (block.style.display == 'none')
	{
		block.style.display = '';
		if (plus)
		{
			block = document.getElementById(blockName+"_expand");
			if (block)
			{
				block.innerHTML = "-";
			}
		}
		if (selectName != "")
		{
			block = document.getElementById(selectName);
			if (block)
			{
				block.focus();
				block.select();
			}
		}
	}
	else
	{
		block.style.display = 'none';
		if (plus)
		{
			block = document.getElementById(blockName+"_expand");
			if (block)
			{
				block.innerHTML = "+";
			}
		}
	}
}

function ShowHideBlock3(blockName, plus, selectName)
{
	var block = document.getElementById(blockName);
	if (block.style.display == 'none')
	{
		block.style.display = '';
		if (plus)
		{
			block = document.getElementById(blockName+"_expand1");
			if (block)
			{
				block.style.display = '';
			}
			block = document.getElementById(blockName+"_expand2");
			if (block)
			{
				block.style.display = 'none';
			}
		}
		if (selectName != "")
		{
			block = document.getElementById(selectName);
			if (block)
			{
				block.focus();
				block.select();
			}
		}
	}
	else
	{
		block.style.display = 'none';
		if (plus)
		{
			block = document.getElementById(blockName+"_expand1");
			if (block)
			{
				block.style.display = 'none';
			}
			block = document.getElementById(blockName+"_expand2");
			if (block)
			{
				block.style.display = '';
			}
		}
	}
}

var traversal = typeof document
                           .createElement('div')
                               .childElementCount != 'undefined';

var next = traversal ? function(node) {
    return node.nextElementSibling;
} : function(node) {
    while(node = node.nextSibling) if(node.nodeType == 1) break;
    return node;
};
var previous = traversal ? function(node) {
    return node.previousElementSibling;
} : function(node) {
    while(node = node.previousSibling) if(node.nodeType == 1) break;
    return node;
};

function ShowHideBlock2(blockName)
{
	var new_vis;
	var block = document.getElementById(blockName+"_expand");
	if (block)
	{
		if (block.innerHTML == "+")
		{
			block.innerHTML = "-";
			new_vis = "";
		}
		else
		{
			block.innerHTML = "+";
			new_vis = "none";
		}
	}

	var block = document.getElementById(blockName);
	var level = block.getAttribute("level");

	if (new_vis == "")
	{
		do
		{
			block = next(block);
			if (block.getAttribute("level")-1 == level)
			{
				block.style.display = new_vis;
			}
		}
		while (block.getAttribute("level") != level);
	}
	else
	{
		do
		{
			block = next(block);
			if (block.getAttribute("level") != level)
			{
				block.style.display = new_vis;
				var plus = document.getElementById(block.getAttribute("id")+"_expand");
				if (plus)
				{
					plus.innerHTML = "+";
				}
			}
		}
		while (block.getAttribute("level") != level);
	}
}

var price = Array();
var old_price = Array();
var stock = Array();
var link = Array();

function SetValue(name, value, def)
{
	if (value == undefined)
	{
		value = def;
	}
	x = document.getElementById(name);
	if (x)
		x.innerHTML = value;
}

function UpdatePrice(productID)
{
	var x;
	var values = '';
	x = document.getElementById('sel_'+productID);
	SetValue('stock_'+productID, stock[productID+'_'+x.value], '');
	SetValue('price_'+productID, price[productID+'_'+x.value], '0');
	SetValue('old_price_'+productID, old_price[productID+'_'+x.value], ' ');
	SetValue('link_'+productID, link[productID+'_'+x.value], ' ');
}

function UpdatePrice2(productID, paramCount)
{
	var x;
	var values = '';
	for(var i=0; i<paramCount; i++)
	{
		x = document.getElementById('sel_'+productID+'_'+i);
		values = values + '_' + x.value;
	}
	SetValue('stock_'+productID, stock[productID+values], '');
	SetValue('price_'+productID, price[productID+values], '0');
	SetValue('old_price_'+productID, old_price[productID+values], ' ');
	SetValue('link_'+productID, link[productID+values], ' ');
}

function keydown(D)
{
	D=D||window.event;
	var A=D.keyCode;
	if (D.ctrlKey)
	{
		var C;

		if (A==37)
		{
			C=document.getElementById("key_left")
		}
		if(A==39)
		{
			C=document.getElementById("key_right")
		}

		if (C)
		{
			location.href=C.href
		}
	}
}

function ClearSelectectionIfExists(D)
{
	var X=(D.target||D.srcElement);
	if (X)
	{
		var E=X.tagName;
		if (E=="INPUT" || E=="TEXTAREA") return;

		C=document.getElementById("invisible_edit");
		if (C)
		{
			C.focus();
			C.select();
		}
	}
}

document.onkeydown=keydown;

function selectFirst()
{
	var x = document.getElementById('focus');
	if (x)
	{
		x.focus();
		x.select();
	}
}

function selectById(id)
{
	var x = document.getElementById(id);
	if (x)
	{
		x.focus();
		x.select();
	}
}

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

function open_link_in_parent(addr)
{
	window.opener.location.href = addr;
	window.close();
}

function open_msg(url)
{
	x = (screen.availWidth-300)/2;
	y = (screen.availHeight-100)/2-20;
	if (x < 0) x = 0;
	if (y < 0) y = 0;
	newWin = window.open(url,'msg','left='+x+',top='+y+',width=300,height=200,location="no",menubar="no",status="no",toolbar="no",resizable="no",scrollbars="no"');
	newWin.focus();
}

function open_pic(pic, width, height)
{
	height = height + 80;
	x = (screen.availWidth-width)/2;
	y = (screen.availHeight-height)/2;
	if (x < 0) x = 0;
	if (y < 0) y = 0;
	newWin = window.open("",'picture','left='+x+',top='+y+',width='+width+',height='+height+',location="yes",menubar="no",status="no",toolbar="no",resizable="no",scrollbars="yes"');
	newWin.moveTo(x,y);
	newWin.resizeTo(width,height);
	newWin.document.writeln("<html><head><title>"+pic+"</title>");
    	newWin.document.writeln("<style>");
    	newWin.document.writeln("<!--");
    	newWin.document.writeln(".no_border  { border: 0px solid white }");
    	newWin.document.writeln("-->");
    	newWin.document.writeln("</style></head>");
    	newWin.document.writeln("<body bgcolor=#FFFFFF leftMargin=0 topMargin=0 marginheight=0 marginwidth=0>");
    	newWin.document.writeln("<table border=0 width='100%' height='100%'><tr><td valign='center' align='center'>");
    	newWin.document.writeln("<a href='#' class='no_border' title='Щёлкните чтобы закрыть окно' onClick='self.close()'><img class='no_border' src='"+pic+"'></a>");
    	newWin.document.writeln("</td></tr></table>");
    	newWin.document.writeln("</body></html>");
    	newWin.document.close();
	newWin.focus();
}

function open_map(addr, toolbars)
{
	var map = new YMaps.Map(document.getElementById("YMapsID"));

	var gc = new YMaps.Geocoder(addr);
	YMaps.Events.observe(gc, gc.Events.Load, function () {
	    if (this.length()) {
	        map.setBounds(this.get(0).getBounds());
		    var geoResult = this.get(0).getBounds().getCenter();
	
	        var content = document.createElement('span');
	        content.innerHTML = addr;
	        map.openBalloon(geoResult, content);
	    }
	});

	if (toolbars == 1)
	{
		var topLeftPos = new YMaps.ControlPosition(YMaps.ControlPosition.TOP_LEFT, new YMaps.Size(150, 15)),
		    topRightPos = new YMaps.ControlPosition(YMaps.ControlPosition.TOP_RIGHT, new YMaps.Size(170, 15)),
		    scaleLine = new YMaps.ScaleLine(),
		    curPos = topLeftPos;
	
		map.addControl(scaleLine, curPos);
		map.addControl(new YMaps.TypeControl(), new YMaps.ControlPosition(YMaps.ControlPosition.TOP_LEFT));
		map.addControl(new YMaps.Zoom());
		map.addControl(new YMaps.MiniMap(), new YMaps.ControlPosition(YMaps.ControlPosition.TOP_RIGHT));
	}
}

function popup_map(addr)
{
	var width = screen.availWidth;
	var height = screen.availHeight;
	x = 0;
	y = 0;
	newWin = window.open("",'map','left='+x+',top='+y+',width='+width+',height='+height+',location="yes",menubar="no",status="no",toolbar="no",resizable="no",scrollbars="yes"');
	newWin.moveTo(x,y);
	newWin.resizeTo(width,height);
	newWin.document.writeln("<html><head><title>"+addr+"</title>");
	newWin.document.writeln("<meta http-equiv='Content-Type' content='text/html; charset=windows-1251'>");
	newWin.document.writeln("<script type='text/javascript' src='http://api-maps.yandex.ru/1.1/index.xml?key="+yandex_map_key+"'></script>");
	newWin.document.writeln("<script type='text/javascript' src='/1js.php'></script></head>");
    	newWin.document.writeln("<body onload='open_map(\""+addr+"\", 1);' bgcolor=#FFFFFF leftMargin=0 topMargin=0 marginheight=0 marginwidth=0>");
    	newWin.document.writeln("<div id='YMapsID' style='height:100%; width:100%;'></div>");
    	newWin.document.writeln("</body></html>");
    	newWin.document.close();
	newWin.focus();
}

function confirmDelete(question, where)
{
	temp = window.confirm(question);
	if (temp)
	{
		window.location=where;
	}
}

function WordCount(text)
{
	words=text.split(" ");
	var count= words.length;
	if ((count > 1) && (words[count-1]=="")) count--;
	if ((count > 0) && (words[0]=="")) count--;
	return count;
}

var tooltip = {

	options: {
		attr_name: "tooltip",
		blank_text: "<br>(откроется в новом окне)",
		newline_entity: "<br>",
		max_width: 0,
		delay: 100,
		skip_tags: ["link", "style"]
	},

	t: document.createElement("DIV"),
	c: null,
	g: false,
	canvas: null,

	m: function(e){
		var x = window.event ? event.clientX + tooltip.canvas.scrollLeft : e.pageX;
		var y = window.event ? event.clientY + tooltip.canvas.scrollTop : e.pageY;
		mouseX = x;
		mouseY = y;
		CheckMenu();
		if (tooltip.g){
			tooltip.a(x, y);
		}
	},

	d: function(){
		tooltip.canvas = document.getElementsByTagName(document.compatMode && document.compatMode == "CSS1Compat" ? "HTML" : "BODY")[0];
		tooltip.t.setAttribute("id", "tooltip");
		document.body.appendChild(tooltip.t);
		if (tooltip.options.max_width) tooltip.t.style.maxWidth = tooltip.options.max_width + "px"; /* all but ie */
		var a = document.all && !window.opera ? document.all : document.getElementsByTagName("*"); /* in opera 9 document.all produces type mismatch error */
		var l = a.length;
		for (var i = 0; i < l; i++){

			if (!a[i] || tooltip.options.skip_tags.in_array(a[i].tagName.toLowerCase())) continue;

			var tooltip_title = a[i].getAttribute("title"); /* returns form object if IE & name="title"; then IE crashes; so... */
			if (tooltip_title && typeof tooltip_title != "string") tooltip_title = "";

			var tooltip_alt = a[i].getAttribute("alt");
			var tooltip_blank = a[i].getAttribute("target") && a[i].getAttribute("target") == "_blank" && tooltip.options.blank_text;
			if (tooltip_title){
				a[i].setAttribute(tooltip.options.attr_name, tooltip_blank ? (tooltip_title ? tooltip_title + " " + tooltip.options.blank_text : tooltip.options.blank_text) : tooltip_title);
				if (a[i].getAttribute(tooltip.options.attr_name)){
					a[i].removeAttribute("title");
					if (tooltip_alt && a[i].complete) a[i].removeAttribute("alt");
					tooltip.l(a[i], "mouseover", tooltip.s);
					tooltip.l(a[i], "mouseout", tooltip.h);
				}
			}else if (tooltip_alt && a[i].complete){
				a[i].setAttribute(tooltip.options.attr_name, tooltip_alt);
				if (a[i].getAttribute(tooltip.options.attr_name)){
					a[i].removeAttribute("alt");
					tooltip.l(a[i], "mouseover", tooltip.s);
					tooltip.l(a[i], "mouseout", tooltip.h);
				}
			}
			if (!a[i].getAttribute(tooltip.options.attr_name) && tooltip_blank){
				/* */
			}
		}
		document.onmousemove = tooltip.m;
		window.onscroll = tooltip.h;
		tooltip.a(-99, -99);
	},
	
	_: function(s){
		s = s.replace(/\&/g,"&amp;");
		s = s.replace(/\</g,"&lt;");
		s = s.replace(/\>/g,"&gt;");
		return s;
	},

	s: function(e){
		if (typeof tooltip == "undefined") return;
		var d = window.event ? window.event.srcElement : e.target;
		if (!d.getAttribute(tooltip.options.attr_name)) return;
		var s = d.getAttribute(tooltip.options.attr_name);
		if (tooltip.options.newline_entity){
			var s = tooltip._(s);
			s = s.replace(eval("/" + tooltip._(tooltip.options.newline_entity) + "/g"), "<br />");
			tooltip.t.innerHTML = s;
		}else{
			if (tooltip.t.firstChild) tooltip.t.removeChild(tooltip.t.firstChild);
			tooltip.t.appendChild(document.createTextNode(s));
		}
		tooltip.c = setTimeout(function(){
			tooltip.t.style.visibility = 'visible';
		}, tooltip.options.delay);
		tooltip.g = true;
	},

	h: function(e){
		if (typeof tooltip == "undefined") return;
		tooltip.t.style.visibility = "hidden";
		if (!tooltip.options.newline_entity && tooltip.t.firstChild) tooltip.t.removeChild(tooltip.t.firstChild);
		clearTimeout(tooltip.c);
		tooltip.g = false;
		tooltip.a(-99, -99);
	},

	l: function(o, e, a){
		if (o.addEventListener) o.addEventListener(e, a, false); /* was true--Opera 7b workaround! */
		else if (o.attachEvent) o.attachEvent("on" + e, a);
			else return null;
	},

	a: function(x, y){
		var w_width = tooltip.canvas.clientWidth ? tooltip.canvas.clientWidth + tooltip.canvas.scrollLeft : window.innerWidth + window.pageXOffset;
		var w_height = window.innerHeight ? window.innerHeight + window.pageYOffset : tooltip.canvas.clientHeight + tooltip.canvas.scrollTop; /* should be vice verca since Opera 7 is crazy! */

		if (document.all && document.all.item && !window.opera) tooltip.t.style.width = tooltip.options.max_width && tooltip.t.offsetWidth > tooltip.options.max_width ? tooltip.options.max_width + "px" : "auto";
		
		var t_width = tooltip.t.offsetWidth;
		var t_height = tooltip.t.offsetHeight;

		tooltip.t.style.left = x + 0 + "px";
		tooltip.t.style.top = y + 20 + "px";
		
		if (x + t_width > w_width) tooltip.t.style.left = w_width - t_width + "px";
		if (y + t_height > w_height) tooltip.t.style.top = w_height - t_height + "px";
	}
}

Array.prototype.in_array = function(value){
	var l = this.length;
	for (var i = 0; i < l; i++)
		if (this[i] === value) return true;
	return false;
};

var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
if (root){
	if (root.addEventListener) root.addEventListener("load", tooltip.d, false);
	else if (root.attachEvent) root.attachEvent("onload", tooltip.d);
}





