$(document).ready(function(){
	$.ajaxSetup({
		url: "includes/request.php",
		timeout: 3000,
		type: "POST",
		cache: false,
		async: true,
		contentType: "application/x-www-form-urlencoded"
	});
	$("#brand").change(function(){document.location='?'+$(this).attr('value')});
	$('div.search input.st').focus(function(){if($(this).val() == 'Поиск по сайту') $(this).val('');});
	$('div.search input.st').blur(function(){if($(this).val() == '') $(this).val('Поиск по сайту');});
	$("input.select").click(function(){$('div.qui').toggle('fast');});
	$("input.m_toggle").click(function(){
		var id = $(this).attr("id");
		if ($("#m_"+id).css('display') == 'none') $(this).attr('src', 'img/close.gif');
		else $(this).attr('src', 'img/open.gif');
		$("#m_"+id).slideToggle("slow");
		$(this).parent("div.m1").parent("div.l_m").children("ul:not(#m_"+id+")").slideUp("slow");
		$('input.m_toggle').not(this).attr('src', 'img/open.gif');
	});
	$("img.other_img").click(function(){
		var sm_link = $(this).attr('src');
		var orig_link = $("#main_img").attr('src');
		$(this).attr('src', orig_link.substr(0, orig_link.length-5)+'s.jpg');
		$("#main_img").attr('src', sm_link.substr(0, sm_link.length-5)+'b.jpg');
	});
	$("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();
	});
	$("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(){
		$(this).parent().removeClass("nosex");
		$("#m").parent().addClass("nosex");
		$("#sex").attr("value", "2");
	});
	$("#m").click(function(){
		$(this).parent().removeClass("nosex");
		$("#w").parent().addClass("nosex");
		$("#sex").attr("value", "1");
	});
	$("a.reload").click(function(){document.location.reload();});
	Cmp();
	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');
	});
	//	сравнение
	$(".comp").live('click', function(){
		var par = $(this).attr('name').split('|');
		$.ajax({
			   data:"event=compar&action="+par[1]+"&id="+par[0],
			   success: function(answer){
					if (par[1] == 'show') $('#comp-show').html(answer).show();
					if (par[1] == 'del' && $('#comp-show').html() != '') $('#comp-show').html('').hide();
					ShowComp();
			   }
		   })
	});
	$("#comp-close").live('click', function(){$('#comp-show').html('').hide();});
	$("#comp > div").live('mouseover', function(){$("#comp > div > table").show(); PosComp();});
	$("#comp > div").live('mouseout', function(){$("#comp > div > table").hide(); PosComp();});
	window.onscroll = function(){PosComp()}
	window.onresize = function(){PosComp()}
	ShowComp();

	$('#pay-type').change(function(){if($(this).val() == 6) $('#pay-msg').show(); else $('#pay-msg').hide()});
	if($('#pay-type').val() == 6) $('#pay-msg').show();
});

function ShowComp()
{
	$.ajax({
	   data:"event=compar&action=get",
	   success: function(answer){
			if (answer == '') $('#comp').html('').hide();
			else
			{
				$('#comp').html(answer).show();
				PosComp(1);
			}
	   }
   })
}

function PosComp(show)
{
	if ($('#comp').html() != '')
	{
		var top = $(window).height() + $(document).scrollTop() - $('#comp').height();
		$('#comp').css({'top':top+'px'});
	}
	if ($('#comp-show').html() != '')
	{
		if ($('#comp-show div.scroll').height() > $(window).height() - 20) $('#comp-show div.scroll').css({'height':($(window).height() - 100)+'px'});
		if ($('#comp-show').width() > $(window).width() - 20) $('#comp-show').css({'width':($(window).width() - 20)+'px'});
		var top = ($(window).height() - $('#comp-show').height()) / 2 + $(document).scrollTop();
		var left = ($(window).width() - $('#comp-show').width()) / 2;
		$('#comp-show').css({'left':left+'px', 'top':top+'px'});
	}
}

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.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=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_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 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);
}