var ProgramBlock = new Class({

	initialize: function(e)
	{
		
		this.e = e;
		
		this.id = this.e.getProperty('id');
		
		if(this.e.getElements('.programBlockText'))
		{
			var programBlockText = new ProgramBlockText(this.e.getElements('.programBlockText')[0]);
		}
		
		this.programBlockImages = Array();
		this.e.getElements('.programBlockImage').each(
			function(e)
			{
				var programBlockImage = new ProgramBlockImage(e, this.e);
				this.programBlockImages.push(programBlockImage);
			}.bind(this)
		);
		
		if(this.e.getElements('.programBlockPayoff'))
		{
			this.payoff = this.e.getElements('.programBlockPayoff')[0];
		}
		
		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;
		
		this.setEdges();
		this.setOverlay();
		this.setEvents();
	},
	
	cancelBubble: function(parent)
	{
		
		parent.getChildren().each(function(child)
		{
			child.addEvent('onmouseover', function(event){event.stopPropagation(); });
		    child.addEvent('onmouseout', function(event){event.stopPropagation(); });
			
			if(child.getChildren().length>0){this.cancelBubble(child);}
		}.bind(this))
	},
	
	setEdges:function()
	{
		var positions = Array('TL','TR','BR','BL');
		
		for(var i=0; i < positions.length; i++)
		{
			var which = Math.floor(Math.random()*2)+1;
			
			switch(positions[i])
			{
				case 'TL':
				var bgpos = 'left top';
				break;
				case 'TR':
				var bgpos = 'right top';
				break;
				case 'BR':
				var bgpos = 'right bottom ';
				break;
				case 'BL':
				var bgpos = 'left bottom';
				break;
			}
			if(which <= 2)
			{
				var overlay = new Element('div', 
				{
					'styles': 
					{
						'display' : 'block',
						'position' : 'absolute',
						'width' : this.w + 'px',
						'height' : this.h + 'px',
						'background' : 'url("/site/img/edge' + positions[i] + which + '.png")',
						'background-position' : bgpos,
						'background-repeat' : 'no-repeat',
						'z-index' :(5+i)
					}
				});
				
				overlay.inject(this.e, 'before');
			}
		}
	},
	
	setOverlay: function()
	{
		this.overlay = new Element('div', 
		{
			'styles': 
			{
				
				'display': 'block',
				'position': 'absolute',
				//'background': 'url(site/img/edges.png)',
				'z-index':10,
				'width': this.w + 'px',
				'height': this.h + 'px'
			}
		}
		);
		
		this.overlay.inject(this.e, 'before');
	},
	
	setEvents: function()
	{
		this.overlay.addEvents
		({
			'mouseover': this.doMouseover.bind(this),
			'mouseout': this.doMouseout.bind(this),
			'click': this.doClick.bind(this)
		});
		
		this.overlay.onclick = function()
		{
			return false;
		};	
		
		this.overlay.onmouseover = function()
		{
			return false;
		};
		
		this.overlay.onmouseout = function()
		{
			return false;
		};
	},
	
	doMouseover: function(ev)
	{
		this.overlay.style.cursor = 'pointer';
		if(this.payoff)this.payoff.setStyle('visibility','visible');
		this.programBlockImages.each(function(e){e.animateOver();})
	},
	
	doMouseout: function(ev)
	{
		this.overlay.style.cursor = 'default';
		if(this.payoff)this.payoff.setStyle('visibility','hidden');
		this.programBlockImages.each(function(e){e.animateOut();})
	},
	
	doClick: function()
	{
		CONTROLLER.doAction('openMovie', this)
	}
	
});
	
