$(document).ready(function(){
	$('.headermenu_list ul li').hover(
		function(){
			var block = $(this).find('div.sub-more');
			block.show();
//			$(this).find('div.shadow').fadeTo(0, 0.3).width(block.width()).height(block.height()).show();
			$(this).children('a.tab').addClass('act');
		},
		function(){
			$(this).find('div.sub-more').hide();
			$(this).find('div.shadow').hide();
			$(this).children('a.tab').removeClass('act');
		}
	);
	$('.authorize_block table tr:even').css('background','white');
	$('.authorize_block table tr:odd').css('background','#EEFCFE');
	$('.authorize_block table tr.result').css('background','#C7E7FB');
	$("a.zoom").fancybox({
		'overlayShow': true,
		'overlayOpacity': 0.8,
		'hideOnContentClick': true,
		'titleShow': false,
		'transitionIn': 'elastic',
		'transitionOut': 'elastic'
	});
	$.ajax({
		timeout: 3000,
		type: "GET",
		cache: false,
		async: true,
		contentType: "application/x-www-form-urlencoded",
		url:"/includes/items.php",
		success: function(answer){$('#all-items').html(answer);}
	});
	
	$('#filials > a.but').click(function(){
		$('#filials div').slideDown(200, function(){ $(document).one('click', function(){ $('#filials div').hide();}); });
	});
});

function Prep_Req()
{
	if (window.XMLHttpRequest) { http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); }}
	else if (window.ActiveXObject)
	{ 	try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}}
	}
	if (!http_request) { alert('Невозможно создать XMLHTTP объект'); return false; }
	return http_request;
}

function Show_Prop(obj, objName) 
{
  var result = "The properties for the " + objName + " object:" + "\n";
 
  for (var i in obj) {result += i + " = " + obj[i] + "\n";}
  
  alert(result);
}

function Cart(id, action, s_id, count)
{
	if (count != null && count <= 0)
	{
		location.reload(false);
		return;
	}
	if (count == null) count = 1;
	http_request = Prep_Req();
	http_request.open('POST', 'includes/cart.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('what='+action+'&id='+id+'&count='+count+'&s_id='+s_id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				//if (action != 'add') 
				location.reload(false);
			}
			else alert('Сервер не отвечает.');
	}
}

function Comparison(id, rub, what, s_id)
{
	if (document.getElementById('comp_'+id))
		if (document.getElementById('comp_'+id).checked == true) what = 'add';
		else what = 'del';
	http_request = Prep_Req();
	http_request.open('POST', 'includes/comp.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('id='+id+'&rub='+rub+'&what='+what+'&s_id='+s_id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				if (document.getElementById('comp_'+id) == null) location.reload(false);
			}
			else alert('Сервер не отвечает.');
	}
}

function Sort(brand, rub, s_id, sel_id)
{
	http_request = Prep_Req();
	http_request.open('POST', 'includes/sort.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('brand='+brand+'&rub='+rub+'&s_id='+s_id);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				document.getElementById(sel_id).innerHTML = http_request.responseText;
			}
			else alert('Сервер не отвечает.');
	}
}

function ToggleMenu(id)
{
	if (document.getElementById('left_menu_'+id).style.display == 'block')
		document.getElementById('left_menu_'+id).style.display='none';
	else
		document.getElementById('left_menu_'+id).style.display='block';
}

function ToggleText(id)
{
	for (i=1; i<=10; i++)
	{
		if (!document.getElementById('itemtext_'+i) || !document.getElementById('itemtext_head_'+i)) continue;
		if (id != i)
		{
			document.getElementById('itemtext_'+i).style.display='none';
			document.getElementById('itemtext_head_'+i).className='prop_header_unsel';
		}
		else
		{
			document.getElementById('itemtext_'+i).style.display='block';
			document.getElementById('itemtext_head_'+i).className='prop_header_sel';
		}
	}
}

function ToggleMessage(id)
{
	if (id == 1)
	{
		if (document.getElementById('otziv').style.display == 'none')
		{
			document.getElementById('otziv').style.display = 'block';
			document.getElementById('question').style.display = 'none';
		}
		else document.getElementById('otziv').style.display = 'none';
	}
	else
	{
		if (document.getElementById('question').style.display == 'none')
		{
			document.getElementById('question').style.display = 'block';
			document.getElementById('otziv').style.display = 'none';
		}
		else document.getElementById('question').style.display = 'none';
	}
}

function Preview(id)
{
	var img_link = document.getElementById(id).src;
	img_link = img_link.substr(0, img_link.length-5);
	void window.open('/image.php?src='+img_link+'b.jpg', 'ImagePreview', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=550,height=550,left=20,top=20');
}

function TopMenu(obj, action, id)
{
	if (action == 'show')
	{
		document.getElementById('sub_'+id).style.display='block';
		obj.style.backgroundColor='#FFF';
		var ln = obj.getElementsByTagName('a');
		ln[0].style.color='#57A5DF';
	}
	else
	{
		document.getElementById('sub_'+id).style.display='none';
		obj.style.backgroundColor='#57A5DF';
		var ln = obj.getElementsByTagName('a');
		ln[0].style.color='#FFF';
	}
}