 
   /** * %%INFO 2009-01-29 15:48:25+0100 rgarcia%%
 * @author Ruben Garcia
 * @usage CartWidget(Object options)
 * @example
 * <script type="text/javascript">
 * // <![CDATA[
var cW = new CartWidget([options]);
 * // ]]>
 * </script>
 */

//Needed methods for string manipulation
if(typeof 'x'.startsWith != 'function'){
	String.prototype.startsWith=function(pattern){return this.indexOf(pattern)===0;};
}
if(typeof [].inArray != 'function') {
	Array.prototype.inArray = function(value) {
		var i;
		for (i=0; i < this.length; i++) {
			if (this[i] === value) {
				return true;
			}
		}
		return false;
	};
}
CartWidget = Class.create();
CartWidget.prototype = {
	initialize: function(options) {
		this.options = Object.extend({
			oidPrefix:               'oid_',
			oidsPrefix:              'oids_',
			oidsSeperator:           '_',
			buttonClassname:         'cart',
			enabledClassname:        'incart',
			disabledClassname:       'off',
			clickableEntity:         'img',
			actionEvent:             'click',
			swapImage:               true,
			swapImageUrl:            'img/icons/icon_cart-on.gif',
			swapBackImageUrl:        'img/icons/icon_cart.gif',
			enabledImageUrl:         'img/icons/icon_cart_enabled.gif',
			enabledSwapImageUrl:     'img/icons/icon_cart_enabled-on.gif',
			disabledImageUrl:        'img/icons/icon_cart_disabled.gif',
			onClick:                 null,
			updateEvent:             true,
			updateDiv:               'cartcontainer',
			updateUrl:               'ajax/_ajaxShoppingCart.html',
			updateSuccessEvent:      true,
			updateSuccessHandler:    null,
			observedClassName:       'ctw'
		}, options || {});

		this.eventHandler = this.onEvent.bindAsEventListener(this);
		this.addHandler = this.addToCart.bindAsEventListener(this);
		this.removeHandler = this.removeFromCart.bindAsEventListener(this);
		if(this.options.updateSuccessEvent) {
			this.updateSuccessHandler = this.options.updateSuccessHandler || this.updateHandler.bindAsEventListener(this);
		}

		this.buttons = $$('.' + this.options.buttonClassname + ' ' + this.options.clickableEntity);
		this.oids = {};
		this.prepareElements();
	},
	prepareElements: function() {
		for(var i=0; i<this.buttons.length; i++) {
			var button = $(this.buttons[i]);
			if(button.hasClassName(this.options.observedClassName)) {
				continue;
			}
			button.addClassName(this.options.observedClassName);
			var classes = this.getParent(button).classNames().toArray();
			for(var j=0; j<classes.length; j++) {
				if(classes.inArray(this.options.disabledClassname)){
					continue;
				}
				if(classes[j].startsWith(this.options.oidPrefix) || classes[j].startsWith(this.options.oidsPrefix)) {
					var orderId;
					if(classes[j].startsWith(this.options.oidPrefix)) {
						orderId = classes[j].substr(this.options.oidPrefix.length);
					} else {
//						orderId = classes[j].substr(this.options.oidsPrefix.length).split(this.options.oidsSeperator);
						orderId = classes[j].substr(this.options.oidsPrefix.length);
					}
					if(classes.inArray(this.options.enabledClassname)){
						button.observe(this.options.actionEvent, this.removeHandler);
					} else if(!classes.inArray(this.options.disabledClassname)){
						button.observe(this.options.actionEvent, this.addHandler);
					}
					if(this.options.swapImage) {
						this.oids[orderId] = false;
						var imagePath = this.options.swapBackImageUrl;
						var hoverPath = this.options.swapImageUrl;
						var changeImageFullPath = this.options.enabledImageUrl;
						var hoverChangeImageFullPath = this.options.enabledSwapImageUrl;
						if(classes.inArray(this.options.enabledClassname)){
							this.oids[orderId] = true;
							imagePath = this.options.swapImageUrl;
							hoverPath = this.options.swapBackImageUrl;
							changeImageFullPath = this.options.enabledSwapImageUrl;
							hoverChangeImageFullPath = this.options.enabledImageUrl;
						}
						var iW = new ImageWidget(button, {
							imageFullPath: imagePath,
							hoverImageFullPath: hoverPath,
							changeImageFullPath: changeImageFullPath,
							changeHoverImageFullPath: hoverChangeImageFullPath
						});
					}
				}
			}
		}
	},
	getButtonOid: function(element) {
		var classes = this.getParent(element).classNames().toArray();
		for(var j=0; j<classes.length; j++) {
			if(classes[j].startsWith(this.options.oidPrefix)) {
				return classes[j].substr(this.options.oidPrefix.length);
			}
			if(classes[j].startsWith(this.options.oidsPrefix)) {
				var tmp = classes[j].substr(this.options.oidsPrefix.length);
				return tmp.split(this.options.oidsSeperator);
			}
		}
	},
	getParent: function(element) {
		return element.up().up();
	},
	onEvent: function(event) {
		var element = Event.element(event);
		if(this.options.onClick) {
			this.options.onClick(event);
		} else {
			var oid = this.getButtonOid(element);
			var action = 'a';
			if(typeof oid != 'string') {
				if(!this.oids[oid.join(this.options.oidsSeperator)]) {
					action = 'r';
				}
				oid = oid.join('&orderId=');
			}else {
				if(!this.oids[oid]) {
					action = 'r';
				}
			}
			var params = 'orderId=' + oid + '&a=' + action;
			/*
			if(this.options.updateEvent) {
				params += '&el=' + this.options.updateDiv;
			}
			*/
			var updaterOptions = {
					parameters: params,
					asynchronous: true,
					evalScripts: true
			};
			if(this.options.updateSuccessHandler) {
				updaterOptions.onSuccess = this.options.updateSuccessHandler;
			}
			var tmp = new Ajax.Updater(this.options.updateDiv, this.options.updateUrl, updaterOptions);
		}
		Event.stop(event);
	},
	addToCart: function(event) {
		var element = Event.element(event);
		var oid = this.getButtonOid(element);
		if(typeof oid != 'string') {
			oid = oid.join(this.options.oidsSeperator);
		}
		this.oids[oid] = true;
		//console.log('addToCart', oid)
		this.getParent(element).addClassName(this.options.enabledClassname);
		this.getParent(element).removeClassName(this.options.disabledClassname);
		element.stopObserving(this.options.actionEvent, this.addHandler);
		element.observe(this.options.actionEvent, this.removeHandler);
		this.onEvent(event);
	},
	removeFromCart: function(event) {
		var element = Event.element(event);
		var oid = this.getButtonOid(element);
		if(typeof oid != 'string') {
			oid = oid.join(this.options.oidsSeperator);
		}
		this.oids[oid] = false;
		//console.log('removeFromCart', oid)
		this.getParent(element).addClassName(this.options.disabledClassname);
		this.getParent(element).removeClassName(this.options.enabledClassname);
		element.stopObserving(this.options.actionEvent, this.removeHandler);
		element.observe(this.options.actionEvent, this.addHandler);
		this.onEvent(event);
	},
	updateHandler: function() {
		var tmp = new Effect.Pulsate(this.options.updateDiv, {pulses:3});
	}
};
//window.onerror = Fehlerbehandlung;

function Fehlerbehandlung (Nachricht, Datei, Zeile) {
  Fehler = "Fehlermeldung:\n" + Nachricht + "\n" + Datei + "\n" + Zeile;
  alert(Fehler);
  return true;
}