//################################
 //##Player Control Functions
 //This is an example of usage of the Unicorn Media SDK in communication with the Unicorn Media Player API, and is being provided for your use on an “as is” basis and without warranty implied or otherwise. Use of this software and example is at your own risk and it may not function properly if modified or altered in such a manner as it ceases to interface properly with the Unicorn Media System. If you are having difficulty with the function or implementation of this software, please contact support@unicornmedia.com.
 //################################
var um = {
	videoEnd: 'open_special',
	playerHomeRedirected: false,
	previouslyPaused: false,
	addEventListeners: function(pause) {
		if (pause) {
			this.thisMovie("mainplayer").UMIExt_JS_AddEventListener("UMOExt_Evt_PlayerState", "um.handle_UMOExt_Evt_PlayerState");
		}
		this.thisMovie("mainplayer").UMIExt_JS_AddEventListener("UMOExt_Evt_MediaItemPlayComplete", "um.handle_UMOExt_Evt_MediaItemPlayComplete");
	},
  	// Returns the handle of the movie to interact with.
    thisMovie: function(movieName) {
    var winobj = undefined;
    var docobj = undefined;

    if (navigator.appName.indexOf("Microsoft") != -1) {
        winobj = window[movieName];
        if (winobj != undefined) {
            return winobj;
        } else {
            docobj = document[movieName];
            if (docobj != undefined)
                return docobj;
        }
    } else {
			docobj = document[movieName];
			if (docobj != undefined)
				return docobj;
		}

    	docobj = document.getElementById(movieName);
    	return docobj;
	},

	// Pauses the current mediaitem
	pause: function(movie) {
		if (typeof this.thisMovie(movie).UMIExt_JS_Pause == 'function') {
			this.thisMovie(movie).UMIExt_JS_Pause();
		}
	},

	mainPlayerState: function(movie) {
		if (!this.previouslyPaused) {
			if (this.thisMovie(movie).UMIExt_JS_GetPlayheadTime() <= 0) {
				window.setTimeout(function() { um.mainPlayerState('mainplayer');}, 50);
			} else {
				this.pauseMain();
			}
		}
	},
	
	pauseMain: function() {
		this.previouslyPaused = true;
		this.thisMovie('mainplayer').UMIExt_JS_Pause();
	},

	getMediaItemGuid: function(movie) {
		return this.thisMovie(movie).UMIExt_JS_GetMediaItemGuid();
	},

	handle_UMOExt_Evt_PlayerState: function(playerState) {
		if (playerState === 'videoplayer_playing') {
			um.mainPlayerState('mainplayer');
		}
	},

	handle_UMOExt_Evt_MediaItemPlayComplete: function() {
		var currentGUID = this.getMediaItemGuid('mainplayer');
		switch (this.videoEnd) {
			case 'open_special':
				if (!this.playerHomeRedirected && $('.MAIN_DefaultShow').find('input[name=uFlashvars]').val() == currentGUID) {
					$('a[rel=MAIN_ButtonsNew]:eq(0)').click();
					this.playerHomeRedirected = true;
				}
			break;

			case 'play_next_video':
				var nextVideo = $('.BTN_Current', '.MAIN_ButtonsOn').parent('li').nextAll('li').length;
				if (nextVideo > 0) {
					nextVideo = (5 - nextVideo);
					$('li a:eq(' + nextVideo + ')', '.MAIN_ButtonsOn').click();
				}
			break;
		}
	}
};

function UMOExt_JS_MediaItemPlayStart() {
//	console.log('player start');
}

