//////////////////////////////////////////////////////////////////////////////////////////////////
/* sLquantity - v1.0 - 3:13 PM 15/03/2007
//////////////////////////////////////////////////////////////////////////////////////////////////
	@author: kc@slajax.com
	@description: allows customers to duplicate a form element and pick multiple product variations
		var sLquantityOpts = {
					conId:'ShoppingDrops', // the container ID of the object to clone
					conClass:'.ShoppingDrops', // the class name of the same object (id will be phased out)
					minusClass:'.ShoppingMinus', // the button class of the element which adds new objects
					plusClass:'.ShoppingPlus', // the button class of the element which removes the last object added
					introDur: 200 // the duration of the fade in for a cloned element
					};
*/
//////////////////////////////////////////////////////////////////////////////////////////////////
// init options - edit these for your purposes
//////////////////////////////////////////////////////////////////////////////////////////////////

window.addEvent('domready', function(){
		var sLquantityOpts = {
					conId:'ShoppingDrops', // the container ID of the object to clone
					conClass:'.ShoppingDrops', // the class name of the same object (id will be phased out)
					minusClass:'.ShoppingMinus', // the button class of the element which adds new objects
					plusClass:'.ShoppingPlus', // the button class of the element which removes the last object added
					introDur: 200 // the duration of the fade in for a cloned element
					};
		var quantity = new sLquantity(sLquantityOpts);
});

//////////////////////////////////////////////////////////////////////////////////////////////////
// class - no editing below this line
//////////////////////////////////////////////////////////////////////////////////////////////////
var sLquantity = new Class({
	initialize:function(options){
		this.setOptions(options);
		//hack to fix invalid xhtml output from engine, will be removed if/when les updates php
		if($(this.options.conId))
		$(this.options.conId).addClass(this.options.conClass.split('.')[1]);
		this.gather(options);
	},
	setOptions:function(options){
		this.options = Object.extend( { }, options || {} );
	},
	gather:function(options){
	$$(options.plusClass).each(function(el,i){
			if(i<1)	$(el).addEvent('mousedown',this.plusHref.pass([el], this));
				else $(el).remove(); // remove secondary add / minus buttons onload
		}.bind(this) );
	$$(options.minusClass).each(function(el,i){
			 if(i<1) $(el).addEvent('mousedown', this.minusHref.pass([el], this));
				else $(el).remove(); // remove secondary add / minus buttons onload
		}.bind(this) );
	},
	fX:function(options, stack){
		introFx = new Fx.Style(stack, 'opacity', {duration: options.introDur, transition: Fx.Transitions.linear});
		introFx.start(0,1);
		stack = null;
	},
	minusHref:function(el){
		if( $$(this.options.conClass).length > 1)
				$$(this.options.conClass)[$$(this.options.conClass).length-1].remove();
	},
	plusHref:function(el){ // remove secondary add / minus buttons on clone
	//console.log($(el).getParent());
		var stack = this.cloneObj( $(el).getParent() ).injectAfter( $(el).getParent() ).setOpacity(0);
			stack.getChildren().each(function(el){
				if( $(el).getTag() == 'img')
					$(el).remove()
				}.bind(this) );
		this.fX(this.options, stack);
	},
	cloneObj:function(el){
		return $(el).clone();
	}
});

//////////////////////////////////////////////////////////////////////////////////////////////////
/* sLvalidate - v1.0 - 5:16 PM 20/03/2007
//////////////////////////////////////////////////////////////////////////////////////////////////

	@author: kc@slajax.com
	@description: validates entries for sLquantity before posting to update cookie
			var sLvalidateOpts = {
					items:'select', // boxes to gather
					submitBtn: 'button', // id of submit button
					valid: true // this must be set to true for validation to occur
					};
*/
//////////////////////////////////////////////////////////////////////////////////////////////////
// init options - edit these for your purposes
//////////////////////////////////////////////////////////////////////////////////////////////////
window.addEvent('domready', function(){
		var sLvalidateOpts = {
					items:'select', // boxes to gather
					submitBtn: 'idbutton', // id of submit button
					valid: true // this must be set to true for validation to occur
					};
		var validate = new sLvalidate(sLvalidateOpts);
});
//////////////////////////////////////////////////////////////////////////////////////////////////
// class - no editing below this line
//////////////////////////////////////////////////////////////////////////////////////////////////
var sLvalidate = new Class({
		initialize:function(options){
			this.setOptions(options);
			this.href(options);
		},
		setOptions:function(options){
			this.options = Object.extend( { }, options || { } );
		},
		gather:function(options){
			this.resetErrors(options);
			this.itemArr = [];
			this.itemObj = {};

		$$(options.items).each(function(el,i){
					this.itemArr[i] = $(el).options[$(el).selectedIndex].value;
					this.itemObj[i] = $(el);
				}.bind(this) );

			this.validate(options);
		},
		validate:function(options){
			this.itemArr.each(function(el,i){
				if ( this.itemArr[i] == 0 )
							this.throwError(options, i);
				}.bind(this) );

		 if( options.valid ) $(options.submitBtn).getParent().getParent().submit();
		 //document.forms[0].submit()
		},
		throwError:function(options, i){
			options.valid = false;
				this.itemObj[i].getParent().getParent().setStyle('background-color','#FF0000');
				this.itemObj[i].getParent().getParent().setStyle('padding','2px 0px 2px 2px');

		},
		resetErrors:function(options){
			options.valid = true;
			$$(options.items).each(function(el,i){
					$(el).getParent().getParent().setStyle('background-color','#FFFFFF');
					$(el).getParent().getParent().setStyle('padding','0');
			}.bind(this) );
		},
		href:function(options){
				if(	$(options.submitBtn) ){
					$(options.submitBtn).addEvent('click', function(){
								this.gather(options);

						}.bind(this) );
				}
		}
});