// JavaScript Document
function trim(s)
{
	return rtrim(ltrim(s));
}

function ltrim(s)
{
	return s.replace(/^\s+/, ''); 
}

function rtrim(s)
{
	return s.replace(/\s+$/, ''); 
}

  
function deleteFromCart($itemId, $sizeId){
	$.get("/ruleMe/system/ajax/cart.php", { action: "delete", itemId:  $itemId, sizeId: $sizeId},
		function($data){
			$("#miniCart").html($data);
			refreshCart();
	});
	return false;
}

function refreshCart(){
	$("div.miniCart ul li").mouseover(function(){
		$("div.miniCart ul li").removeClass("act");
		$(this).addClass("act");
	}).mouseout(function(){
		$(this).removeClass("act");
	});	
}

function goToOrder(){
	window.location.href = "/order.php";
}

function addLine(){
	var $i						= 0
	var $left					= 293;
	var $top					= 290;
	var $bgLeft					= 150;
	var $bgTop					= 150;
	while($i < 100){
		var $newDiv				= "<div "
		+ "style='"
		+ "position:absolute; top:"+$top+"px; left:"+$left+"px; "
		+ "background:url(/i/line.png) -"+$bgLeft+"px -"+$bgTop+"px no-repeat; "
		+ "width:40px; height:40px;"
		+ "'>"
		+ "</div>";
		$left					+= 20;
		$top					+= 18;
		$bgLeft					+= 20;
		$bgTop					+= 18;
		$("div#all").append($newDiv);
	$i++;
	}
}

