// JavaScript Document
$(document).ready(function(){
assignResize();
  $('.productBrowse .prods_table tr:even').addClass('ev');
  
  $("#manufid").selectbox({
  	onOpen: function (inst) {
  		//console.log("open", inst);
  	},
  	onClose: function (inst) {
  		//console.log("close", inst);
  	},
  	effect: "slide"
  });
  
  $("#saifid").selectbox({
  	onOpen: function (inst) {
  		//console.log("open", inst);
  	},
  	onClose: function (inst) {
  		//console.log("close", inst);
  	},
  	effect: "slide"
  });
  
    $("#subcatid").selectbox({
  	onOpen: function (inst) {
  		//console.log("open", inst);
  	},
  	onClose: function (inst) {
  		//console.log("close", inst);
  	},
  	onChange: function (val, inst) {
  		$.ajax({
  			type: "GET",
  			data: {id: val},
  			url: "_pick_saif.php",
  			success: function (data) {
  				$("#saifSelect").html(data);
  				$("#saifid").selectbox();
  			}
    	});
  	},
  	effect: "slide"
  });
  
    $("#catid").selectbox({
  	onOpen: function (inst) {
  		//console.log("open", inst);
  	},
  	onClose: function (inst) {
  		//console.log("close", inst);
  	},
  	onChange: function (val, inst) {
  		$.ajax({
  			type: "GET",
  			data: {id: val},
  			url: "_pick_subcat.php",
  			success: function (data) {
  				$("#subcatSelect").html(data);
  				$("#subcatid").selectbox({
            onChange: function (val, inst) {
          		$.ajax({
          			type: "GET",
          			data: {id: val},
          			url: "_pick_saif.php",
          			success: function (data) {
          				$("#saifSelect").html(data);
          				$("#saifid").selectbox();
          			}
            	});
            	}
          });
    	}
  	});
   } ,
  	effect: "slide"
  });
   
});
function insertIntoCart(prodId,target){
  var prods=$.cookie("prods");
  if((prods==null)||(prods=="")){
    amount=$('#item'+prodId+' .amount').val();
	
    $.cookie("prods",prodId+':'+amount, { expires: 1 , path: '/' }); 
     $('#smallCart').html('<a href="index-products.php?page=cart"><span class="cartLabel" ><span id="countity"></span>&nbsp;<span>מוצרים</span></span></a>');  
      $('#countity').text('1'); 
  }else{
    var prodsArr=prods.split(',');
    
      if(!prodsArr.inArray(prodId)){
        amount=$('#item'+prodId+' .amount').val();
		
        prodsArr.push(prodId+':'+amount);
        $.cookie("prods",prodsArr, { expires: 1 , path: '/' });
        $('#countity').text(prodsArr.length);    
      }
    }
	var shownamount=document.getElementById("amountof"+prodId).value;
	updateCart(prodId,shownamount);
			target=document.getElementById("cart_cont"+prodId);
             
            target.innerHTML=" כרגע בסל קניות: <font size=2 style=' left:-100px;' color=black>  <b>("+amount+") </b></font>";
  // window.location='#fullheader1';

}
function checkifexists(prodId)
{
  var prods=$.cookie("prods");
  if(prods!=null){
    var prodsArr=prods.split(',');
    
      if(prodsArr.inArray(prodId)){
        amount=$('#item'+prodId+' .amount').val();

			target=document.getElementById("cart_cont"+prodId);
             
            target.innerHTML=" כרגע בסל קניות: <font size=2 style=' left:-100px;' color=black>  <b>("+amount+") </b></font>";
      }
	  else {			target=document.getElementById("cart_cont"+prodId);
             
            target.innerHTML=" כרגע לא בסל קניות.";
	  }
    }



};
function removeFromCart(prodId){
     var prods=$.cookie("prods");
     var prodsArr=prods.split(','); 
     removeFromArray(prodId,prodsArr);
}

