(function() {

var Dom = YAHOO.util.Dom;
	
MspotPlayer = {
	player: null,
	
	MIN_VERSION: '10.1.0',

	createPlayer: function() {
		var self = this;
		var callback = function(e) { self.initializeBridge();  };
		YAHOO.util.Event.onAvailable('flashPlayer', callback);
		document.getElementById('topPane').innerHTML = '<div style="height: 360px; background-color:#000;"><div style="margin-left: 132px;"><div id="flashPlayerContainer"></div></div></div>';
		if(swfobject.hasFlashPlayerVersion(MspotPlayer.MIN_VERSION)) {
			swfobject.embedSWF(URL_BASE + 'flash/' + MSPOT_PLAYER_NAME, 'flashPlayerContainer', 
					800, 360, MspotPlayer.MIN_VERSION, false, {'bridgeName': 'mspotPlayer', 'conf': PLAYER_CONF}, {'allowFullScreen': "true", 'bgcolor': '#000000'}, {'id': 'flashPlayer'});		
		}
		else {
			document.getElementById('topPane').innerHTML = '<div class="error-frame">' + 
				'Flash 10 or greater is required to use this site.<br><br>' + 
				'<a href="http://www.adobe.com/go/getflashplayer" rel="nofollow">' + 
				'	<img src="../sprites/get_flash_player.gif" border="0" alt="Get Adobe Flash player">' + 
				'</a>' + 
				'</div>';
		}
	},
	
	initializeBridge: function() {
		var self = this;
		FABridge.addInitializationCallback("mspotPlayer", function() { self.onPlayerReady(); });
	},

	onPlayerReady: function() {
		this.player = FABridge.mspotPlayer.root();
		if(this.playerReadyCallback) {
			this.playerReadyCallback(this.player);
		}
		
		// Add an event listener to refresh the header if the user logs in
		if(refreshHeader) {
			this.player.addEventListener('login', refreshHeader);
		}
	},

	playerAction: function(f) {
		Dom.get('topPane').scrollIntoView();
		if(this.player) {
			try {
				f(this.player);
			}
			catch(e) {
				// Try to recreate the bridge
				this.playerReadyCallback = f;
				this.initializeBridge();
			}
		}
		else {
			this.playerReadyCallback = f;
			this.createPlayer();
		}
	},

	playMovie: function(id) {
		this.playerAction(function(player) {
			player.playMovie(id.toString());
		});
	},

	playTrailer: function(id) {
		this.playerAction(function(player) {
			player.playTrailer(id.toString());
		});
	},
	
	rentMovie: function(id) {
		this.playerAction(function(player) {
			player.rentMovie(id.toString());
		});
	},
	
	// Initializes the player from the current url if arguments are present
	loadFromUrl: function() {
		// save movie offset when user leave the page
		window.onbeforeunload = function() {if(MspotPlayer.player.handleClosePlayer()){ return "The movie you are currently playing will be stopped when you leave the page. \nThis Movie will automatically resume to current position next time you start it.";}};

		if(window.location.hash != '' && window.location.hash.length > 1) {
			var paramsList = window.location.hash.substr(1).split('&');
			var params = {};
			for(var i = 0; i < paramsList.length; i++) {
				var p = paramsList[i].split('=');
				params[p[0]] = p[1];
			}

			if(params.action) {
				var action = params.action;
				if(action == 'playMovie' && params.id) {
					MspotPlayer.playMovie(params.id);				
				}
				else if(action == 'playTrailer' && params.id) {
					MspotPlayer.playTrailer(params.id);
				}
				else if(action == 'rentMovie' && params.id) {
					MspotPlayer.rentMovie(params.id);
				}
			}
		}		
	}
};

})();

