var popupwin;
function popup_image(image, width, height) {
  popup('', width, height, false);
  popupwin.document.write('<html><body style="margin: 0px; overflow: hidden;">');
  popupwin.document.write('<a href="#" onclick="window.close()">');
  popupwin.document.write('<img border=0 width='+width+' height='+height+' src="'+image+'" />');
  popupwin.document.write('</a>');
  popupwin.document.write('</body></html>');
  return false;
}

function popup(src, width, height, scrollbars) {
  if(popupwin)
    popupwin.close();
  scrollbars=scrollbars ? 'yes' : 'no';
  popupwin=window.open(src, 'popupwin', 'scrollbars='+scrollbars+',width='+width+',height='+height);
  popupwin.focus();
  return false;
}

(function($) {
  $.fn.extend({
    simplecart: function(image, o) {
      o=$.extend({
        addmessage: 'Added to cart!',
        delmessage: 'Removed from cart.'
      }, o);
      if(image) {
        function get_cookie() {
          var cookie='';
          var cookies=document.cookie.split('; ');
          for(var i in cookies) {
            var cookarr=cookies[i].split('=');
            if(unescape(cookarr[0])=='simplecart') {
              if(cookarr[1])
                cookie=unescape(cookarr[1]);
              break;
            }
          }
          if(cookie.length)
            cookie=cookie.split('\n');
          else
            cookie=new Array;
          return cookie;
        }
        this.each(function() {
            var cookie=get_cookie();
            var found=false;
            for(var i in cookie)
              if(cookie[i]==image) {
                $(this).attr('checked', true);
                found=true;
                break;
              }
            $(this).attr('checked', found);
            $(this).click(function() {
                var cookie=get_cookie();
                for(var i in cookie)
                  if(cookie[i]==image) {
                    cookie.splice(i, 1);
                    break;
                  }
                if($(this).attr('checked')) {
                  cookie.push(image);
                  $(this).next().text(' '+o.addmessage);
                } else
                  $(this).next().text(' '+o.delmessage);
                document.cookie='simplecart='+escape(cookie.join("\n"))+';path=/';
              });
          });
      }
    }
  });
})(jQuery);

