window.addEvent('domready', function() {
	
	if($('total-search-submit')) {
		
		$('total-search-submit').addEvent('click', function(){
			
			var p_type = $('total-search-prod-type');
			var form = $('total-search-form');
			var action = '';
			
			if(p_type) {
				
				switch(p_type.value) {
					
					default:
						return false;
					break;
					
					case 'watch':
						action = 'watches/';
					break;
					
					case 'access':
						action = 'accessories/';
					break;
					
					case 'strap':
						action = 'straps/';
					break;
					
					case 'gift':
						action = 'gifts/';
					break;
					
					case 'jew':
						action = 'jewellery/';
					break;
				}
				
				if(form) {
					
					form.action += action;
				}
			}
		});
		
		$('total-search-prod-type').addEvent('change', function() {
			
			var btn = $('total-search-submit');
			
			if(this.value != 0) {
				
				btn.src = '/public/images/chose_ru.gif';
			}
			else {
				
				btn.src = '/public/images/chose_ru_dis.gif';
			}
		});
	}
	
	if($('total-search-submit-ua')) {
		
		$('total-search-submit-ua').addEvent('click', function(){
			
			var p_type = $('total-search-prod-type');
			var form = $('total-search-form');
			var action = '';
			
			if(p_type) {
				
				switch(p_type.value) {
					
					default:
						return false;
					break;
					
					case 'watch':
						action = 'watches/';
					break;
					
					case 'access':
						action = 'accessories/';
					break;
					
					case 'strap':
						action = 'straps/';
					break;
					
					case 'gift':
						action = 'gifts/';
					break;
					
					case 'jew':
						action = 'jewellery/';
					break;
				}
				
				if(form) {
					
					form.action += action;
				}
			}
		});
		
		$('total-search-prod-type').addEvent('change', function() {
			
			var btn = $('total-search-submit-ua');
			
			if(this.value != 0) {
				
				btn.src = '/public/images/ua/chose.gif';
			}
			else {
				
				btn.src = '/public/images/ua/chose_dis.gif';
			}
		});
	}
	
	if($('order-form')) {
		
		var OrderForm = new Class({
			
			initialize: function() {
				
				this.form = $('order-form');
				this.button = $('of_submit_button');
				this.error = $('order-error');
				this.error_msg = $('error-msg');
				this.success = $('order-success');
				this.firstname = '';
				this.phone = '';
				this.email = '';
				this.city = '';
				this.address = '';
				this.prod_type = '';
				this.prod_id = '';
				this.captcha = '';
				this.url = '/watches/';
				
				this.setEventHandlers();
			}
		});
		
		OrderForm.implement({
			
			setEventHandlers: function() {
				
				var obj = this;
				
				this.form.addEvent('submit', function(e) {
					
					new Event(e).stop();
					
					obj.firstname = this.firstname.value;
					obj.phone = this.phone.value;
					obj.email = this.email.value;
					obj.city = this.city.value;
					obj.address = this.address.value;
					obj.captcha = this.captcha.value;
					obj.prod_id = this.prod_id.value;
					obj.prod_type = this.prod_type.value;
					
					obj.send();
				});
			}, 
			
			disable: function() {
				
				this.form.set('styles', {
					opacity: 0.5
				});
				
				this.form.firstname.disabled = true;
				this.form.phone.disabled = true;
				this.form.email.disabled = true;
				this.form.city.disabled = true;
				this.form.address.disabled = true;
				this.form.captcha.disabled = true;
				this.button.disabled = true;
			}, 
			
			enable: function() {
				
				this.form.firstname.disabled = false;
				this.form.phone.disabled = false;
				this.form.email.disabled = false;
				this.form.city.disabled = false;
				this.form.address.disabled = false;
				this.form.captcha.disabled = false;
				this.button.disabled = false;
				
				this.form.set('styles', {
					opacity: 1
				});
			}, 
			
			send: function() {
				
				var obj = this;
				
				obj.disable();

				new Request.JSON({url: obj.url, onComplete: function(json) {
					
					if(json.success) {
						
						obj.showSuccess()
					}
					else {
						
						obj.showError(json.errors);
					}
				}}).post({
					firstname: obj.firstname,
					phone: obj.phone,
					email: obj.email,
					city: obj.city,
					address: obj.address,
					prod_type: obj.prod_type,
					prod_id: obj.prod_id,
					captcha: obj.captcha,
					Event: 'Order'
				});
			}, 
			
			showSuccess: function() {
				
				var obj = this;
				
				this.form.set('styles', {
					overflow: 'hidden'
				});
				this.error.set('styles', {
					opacity: 0
				});
				
				var coord = this.form.getCoordinates();
				
				new Fx.Morph(obj.form, {onComplete: function(){
					
					obj.form.set('styles', {
						display: 'none'
					});
				}}).start({
					height: [coord.height, 0]
				});
				
				new Fx.Morph(this.success, {onComplete: function(){
					
					var effect = new Fx.Morph(obj.success);
					effect.set({
						opacity: 0
					});
					
					obj.success.removeClass('hide-content');
					
					effect.start({
						opacity: [0, 1]
					});
					
				}}).start({
					height: [0, 150]
				});
			}, 
			
			showError: function(errors) {
				
				var obj = this;
				
				var msg = '';
				
				obj.form.phone.style.background = '#FFFFFF';
				obj.form.captcha.style.background = '#FFFFFF';
				obj.form.email.style.background = '#FFFFFF';
				
				for(var i in errors) {
					
					msg += errors[i] + '<br />';
					
					switch(i) {
						case 'phone_email':
							obj.form.phone.style.background = '#FF5555';
							obj.form.email.style.background = '#FF5555';
						break;
						
						case 'email':
							obj.form.email.style.background = '#FF5555';
						break;
						
						case 'phone':
							obj.form.phone.style.background = '#FF5555';
						break;
						
						case 'captcha':
							obj.form.captcha.style.background = '#FF5555';
						break;
					}
				}
				obj.error_msg.innerHTML = msg;
				
				var coord = obj.error.getCoordinates();
				
				if(!coord.height) {
					
					new Fx.Morph(obj.error, {onComplete: function() {
						
						var effect = new Fx.Morph(obj.error);
						effect.set({
							opacity: 0
						});
						
						obj.error.removeClass('hide-content');
						
						effect.start({
							opacity: [0, 1]
						});
						
					}}).start({
						height: [0, 170]
					});
				}
				
				obj.enable();
			}
		});
		
		new OrderForm;
	}
	
	if($('gallery-image-container')) {
		
		new Gallery('gallery-image-container', 'gallery-number', 'gallery-title', 
					'gallery-preview-container', 'gallery-preview', 
					'g-ctrl-prev-link', 'g-ctrl-next-link');
	}
	
	$$('input.clickable').each(function(input){
		
		input.addEvent('click', function(){
			
			if(input.value == input.title) {
				
				input.value = '';
			}
		});
		
		input.addEvent('blur', function() {
			
			if(input.value == '') {
				
				input.value = input.title;
			}
		});
	});
	
	$$('form.clear-before-submit').each(function(form){
		
		form.addEvent('submit', function(){
			
			$$('input.clickable').each(function(input){
				
				if(input.value == input.title) {
					
					input.value = '';
				}
			});
		});
	})
});
