﻿function initActiveIconsFromCookie()
{
     var types = new Array("ctlg","wtch","cmpr");
     for (var i=0; i< types.length; i++)
     {
          var type = types[i];
          var data = getCookieData(type);
          if(data)
          {
      
            $(data).each(function(){
                var obj = $("#" + type + "_" + this);
                if(obj)
                    obj.addClass("active");
            });
          //add current num to the label
            if(type!="wtch")
            {
                $("#lbl_" + type).text(data.length-1);
                if(type=="ctlg" && data.length>1)
                {
                 show_add_to_ecatalog_tooltip=false; //אם קיימים מוצרים בקטלוג לנטרל את הטולטיפ
                }
            }
            
          }
          
    }
}

    function addEventToActive(item,activewidth, unactivewidth)
    {
        var alt = item.find('div.remove');
        //alt.css("width","auto");
        //var offset_width=alt.width()+8;
        var productid=item.attr("id"); //product id
        var type = item.attr("name"); // type - catalog wathch or compare

       //item.unbind('mouseenter').unbind('mouseleave').unbind('click');
        item.unbind().mouseenter(function(){$(this).addClass('over');
            if(alt)
            {
                alt.css('width',0).animate({ opacity: 1, width: unactivewidth+'px'}, 200 );
            }
        })
        .mouseleave(function(){$(this).removeClass('over');
            if(alt)
            {
                alt.stop().css({'opacity':0,'display':'none','width':'0px'});
            }
        })
        .click(function(){
            //send the data via ajax;
            AddRemoveUserProduct(productid,false,type , true);
            if(alt)
            {
                alt.stop().css({'opacity':0,'display':'none','width':'0px'});
            } 
            $(this).removeClass('over').removeClass('active');
            addEventToUnActive($(this),activewidth, unactivewidth );
           
        })
   
    }
    function addEventToUnActive(item,activewidth, unactivewidth)
    {
        var alt = item.find('div.add');
        //alt.css("width","auto");
        //var offset_width=alt.width()+8;
        var productid=item.attr("id"); //product id
        var type = item.attr("name"); // type - catalog wathch or compare
        item.unbind().mouseenter(function(){$(this).addClass('over');
            if(alt)
            {
                alt.css('width',0).animate({ opacity: 1, width: activewidth+'px'}, 200 );
            }
        })
        .mouseleave(function(){$(this).removeClass('over');
            if(alt)
            {
                alt.stop().css({'opacity':0,'display':'none','width':'0px'});
            }
        })
        .click(function(){
           AddRemoveUserProduct(productid,true,type,false );
           if(alt)
           {
                alt.stop().css({'opacity':0,'display':'none','width':'0px'});
           } 
            $(this).removeClass('over').addClass('active');
            addEventToActive($(this), activewidth, unactivewidth );
          
        })
    }
    
    function ShowBigImagesLightBox()
    {
    
          $(".img-link").click( function()
          {
            var url = $(this).attr("href");
            var title =  $(this).attr("title");
            var _imgPreloader = new Image();
            _imgPreloader.onload = function(){
          	    _imgPreloader.onload = null;
          	    var _width;
          	    var _height;
          	    if(_imgPreloader.width<770)
          	         _width = _imgPreloader.width + 30;
          	    else
          	        _width = 770 + 30;
          	    if(_imgPreloader.height<500)
          	      _height = _imgPreloader.height + 70;
          	     else
          	      _height = 500 + 70;
          	    $("#LightBoxImage").attr("src", url);
          	    $("#generic_lightbox_title").text(title);
               lb= tb_show(title,"#TB_inline?height="+ _height +"+ &width=" + _width+"&inlineId=generic_lightbox&modal=true",false);
          	}
            _imgPreloader.src=url;
           return false;//cancel the href event
          });
          $("#closelightBox").click(function() { lb = closeLightBox(); return false; });
    
    }
    
    
//types of type "ctlg","wtch","cmpr"
function AddRemoveUserProduct(productId, isAdd,type,showBallon)
{
    productId = getProductId(productId);
    if(isAdd)
    {
         setCookieData(type, productId,showBallon);
    }
    else
    {
      removeCookieData(type, productId);
    }
}




function getCookieData(type)
{
    var cookieName="COOKIE_" + type;
    if($.cookie(cookieName))  //cookie exist
    {
        var str = $.cookie(cookieName)
        var temp = new Array();
        temp = str.split(',');
        return temp;
    }
  return null;
}
function setCookieData(type,productid)
{
    var data = getCookieData(type);
    if(data)
    {
       var isExist = checkIfElementExists(data,productid)
       if(isExist) return;
       else //item not existing yet than add this item
       {
         var cookieName="COOKIE_" + type; 
         $.cookie(cookieName, data.toString() + productid +"," , {});
         if(type!="wtch")
         {
           $("#lbl_" + type).text(($("#lbl_" + type).text()*1 )+1);
           if(type =="ctlg" && show_add_to_ecatalog_tooltip){
                ShowBallonItemAddedToCatalog(); //show the ballon of item added to catalog
                show_add_to_ecatalog_tooltip=false;
           }
         }
       }
    }
    else //no cookie yet 
    {
         var cookieName="COOKIE_" + type;
         $.cookie(cookieName, productid +"," , {  });
        if(type!="wtch")
         {
           $("#lbl_" + type).text(($("#lbl_" + type).text()*1 )+1);
           if(type =="ctlg" && show_add_to_ecatalog_tooltip){
                ShowBallonItemAddedToCatalog(); //show the ballon of item added to catalog
                show_add_to_ecatalog_tooltip=false;
            }
         }
    }
}
function setCookieDataArray(type,strArray)
{
     var cookieName="COOKIE_" + type; 
     $.cookie(cookieName, strArray , { });
}
function removeCookieData(type, productid) {
    var data = getCookieData(type);
    if (data) {
        if (productid != null) {
            var isExist = checkIfElementExists(data, productid)
            if (isExist) {
                var str = data.toString();
                str = str.replace(productid + ",", "");
                if (str.length == 0) str = null;
                setCookieDataArray(type, str);
                if (type != "wtch") {
                    $("#lbl_" + type).text(($("#lbl_" + type).text() * 1) - 1);
                }
            }
            else //item not existing yet than add this item
            {
            }
        }
        else {
            // clear cookie
            setCookieDataArray(type, null);
        }
    }
    else //no cookie yet 
    {
    }

}

function getProductId(productid)
{
    return productid.toString().substr(5);
}

function checkIfElementExists(array, value)
{
        for (var i=0; i<array.length; i++)
        {
            if (array[i]*1 == value*1) return true;
        }
    return false;
}





var showballonTimer;

function ShowBallonItemAddedToCatalog()
{
   $("#add_to_catalog_ballon").show(0);
   $.scrollTo( 0, 800, {queue:true} );
   clearTimeout ( showballonTimer );
   showballonTimer= setTimeout(function() { $("#add_to_catalog_ballon").hide(0); }, 4000);
}


function AddAllItemsToCatalogEvent()
{
$("#ctlg_all").click(function(){
  $(".catlaog-icon").each(function(i,item){
        if(!$(item).hasClass("active"))
        {
            $(item).click();
        }
    });
    return false;
  });
  
}




function initProductOver()
{
   $('.product-item').mouseenter(function(){$(this).addClass('product-over');}).mouseleave(function(){$(this).removeClass('product-over');});
}