function updateCart(prodId,amount){
  var products= $.cookie("prods");
  products=products.split(',');
  for(var i=0;i<products.length;i++){
     var prodTemp=products[i].split(':');
     if(prodId==prodTemp[0]){
       prodTemp[1]=amount;
     }
     products[i]=prodTemp.join(':');
  }
    products=products.join(',');
    $.cookie("prods",products, { expires: 1 , path: '' });
}
function resetOnMouseOver()
{
}
function decreaseInCart(prodId){ 
  amount=$('#item'+prodId+' .amount').val();
   amount--;
   if(amount<1)
   amount=1;
   $('#item'+prodId+' .amount').val(amount);
   updateCart(prodId,amount);
}

function increaseInCart(prodId){ 
   amount=$('#item'+prodId+' .amount').val();
   amount++;
   $('#item'+prodId+' .amount').val(amount);
      updateCart(prodId,amount);
}


function decreaseAmount(prodId){
  amount=$('#item'+prodId+' .amount').val();
   amount--;
   if(amount<1)
   amount=1;
   $('#item'+prodId+' .amount').val(amount);
}

function increaseAmount(prodId){   
   amount=$('#item'+prodId+' .amount').val();
   amount++;
   $('#item'+prodId+' .amount').val(amount);
}

Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array. Returns false if it is not.
{
  var i;
  for (i=0; i < this.length; i++) {
    // Matches identical (===), not just similar (==).
   // if (this[i] == value) {
    //  return true;
    //}
     var prodTemp=this[i].split(':');
     if(value==prodTemp[0]){
       return true;
     }
  }
  return false;
};

function removeFromArray(value,arr)
{

  var i; 
  for (i=0; i < arr.length; i++) {
     var prodTemp=arr[i].split(':');
     if(value==prodTemp[0]){ 
       arr.splice(i,1); 
     }
  }
   
   products=arr.join(',');  
   
   if(arr.length>0) { 
        $.cookie("prods", products, { expires: 1 , path: '' });
        $('#countity').text(arr.length); 
        $('#item'+value).css('display','none');
   }
    else {
        $.cookie("prods", "" , { expires: 1 , path: '' });
        $('#countity').text(arr.length); 
        $('#item'+value).css('display','none');
        window.location.href="index.php?page=cart"; 
       }
}

function resetProductSearchTextmsg(){ 
  $('#searchTerms').val('');
 $('#searchTerms').css('color','#000');
}





function resetProductSearchTextmsg3(){
 if(('#searchTerms').val('')){
 $('#searchTerms').val('שם המוצר /SKU');
 $('#searchTerms').css('color','#D2D2C9');
 }
}

//auto resizer by value
// add onload="autoResize(this,'your value + px/%','align');"
function assignResize(){ 

 var array=getElemetsByClassName('fit','img');

 var i=0;

 var pattern = new RegExp('autoCenter');

 while (array[i]!=undefined)

     {  

	  array[i].onload=function(e){if (e == null) e = window.event; 

                                  var target= e.target != null ? e.target : e.srcElement;   

								  if (pattern.test(target.className))

								  {  

                   autoResize(target,'100%',true); 

								  } else {autoResize(target,'100%');}

							     }  

	 if (pattern.test(array[i].className))

	    { autoResize(array[i],'100%',true); 

		} else {autoResize(array[i],'100%');}

	  i++;

     }
   if (window.location.href.length<35)
	 window.setTimeout("assignResize();",100);

}


