var MovieBlock = new Class({
							   
	initialize:function(e)
	{
		this.e = e;
		
		var coordinates = this.e.getCoordinates();
		this.w = coordinates.width;
		this.h = coordinates.height;
		this.left = coordinates.left;
		this.top = coordinates.top;
		this.right = coordinates.right;
		this.bottom = coordinates.bottom;
		if(this.e.getElements('.movieSlideshow')[0])
		{
			
			this.slideshow = new MovieSlideshow(this.e.getElements('.movieSlideshow')[0]);
		}
		
		this.movieInfoBlock = $('movieInfoBlock');
		
		if(this.e.getElements('.trailerButton'))
		{
			this.trailerButton = this.e.getElements('.trailerButton')[0];
			this.trailerContainer = this.e.getElements('.trailerContainer')[0];
			//this.trailer = escape(this.trailerContainer.getProperty('id') + '&fmt=35');
			
			this.trailer = this.trailerContainer.getProperty('id');
			
			this.trailerOverlay = this.e.getElements('.trailerOverlay')[0];
			this.setTrailerEvents();
		}
		
		this.close = $('closeLightBox');
		this.setLightBoxEvents();
		
		window.addEvents
		({
			'mousewheel': this.handleWindowMousewheel.bind(this)
		}); 
		
		this.movieInfoBlock.addEvents
		({	
			'mousewheel': this.handleMousewheelLightBox.bind(this)
		}); 
	},
	
	setTrailerEvents: function()
	{
		if(this.trailerButton)
		{
			this.trailerButton.addEvents
			({
				'click': this.doTrailerButtonClick.bind(this)
			});
			
			this.trailerButton.onclick = function()
			{
				return false;
			};	
		}
		
	},
		
	doTrailerButtonClick: function()
	{
		this.setTrailerOverlayEvents();
		this.initPlayer();
	},
	
	initPlayer:function()
	{
		this.trailerOverlay.setStyles({'display':'block'});
		var player = new SWFObject('/_lib/mediaplayer-5.2/_player.swf','player','640','355','9','#000000');
		player.addParam('allowfullscreen','true');
		player.addParam('allowscriptaccess','always');
		player.addParam('wmode','opaque');
		player.addVariable('volume', '100');
		player.addVariable('mute', 'false');
		player.addVariable('overstretch', 'false');
		player.addVariable('autostart', 'true');
		player.addVariable('controlbar','none');
		player.addVariable('repeat','none');
		player.addVariable('file', this.trailer);
		player.write(this.trailerContainer);
		this.player = player;
	},
	
	destroyPlayer:function()
	{
		this.trailerOverlay.setStyles({'display':'none'});
		this.setLightBoxEvents();
	},
	
	handleLightBoxKeyDown: function(event)
	{
		this.handleKeyScroll(event);
		if(event.key =='esc')
		{	
			window.removeEvents('mousewheel');
			LB.doClose();
		};
	},
	
	handleLightBoxClick: function(event)
	{
		window.removeEvents('mousewheel');
		this.movieInfoBlock.removeEvents('mousewheel');
		LB.doClose();
	},
	
	handleMousewheelLightBox: function(event)
	{
		this.movieInfoBlock.scrollTo(0, (this.movieInfoBlock.getScroll().y + (-15*event.wheel)));
	},
	
	setLightBoxEvents: function()
	{
		this.removeListeners();
		
		window.addEvents
		({
			'keydown': this.handleLightBoxKeyDown.bind(this),
			'mousewheel': this.handleWindowMousewheel.bind(this)
		}); 
		
		LB.bg.addEvents
		({
			'click': this.handleLightBoxClick.bind(this)
		}); 
		
		this.close.addEvents
		({
			'click': this.handleLightBoxClick.bind(this)
		}); 
		
	},
	
	handleWindowMousewheel: function()
	{
		return false;
	},
	
	handleTrailerOverlayKeyDown: function(event)
	{
		if(event.key =='esc')
		{
			this.destroyPlayer();
		};
		this.handleKeyScroll(event);
		return false;
	},
	
	handleTrailerOverlayClick: function(event)
	{
		this.destroyPlayer();
	},
	
	setTrailerOverlayEvents: function()
	{
		this.removeListeners();
		
		window.addEvents
		({
			'keydown': this.handleTrailerOverlayKeyDown.bind(this)
		}); 
		
		LB.bg.addEvents
		({
			'click': this.handleTrailerOverlayClick.bind(this)
		}); 
		
		this.close.addEvents
		({
			'click': this.handleTrailerOverlayClick.bind(this)
		}); 
	},
	
	removeListeners:function()
	{
		window.removeEvents('keydown');
		LB.bg.removeEvents('click');
		this.close.removeEvents('click');	
	},
	
	handleKeyScroll : function(event)
	{
		if(event.key =='up')
		{
			this.movieInfoBlock.scrollTo(0, (this.movieInfoBlock.getScroll().y - 15));
			return false;

		};
		if(event.key =='down')
		{
			this.movieInfoBlock.scrollTo(0, (this.movieInfoBlock.getScroll().y + 15));
			return false;
		};
		if(event.key =='right')
		{
			if(event.target != $('reservationAmount') && event.target != $('reservationEmail') )
			{
				CONTROLLER.doAction('openNextMovie');
			}
		};
		if(event.key =='left')
		{
			if(event.target != $('reservationAmount') && event.target != $('reservationEmail') )
			{
				CONTROLLER.doAction('openPreviousMovie');
			}
		};
	}
	

})
