var emailRegex=/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/;

window.addEvent('domready',function(){

	/*	If homepage, insert flash movie */
	
	if($('homepageFlash')){
	
		var homepageFlash=new Swiff('/flash/homepage.swf',{
		
			'width':905,
			'height':384,
			'container':'homepageFlash',
			'vars':{
			
				'homepageSubTitle':homepageSubTitle,
				'homepageTitle':homepageTitle,
				'homepageText':homepageText,
				'homepageCollection':homepageCollection,
				'homepageColor':homepageColor,
				'homepageNewsTitle':homepageNewsTitle,
				'homepageNewsContents':homepageNewsContents
			
			}
		
		});
	
	}
	
	/*	Apply squeezebox code */
	
	SqueezeBox.assign($$('a[rel=boxed]'),{
	
		'size':{'x':300,'y':430},
		'ajaxOptions':{
		
			'method':'get' // we use GET for requesting plain HTML
			
		},
		'onShow':function(){
			
			$('productFormButton').addEvent('click',function(){
		
				processProductForm();
			
			});
		
		}
		
	});
	
	/*	Carousel */
	
	if($('carousel')) carousel=new Carousel({'viewWidth':908,'itemsPerView':4});
	if($('previousBtn')) $('previousBtn').addEvent('click',function(){carousel.previousItem();});
	if($('nextBtn')) $('nextBtn').addEvent('click',function(){carousel.nextItem();});

});

/*	Products Form Processing */

function processProductForm(){

	if($('productFormName').value==''||$('productFormEmail').value==''||$('productFormMessage').value==''){
	
		$('productFormError').set('text','Please complete this form.');
		return;
	
	}
	
	if(!emailRegex.test($('productFormEmail').value)){

		$('productFormError').set('text','Invalid email address');
		return;
	
	}

	var data={'name':$('productFormName').value,'email':$('productFormEmail').value,'message':$('productFormMessage').value,'photo':$('photo').value};

	var request=new Request({
	
		'method':'post',
		'data':data,
		'url':'/en/products/send.php'
	
	}).send();
	$('productForm').set('html','<div class="success" style="text-align:center; padding:100px 0 0 0;">Your Enquiry Has Been Sent!</div>');

}

/*	Contact Form Processing */

function processContactForm(){

	/*	Housekeeping */
	
	$('contactFormError').empty();
	
	/*	Validate Non-Empty Fields */

	if($('name').value==''||$('email').value==''||$('message').value==''){
	
		$('contactFormError').set('text','Please complete this form');
		return false;
	
	}
	
	/*	Validate Email Address */
	
	if(!emailRegex.test($('email').value)){
	
		$('contactFormError').set('text','Invalid email address');
		return false;
	
	}
	
	return true;

}

/*	Carousel */

var Carousel=new Class({
					  
	'Implements':Options,
	
	'options':{
	
		'data':[],
		'viewWidth':0,
		'itemsPerView':1,
		'maxMargin':0,
		'animation':null,
		'items':null,
		'autoStart':true,
		'periodical':0
				
	},
	
	'initialize':function(options){
		
		this.setOptions(options);
		this.setup();
		
	}

});

Carousel.implement({setup:function(){

	this.options.items=$$('#carousel li');
	this.options.maxMargin=this.options.items.length/this.options.itemsPerView*this.options.viewWidth-this.options.viewWidth;
	this.options.animation=new Fx.Tween($('carousel'),{'duration':500});
	
	if(this.options.periodical>0) this.nextItem.periodical(this.options.periodical,this);

}});

Carousel.implement({nextItem:function(){

	var pos=parseInt($('carousel').getStyle('left'));
	
	if(pos==-this.options.maxMargin*this.options.itemsPerView) this.options.animation.start('left',0);
	else this.options.animation.start('left',pos-this.options.viewWidth);

}});

Carousel.implement({previousItem:function(){

	var pos=parseInt($('carousel').getStyle('left'));

	if(pos==0) this.options.animation.start('left',-this.options.maxMargin);
	else this.options.animation.start('left',pos+this.options.viewWidth);

}});
