var AJAX=ROOT+'/'+CONTROLLER+'/ajax/';
window.addEvent('domready', function(){
	aPanels=$$('div.panel');
	curIndex=0;
	
	aPanels.each(function(el, index){
		el=$(el);
		el.setStyles({
			left:830*index,
			top:0
		});
	});
	
	$('left').addEvent('click', function(){
		curIndex--;
		if(curIndex<0){
			curIndex=0;
		}
		new Fx.Scroll($('panels')).toElement(aPanels[curIndex]);
	});
	$('right').addEvent('click', function(){
		curIndex++;
		if(curIndex>aPanels.length-1){
			curIndex=aPanels.length-1;
		}
		new Fx.Scroll($('panels')).toElement(aPanels[curIndex]);
	});
	
	$$('input').addEvent('focus', function(){
		el=$(this);
		el.removeClass('unfocussed');
		el.value='';
	});
	$('download-form').onsubmit=function(){
		if(validateEmail($('emailfield').value)){
			emailAddress=$('emailfield').value;
			$('download-form').setStyle('display', 'none');
			$('loader').setStyle('display', 'block');
			new Request.JSON({
				url:AJAX+'sendemails',
				onSuccess:function(data){
					$('loader').setStyle('display', 'none');
					$('message').setStyle('display', 'block');
				},
				onError:function(){
					alert("There was a problem completing your request. Please try again.");
					window.location.href.reload();
				}
			}).post({address:emailAddress});
		} else {
			alert("This does not appear to be a valid email address.");
		}
		return false;
	}
	
});

function validateEmail(id){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(id);
}