function validateMail($email) {
   var reg							= /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   if(reg.test($email)) {
      return true;
   }else{
	  return false;
	}
}


 
  $(function(){
	
	addLine();
	
	refreshCart();
	
	$("#orderForms").submit(function(){
		var $die					= 0;
		if($("#FIO").val()	==	""){
			$die					= 1;
			$("#FIO").parent("div.dotted").css({backgroundImage: "url(/i/dotsPink.gif)"});
			$("#FIO").parent("div.dotted").children("span").fadeIn("slow");
		}else{
			$("#FIO").parent("div.dotted").css({backgroundImage: "url(/i/dotsBlack.gif)"});
			$("#FIO").parent("div.dotted").children("span").fadeOut("slow");
		}
		if(validateMail($("#email").val()) == false){
			$die					= 1;
			$("#email").parent("div.dotted").css({backgroundImage: "url(/i/dotsPink.gif)"});
			$("#email").parent("div.dotted").children("span").fadeIn("slow");
		}else{
			$("#email").parent("div.dotted").css({backgroundImage: "url(/i/dotsBlack.gif)"});
			$("#email").parent("div.dotted").children("span").fadeOut("slow");
		}
		if($("#phone").val()	==	""){
			$die					= 1;
			$("#phone").parent("div.dotted").css({backgroundImage: "url(/i/dotsPink.gif)"});
			$("#phone").parent("div.dotted").children("span").fadeIn("slow");
		}else{
			$("#phone").parent("div.dotted").css({backgroundImage: "url(/i/dotsBlack.gif)"});
			$("#email").parent("div.dotted").children("span").fadeOut("slow");
		}
		
		if($die != 0)
			return false;
		
	});

	$("#feedbackForm").submit(function(){
		var $die					= 0;
		if($("#FIO").val()	==	""){
			$die					= 1;
			$("#FIO").parent("div.dotted").css({backgroundImage: "url(/i/dotsPink.gif)"});
			$("#FIO").parent("div.dotted").children("span").fadeIn("slow");
		}else{
			$("#FIO").parent("div.dotted").css({backgroundImage: "url(/i/dotsBlack.gif)"});
			$("#FIO").parent("div.dotted").children("span").fadeOut("slow");
		}
		if(validateMail($("#email").val()) == false){
			$die					= 1;
			$("#email").parent("div.dotted").css({backgroundImage: "url(/i/dotsPink.gif)"});
			$("#email").parent("div.dotted").children("span").fadeIn("slow");
		}else{
			$("#email").parent("div.dotted").css({backgroundImage: "url(/i/dotsBlack.gif)"});
			$("#email").parent("div.dotted").children("span").fadeOut("slow");
		}		
		if($("#comment").val()	==	""){
			$die					= 1;
			$("#comment").parent("div.dottedTextarea").css({backgroundImage: "url(/i/dotsPink.gif)"});
			$("#comment").parent("div.dottedTextarea").children("span").fadeIn("slow");
		}else{
			$("#comment").parent("div.dottedTextarea").css({backgroundImage: "url(/i/dotsBlack.gif)"});
			$("#comment").parent("div.dottedTextarea").children("span").fadeOut("slow");
		}
		
		if($die != 0)
			return false;
		
	});
	
	$(".dotted input").keypress(function(){
		$(this).parent("div.dotted").css({backgroundImage: "url(/i/dotsBlack.gif)"});
		$(this).parent("div.dotted").children("span").fadeOut("slow");
	});
	
	
	
	$(".dottedTextarea textarea").keypress(function(){
		$(this).parent("div.dottedTextarea").css({backgroundImage: "url(/i/dotsBlack.gif)"});
		$(this).parent("div.dottedTextarea").children("span").fadeOut("slow");
	});
	
	
	$("div#slider ul li").click(function(){
		
		if($(this).hasClass("act")){
			
		}else{
			$("div#slider ul li").removeClass("act");
			$(this).addClass("act");
			
			
			var $leftOffset		= 0;
			var $trigger		= 0;
			
			
			$("div#slider ul li").each(function(){
				if($(this).hasClass("act")){
					$trigger	= 1;	
				}
												
				if($trigger == 0){
					$leftOffset	= $leftOffset + $(this).width()+20;
				}
			});
			
			var $wraperWidth	= $("div#slider").width()/2;
			
			$leftOffset			= 0-$leftOffset + $wraperWidth - 120;
			
			$("div#slider ul").animate({left: $leftOffset}, 200)
			
		}
		
	});
	
	$('#scale').slider({ 
        handle: '#toddler',
		value: 0,
        max: 100,
        slide: function(e, ui) { 
            var $val			= Math.round(ui.value);
			$val				= 0.5 + $val/100;
//			var $height			= 399*$val;
//			var $width			= 315*$val;
			var $height			= 600*$val;
			var $width			= 450*$val;
			$("div#slider div.wraper img").height($height).width($width).css({marginLeft: 0-($width/2), marginTop: (0-$height/2)});
        } 
    }); 
	
	
	$("a.colorBox").click(function(){

		var $img				= $(this).attr("img");
/*		var $itemId				= $(this).attr("itemId");
		var $lastItemId			= $("#itemId").val();
		var $sizeId				= $("a.size.act").attr("sizeId");
		
		
		if($itemId != $lastItemId){
			$("#itemId").val($itemId);
			$("a.size").removeClass("act");
			$("a.size").hide();
			$("a.size.item_"+$itemId).show();
			if(!$("a.size.item_"+$itemId+".size_id_"+$sizeId+":visible").html()){
				$("a.size.item_"+$itemId+":visible:first").addClass("act");
			}else{
				$("a.size.item_"+$itemId+".size_id_"+$sizeId+":visible").addClass("act");
			}
		}
*/		$("a.colorBox").removeClass("act");
		$(this).addClass("act");
		$("body").css({background : "url("+$img+")"});
		return false;
	});
	
	$("a.colorTitle").click(function(){
		var $itemId				= $(this).attr("itemId");
		var $lastItemId			= $("#itemId").val();
		var $sizeId				= $("a.size.act").attr("sizeId");
		$("a.colorTitle").removeClass("act");
		$(this).addClass("act");
		
		if($itemId != $lastItemId){
			$("#itemId").val($itemId);
			$("a.size").removeClass("act");
			$("a.size").hide();
			$("a.size.item_"+$itemId).show();
			if(!$("a.size.item_"+$itemId+".size_id_"+$sizeId+":visible").html()){
				$("a.size.item_"+$itemId+":visible:first").addClass("act");
			}else{
				$("a.size.item_"+$itemId+".size_id_"+$sizeId+":visible").addClass("act");
			}
		}
		
		return false;
	});
	
	
	
	$("a.size").click(function(){
		$("a.size").removeClass("act");
		$(this).addClass("act");
		return false;
	});
	
	
	$("#addToCart").click(function(){
		var $itemId				= $("#itemId").val();
		var $itemTitle			= $("#itemTitle").text();
		var $sizeId				= $("#product a.size.act").attr("sizeId");
		var $sizeTitle			= $("#product a.size.act").text();
		var $itemPrice			= $("#product #price").text();
		var $colourId			= $("#product a.colorTitle.act").attr("colourId");
		var $colourCode			= $("#product a.colorTitle.act").attr("colourCode");
		var $colourTitle		= $("#product a.colorTitle.act").attr("colourTitle");
		var $itemUrl			= location.href;
		
		$.get("/ruleMe/system/ajax/cart.php", { action: "add", itemId:  $itemId, itemUrl:  $itemUrl, itemPrice:  $itemPrice,  itemTitle: $itemTitle,  sizeId: $sizeId, sizeTitle: $sizeTitle, colourCode: $colourCode, colourId: $colourId, colourTitle: $colourTitle},
			function($data){
				$("#miniCart").html($data);
				refreshCart();
			});
		return false;
	});
	
	$("#showOrderForm").click(function(){
		$("#orderTable").slideUp();
		$("#orderForm").slideDown();
	});
	
	$("select.menu").change(function(){
		var $href			= $(this).val();
		if($href != ""){
			window.location.href = $href;
		}
	});
	
	
	/*Красивые инпуты*/
	$("input.changeble").focus(function(){
		var $defValue		= $(this).attr("defValue");
		var $curValue		= $(this).val();
		if($curValue == $defValue){
			$(this).val("");
		}		
	}).blur(function(){
		var $defValue		= $(this).attr("defValue");
		var $curValue		= $(this).val();
		if($curValue == ""){
			$(this).val($defValue);
		}
	});

	$("#orderTable a.delete").click(function(){
		var $itemId			= $(this).attr("itemId");
		var $sizeId			= $(".size_for_" + $itemId + " option:selected").attr("sizeId");
		var $colourId		= $(".colour_for_" + $itemId).attr("colourId");
//		alert($itemId + " : " + $sizeId + " : " + $colourId);
		$.get("/ruleMe/system/ajax/cart.php", { action: "delete", itemId:  $itemId, sizeId: $sizeId});
		$(this).parent("td").parent("tr").slideUp().remove();
		return false;
	});
	
	
});






  
  
  
  