window.addEvent('domready', function(){
	removePicDrag();
	resizeBackground();
	switchBackground();
	switchProducts();
	zoomProduct();
	switchZoom();
	sendToFriend();
	switchDelivery();
	prepareOrder();
	dragProducts();
	switchPromotion();
	checkForm();
});

window.addEvent('resize', function(){
	resizeBackground();
	zoomProduct();
});

var resizePicture = function(img){
	img.hide();
	var myImage = Asset.image(img.get('src'),{
		onLoad : function(){
			var widthP = img.getDimensions().width;
			var heightP = img.getDimensions().height;
			var widthW = window.getSize().x;
			var heightW = window.getSize().y;
			var ratioW = widthW / heightW;
			var ratioP = widthP / heightP;
			
			if(ratioW > ratioP){
				var newH = widthW / widthP * heightP;
				var newW = widthW;
			}
			else{
				var newH = heightW;
				var newW = heightW / heightP * widthP;
			}
			
			var newT = 0 - ((newH - heightW) / 2);
			var newL =  0 - ((newW - widthW) / 2);
			
			img.setStyles({
				'width': newW,
				'height': newH,
				'top': newT,
				'left': newL
			});
			img.show();
		}
	});
}

var removePicDrag = function(){
	$$('div.bilbaoriginal img').removeEvents('mousedown').addEvent('mousedown', function(e){
		e.stop();
	});
}

var resizeBackground = function(){
	$$('div.bilbaoriginal div.background img').each(resizePicture);
}

var switchBackground = function(){
	$$('div.bilbaoriginal div.spirit').each(function(el){
		var spirits = el.getParent('div.bilbaoriginal').getElements('div.spirit li');
		var i = 0;
		spirits.hide();
		spirits[0].show();
		
		$$('div.bilbaoriginal div.body a.sarrow').removeEvents().addEvent('click', function(e){
			e.stop();
			if(this.hasClass('previous')){
				i--;
				if(i < 0) i = spirits.length - 1;
			}
			else{
				i++;
				if(i >= spirits.length) i = 0;
			}
			spirits.hide();
			spirits[i].show();
		});
	});
}

var switchProducts = function(){
	$$('div.bilbaoriginal div.body div.products').each(function(el){
		var box = el.getParent('div.body').getElements('div.box');
		var product = 0;
		el.getElements('a.arrow').removeEvents('click').addEvent('click', function(e){
			e.stop();
			if(this.hasClass('previous')){
				product--;
				if(product < 0) product = box.length - 1;
			}
			else{
				product++;
				if(product >= box.length) product = 0;
			}
			
			box.removeClass('selected');
			box[product].addClass('selected');
		});
	});
	
	$$('div.bilbaoriginal div.box ul.collection li.model').each(function(el){
		var picture = el.getParent('li').getElement('img.main').get('src');
		var caption = el.getParent('li').getElement('li.name span').get('html');
		var total = el.getParent('li').getElement('li.name span.promotion').get('html');
		el.removeEvents().addEvents({
			'mouseover': function(){
				this.getParent('li').getElement('img.main').set('src', this.getElement('a').get('title'));
				this.getParent('li').getElement('li.name span').set('html', this.getElement('img').get('title'));
				this.getParent('li').getElement('li.name span.promotion').set('html', this.get('title'));
			},
			'mouseout': function(){
				this.getParent('li').getElement('img.main').set('src', picture);
				this.getParent('li').getElement('li.name span').set('html', caption);
				this.getParent('li').getElement('li.name span.promotion').set('html', total);
			}
		});
	});
}

var switchZoom = function(){
	$$('div.bilbaoriginal div.product li.picture a').removeEvents().addEvent('click', function(e){
		if(e) e.stop();
		this.getParent('div.product').getElement('li.zoom').setStyle('visibility','visible');
		this.getParent('div.product').getElements('li.picture, li.details').hide();
	});
	
	$$('div.bilbaoriginal div.product li.zoom').removeEvents().addEvent('click', function(){
		this.getParent('div.product').getElement('li.zoom').setStyle('visibility','hidden');
		this.getParent('div.product').getElements('li.picture, li.details').show();
	});
}

var zoomProduct = function(){
	$$('div.bilbaoriginal div.product li.zoom').each(function(el){
		new Zoomer(el.getElement('div.sd img'), el.getElement('div.hd img'), el.getElement('div.hd p'));
	});
}

var sendToFriend = function(){
	$$('div.bilbaoriginal div.sendtofriend a').removeEvents().addEvent('click', function(e){
		if(e) e.stop();
		this.getParent('div.sendtofriend').getElement('div').toggle();
	});
}

