/**
 * @author thomas
 */
(function(){
	var reMoozOpts = {
		'origin': 'img',
		'shadow': 'onOpenEnd',
		'resizeFactor': 0.8,
		'cutOut': false,
		'opacityResize': 0.4,
		'dragging': true,
		'centered': true
	};	
	
	var GenericSlider = new Class({
		Implements: [Options, Events],
		
		options: {
			'items': null,
			'interval': 3000,
			'fxOpts': {},
			'autoPlay': true,
			'autoPlayDirection': 'next'
		},
		
		initialize: function(options){
			this.setOptions(options);
			this.curIndex = 0;
			this.lastIndex = 0;
			
			if (this.options.autoPlay && this.options.items.length > 1) {
				this.slideTo(this.curIndex);
				this.play(this.options.interval, this.options.autoPlayDirection);
			}
		},
		
		slide: function(){
			return this;
		},
		
		slideTo: function(index, manual, fx){
			fx = ($defined(fx) ? fx : true);
			
			if (manual) {
				this.stop();
			}
			
			this.lastIndex = this.curIndex;
			this.curIndex = index;
			
			if (this.curIndex >= this.options.items.length) {
				this.curIndex = 0;
			}
			else if (this.curIndex < 0) {
				this.curIndex = this.options.items.length - 1;
			}
			
			this.slide();
			
			return this;
		},
		
		next: function(manual, fx){
			return this.slideTo(this.curIndex + 1, manual, fx);
		},
		prev: function(manual, fx){
			return this.slideTo(this.curIndex - 1, manual, fx);
		},
		
		play: function(interval, direction){
			this.stop();
			this._play = this[direction].periodical(interval, this);
			return this;
		},
		
		resume: function(){
			if (this.options.autoPlay) {
				return this.play(this.options.interval, this.options.autoPlayDirection);
			}
			return this;
		},
		
		stop: function(){
			$clear(this._play);
			return this;
		}
	});
	
	var NewsSlider = new Class({
		Extends: GenericSlider,
		
		options: {
			'imgContainer': null,
			'handles': null
		},
		
		initialize: function(options){
			this.parent(options);
			var klass = this;
			if (this.options.handles) {
				this.options.handles.each(function(handle, i){
					handle.addEvents({
						'mouseenter': function(e){
							klass.slideTo(i, true);
						},
						'mouseleave': function(e){
							if (klass.options.autoPlay) {
								klass.resume();
							}
						}
					});
				});
			}
		},
		
		slide: function(){
			var item = this.options.items[this.curIndex];
			var lastItem = this.options.items[this.lastIndex];
			var imgContainer = document.id(this.options.imgContainer);
			
			lastItem.getElement('h1').removeClass('active');
			item.getElement('h1').addClass('active');
			Cufon.refresh('h1');
			this.changeImage(imgContainer.getElement('img'), item.getElement('img').clone());
			return this;
		},
		
		changeImage: function(oldImg, newImg){
			oldImg.set('tween', $merge(this.options.fxOpts, {
				'onComplete': (function(){
					newImg.set('tween', this.options.fxOpts).set('opacity', 0);
					this.options.imgContainer.empty().adopt(newImg);
					newImg.tween('opacity', 1);
				}).bind(this)
			})).tween('opacity', 0);
		}
	});
	
	var GallerySlider = new Class({
		Extends: GenericSlider,
		
		options: {
			'handles': null,
			'thumbsWidth': 81
		},
		
		initialize: function(options){
			this.setOptions(options);
			
			if(this.options.items){
				$$(this.options.items).setStyle('z-index', '1');
			}
			
			this.parent();
			var klass = this;
			if (this.options.handles) {
				var cont = this.options.handles[0].getParent();
				var width = this.options.thumbsWidth * this.options.handles.length;
				cont.setStyle('width', width);
				
				this.options.handles.each(function(handle, i){
					handle.addEvents({
						'click': function(e){
							klass.slideTo(i, true).resume();
						}
					});
				});
				
				this.makeAutoSlide(cont.getParent());
			}
		},
		
		slide: function(){
			var item = this.options.items[this.curIndex];
			var lastItem = this.options.items[this.lastIndex];
			var handle = this.options.handles[this.curIndex];
			var lastHandle = this.options.handles[this.lastIndex];
			
			lastHandle.removeClass('active');
			handle.addClass('active');
			if(this.scroller && !this.sliderIsActive){
				this.scroller.toElement(handle);
			}
			this.changeImage(lastItem, item);
			return this;
		},
		
		changeImage: function(lastItem, item){
			$$(this.options.items).setStyle('z-index', 1);
			lastItem.setStyle('z-index', 2);
			item.set('tween', this.options.fxOpts).setStyles({
				'opacity': 0,
				'z-index': 3
			}).tween('opacity', 1);
		},
		
		makeAutoSlide: function(cont){
			this.scroller = new Fx.Scroll(cont);
			
			var slider = new Scroller(cont,{area: 50});
			
			cont.addEvents({
				'mouseenter': (function(){
					slider.start();
					this.sliderIsActive = true;
				}).bind(this),
				'mouseleave': (function(){
					slider.stop();
					this.sliderIsActive = false;
				}).bind(this)
			});
		}
	});	
	
	window.addEvent('domready', function(){
		if (document.getElement('.start')) {
			new NewsSlider({
				items: document.getElements('.hero-text'),
				handles: document.getElements('.hero-text'),
				imgContainer: document.getElement('.hero-img'),
				interval: 10000
			});
		}
		else if(document.getElement('.news-list')){
			new NewsSlider({
				autoPlay: false,
				items: document.getElements('.hero-text'),
				handles: document.getElements('.hero-text'),
				imgContainer: document.getElement('.hero-img')				
			}).slideTo(0);
		}
		
		if(document.id('banners_container')){
			new GallerySlider({
				items: document.getElements('#banners img'),
				handles: document.getElements('#thumb-container li'),
				autoPlay: false
			}).slideTo(0);
		}
		
		if(document.getElement('.shop')){
			new ShoppingCart({
				items: document.getElements('.add-to-cart.bt-bestellen'),
				cookieOpts: {domain: 'ma-villach.com', path: '/'},
				shippingPrice: 5.9,
				onSave: function(){
					var cart = document.getElement('.cart');
					var articles = 0;
					this.store.each(function(amount){
						articles += amount.toInt();
					});
					cart.set('text', (articles === 0 ? 'keine' : articles) + ' artikel').highlight();
				}
			});
		}
		
		if(document.getElement('.shopping-cart-table')){
			var table = document.getElement('.shopping-cart-table');
			
			var updateOrderPriceField = function(){
				var order_price = 0;				
				document.getElements('.total-price').get('text').each(function(value){
					order_price += value.toFloat();
				});
				order_price += document.getElement('.shipping-price').get('text').toFloat();
				document.getElement('.order-price').set('text', order_price.round(2)).highlight();
			};
			
			var sc = new ShoppingCart({
				cookieOpts: {domain: 'ma-villach.com', path: '/'},
				shippingPrice: 5.9,
				onSetItem: function(id, total){
					var row = document.getElement('input[name="item_' + id + '"]').getParent('tr');
					row.getElement('.total-price').set('text', (row.getElement('.article-price').get('text').toFloat() * total).round(2)).highlight();
					updateOrderPriceField();
				},
				onRemoveItem: function(id, amount, total){
					var row = document.getElement('input[name="item_' + id + '"]').getParent('tr');
					if(row){
						row.destroy();
						updateOrderPriceField();
					}
				},
				onSetShippingPrice: function(amount){
					document.getElement('.shipping-price').set('text', amount);
					updateOrderPriceField();
				}
			});
			
			table.addEvents({
/*
				'click:relay(input)': function(){
					var value = (this.get('value') > 0 ? this.get('value') : 1);
					sc.setItem(this.get('name').replace('item_', ''), value);
					this.set('value', value);
				},
*/
				
				'click:relay(.delete-article)': function(e){
					e.stop();
					if (confirm("Artikel aus Tüte löschen?")) {
						sc.removeItem(this.get('rel'));
					}
				},
				
				'click:relay(.update-article)': function(e){
					e.stop();
					var input = this.getParent('tr').getElement('input');
					var value = (input.get('value') > 0 ? input.get('value') : 1);
					sc.setItem(input.get('name').replace('item_', ''), value);
					input.set('value', value);
				}
			});
			
			var sc_form = $('sc-form');
			if(sc_form){
				new FormValidator(sc_form, {
					onElementFail: function(el){
						el.getParent().getFirst('label').addClass('validation-failed');
					},
					onElementPass: function(el){
						el.getParent().getFirst('label').removeClass('validation-failed');
					}
				});
				
				document.id('country').addEvent('change', function(e){
					if (this.get('value') == 'Österreich') {
						sc.setShippingPrice(5.90);
					}
					else{
						sc.setShippingPrice(12.90);
					}
				});
				
				document.id('shipping_country').addEvent('change', function(e){
					if(document.id('shipping_name').get('value')){
						if (this.get('value') == 'Österreich') {
							sc.setShippingPrice(5.90);
						}
						else{
							sc.setShippingPrice(12.90);
						}						
					}
				});
				
				document.id('payment').addEvent('change', function(e){
					if(this.get('value') != 'vorrauskasse'){
						this.store('shipping-price', sc.shippingPrice);
						sc.setShippingPrice(0);
					}
					else{
						sc.setShippingPrice(this.retrieve('shipping-price', sc.shippingPrice));
					}
				});
			}			
		}
		ReMooz.assign('.remooz', reMoozOpts);
	});
	
	Cufon.replace('h1', {
		fontFamily: 'Futura ICG Demi'
	});
	
})();