function autoResize(obj,value,autoCenter){
 window.setTimeout(function(){autoResizeElf(obj,value,autoCenter);},200);
 if (window.location.href.length<35)
 {obj.className=obj.className.replace(/fit/g,"");
 obj.className=obj.className.replace(/autoCenter/g,"");
 obj.className+="fit autoCenter";}
 if (obj.height>=obj.width) {obj.style.width='auto';obj.style.height=value;
                             var target_parent=obj.parentNode;
							  if ((target_parent.tagName=="a")||(target_parent.tagName=="A"))
							  {target_parent=target_parent.parentNode;
							  }
                             if (autoCenter!=undefined) {obj.style.marginTop=="auto";obj.style.marginBottom="auto";
								                         obj.style.marginRight=(target_parent.offsetWidth-obj.width)/2+"px";
							                             obj.style.marginLeft=(target_parent.offsetWidth-obj.width)/2+"px";}
                            }
 else {obj.style.height='auto';obj.style.width=value;
       var target_parent=obj.parentNode;
							  if ((target_parent.tagName=="a")||(target_parent.tagName=="A"))
							  {target_parent=target_parent.parentNode;
							  }
      if (autoCenter!=undefined) {obj.style.marginRight="auto";obj.style.marginLeft="auto";
		                          obj.style.marginTop=(target_parent.offsetHeight-obj.height)/2+"px";
	                              obj.style.marginBottom=(target_parent.offsetHeight-obj.height)/2+"px";}
      }
}

function autoResizeElf(obj,value,autoCenter){

 if (window.location.href.length<35)
 {obj.className=obj.className.replace(/fit/g,"");
 obj.className=obj.className.replace(/autoCenter/g,"");
 obj.className+="fit autoCenter";}
 if (obj.height>=obj.width) {obj.style.width='auto';obj.style.height=value;
                             var target_parent=obj.parentNode;
							  if ((target_parent.tagName=="a")||(target_parent.tagName=="A"))
							  {target_parent=target_parent.parentNode;
							  }
                             if (autoCenter!=undefined) {obj.style.marginTop=="auto";obj.style.marginBottom="auto";
								                         obj.style.marginRight=(target_parent.offsetWidth-obj.width)/2+"px";
							                             obj.style.marginLeft=(target_parent.offsetWidth-obj.width)/2+"px";}
                            }
 else {obj.style.height='auto';obj.style.width=value;
       var target_parent=obj.parentNode;
							  if ((target_parent.tagName=="a")||(target_parent.tagName=="A"))
							  {target_parent=target_parent.parentNode;
							  }
      if (autoCenter!=undefined) {obj.style.marginRight="auto";obj.style.marginLeft="auto";
		                          obj.style.marginTop=(target_parent.offsetHeight-obj.height)/2+"px";
	                              obj.style.marginBottom=(target_parent.offsetHeight-obj.height)/2+"px";}
      }
}

function autoResizeConfirm(obj,value,autoCenter){
 if (obj.height>=obj.width) {obj.style.width='auto';obj.style.height=value;
                             var target_parent=obj.parentNode;
							  if ((target_parent.tagName=="a")||(target_parent.tagName=="A"))
							  {target_parent=target_parent.parentNode;
							  }
                             if (autoCenter!=undefined) {obj.style.marginTop=="auto";obj.style.marginBottom="auto";
								                         obj.style.marginRight=(target_parent.offsetWidth-obj.width)/2+"px";
							                             obj.style.marginLeft=(target_parent.offsetWidth-obj.width)/2+"px";}
                            }
 else {obj.style.height='auto';obj.style.width=value;
       var target_parent=obj.parentNode;
							  if ((target_parent.tagName=="a")||(target_parent.tagName=="A"))
							  {target_parent=target_parent.parentNode;
							  }
      if (autoCenter!=undefined) {obj.style.marginRight="auto";obj.style.marginLeft="auto";
		                          obj.style.marginTop=(target_parent.offsetHeight-obj.height)/2+"px";
	                              obj.style.marginBottom=(target_parent.offsetHeight-obj.height)/2+"px";}
      }
      
}

 function getElemetsByClassName(searchClass,tag)

{      

    var classElements = new Array();

    var pattern = new RegExp(searchClass);

	var j=0;

   for(var i in  document.getElementsByTagName(tag))

   {         

    if (document.getElementsByTagName(tag)[i]!=undefined)

         if ( pattern.test(document.getElementsByTagName(tag)[i].className) ) {

             classElements[j] = document.getElementsByTagName(tag)[i];

             j++;

         }

    }

    return classElements;

};

function test_oversize(obj)
{
 if (parseFloat(obj.width)>500) {obj.style.width="500px";}



}
