// Caddy managing
 ista = "Ajouter cette image &agrave; mon caddie";
 istb = "Enlever cette image du caddie";
 iscook = "imgcaddie";
 var sellbol = false;

 function sellswitch(obj, itemid) {
  if (!isItemCaddie(itemid)) {
   $(obj).html(ista);
   sellbol = false;
  }
  else {
   $(obj).html(istb);
   sellbol = true;
  }
 }

 $(document).ready(function() {
   // Img protection
   $("#img").each(function() {
    this.oncontextmenu = function() { return false; };
    $(this).mouseup(function(e) {
     if( e.button == 2 ) {
      alert("Attention, cette image, qui est soumise aux droits d\'auteurs, a une interdiction de copie.");
      return false;
     }
    });
   });
   // Caddy
   $("#sellingimg").each(function() {
     sellswitch($(this).find("span"), curritem);
     $(this).find("span").click(function() {
      $(this).effect("highlight", {color: '#0DB3FF'}, 500);
      if (!sellbol) {
       addItemCaddie(curritem);
       sellswitch($(this), curritem);
      }
      else {
       remItemCaddie(curritem);
       sellswitch($(this), curritem);
      }
     });
   });
 });
 
 function isItemCaddie(itemid) {
  var items = $.cookie(iscook);
  if (items == null) {
   return false;
  }
  else {
   var itemstab = items.split(',');
   for (i=0; i < itemstab.length; i++) {
    if (itemstab[i] == itemid) {
     return true;
    }
   }
   return false;
  }
 }

 function addItemCaddie(itemid) {
  var cookval = $.cookie(iscook)
  var newcookval = itemid+',';
  if (cookval == null) {
   // Create cookie
   $.cookie(iscook, newcookval, { expires: 7 } );
  }
  else {
   // Append if exists
   $.cookie(iscook, newcookval+cookval);
  }
  return true;
 }

 function remItemCaddie(itemid) {
  if (isItemCaddie(itemid) == false) {
   return true;
  }
  var cookval = $.cookie(iscook)
  var matchtxt = itemid+',';
  var re = new RegExp(matchtxt, 'gi');
  var newcookval = cookval.replace(re, '');
  $.cookie(iscook, newcookval);
  return true;
 }

