/*
  Keeps track of WW products that have been viewed in a cookie list. If the product is viewed again in another context it is removed from the list.
  If the product is in the cart, it can not be removed from the cookie list. 
  Products in the cookie list for WW do not affect the same products when viewed in other contexts. 

  -- addtion two types, recently viewed and cross sells cookie data is now prod => {type => location} 
*/
dojo.require("generic.cookie"); // probably not needed but wanted to be clear what this file is for, keeping track of a CM cookie.


function addCmProdww(prod,label,location_flag) {
     var cookie = dojo.cookie("cmww.prods");
      console.log("prod is ", prod); 
      console.log("Type is ",label);
      console.log("Location is ",location_flag);
       

     if (cookie && cookie !== null) {
          var ww_prod_cache = dojo.fromJson(cookie);

           console.log("cookie has ", ww_prod_cache);  
           if( notInCookie(ww_prod_cache,prod) ){
                ww_prod_cache[prod] = {}; ww_prod_cache[prod][label] =  location_flag;
           }else{
                console.log("updating but already in cache",prod);
                ww_prod_cache[prod] = {}; ww_prod_cache[prod][label] =  location_flag;
           }
           console.log("new cookie has ",ww_prod_cache); 
           
           //ww_prod_cache.push(prod); 
           dojo.cookie.update("cmww.prods", dojo.toJson(ww_prod_cache), {path:"/",expires: 356});
          
     }else{
        aa = { }; aa[prod] = { }; 
        aa[prod][label] = location_flag; 
        dojo.cookie("cmww.prods",dojo.toJson(aa),{path:"/",expires: 356} );
     }

}

function notInCookie(cookieval,prod){

    if (cookieval && cookieval != null){
        for (i in cookieval){
            if (i == prod){
                console.log("notin exists ",prod);
                return false;
            }
        }
    }
    return true;
}

function InCookie(cookieval,prod){

    if (cookieval && cookieval != null){
        for (i in cookieval){
            if (i == prod){
                console.log("in exists ",prod);
                return true;
            }
        }
    }
    return false;
}
// return true if prod is added with success or exists in cookie
function checkCmProdww(prod,label,location_flag){
  
     if (typeof prod == "object"){
          prod = prod[0].match("[0-9]+");
          prod = prod[0];
         console.log("fixed prod is ",prod);
     }else{
           //prod = prod.match("[0-9]+");
     }
    
     var cookie = dojo.cookie("cmww.prods");

      if (label && label != null){   // null if loading a NORMAL product view, will reset flags for that product UNLESS it is in the cart already
          if (checkCartPagedata(prod) == true){ 
               console.log("ADDING ",prod,label,location_flag);
               addCmProdww(prod,label,location_flag);
          }
      
      }else{
           if (cookie && cookie !== null) {
               var ww_prod_cache = dojo.fromJson(cookie);

                 if ( InCookie(ww_prod_cache,prod) ){
                     removeCmProdww(prod);   // if it is in the cookie, remove it as we just viewed it as a reg product
                 }

           }
           //remove from cookie, unless it is in the cart..... 
      } 

}

function shopCheckww(prod, prod_attr){
     if (typeof prod_attr == 'undefined') { prod_attr = '-_--_--_--_-'; }
     var cookie = dojo.cookie("cmww.prods");

     if (cookie && cookie !== null) {
         var ww_prod_cache = dojo.fromJson(cookie);
         if ( InCookie(ww_prod_cache,prod) ){
              console.log(" in cookie done ",ww_prod_cache);
              for (var i in ww_prod_cache){
                   console.log("i is ",i);
                   if (i == prod){
                       attr = getAttrs(prod,ww_prod_cache, prod_attr); 
                       console.log("shopCheck returning ",attr);
                       return attr; 
                   }
              } 
      
         }
       
     }else{
         console.log("NO WW prod cookie yet");
     }
     
 return prod_attr;


}


function getAttrs(prod,ww_prod_cache,prod_attr){

    for (var i in ww_prod_cache){
        if (i == prod){
            if (typeof ww_prod_cache[i] == "object"){
                 for (var n in ww_prod_cache[i]){
                     retval = prod_attr + '-_-' + n;
                     if (ww_prod_cache[i][n]){
                         retval = retval + '-_-' + ww_prod_cache[i][n];
                     }
                     return retval;
                 }     
            }
        }
    }
}


// to set arg ww_att on links
function shopCheckwwTF(prod,url){
console.log("SHOP CHECK TF RUN");
     var cookie = dojo.cookie("cmww.prods");

     if (cookie && cookie !== null) {
         var ww_prod_cache = dojo.fromJson(cookie);
         if ( notInCookie(ww_prod_cache,prod) ){
            return url + '&ww_att=1';
         }
     }

    return url; 
}

function removeCmProdww(prod){
     var thisbs = checkCartPagedata(prod);
     console.log("cart check returned ",thisbs,prod);

    if (checkCartPagedata(prod)){ // If not in cart
      clearWw(prod); 
      console.log("REMOVE FROM COOKIE");
      

    }else{
      console.log("DON:T REMOVE in cart");
    }
}

function clearWw(prod){

    var cookie = dojo.cookie("cmww.prods");

     if (cookie && cookie !== null) {
         var ww_prod_cache = dojo.fromJson(cookie);
         if ( InCookie(ww_prod_cache,prod) ){
              for (var i in ww_prod_cache){
                 console.log("Checking ",i,prod);   
                 if (i == prod){
                      //ww_prod_cache.splice(i,1);
                      ww_prod_cache[i] = null;
                 }
              }
        
            dojo.cookie.update("cmww.prods", dojo.toJson(ww_prod_cache), {path:"/",expires: 356}); 
         }
     }

}

// return true if prod is in cart
function checkCartPagedata(prod) {
 
    var retval=true; 
    var cartItems = page_data.initial_rpc['Cart.contents'].contents; 
    console.log("CART ITEMS ", cartItems);
   
    for (var i in cartItems){
         console.log(cartItems[i].obj['product.product_id']); 
         if (prod == cartItems[i].obj['product.product_id']){
              console.log("DON:T ADD or change, in CART",prod);
              retval=0;
         } 
    }
      

    cartItems = page_data.cm_contents;
    console.log("CART ITEMS from page_data.contents ",cartItems);
    for (var i in cartItems){
        if (prod == cartItems[i].prod_id ){
             console.log("DON:T ADD or change, in CART",prod);
             retval=0;
        }
    }
 
   return retval;  

}

//only gets cleared on checkout complete
function checkoutCompleted() {

     var cookie = dojo.cookie("cmww.prods");
      dojo.cookie.update("cmww.prods", null, {path:"/",expires: 0});

}

