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) );
			this.players.id1 = $('#player_id_1').remove();
			this.players.id2 = $('#player_id_2');
		},
		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 ()
		{
			if (!this.playerId)
				this.playerId = 2;
			
			switch (this.playerId) {
				case 1: // SWITCH TO NEW PLAYER
					this.players.id1.hide().remove();
					this.players.id2.prependTo(this.playerContainer).show();
					$('#media_controls .legacy-player').html('try the legacy player');
					this.playerId = 2;
					break;
				case 2: // SWITCH TO OLD PLAYER
					this.players.id2.hide().remove();
					this.players.id1.prependTo(this.playerContainer).show();
					$('#media_controls .legacy-player').html('switch back');
					this.playerId = 1;
					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);