var shop;

var shop94=new Class({
			
	initialize: function() {
		var el=$('processCart');
		if (el)
			{
			this.cart=new Form(el,{
				onBlur: this.onClick.bind(this),
				onValid: function(prop) {prop.element.removeClass('invalid').set('title','');},
				onInvalid: function(prop) {prop.element.addClass('invalid').set('title',prop.message);},
				onSubmit: this.onSubmit.bind(this)
			});
			el.getElements('a.remove').addEvent('click',this.onClick.bind(this));
			el.getElement('select[name=CountryID]').addEvent('change',this.onClick.bind(this));
			}		
	},

	onClick: function(el) {
		el=$type(el)=='event' ? $(el.target) : el;
		var value=el.getValue();
		var original=el.get('rel');
		if (el.hasClass('remove'))
			{
			this.cart.setValue({'action': 'remove', 'cart': (el.getParent('tr').get('rel'))});
			this.cart.directSubmit();
			}
		else if (el.hasClass('quantity') && parseInt(value)!=0 && value!=original)
			{
			this.cart.setValue({'action': 'quantity', 'Quantity': (el.getValue()), 'cart': (el.getParent('tr').get('rel'))});
			this.cart.directSubmit();
			}
		else if (el.id=='CountryID' && value!=original)
			{
			this.cart.setValue('action','shipping');
			this.cart.directSubmit();
			}
	},
	
	onSubmit: function() {
		this.cart.setValue('action','process');
		this.cart.directSubmit();
	},
	
	changeVariant: function(el) {
		if (el)
			{
			el=$(el);
			var value=el.getValue();
			var variant=el.id.substr(1);
			if (product.variantCount>1)
				{
				switch (variant) {
					case '1':
						html='';
						var options=product.variants[value];
						Object.each(options,function(value,key){
							html+='<option value="'+value[0]+'" rel="'+value[1]+'">'+key+'</option>';
						});
						$('v2').set('html',html);
						break;
					}
				}
			if (variant==1)
				{this.changeImage(el);}
			}
		if (product.variantCount==0)
			{product.variantId=0;}
		else
			{
			var option=$('v'+product.variantCount).getOption();
			product.variantId=option.get('value');
			if (el)
				{
				var price=option.get('rel');
				if (price.contains(';'))
					{
					price=price.split(';');
					price=product.label.substitute([price[0].toCurrency(),price[1].toCurrency()]);
					}
				else
					{price.toCurrency();}
				$('Price').set('html',price);
				}
			}		
	},
	
	changeImage: function(el) {
		var value=$(el).getOptionText();
		var img=$('productImage');
		var src=img.get('src');
		var i=src.lastIndexOf('_');
		var j=src.lastIndexOf('.');
		if (i>0)
			{
			src=src.substr(0,i+1)+value+src.substr(j);
			img.set('src',src);
			i=src.lastIndexOf('/');
			src=src.substr(i+1);
			$('_image').set('value',src);
			}
	}
	
});

var carousel;
window.addEvent('domready', function() {

	var teaser=$('teaser');
	if (teaser)	
	 	{
	 	carousel = new Fx.Scroll.Carousel('teaser',{
	  	mode: 'horizontal',
	  	periodical: 5500,
	  	childSelector: 'a'
		});
		carousel._run(true);
		$('teaser').addEvents({
			'mouseenter': function() {$clear(carousel._runner);},
			'mouseleave': function() {carousel._run(true);}	
		});
    //new XtLightbox('a.video', {
    //    adaptors: ['YouTube'],
    //    rendererOptions: {
    //        positionText: 'Video {x} van {total}.'
    //    }
    //});		
		}
	else
		{
		shop=new shop94();
		}		
	
});
