/**
* Cookie functions
*/
function cookSetCookie(name, value)
{
    $.cookie(name, value, { expires: 365, path: '/'});//lg     
}

/** Main cookie function checks for cart wdgt and sets appropriate info etc **/
function cook()
{
    crtWdgtId = "#crtSummary";
    
    if ($(crtWdgtId))
    {
        cartItms = $.cookie('CART_ITEMS');
        if (cartItms==null || cartItms == "")
        {
            cartItms = 0;
        }
        /*
        if (cartItms==0)
            $("#crtSummary").html("&nbsp;");
        else  */
        if (cartItms!=1)
            txt = " items in your cart";
        else
            txt = " item in your cart";
        $("#crtSummary").html(cartItms+txt);//lg
        //if (cartItms>0) 
          //  $("#crtSummary").css({'font-weight' : 'bold'});     


        //hiding an element
        //$("#crtSummary").css({'display' : 'none'});        
    }

   //Shopping cart widgt functions
    $("#shoppingCartWdgt").click(function(){
        document.location.href='/_src/cart_viewCart.php5';
    });

   //checks for last viewed wdgt and displays it
   cookDisplayLastViewed();
    
    
}

function cookProductDetail(gnsId, imgLnk, prodName, prodDetailLnk)
{
    // cooking time
   //standard cookie functions
   cook();
   //add to last viewed cookie
   cookProductDetail_addToLastViewed(gnsId, imgLnk, prodName, prodDetailLnk);
}

function cookProductDetail_addToLastViewed(gnsId, imgLnk, prodName, prodDetailLnk)
{   /** Sets browsing history cookie, this function will either create a new cookiew or append to the existing one **/

    /** Preventing duplicates **/
    name = "BROWSINGHIST_GNSLIST";
    value = $.cookie(name);
    if (value!=null)
    {
        test = "DUMMY"+value+"DUMMY";
        arr = test.split("|"+gnsId+"|");
        if (arr.length>1) //if the value is found the array will have 2 or more elements
            return; //already in list - exit function - do not add to list again
        else
            value += "|"+gnsId+"|";
    }
    else
        value += "|"+gnsId+"|";
           
    cookSetCookie(name, value);    
    /** End Preventing duplicates **/

    name = "BROWSINGHIST";
    value = $.cookie(name);
    if (value==null)
        value = "";
    else
        value += "||";
    
    value += imgLnk+"|"+prodName+"|"+prodDetailLnk; // SMALL IMAGE SRC | PRODUCT NAME | DETAIL PG LINK
    ////value += "/images/store/products/BB005DHT/s/BB005DHT_s_1.jpg|DANDRUFF HAIR TONIC|/products/bodyandbath/haircare/styling/dandruffhairtonic-bb005dht.html"; // SMALL IMAGE SRC | PRODUCT NAME | DETAIL PG LINK
    //reset cookie
    cookSetCookie(name, value);
}


function cookDisplayLastViewed()
{   /** Sets browsing history cookie, this function will either create a new cookiew or append to the existing one **/
    lastViewedWdgtId = "#browsingHistory";
    if ($(lastViewedWdgtId))
    {
        $html = "";
        name = "BROWSINGHIST";
        value = $.cookie(name);
        if (value != null)
        {
            arr = value.split("||");
            cnt=1;
            for(i = arr.length-1; i > 0 && cnt<=5; i--)
            {
                cnt++;
                info = arr[i].split("|");
                prodDetailLnk = info[2];
                imgLnk = info[0];
                prodName = info[1];
                
                $htmlRow = cookHtmlLastViewedRow(prodDetailLnk, imgLnk, prodName);        
                $html += $htmlRow;
                ////alert($htmlRow);
            }            
        }
        
        /** if no itms exist then do not display wdgt else add items to widgt and display**/
        if ($html=="")
        {
            $("#browsingHistory").css({'display' : 'none'});     
        }
        else
        {
            $("div#browsingHistory div#items").append($html);
            if (arr.length <= 5)
            {
                $("div#browsingHistory a.footer").css({'color' : 'white'});
            }
            $("#browsingHistory").css({'display' : 'block'});     
        }
        
    }
    
}


function cookHtmlLastViewedRow(prodDetailLnk, imgLnk, prodName)
{
    //html = "<dl><dd class=\"img\"><a href=\"%PRODDETAILLNK%\"><img class=\"s\" src=\"%IMGLINK%\"></a></dd><dt><a href=\"%PRODDETAILLNK%\">%GNS_TXT_NAME%</a></dt></dl>";
    html = "<dl><dd class=\"im\"><a href=\"%PRODDETAILLNK%\"><img class=\"thb\" src=\"%IMGLINK%\"></a><br><a href=\"%PRODDETAILLNK%\">%GNS_TXT_NAME%</a></dd></dl>";
    html = "<a href=\"%PRODDETAILLNK%\"><img class=\"thb\" src=\"%IMGLINK%\"></a><br><a href=\"%PRODDETAILLNK%\">%GNS_TXT_NAME%</a>";
    html = "<div class=\"lastViewed\"><a href=\"%PRODDETAILLNK%\"><img class=\"ss\" src=\"%IMGLINK%\"></a><a class=\"name\" href=\"%PRODDETAILLNK%\">%GNS_TXT_NAME%</a></div>";
    html = html.replace(/\%PRODDETAILLNK\%/g, prodDetailLnk);
    html = html.replace(/\%IMGLINK\%/g, imgLnk);
    html = html.replace(/\%GNS_TXT_NAME\%/g, prodName);
    return html;
}


