var PATH = {};
PATH.root = '/game/';
PATH.site = 'game/';

var Controller = new Class({

	initialize: function()
	{
		//nothing really
	},
	
	doAction: function(action, instance)
	{
		VO.action =  action;
		this.instance = instance;
		this.url = PATH.root + PATH.site + 'php/do.php';

		switch(action)
		{
			case 'startGame':
				this.update = $('content');
				this.doRequest('html');
				break;
			case 'highScore':
				this.update = $('highScore');
				this.doRequest('html');
				break;
			case 'checkWinnerForm':
				if(this.checkWinnerForm())
				{
					VO.lastName = escape($('lastName').value);
					VO.email = escape($('email').value);
					
					this.update = $('content');
					this.doRequest('html');
				}
				break;
			case 'back':
				window.location = 'http://paturain.com';
			break
			case 'sendFriend':
				instance.blur();
				this.update = $('content');
				this.doRequest('html');
				break;
			case 'confirmSendFriend':
				instance.blur();
				if(this.checkSendFriendForm())
				{
					VO.name = escape($('name').value);
					VO.email = escape($('email').value);
					var recipients = new Array();
					recipients = $('recipients').value.split(',');
					
					recipients.each(function(e, i, r){e = e.replace(/^\s*|\s*$/,""); r[i] = e;}, this);
					//recipients.each(function(e){alert(e);});
					VO.recipients = recipients;
					
					this.update = $('content');
					this.doRequest('html');
				}
				break;
			case 'score':
				if(instance)
				{
					instance.blur();
				}
				this.update = $('content');
				this.doRequest('html');
				break;
			case 'confirmScore':
				if(instance)
				{
					instance.blur();
				}
				if(this.checkScoreForm())
				{
					VO.name = escape($('name').value);
					VO.email = escape($('email').value);
					
					this.update = $('content');
					this.doRequest('html');
				}
				break;
			default:
				alert(action);
		}
	},
	
	afterRequest:function(rT, rE, rH, rJ)
	{
		switch(VO.action)
		{
			
			case 'startGame':
				this.placeSwiff();
				controller.doAction('highScore');
				break;
			case 'sendFriend':
			case 'score':
				$('name').focus();
				break;
			case 'confirmSendFriend':
		
			case 'highScore':
			case 'back':
				break;
			case 'confirmScore':
				this.doAction('highScore');
				break;
			default:
			alert(rH);
		}
	},

	doRequest:function(type)
	{
		//this.startRequest();
		switch(type)
		{
			case 'html':
				new Request.HTML(
					{
						url:this.url, 
						update:this.update, 
						data:VO,
						onComplete:function(rT, rE, rH, rJ)
						{
							this.afterRequest(rT, rE, rH, rJ);
						}.bind(this) 
					}
				).send();
				break;
			default:
				new Request(
					{
						url:this.url, 
						data:VO, 
						onComplete:function(rT, rE)
						{
							this.afterRequest(rT, rE);
						}.bind(this) 
					}
				).send();
		}
	},
	
	checkScoreForm: function()
	{
		if($('name').value == '')
		{	
			$('notice').setStyle('color','#d30014');
			$('notice').set('html', 'U moet uw naam invullen.');
			return false;
		}else{
			$('notice').setStyle('color','#01821e');
		}
		
		if($('email').value == '')
		{	
			$('notice').setStyle('color','#d30014');
			$('notice').set('html', 'U moet uw email invullen.');
			return false;
		}else{
			$('notice').setStyle('color','#01821e');
		}
		
		if(!this.checkEmail($('email').value))
		{	
			$('notice').setStyle('color','#d30014');
			$('notice').set('html', 'Ongeldig email adres.');
			return false;
		}else{
			$('notice').setStyle('color','#01821e');
		}
	
		return true
	},
	
	checkSendFriendForm: function()
	{
		if($('name').value == '')
		{	
			$('notice').setStyle('color','#d30014');
			$('notice').set('html', 'U moet uw naam invullen.');
			return false;
		}else{
			$('notice').setStyle('color','#01821e');
		}
		
		if($('email').value == '')
		{	
			$('notice').setStyle('color','#d30014');
			$('notice').set('html', 'U moet uw email invullen.');
			return false;
		}else{
			$('notice').setStyle('color','#01821e');
		}
		
		if(!this.checkEmail($('email').value))
		{	
			$('notice').setStyle('color','#d30014');
			$('notice').set('html', 'Ongeldig email adres.');
			return false;
		}else{
			$('notice').setStyle('color','#01821e');
		}
		
		if($('recipients').value == '')
		{	
			$('notice').setStyle('color','#d30014');
			$('notice').set('html', 'U moet minimaal 1 ontvanger opgeven.');
			return false;
		}else{
			$('notice').setStyle('color','#01821e');
		}
		
		if($('recipients').value != '')
		{	
			var recipients = new Array();
			recipients = $('recipients').value.split(',');
			if(recipients.length >5)
			{
				$('notice').setStyle('color','#d30014');
				$('notice').set('html', 'U mag maximaal 5 ontvangers opgeven.');
				return false;
			}
			
			var error = false;
			recipients.each(function(e){e = e.replace(/^\s*|\s*$/,"");if(!this.checkEmail(e)){error = true;}}, this);
			if(error)
			{	
				$('notice').setStyle('color','#d30014');
				$('notice').set('html', 'Een van de opgegeven email adressen is onjuist.');
				return false;
			}else{
				$('notice').setStyle('color','#01821e');
			}
		}
		
		
		return true
	},
	
	checkEmail: function(email)
	{	
		//ill= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;&& e.email.value.match(ill)
		re = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		var check = email.match(re)? true : false;
		return check;
	},
	
	placeSwiff: function ()
	{
	var obj = new Swiff('main.swf', {
		id: 'swiff',
		container : $('swiffContainer'),
		width: 640,
		height: 480,
		params: {
			wmode: 'opaque',
			bgcolor: '#FFFFFF',
			allowscriptaccess: 'always', 
			allowfullscreen: 'true'
			}
		});
	}
	
	
})
