$(document).ready(function(){
	$.ajaxSetup({
		url: "includes/request.php",
		timeout: 3000,
		type: "POST",
		cache: false,
		async: true,
		contentType: "application/x-www-form-urlencoded"
	});
	$("input.sb").focus(function(){if($(this).attr('value')=='Поиск на сайте') $(this).attr('value', '');});
	$("input.sb").blur(function(){if($(this).attr('value')=='') $(this).attr('value', 'Поиск на сайте');});
	$("input.select").click(function(){$('div.qui').toggle('fast');});
	$("#brand").change(function(){document.location='?b='+$(this).attr('value')});
	$("img.hover").hover(function(){$(this).attr('src', $(this).attr('src').replace('_off', '_on'))},
						 function(){$(this).attr('src', $(this).attr('src').replace('_on', '_off'))});
	$("#left-border").css({'height':$("td.left-menu").height()});
	$("img.m_toggle").click(function(){
		$("#left-border").css({'height':'auto'});
		var id = $(this).attr("id");
		if ($("#m_"+id).html().length < 20)
		{
			$.ajax({
				data:"event=menu&id="+id,
				success:function(XMLHttpRequest, textStatus){
					$("#m_"+id).html(XMLHttpRequest);
					$("#m_"+id).slideToggle("slow", function(){
						$("#left-border").css({'height':$("td.left-menu").height()});
					});
				}
			});
		}
		else $("#m_"+id).slideToggle("slow", function(){$("#left-border").css({'height':$("td.left-menu").height()});});
		$("div.cat-menu ul li ul:not(#m_"+id+")").slideUp("slow");
	});
	$("img.other_img").click(function(){
		var sm_link = $(this).attr('src');
		var orig_link = $("#main_img").attr('src');
		$(this).attr('src', orig_link.replace('b.jpg', 's.jpg'));
		$("#main_img").attr('src', sm_link.replace('s.jpg', 'b.jpg'));
	});
	$("input.set").keyup(function(){
		$.ajax({
			   data:"event=backet&action=set&count="+$(this).attr("value")+"&id="+$(this).attr("name"),
			   success: function(XMLHttpRequest, textStatus){
				   var data = XMLHttpRequest.split('&');
				   $("#backet_count").html(data[0]);
				   $("#backet_cost").html(data[1]);
				   $("#backet_total").html(data[1]);
			   }
		   })
	});
	$("#w").click(function(){
		$("#m").parent().removeClass("sex");
		$(this).parent().addClass("sex");
		$("#sex").attr("value", "2");
	});
	$("#m").click(function(){
		$("#w").parent().removeClass("sex");
		$(this).parent().addClass("sex");
		$("#sex").attr("value", "1");
	});
	$("a.reload").click(function(){document.location.reload();});
	Cmp();
	Del();
	ShowClock();
	$("a.print").click(function(){
		void window.open('/print.php?'+$(this).attr("name"), 'Версия_для_печати', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=850,height=800,left=20,top=20');
	});
	$("img.zoom, a.zoom").click(function(){
		var img_link = $("#"+$(this).attr('name')).attr('src');
		img_link = img_link.replace('s.jpg', 'b.jpg');
		void window.open('image.php?src='+img_link, 'ImagePreview', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=640,height=640,left=20,top=20');
	});
	Correct();
});

function Cmp()
{
	$("select.compar").change(function(){
		$.ajax({
			   data:"event=compar&item_1="+$("#item_1").attr("value")+"&item_2="+
			   		$("#item_2").attr("value")+"&item_3="+$("#item_3").attr("value"),
			   success: function(XMLHttpRequest, textStatus){
				   $("#comparation").html(XMLHttpRequest);
			   }
		   })
	});
	$("#main_img, img.main_img, img.other_img").css({'cursor':'pointer'});
	$("#main_img, img.main_img").click(function(){
		var img_link = $(this).attr('src');
		img_link = img_link.replace('s.jpg', 'b.jpg');
		void window.open('/image.php?src='+img_link, 'ImagePreview', 'resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=640,height=640,left=20,top=20');
	});
	$("a.buy").click(function(){
		$.ajax({
			   data:"event=backet&action=add&id="+$(this).attr("name"),
			   success: function(XMLHttpRequest, textStatus){
				   var data = XMLHttpRequest.split('&');
				   $("#backet-body").html(data[3]);
				   $("#backet_count").html(data[0]);
				   $("#backet_cost").html(data[1]);
				   $("#backet_name").html(data[2]);
				   $("#backet").css({'top':getInnerHeight()/3 + getScrollY()});
				   $("#backet").show(20, function(){
						$("#backet").animate({'top':getInnerHeight()/4 + getScrollY()},1500,function(){$("#backet").hide(200);})
					});
			   }
		   })
	});
}

function Brands(id)
{
	$.ajax({
		data:"event=brands&action=1&id="+id,
		success: function(request, textStatus){
			$("#brands").html(request);
		}
   });
}

function Del()
{
	$("a.del").click(function(){
		$.ajax({
			   data:"event=backet&action=del&id="+$(this).attr("name"),
			   success: function(XMLHttpRequest, textStatus){
				   var data = XMLHttpRequest.split('&');
				   $("#backet_count").html(data[0]);
				   $("#backet_cost").html(data[1]);
				   $("#backet_total").html(data[1]);
				   if (data[0] == 0) $("#order").slideUp("slow");
				   }
			   })
		$(this).parent().parent().remove();
	});
}

function getScrollY() 
{
    scrollY = 0;    
    if (typeof window.pageYOffset == "number") {
        scrollY = window.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        scrollY = document.documentElement.scrollTop;
    }  else if (document.body && document.body.scrollTop) {
        scrollY = document.body.scrollTop; 
    } else if (window.scrollY) {
        scrollY = window.scrollY;
    }
    return scrollY;
}
  
function getInnerHeight()
{
    height = 0;
    if (window.innerHeight) {
        height = window.innerHeight - 18;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        height = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        height = document.body.clientHeight;
    }
    return height;
}

function ShowClock()
{
	var Digital = new Date();
	var hours = Digital.getHours();
	var minutes = Digital.getMinutes();
	var seconds = Digital.getSeconds();

	if (minutes <= 9) { minutes = "0"+minutes; }
	if (seconds <= 9) { seconds = "0"+seconds; }
	myclock = '';
	myclock += hours+':'+minutes+':'+seconds;
	$("#clock").html(myclock);
	setTimeout("ShowClock()", 1000);
}

function Correct()
{
	var h = 0;
	$('ul.item-list li div.el').each(function(){
		if ($(this).height() > h) h = $(this).height();
	});
	$('ul.item-list li').height(h);
}

function cs(cur_w)
{
	var w = $('#cont').width();
	if (w == cur_w) return;
	$.ajax({
		url: "includes/request.php",
		data:"event=content_size&id=1&size="+w,
		timeout: 3000,
		type: "POST",
		cache: false,
		async: true,
		contentType: "application/x-www-form-urlencoded",
		success: function(XMLHttpRequest, textStatus){}
	});
}