Shadowbox.loadSkin('classic','js/shadowbox-2.0/build/skin');
Shadowbox.loadLanguage('en','js/shadowbox-2.0/build/lang');
Shadowbox.loadPlayer(['html','img','flv','swf'],'js/shadowbox-2.0/build/player');

$(document).ready(function(){
	Shadowbox.init({
		flvPlayer: 'js/shadowbox-2.0/flvplayer.swf',
		handleOversize: 'resize',
		onOpen: function(args)
		{
			//lets the titles extend beyond the width of the box for
			//slightly longer titles that normally wrap to the next line.
			$('#shadowbox_title_inner, #shadowbox_title')
				.css({
					'font-size':'13px',
					'font-family':'"Lucida Grande", Arial, Verdana, sans-serif',
					'white-space':'nowrap',
					'overflow':'visible',
					'opacity': 0
				});
			
			//this is used to start the timer in the event that the video player
			//isn't fully loaded yet. this typically only happens on the first 
			//load of the player.
			if(args.player === 'flv')
				timer.start();
		},
		onFinish: function(args)
		{
			//lets the titles extend beyond the width of the box for
			//slightly longer titles that normally wrap to the next line.
			$('#shadowbox_title_inner, #shadowbox_title')
				.animate({
					'opacity':1
				},500);
				
			//onFinish isn't called after all of the new content is loaded
			//#shadowbox_content doesn't exist by this point, but it needs this event listener
			//there are no good hooks for post-finish in shadowbox.
			setTimeout(function(){
				if(args.player === 'flv')
				{
					var p = window.document['shadowbox_content'];
					p.addModelListener('STATE','track');
				}
			},200);
			
		},
		onChange: function()
		{
			//lets the titles extend beyond the width of the box for
			//slightly longer titles that normally wrap to the next line.
			$('#shadowbox_title_inner, #shadowbox_title')
				.css({
					'opacity': 0
				});
			
			//onChange can be called without the user stopping the video.
			//this sends an event to the player so that it can be tracked
			//in google analytics
			if($('object#shadowbox_content')[0])
			{
				var p = window.document['shadowbox_content'];
				p.sendEvent('STOP');
			}
		},
		onClose: function(args)
		{
			//a user can close the shadowbox before the video is done and w/o stopping it.
			//this line forces google analytics to track the event of "stopping" the video.
			if(args.player === 'flv' && timer.isTiming)
			{
				pageTracker._trackEvent('Video','Watch Time [Stopped]',args.title,timer.stop());
			}
		}
	});
	
	//added because target is not a valid attribute for a tags
	$('a.external').attr('target','_blank');
});

function track(obj)
{
	console.log(obj.newstate);
	var title = $('#shadowbox_title_inner').html();
	if(obj.oldstate !== obj.newstate)
	{
		switch(obj.newstate)
		{
			case 'PLAYING':
				timer.start();
				break;
			case 'PAUSED':
				timer.pause();
				break;			
			case 'IDLE':
				pageTracker._trackEvent('Video','Watch Time [Stopped]',title,timer.stop());
				timer.reset();				
				break;
			case 'COMPLETED':
				pageTracker._trackEvent('Video','Watch Time [Completed]',title,timer.stop());
				timer.reset();
				break;
		}
	}
}