function getWindowInnerSize(pDoc)
{
	var width, height;
	if (!pDoc)
		pDoc = document;

	if (self.innerHeight) // all except Explorer
	{
		width = self.innerWidth;
		height = self.innerHeight;
	}
	else if (pDoc.documentElement && pDoc.documentElement.clientHeight) // Explorer 6 Strict Mode
	{
		width = pDoc.documentElement.clientWidth;
		height = pDoc.documentElement.clientHeight;
	}
	else if (pDoc.body) // other Explorers
	{
		width = pDoc.body.clientWidth;
		height = pDoc.body.clientHeight;
	}
	return {innerWidth : width, innerHeight : height};
}

function getWindowScrollPos(pDoc)
{
	var left, top;
	if (!pDoc)
		pDoc = document;

	if (self.pageYOffset) // all except Explorer
	{
		left = self.pageXOffset;
		top = self.pageYOffset;
	}
	else if (pDoc.documentElement && pDoc.documentElement.scrollTop) // Explorer 6 Strict
	{
		left = document.documentElement.scrollLeft;
		top = document.documentElement.scrollTop;
	}
	else if (pDoc.body) // all other Explorers
	{
		left = pDoc.body.scrollLeft;
		top = pDoc.body.scrollTop;
	}
	return {scrollLeft : left, scrollTop : top};
}


$(function(){
	
    var ws = getWindowInnerSize();
	var wss = getWindowScrollPos();
	var marginTop = wss.scrollTop + (ws.innerHeight/2) - 100;
	$(".small_basket").css({"top": marginTop+"px"});
	
	$(".right").click(function () {
		
		scrolling = true;
		n = $(this).parent("div").find(".scroller_inner UL LI").size();
		h = $(this).parent("div").attr("alt");
		//alert(n);
		if(h < n-2)
		{
			$(this).parent("div").find(".scroller_inner UL").animate({"left": "-=315px"}, "slow");
			
			h++;
			$(this).parent("div").attr("alt", h);
		}
				
	});
	
	$(".left").click(function () {
		
		scrolling = true;
		n = $(this).parent("div").find(".scroller_inner UL").size();
		h = $(this).parent("div").attr("alt");
		
		if(h > 1)
		{
			$(this).parent("div").find(".scroller_inner UL").animate({"left": "+=315px"}, "slow");
			
			h--;
			$(this).parent("div").attr("alt", h);
		}
		
	});

	$(".top").click(function () {
		$(this).parent("li").find(".sub").eq(0).slideToggle();
		
		$(this).toggleClass("active");
		$(this).find("span.cat_arrow").toggleClass("cat_arrow_sel");
		return false;
	});
	
	$(".sizes span").click(function () {
		
		$(this).parent("div").find("span.selected_size").toggleClass("selected_size").find("input").click();
		
		$(this).toggleClass("selected_size");
		$(this).find("input").click();
		
	});
	
	$(".colors span").click(function () {
		
		$(this).parent("div").find("span.selected_color").toggleClass("selected_color").find("input").click();
		
		$(this).toggleClass("selected_color");
		$(this).find("input").click();
		
	});
	
	var animate = false;
	$(".small_basket").hover(
		function(){
			$(this).stop().animate({"right": "0px"}, "slow");
			
		}, 
		function(){
	       	$(this).stop().animate({"right": "-230px"}, "slow");	
		}
    );
	
	$("#go2order").submit(function() {
		$(".small_basket").show();
        var options = {
			target: ".small_basket_content span",
			url: "/bitrix/ajax/ajax.php",
			success: function() {
				$(".small_basket").animate({"right": "0px"}, "slow");
				setTimeout(function() { $(".small_basket").animate({"right": "-230px"}, "slow"); }, 3000)
			}
		};
		// передаем опции в  ajaxSubmit
		$("#go2order").ajaxSubmit(options);
		
		return false;
	});
	
	$(".del_pos").click(function () {
		
		if(confirm('Вы действительно хотите удалить позицию?'))
		{
			var id = $(this).attr("id");
			
			//alert(id);
			$.ajax({
				type: "POST",
				url: "/bitrix/ajax/del_pos.php",
				data: "id=" + id,
				success: function(msg){
					$(".ansver").html(msg);
				}
			});
		}
		
	});
	
	function validate(id)
	{
		if ($("input#" + id).val().length > 0) 
		{
			$("input#" + id).removeClass("notValid");
			return true;
		}
		else
		{
			$("input#" + id).addClass("notValid");
			$("input#" + id).focus();
			return false;
		}
	}
	$("form#validate").submit(function() {

		if((validate("fio") == true) && (validate("phone") == true) && (validate("email") == true))
		{
			var options = {
				target: ".order-form",
				url: "/bitrix/ajax/order.php",
				success: function() {
					//alert("OK");
				}
			};
			// передаем опции в  ajaxSubmit
			$("#validate").ajaxSubmit(options);
		}
		return false;

		   
	});    
	
});