var switchDelivery = function(){
	$$('div.bilbaoriginal div.bag p.delivery select').removeEvents().addEvent('change', function(){
		var price = parseFloat(this.getParent('div.bag').getElement('p.delivery input.default').get('value'));
		var subtotal = this.getParent('div.bag').getElement('p.delivery span.subtotal');
		subtotal.set('html', price + parseFloat(this.options[this.selectedIndex].get('title')));
	});
}

var prepareOrder = function(){
	$$('div.bilbaoriginal div.bag form').removeEvents().addEvent('submit', function(e){
		e.stop();
		var bbsize = new Array();
		this.getElements('td.size select').each(function(el){
			var index = el.options[el.selectedIndex].get('title').toInt();
			bbsize[index] = el.get('value');
		});
				
		var query = new Request.JSON({
			method: 'post',
			url: this.get('action'),
			data:{
				'ajax': true,
				'bbsize': bbsize,
				'bbcountry': this.getElement('p.delivery select').get('value')
			},
			onRequest: function(){
			},
			onSuccess: function(json){
				this.set('action', json.URL);
				new Element('input', {'type':'hidden', 'name':'cmd', 'value':json.CMD}).inject(this.getElement('p.submit'));
				new Element('input', {'type':'hidden', 'name':'subtotal', 'value':json.SUBTOTAL}).inject(this.getElement('p.submit'));
				new Element('input', {'type':'hidden', 'name':'shipping', 'value':json.SHIPPING}).inject(this.getElement('p.submit'));
				new Element('input', {'type':'hidden', 'name':'business', 'value':json.BUSINESS}).inject(this.getElement('p.submit'));
				new Element('input', {'type':'hidden', 'name':'paymentaction', 'value':json.PAYMENTACTION}).inject(this.getElement('p.submit'));
				new Element('input', {'type':'hidden', 'name':'template', 'value':json.TEMPLATE}).inject(this.getElement('p.submit'));
				new Element('input', {'type':'hidden', 'name':'invoice', 'value':json.INVOICE}).inject(this.getElement('p.submit'));
				new Element('input', {'type':'hidden', 'name':'currency_code', 'value':json.CURRENCY_CODE}).inject(this.getElement('p.submit'));
				new Element('input', {'type':'hidden', 'name':'return', 'value':json.RETURN}).inject(this.getElement('p.submit'));
				
				this.submit();
			}.bind(this)
		}).send();
	});
}

var dragProducts = function(){
	$$('div.admin div.box p.picture').each(function(el){
		var morph = new Fx.Morph(el, {duration: 'short'});
		var drag = new Drag.Move(el,{
			droppables: $$('div.admin div.box div').erase(el.getParent('div')),
			onStart: function(){
				el.addClass('dragged');
			},
			onEnter: function(element, droppable){
				droppable.addClass('entered');
			},
			onLeave: function(element, droppable){
				droppable.removeClass('entered');
			},
			onDrop: function(element, droppable, event){
				el.removeClass('dragged');
				if(droppable){
					var nochild = !droppable.getElements('p.picture').length;
					var clone = element.getParent('div').clone();
					var query = new Request({
						method: 'post',
						url: './admin/alter-box/' + element.get('title'),
						data:{
							ajax: true,
							box: droppable.getParent('div.box').get('title'),
							order: droppable.get('title')
						},
						onSuccess: function(response){
							reloadProducts();
						}
					}).send();
					
					droppable.removeClass('entered');
					element.getParent('div').destroy();
					clone.getElement('p.picture').setStyles({'top': 0, 'left': 0})
					clone.inject(droppable, 'after');
					if(nochild) droppable.destroy();
				}
				else{
					morph.start({'top': 0, 'left': 0});
				}
			}
		});
	});
}

var switchPromotion = function(){
	$$('div.admin form.products input#promotion').removeEvents().addEvent('click', function(){
		reloadProducts();
	});
}

var reloadProducts = function(){
	$$('div.admin form.products').each(function(el){
		var query = new Request({
			method: 'post',
			url: el.get('action'),
			data:{
				ajax: true,
				promotion: el.getElement('input#promotion').get('checked')
			},
			onSuccess: function(response){
				el.getElement('div').set('html', response);
				dragProducts();
			}
		}).send();
	});
}

var checkForm = function(){
	$$('form.check').each(function(el){
		new Form.Validator(el,{
			onFormValidate: function(validate, form, event){
				if(validate){
					if(form.hasClass('ajax')){
						event.stop();
						var query = new Form.Request(form, form.getElement('h3.response'),{
							extraData:{
								ajax: true
							},
							onSuccess: function(target, tree, els, html){
							}
						}).send();
					}
				}
			}
		});
	});
}
