if(typeof CSD == "undefined" || ! CSD){
	var CSD = {};
}
var application;
CSD.Application = function(){};
CSD.Application.prototype = new JSoft.Application();
CSD.Application.prototype.init = function(){

	if(YAHOO.env.ua.ie == 6){ 
		var box = document.getElementById('cartBoxImage');
		YAHOO.util.Dom.addClass(box, "cartBoxIE6"); 
	}
	this.setupTables('table');
	
	this.fixSideBar();
	
	var flashObject = /flashObject=(\w+)/.exec(document.location.search);
	if(flashObject && flashObject.length == 2)
		this.flashObject(flashObject[1]);
	if(this.context && this.context.flashObject)
		this.flashObject(context.flashObject);
		
	var cartForm = document.getElementById('formCart');
	if(cartForm){
		this.cart = new CSD.Cart();
		this.cart.getItems();
	}
}

CSD.Application.prototype.flashObject = function(id){
	
	var object = document.getElementById(id);
	if(object){
		var animation = new YAHOO.util.ColorAnim(object, { backgroundColor: { from: '#ffffff', to: '#9bd1e0' } }, 1);
		animation.onComplete.subscribe(this.resetAlert, this);
		animation.animate();
	}
}

CSD.Application.prototype.resetAlert = function(e, me){
	// event handler for animation on.complete, this=animation
	this.attributes.backgroundColor.to = '#ffffff';
	this.attributes.backgroundColor.from = '#9bd1e0';
	this.onComplete.unsubscribe(me.resetAlert);
	this.animate();
}

CSD.Application.prototype.fixSideBar = function(){
	/* 
	 * the sidebar on the left doesn't extend 'till the footer when the main content
	 * is longer. This will fix this.
	 * Firefox (1.5 / 2.0) shows a 1 pixel gap, so 2px are added to the height.
	 */
	 
	var mainContainer = YAHOO.util.Dom.get('yui-main');
	var sideBar = YAHOO.util.Dom.get('sidebar');
	if(mainContainer && sideBar){
		if(mainContainer.offsetHeight > sideBar.offsetHeight){
			YAHOO.util.Dom.setStyle(sideBar,'height', (mainContainer.offsetHeight+2) + 'px');
		}
	};
};

function setupApplication(){
	application = new CSD.Application();
	application.init();
}

CSD.Cart = function(){
	
}

CSD.Cart.prototype = {

	items: {},
	form: null,
	button: null,
	
	getItems: function(){
		
		this.button = document.getElementById('btnUpdateCart');
		YAHOO.util.Dom.setStyle(this.button, 'display', 'none');
		
		this.form = document.getElementById('formCart');
		for(var i=0; i<this.form.elements.length; i++){
			var el = this.form.elements[i];
			var match = /product_(\d+)/.exec(el.name);
			if(match.length == 2){
				this.items[match[1]] = el.value;
				YAHOO.util.Event.on(el, 'change', this.onChangeCart, this, true);
				YAHOO.util.Event.on(el, 'keyup', this.onChangeCart, this, true);
			}
		}
	},
	
	hasChanged: function(){
		
		result = true;
		this.form = document.getElementById('formCart');
		for(var i=0; i<this.form.elements.length; i++){
			var el = this.form.elements[i];
			var match = /product_(\d+)/.exec(el.name);
			if(match.length == 2){
				result &= this.items[match[1]] == el.value;
			}
		}
		return ! result;
	},
	
	onChangeCart: function(event){
		
		if(this.hasChanged()){
			YAHOO.util.Dom.setStyle(this.button, 'display', '');
		} else {
			YAHOO.util.Dom.setStyle(this.button, 'display', 'none');
		}
	}
}

YAHOO.util.Event.on(window, 'load', setupApplication);