/**
 * @author ezdermam
 * 
 * This class will respond to the video player events in the video-detail page
 */
if(typeof NICK == "undefined" || !NICK) var NICK = {};

NICK.namespace = function() {
    var a = arguments, o = null, i, j, d;
    for (i = 0; i < a.length; i++) {
        d = a[i].split(".");
        o = NICK;

        // NICK is implied, so it is ignored if it is included
        for (j = (d[0] == "NICK") ? 1 : 0; j < d.length; j++) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
};

NICK.namespace("videoPlayer");
	
var player;
var controller;

NICK.videoPlayer.init = function(playerName){
	controller = new MTVNPlayerController(playerName, 'NICK.videoPlayer.onPlayerLoaded');
}

NICK.videoPlayer.onPlayerLoaded = function (controller){
    player = controller.player;
    player.addEventListener('READY', 'NICK.videoPlayer.onReady');
    player.addEventListener('METADATA', 'NICK.videoPlayer.onMetaData');
    player.addEventListener('STATE_CHANGE', 'NICK.videoPlayer.onStateChange');
    player.addEventListener('PLAYHEAD_UPDATE', 'NICK.videoPlayer.onPlayheadUpdate');
    player.addEventListener('NO_AD', 'onNoAd');
    player.addEventListener('AD_PACKAGE_LOADED', 'NICK.videoPlayer.onAdPackageLoaded');
    player.addEventListener('MEDIA_ENDED', 'NICK.videoPlayer.onMediaEnded');
    player.addEventListener('PLAYLIST_COMPLETE', 'NICK.videoPlayer.onPlaylistComplete');
    player.addEventListener('PLAYLIST_ERROR', 'NICK.videoPlayer.onPlaylistError');
    //alert("LOADED");   
};

NICK.videoPlayer.onReady = function(){
    //updatePlaylist(player.getPlaylist().index);
	$(document).trigger("PlayerEvent.ON_READY"); 
};

NICK.videoPlayer.onMetaData = function(metadata){
	$(document).trigger("PlayerEvent.ON_META_DATA", metadata); 
    //alert("Now watching: " + metadata.franchise);
};

NICK.videoPlayer.onStateChange = function(playState){
	$(document).trigger("PlayerEvent.ON_STATE_CHANGE", playState); 
    //alert("playstate change");
};

NICK.videoPlayer.onPlayheadUpdate = function(info){
	$(document).trigger("PlayerEvent.ON_PLAYHEAD_UPDTAE", info); 
    //alert("playhead update");
};

NICK.videoPlayer.onNoAd = function(info){
	$(document).trigger("PlayerEvent.ON_NO_AD", info); 
    //alert("no ad");
};

NICK.videoPlayer.onAdPackageLoaded = function(info){
	$(document).trigger("PlayerEvent.ON_AD_PACKAGE_LOADED", info); 
    //alert("onAdPackageLoaded");
};

NICK.videoPlayer.onMediaEnded = function(info){
	NICK.utils.doLog("player.getMetadata().isAd:"+player.getMetadata().isAd);
	NICK.utils.doLog("player.getMetadata().isBumper:"+player.getMetadata().isBumper);
	if(player.getMetadata().isAd!=true && player.getMetadata().isBumper!=true){
		
		$(document).trigger("PlayerEvent.ON_MEDIA_ENDED", info); 
	}
   // alert("onMediaEnded");
};

NICK.videoPlayer.onPlaylistComplete = function (info){
	$(document).trigger("PlayerEvent.ON_PLAYLIST_COMPLETE", info); 
    //player.playIndex(0, 0);
};

NICK.videoPlayer.onPlaylistError = function(info){
	$(document).trigger("PlayerEvent.ON_PLAYLIST_ERROR", info); 
    //alert("onPlaylistError");
};

NICK.videoPlayer.nextVideo = function(){
	$(document).trigger("PlayerEvent.ON_PLAYHEAD_UPDTAE", info); 
    if (player && !player.getMetadata().isAd && !player.getMetadata().isBumper) {
        player.next();
    }
};

NICK.videoPlayer.prevVideo = function(){
    if (player && !player.getMetadata().isAd && !player.getMetadata().isBumper) {
        player.previous();
    }
};

NICK.videoPlayer.getCMSIDfromGUID = function(guid){
    var a = guid.split(":");
    return (a[4]);
};

NICK.videoPlayer.playVideo = function(index){
    //alert("playvideo = " + index);
    if (player && !player.getMetadata().isAd && !player.getMetadata().isBumper) {
        player.playIndex(index, 0);
    }
};

NICK.videoPlayer.thisMovie = function(movieName){
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
        return document[movieName];
    }
}

//coad
NICK.videoPlayer.mtvnSetCoad = function(adObject){
	try{
		if(KIDS.get("videoType").length>0 || KIDS.get("isPlayList")=="true"){
		    alert("video ad, set coad");
			var src = adObject.url;
		    var w = adObject.width;
		    var h = adObject.height;
		    var clickTag = adObject.clickUrl;
		    document.getElementById("ad-300x250IF").src = src;
		    $("#ad-300x250IF").attr("id","coad300x250");
		}
	}catch(e){KIDS.utils.doLog("NICK.videoPlayer.mtvnSetCoad failed:" + e.toString());}
};
mtvnSetCoad  	= NICK.videoPlayer.mtvnSetCoad;
