var VideoBox = new Class(
{
	initialize: function()
	{
		// assume no video links will be found //
		this.foundVideos = false;
		
		// inspect all the links on this page //
		$$('.content-area a').each((function(el)
		{
			if (el.hasClass('ignore-video')) return;
			
			var url = el.getProperty('href');
			if (url.contains('youtube.com') || url.contains('.flv'))
			{
				el.addEvent('click', this.onVideoClick.bind(this, el));
				this.foundVideos = true;
			}
		}).bind(this));
	
		if (!this.foundVideos) return;
		
		this.topOpen = 75;
		this.topClosed = -800;
		
		this.canvas = new Element('div', {'class':'abs', style:'width:550px; height:440px; z-index:1'}).setStyle('top', this.topClosed);
		this.shadow = new Element('div', {'class':'abs', style:'width:550px;height:440px;background-color:#000;z-index:-1'}).setStyle('opacity', 0.75).inject(this.canvas);
		
		this.titlebar = new Element('div', {style:'text-align:right; line-height:1.5em;padding:3px;'}).inject(this.canvas);
		this.titlebar.grab(new Element('a', {href:'javascript:void(0);', title:'hide', text:'HIDE', style:'padding-right:5px;'}));
		
		this.videobox = new Element('div', {style:'margin:0 auto;width:460px;margin-top:46px;'}).inject(this.canvas);
		
		this.flashbox = new Element('div', {style:'height:258px;', 'class':'flash'}).inject(this.videobox);
		this.descr = new Element('p', {style:'padding:10px;background-color:#000;color:#FFF;'}).inject(this.videobox);
		
		this.titlebar.getFirst().addEvent('click', this.onCloseClick.bind(this));
		
		$(document.body).grab(this.canvas);
		
		this.canvas.setStyle('left', (window.getSize().x - this.canvas.getSize().x) / 2); 
		
		this.canvas.set('tween', 
		{
			duration : 900,
			onComplete : this.onTweenComplete.bind(this),
			transition : Fx.Transitions.Bounce.easeOut
		});
		
	},
	
	onCloseClick: function()
	{
		this.canvas.tween('top', this.topClosed);
		(function(){this.flashbox.empty();}).delay(500, this);
	},
	
	onVideoClick: function(el)
	{
		$(document.body).scrollTo(0,0);
		
		this.canvas.tween('top', this.topOpen);
		this.descr.set('text', el.getProperty('title'));
		var url = el.getProperty('href');
		if (url.contains('youtube.com'))
			this.embedYoutube(el.getProperty('href'));
		else
			this.embedVideo(el.getProperty('href'));
		return false;
	},
	
	onTweenComplete: function()
	{
		//console.log('hi');	
	},
	
	embedVideo: function(url)
	{
		var embed = new Swiff(TEMPLATE_URL + '/swf/mediaplayer.swf', 
		{
		    id: 'flvplayer',
		    width: 460,
		    height: 258,
		    params: {
		        wmode: 'opaque',
		        bgcolor: '#ffffff',
		        quality: 'high',
				allowfullscreen: 'true'
		    },
		    vars: {
		        width: 460,
		        height: 258,
		        file: url,
		        image: '',
		        autostart:true
		    },
		    callBacks: { }
		});
		
		this.flashbox.empty().grab(embed);
	},
	
	embedYoutube: function(url)
	{
		var embed = new Swiff(url.replace('watch?v=', 'v/') + '?hl=en_US&amp;fs=1&amp;rel=0', 
		{
		    id: 'flvplayer',
		    width: 460,
		    height: 258,
		    params: {
		        wmode: 'opaque',
		        bgcolor: '#ffffff',
		        quality: 'high',
				allowfullscreen: 'true'
		    },
		    vars: {},
		    callBacks: { }
		});
		
		this.flashbox.empty().grab(embed);
	}
});


var LinkTargeter = new Class(
{
	initialize: function()
	{
		// find all sound file links and give them a target attribute //
		$$('a').each(function(el)
		{
			var href = el.getProperty('href');
			if (href.test(/.*\.mp3|.*\.aif|.*\.4mp|.*\.au|.*\.m1a|.*\.wav/)) 
				el.setProperty('target', '_new');
			else if (href.test(/^http:.*/) && href.test('^http:..' + window.location.host + '.*') == false)
				el.setProperty('target', '_new');
		});
	}
});

window.addEvent('domready', function()
{
	new VideoBox();
	new LinkTargeter();
});
