
var Controller = new Class({

	initialize: function()
	{
		LB = new lb($('lb'), PATH.root + PATH.site + 'lb/');
		$$('.programBlock').each(function(e){new ProgramBlock(e);})
		$$('.navItem').each(function(e){new NavItem(e);})
		new SmoothScroll({duration:800} );

	},
	
	doAction: function(action, instance)
	{
		VO.action =  action;
		this.instance = instance;
		this.url = PATH.root + PATH.site + 'php/do.php';
		
		switch(action)
		{
			case 'getDayProgram':
				/*var locationHashAsString = instance.getProperty("rel");//.replace('#', '');// window.location.hash.replace('#', '')
				var hashVars = locationHashAsString.split('&');
				this.locationHash = new Hash(); 
				hashVars.each
				(
					function(e)
					{
						 var keyValue = e.split("=");
						 this.locationHash.include(keyValue[0], keyValue[1]);
						  if(VO.has(keyValue[0]))
						 {
							VO.set(keyValue[0], keyValue[1]);
						 }else{
							VO.include(keyValue[0], keyValue[1]);
						 }
					}.bind(this)
				);*/
				VO.d = instance.id; 
				this.update = $('today');
				this.doRequest('html');
				break;
			case 'initLB':
				if(VO.actionBeforeLB == 'openMovie')
				{
					$$('.movieBlock').each(function(e){;new MovieBlock(e);})
				}
				if(VO.actionBeforeLB == 'openPage')
				{
					$$('.pageBlock').each(function(e){;new PageBlock(e);})
				}
				LB.doPosition();
				break;
			case 'closeLB':
				LB.doClose();
				break;
			case 'openPage':
				VO.id = instance.id;
				this.currentPage = VO.id;
				VO.actionBeforeLB = 'openPage';
				LB.doLoad(VO, 850);
				break;
			case 'openMovie':
				VO.id = instance.id;
				this.currentMovie = VO.id;
				VO.actionBeforeLB = 'openMovie';
				LB.doLoad(VO, 850);
				break;
			case 'openNextMovie':
				this.currentMovie = this.getMovie('next');
				VO.id = this.currentMovie;
				action = 'openMovie';
				VO.action = action;
				VO.actionBeforeLB = action;
				LB.doLoad(VO, 850);
				break;
			case 'openPreviousMovie':
				this.currentMovie = this.getMovie('previous');
				VO.id = this.currentMovie
				action = 'openMovie';
				VO.action = action;
				VO.actionBeforeLB = action;
				LB.doLoad(VO, 850);
				break;
			case 'setReservationShowTime':
				//VO.id = instance.getProperty('id');
				VO.id = instance.id;
				this.update = $('reservationBlock');
				this.doRequest('html');
			break;
			case 'cancelReservation':
				$('reservationShowTimeBlock').setStyle('display','block');
				$('reservationBlock').setStyle('display','none');
				$('reservationEmail').removeEvents('keydown');
				$('reservationAmount').removeEvents('keydown');
				$('reservationAmountTrue').setStyle('display', 'none');
				$('reservationEmailTrue').setStyle('display', 'none');
				break;
			case 'doReservation':
				VO.email = escape($('reservationEmail').value);
				VO.amount = escape($('reservationAmount').value);
				this.update = $('reservationBlock');
				this.doRequest('html');
				break;
			default:
				alert(action);
		}
	},
	
	afterRequest:function(rT, rE, rH, rJ)
	{
		VO.checkSession = false;

		switch(VO.action)
		{
			case 'getDayProgram':
				$$('#today .navItem').each(function(e){new NavItem(e);})
				break;
			case 'setReservationShowTime':
				$('reservationShowTimeBlock').setStyle('display','none');
				$('reservationBlock').setStyle('display','block');
				$('reservationEmail').focus();
		
				this.reservation = false;
				this.amount = false;
				$('reservationEmail').addEvents
				({
					'keyup': this.handleReservationEmailKeyDown.bind(this)
				});
				$('reservationAmount').addEvents
				({
					'keyup': this.handleReservationAmountKeyDown.bind(this)
				});
				break;
			case 'setReservationShowTime':
				
				break;
			case 'doReservation':
				
				break;
			default:
			alert(rH);
		}
	},
	
	doRequest:function(type)
	{
		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;
			case 'json':
				new Request.JSON({url:this.url, method:'post', data:this.vo, onComplete:function(rT, rE, rH, rJ){endRequest();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();
		}
	},
	
	handleReservationEmailKeyDown: function(event)
	{
		if(this.checkEmail($('reservationEmail').value))
		{
			this.reservation = true;	
			$('reservationEmailTrue').setStyle('display', 'block');
		}else{
			this.reservation = false;
			$('reservationEmailTrue').setStyle('display', 'none');
		}
		this.checkReservation();
	}, 
	
	handleReservationAmountKeyDown: function(event)
	{
		if(this.checkAmount($('reservationAmount').value))
		{
			this.amount = true;	
			$('reservationAmountTrue').setStyle('display', 'block');
		}else{
			this.amount = false;
			$('reservationAmount').value = '';
			$('reservationAmountTrue').setStyle('display', 'none');
		}
		this.checkReservation();
	}, 
	
	checkReservation: function()
	{
		if(this.amount && this.reservation)
		{
			$('reservationButton').setProperty('disabled', false);	
		}else{
			$('reservationButton').setProperty('disabled', true);
		}
	},
	
	checkEmail: function(email)
	{	
		var re = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		var check = email.match(re)? true : false;
		return check;
	},
	
	checkAmount: function(amount)
	{	
		var re = /[1-8]/g;
		var check = amount.match(re)? true : false;
		return check;
	},
	
	getMovie:function (which)
	{
		index = PROGRAMDATA.indexOf(this.currentMovie);
		switch(which)
		{
			case 'next':
			index++;
			if(index == PROGRAMDATA.length) index = 0;
			break;
			case 'previous':
			index--;
			if(index == -1) index = PROGRAMDATA.length - 1;
			break;
		}
		return PROGRAMDATA[index];
	}
	
})