var Media = {};

(function($) {
	
	$.extend(Media, {
		isHidden : false,
		childWindows : [],
		players : {},
		playerContainer : null,
		playerContainerParent : null,
		init : function ()
		{
			this.playerContainer = $('#silverlightcontainer');
			this.playerContainerParent = $( this.playerContainer.parent().get(0) );
			
			// store references
			this.players.id1 = $('#player_id_1');
			this.players.id2 = $('#player_id_2');
			
			// grab current player from cookie
			this.playerId = parseInt( $.cookie('player_version'), 10 );
			if (!this.playerId)
				this.playerId = 2;
			
			this.swap_players(this.playerId);
		},
		hide : function ()
		{
			if (this.isHidden)
				return;
			
			this.playerContainer.hide().remove();
			$('#media_swap').slideDown();
			this.isHidden = true;
		},
		show : function ()
		{
			if (!this.isHidden)
				return;

			this.closeChildWindows();
			$('#media_swap').hide();
			this.playerContainer.prependTo(this.playerContainerParent).show();
			this.isHidden = false;
		},
		closeChildWindows : function ()
		{
			while(this.childWindows.length) {
				try {
					this.childWindows.pop().close();
				} catch (e) {
					// cross domainageness
				}
			}
		},
		childIsClosed : function (winRef)
		{
			for(var i=0; i < this.childWindows.length; i++) {
				if (this.childWindows[i] == winRef) {
					this.childWindows.splice(i, 1);
					break;
				}
			}
		},
		new_window : function (link)
		{
			var uri = link.href + '&playerid=' + (this.playerId ? this.playerId : 2);
			this.childWindows.push(
				window.open(
					uri,
					'smallPlayer',
					'left=20,top=20,width=620,height=348,toolbar=0,location=0,status=0,menubar=0,directories=0,resizable=1'
					)
				);
			this.hide();
		},
		fullscreen : function (link)
		{
			var w = screen.width ? screen.width : 800,
				h = screen.height ? screen.height: 450,
				uri = link.href + '&playerid=' + (this.playerId ? this.playerId : 2);
			this.childWindows.push(
				window.open(
					uri,
					'smallPlayer',
					'left=0,top=0,width='+w+',height='+h+',toolbar=0,location=0,status=0,menubar=0,directories=0,resizable=1'
					)
				);
			this.hide();
		},
		enlarge : function ()
		{
			if (this.enlarge_animate == true)
				return false;
			
			this.enlarge_animate = true;
			$right = $('#media_wrap > .media_wrap_right');
			$left = $('#media_wrap > .media_wrap_left');
			$cont = $('#silverlightcontainer');
			$control = $('#media_controls .enlarge');
			$this = this;
			
			if (!this.enlarge_open)
			{
				$right.slideUp().queue( function () {
					$left.addClass('left_wide');
					$cont.css({ 'width': '100%', 'height': '400px' });
					$this.enlarge_animate = false;
					$this.enlarge_open = true;
					$control.html('shrink');
					$(this).dequeue();
				});
			}
			else
			{
				$left.removeClass('left_wide');
				$cont.css({ 'width': '560px', 'height': '315px' });
				$right.slideDown();
				$control.html('enlarge');
				this.enlarge_animate = false;
				this.enlarge_open = false;
			}
			
		},
		show_share : function ()
		{
			alert('coming soon!');
		},
		swap_players : function (id)
		{
			
			// only handle changes
			//if (id && id == this.playerId)
			//	return;
				
			if (!id) {
				this.playerId++;
				if (this.playerId == 3)
					this.playerId = 1;
				id = this.playerId;
			}
			
			// save to cookie
			$.cookie('player_version', id);
			
			switch (id) {
				case 1: // SWITCH TO OLD PLAYER
					this.players.id2.remove();
					this.players.id1.show().prependTo(this.playerContainer);
					$('#media_controls .legacy-player').html('switch back');
					break;
				case 2: // SWITCH TO NEW PLAYER
					this.players.id1.remove();
					this.players.id2.show().prependTo(this.playerContainer);
					$('#media_controls .legacy-player').html('try the legacy player');
					break;
			}
		}
	});
	
	$(function(){
		Media.init();
		$controls = $('#media_controls');
		$('.newwindow', $controls).click(function(){ Media.new_window(this); return false; });
		$('.fullscreen', $controls).click(function(){ Media.fullscreen(this); return false; });
		$('.enlarge', $controls).click(function(){ Media.enlarge(); return false; });
		//$('.share', $controls).click(function(){ Media.show_share(); return false; });
		$('.legacy-player', $controls).click(function(){ Media.swap_players(); return false; });
	});
	
	window.onbeforeunload = function(e) { 
		if (Media.childWindows.length > 0) {
			//return 'Navigating away from here will close all your media popups.';
		} else {
			// show confirmation
			//return '';
		}
	};
	
	
	$(window).unload( function () {
		// forces all windows closed on reload
		// necessary for heatbeat to work properly, but forces person to stay on page while watching
		//Media.closeChildWindows();
		
		// Alert parent window
		try {
			window.opener.Media.childIsClosed(window);
		} catch (e) { }
	});
	
})(jQuery);