jQuery.fn.extend({

    wishlist: function(o) {
        return this.each(function() {
            new jQuery.wishlist(this, o);
        });
    }

});


jQuery.extend({

  wishlist: function(e, o) {

    var publ = this;

    publ.add = function(data) { return priv.addItem(data); };

    var priv = {

      cFg: {
        url: null,
        onRemoveItem: o.onRemoveItem,
        onMoveItem: o.onMoveItem
      },

      list: e,
      scope: null,
      size: 0,


      prepare: function()
      {

        var scope = jQuery(priv.list).wrap('<div class="wishList-scope"></div>').parent().get(0);
        priv.scope = scope;

        priv.size = jQuery("li", priv.list).size();

        if (priv.size > 0) {
          var idx = 1;
          jQuery("li", priv.list).each(function() { priv.initItem(this, idx++); });
        }

      },

      initItem: function(item)
      {

        var pId  = $('.wlPid',item).val();

        // set row id = product id
        $(item).attr('id','wl'+pId);

        // bind actions wishlist bt
        $('a.moveToCart',item).bind('click',function(){
          priv.moveToCart(pId);
          return false;
        });


        // bind actions remove bt
        $('a.wlRemove',item).bind('click',function(){
          priv.removeItem(pId);
          return false;
        });


        $(item).hover(function(){
          $(item).addClass("jHover");
        },function(){
          $(item).removeClass("jHover");
        });

      },

      format: function()
      {

       if( jQuery("li", priv.list).size() > 1 && $('.ctxDialog',priv.list)  ) {
         $('.ctxDialog',priv.list).remove();
       }

       $('li:first', priv.list).css("border-top","1px solid #dbdbdb");
       $('li',priv.list).removeClass('even').removeClass('odd');
       $('li:even',priv.list).addClass('even');
       $('li:odd',priv.list).addClass('odd');
      },


      handler: function(handler,arg) {

        if (priv.cFg[handler] == undefined) {
          return;
        }

        var handler = priv.cFg[handler] ;

        if (typeof handler != 'function') {
          return;
        }

        handler(this,arg);

      },



      removeItem: function(idx)
      {

        var url = 'services.php?act=removeWl&pId='+idx;

        $.get(url, function(data) {

            if( data != 1 ) {
              alert("Une erreur c'est produite");
              return false;
            }

            $('#wl'+idx).fadeOut(function(){
              $(this).remove();
              priv.format();
            });

          });

      },



      moveToCart: function(idx)
      {

        var productDatas = {
          item: $('#wl'+idx),
          id: idx,
          name: $('#wl'+idx+' .name a').html(),
          price: $('#wl'+idx+' .price').html()
        };

        var url = 'services.php?act=moveToCart&pId='+idx;

          $.get(url, function(data) {

            if( data != 1 ) {
              alert("Une erreur c'est produite");
              return false;
            }

            $(productDatas.item).DropOutUp("fast",function(){
              $(this).remove();
              priv.format();
            });


            if( priv.cFg.onMoveItem) {
              priv.handler("onMoveItem",productDatas);
            }

            priv.format();
          });

      },


      addItem: function(data)
      {

        var prod = {
          id: data.id,
          name: data.name,
          price: data.price
        };


        var itemHtml =
        '<li class="wishListRow" style="display:none;" >'+
        '<p class="name">'+
        '<input type="hidden" class="wlPid" name="wlPid" value="'+data.id+'" />'+
        '<a href=product_info.php?products_id='+prod.id+'>'+prod.name+'</a></p>'+
        '<p class="price">'+prod.price+'</p>'+
        '<p class="wlActions">'+
        '<a class="moveToCart" href="shopping_cart.php?action=moveToCart&pId='+prod.id+'">'+
        '<span>ajouter au panier</span></a>'+
        '<a class="wlRemove" href="shopping_cart.php?action=remove_product&products_id='+prod.id+'">'+
        '<span>Supprimer</span></a>'+
        '</p>'+
        '</li>';

        var newItem = $(itemHtml).appendTo(priv.list).DropInDown();

        //var newItem = $(priv.list).find('li:last').get(0);

        priv.initItem(newItem);
        priv.format();

      }


    };

    // Initialize the shoppingCart
    priv.prepare(e, o);
    priv.format();

    $.fn.WishlistAddItem = function(data) {
  		return publ.add(data);
  	};

  }

});