// Return new array with duplicate values removed
if(![].unique) {
Array.prototype.unique =
  function() {
    var a = []; var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};


J.wishlist = {

    COOKIE : 'jacksons_wishlist_cookie',
    
    init : function () {
        $('.wishlist-product').hover(function(){
            $(this).find('a.close').show();
        }, function(){
            $(this).find('a.close').hide();
        });
    },    
    
    // returns the wishlist array
    _get_list : function() {
        return $.cookie(J.wishlist.COOKIE) ? $.cookie(J.wishlist.COOKIE).split(',') : [];
    },
    
    // saves the wishlist array
    // $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
    _save_list : function(list) {
        $.cookie(J.wishlist.COOKIE, list, { expires: 6000, path: '/' }); // save the cookie for 6000 days.
    },
    
    _add_to_list : function(item, is_authenticated, url, callback) {
        if(is_authenticated){
            $.post(url, {product_pk:item}, function(data){
                if(DEBUG) { console.log(data) }
                callback(data.success);
            }, 'json');            
        }else{
            // get the wishlist as an array
            var list = J.wishlist._get_list();
            // add our new item. Always save strings in the cookie
            list.push(item.toString());
            // make sure our list only has unique values
            J.wishlist._save_list(list.unique());
            callback(true);
        }
    },
    
    _remove_from_list : function(item, is_authenticated, url, callback) {
        if(is_authenticated){
            $.post(url, {product_pk:item}, function(data){
                if(DEBUG) { console.log(data) }
                callback(data.success);
            }, 'json');
        }else{
            var list = J.wishlist._get_list();
            var index = -1;
            for (var i = 0; i < list.length; i++ ){
                if(list[i].toString()==item.toString()){
                    // found the item in the list
                    list.remove(index); break;
                }
            }
            J.wishlist._save_list(list);
            callback(true);
        }
    },
    
    // adds a product to an anonomous users wishlist.
    addProduct : function(anchor, product_pk, calling_page, is_authenticated) {
        J.wishlist._add_to_list(product_pk, is_authenticated, $(anchor).attr('href'), function(success){
            if(success){
                switch(calling_page){
                    case 'detail' : 
                        J.wishlist.detailPageChangeWishlistLink(false);
                    break;
                }
            }else{
                // swap the hidden flag around
            }
        });
    },

    //J.wishlist.removeProduct(this, 3111, 'list', true); return false;
    removeProduct : function(anchor, product_pk, calling_page, is_authenticated) {
        J.wishlist._remove_from_list(product_pk, is_authenticated, $(anchor).attr('href'), function(success){
            if(success){
                switch(calling_page){
                    case 'list':
                        $('#wishlist-product-pk-'+product_pk).fadeOut(500);
                    break;
                    case 'detail' : 
                        J.wishlist.detailPageChangeWishlistLink(true);
                    break;
                }
            }else{
                // swap the hidden flag around
            }
        });
    },
    
    detailPageChangeWishlistLink : function(show_add) {
        if(show_add){
            $('#wishlist-add').removeClass('hide');
            $('#wishlist-remove').addClass('hide');
        }else{
            $('#wishlist-add').addClass('hide');
            $('#wishlist-remove').removeClass('hide');
        }
    }    
    
    
}
