if (!window.iCart)
	var iCart = new Object();

iCart.Methods = {
  title: 'Add to Cart',
  width: 500,
  overlay: false,
  overlayClose: false,
  autoFocusing: true,
  confirmDelete: 'Are you sure you would like to remove this item from the shopping cart?',
  
  init: function(options) {
	Object.extend(this, options || {});
  },
  
  updateLinks: function () {
    var links = $$('a');
    links.each( function (link) {
        if (link.href.match(/\/checkout\/cart\/delete\//)){
        	$(link).writeAttribute('onclick', 'iCart.updateCart(\''+link.href+'\', $(this)); return false;');
        	$(link).writeAttribute('href', 'javascript:iCart.updateCart("'+link.href+'");');
$(link).writeAttribute('href', '#');
        }
    });
    if (typeof productAddToCartForm != 'undefined'){
        productAddToCartForm.submit = function(){
            if (this.validator.validate()) {
                    iCart.submitForm(this.form, 'post');
            }
        }.bind(productAddToCartForm);
    }
  },
  
  updateCart: function (url, el){
	if (confirm(this.confirmDelete)){
		try {
		  	if (el){
		  		row = $(el).up('tr') ? $(el).up('tr') : $(el).up('li');
		  		$(row).fade({ duration: 0.3, from: 1, to: 0.2 });
		  	}
		} catch(e){}
		url = url.replace('/cart', '/icart');
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {
			var response = new String(transport.responseText);
			this._eval(response);
			this.updateLinks();
		  }.bind(this)
		});
	}
  },
  
  _eval: function (scripts){
	  try {	
			if(scripts != ''){
				var script = '';
				scripts = scripts.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(){ if (scripts !== null) script += arguments[1] + '\n'; return ''; });
				if (script) (window.execScript) ? window.execScript(script) : window.setTimeout(script, 0);
			}
			return false;
		}
		catch(e)
		{
			alert(e);
		}
  },
  
  setLocation: function(url){
	  if (url.match(/\/checkout\/i?cart\/add\//)){
		  url = url.replace('/cart', '/icart');
		  this.open(url);
	  }
	  else window.location.href = url;
  },
  
  setPLocation: function(url, setFocus){
		if (url.match(/\/checkout\/i?cart\/add\//)){
			url = url.replace('/cart', '/icart');
			this.open(url);
		}
		else{
			if(setFocus) {
				window.opener.focus();
			}
			window.opener.location.href = url;
		}
  },
  
  submitForm: function(form, method){
	  this.open(form.action.replace('/cart', '/icart'), {params: form.serialize(), method: method});
  },
  
  open: function(url, params){
	  /*if (this.pe)
		  this.pe.stop();*/
	  Modalbox.setOptions({title: this.title, width: this.width, overlay: this.overlay, overlayClose: this.overlayClose, autoFocusing: this.autoFocusing});
	  Modalbox.show(url, params);
  },
  
  close: function(){
	  //this.pe.stop();
	window.location = window.location;
	  Modalbox.hide({transitions: true});
  },
  
  autoClose: function(seconds){
	  /*if (this.pe)
		  this.pe.stop();
	  this.pe = new PeriodicalExecuter(function(pe) {
		pe.stop();
	  	this.close();
	  }.bind(this), seconds);*/
	 Modalbox.autoHide(seconds, {transitions: true});
  }
};

Object.extend(iCart, iCart.Methods);

setLocation = function (url){
    iCart.setLocation(url);
};

setPLocation = function (url, setFocus){
    iCart.setPLocation(url, setFocus);
};

