var Vevo = window.Vevo || {};

// VideoPlayer
Vevo.VideoPlayer = (function() {

	var PLAYERID = 'thePlayer'; // reasonable default?
	var VP = null;
	var $VP = null;

	var playList = false;
	var playListPos = 0;
	var _this = null;
	var isFullScreen = false; // currently in fullscreen mode?

	return {
		// there is some weird stuff going on with the complete event, which is why i'm "watching" it this way
		fire: function(e) {
			//console.warn('Trying to fire: ' + e.type);
			if (e.type === 'complete') {
				//console.info('firing: complete on ' + e.isrc);
				$(Vevo).trigger('videoComplete');
			}
			else {
				//console.warn('firing: ' + e.type + ' on ' + e.isrc);
				$(swfobject.getObjectById(PLAYERID)).trigger(e.type, e);
			}
		}, // fire

//		popout: function(e, eventObj) {
//			// eventObj comes from the flash player. it will have: type (event type), isrc, playing (bool), and time
//			var playlistParams = (Vevo.pageData.pageType == 'watchPlaylist') ? '&isPlaylist=true&playlistId=' + Vevo.pageData.playlistID : '';
//			window.open('/Watch/Popout?videoId=' + eventObj.isrc + '&autoplay=' + eventObj.playing + '&startTime=' + eventObj.time + playlistParams, 'PopoutPlayer', 'width=850,height=350,location=0,toolbar=0,menubar=0,directories=0,scrollbars=0,status=0,resizable=0');
//		}, // popout

		fullscreen: function(e,data) {
			//console.log(data);
			isFullScreen = data.fullscreen;
			$(Vevo).trigger('videoFullscreenChange');
		}, // fullscreen

		seek: function(idx) {
			swfobject.getObjectById(PLAYERID).seek(idx);
		}, // seek

		pause: function() {
			swfobject.getObjectById(PLAYERID).pause();
		}, // pause

		play: function() {
			swfobject.getObjectById(PLAYERID)._play();
		}, // play
	
		isPlaying: function() {
			return swfobject.getObjectById(PLAYERID)._isPlaying();
		},

		stop: function() {
			swfobject.getObjectById(PLAYERID)._stop();
		}, // stop

		load: function(e, isrc, isPlaylist) {
			swfobject.getObjectById(PLAYERID).loadVideo(isrc, isPlaylist);
			$(Vevo.VideoPlayer).trigger('videoLoaded', [isrc]);
		}, // load

		toggleAutoPlay: function(e) {
			swfobject.getObjectById(PLAYERID).setAutoPlay(!VP.getAutoPlay());
		}, // setAutoPlay

		setPlaylistId: function(id) {
			try {
				swfobject.getObjectById(PLAYERID).setPlaylistId(id);
			} catch(e) {} ;
		}, // setAutoPlay

		isAvailable:function() {
			try {
				return typeof (swfobject.getObjectById('thePlayer').loadVideo) == 'function';
			} catch(e) { return false; } ;
		},

		init: function() {
			//console.info('Initializing JS Flash API');

			_this = this;

			// first i need to reference the player
			VP = swfobject.getObjectById(PLAYERID);
			$VP = $(VP);

			// "anonymous" function for binding
			//var lambda = function() { };

			// basically, maps the desired events (from the Flash player) to nice DOM-friendly events
			var events = ['complete', 'fullscreen', 'error'];
			for (var i = 0, eLen = events.length, e; i < eLen; i++) {
				VP.addFlashListener(events[i], 'Vevo.VideoPlayer.fire');
				//$VP.bind(events[i], lambda);
			}
			//$VP.bind('popout', this.popout);
			$VP.bind('fullscreen', this.fullscreen);

			//$(Vevo).trigger('playerAvailable');
		} // init

	}; // returning

})();    // VideoPlayer

$(document).ready(function() {
	$(Vevo).bind('loadVideo', Vevo.VideoPlayer.load);
});
