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;
}

$(document).ready(function(){
	$.ajaxSetup({
		url: "includes/request.php",
		timeout: 3000,
		type: "POST",
		cache: false,
		async: true,
		contentType: "application/x-www-form-urlencoded"
	});
	$("a.history").click(function(){
		$("#history_an").slideToggle("fast");
		$("#history_full").slideToggle("fast");
		$("a.history").toggle();
		});
	$("#s").focus(function(){if ($(this).attr("value") == 'Поисковый запрос') $(this).attr({value: ''});});
	$("#s").blur(function(){if ($(this).attr("value") == '') $(this).attr({value: 'Поисковый запрос'});});
	Vote('get');
	$("img.otherimg").click(function(){
		var sm_link = $(this).attr('src').replace("images/", "image.php?src=");
		var orig_link = $("#main_img").attr('src');
		$("img.otherimg_sel").addClass('otherimg');
		$("img.otherimg_sel").removeClass('otherimg_sel');
		$(this).removeClass('otherimg');
		$(this).addClass('otherimg_sel');
		$("#main_img").attr('src', sm_link.substr(0, sm_link.length-5)+'b.jpg');
	});
	$("#imgleft, #imgright").click(function(){
		var cur, next, m;
		if ($("img.otherimg_sel").attr("name")-0) cur = $("img.otherimg_sel").attr("name");
		else cur = 1;
		m = $("#imgright").attr("rel");
		if ($(this).attr('id') == 'imgleft')
			if (cur == 1) next = m;
			else next = cur - 1;
		else
			if (cur == m) next = 1;
			else next = cur - 0 + 1;
		$("img.otherimg_sel").addClass('otherimg');
		$("img.otherimg_sel").removeClass('otherimg_sel');
		next = $("img[name='"+next+"']");
		next.removeClass('otherimg');
		next.addClass('otherimg_sel');
		var src = next.attr('src');
		$("#main_img").attr('src', src.substr(0, src.length-5)+'b.jpg');
	});
	$("#brand").change(function(){
		$.ajax({
			data:'type=items&id='+$(this).attr('value'),
			success:function(XMLHttpRequest, textStatus){
				$("#model").html(XMLHttpRequest);
				}
			});
	});
});

function Vote(act, attr)
{
	$.ajax({
		data:'type=opros&act='+act+'&'+attr,
		success:function(XMLHttpRequest, textStatus){
			$("#opros").html(XMLHttpRequest);
			$("#vote").click(function(){
				var attr = "opros="+$("#vote").attr("value")+'&ans='+$("input[name='answer']:checked").attr('value');
				Vote('set', attr);
				});
			}
		});
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function getBodyScrollTop()
{
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

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)
			{
				switch (action)
				{
					case 'buy': document.location='order.htm'; break;
					case 'buy_credit': document.location='order.htm'; break;
					case 'buy_none': document.location='order.htm'; break;
					default:
						if (action == 'add')
						{
							document.getElementById('cart_it').innerHTML = http_request.responseText;
							document.getElementById('cart_message').style.top = parseInt(getClientHeight()/4)+getBodyScrollTop()+'px';
							document.getElementById('cart_message').style.display = 'block';
						}
						else location.reload(false);
				}
			}
			else alert('Сервер не отвечает.');
	}
}

function Comparison(id, rub, what, s_id, pos)
{
	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+'&pos='+pos);
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				//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 ToggleCatalog(id)
{
	if (document.getElementById(id).style.display == 'none')
		document.getElementById(id).style.display = 'block';
	else
		document.getElementById(id).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');
}