RegisterNamespace('NPR.Player');
RegisterNamespace('NPR.Player.Action');
RegisterNamespace('NPR.Player.Type');
RegisterNamespace('NPR.Player.Mode');
  
NPR.Player.MediaPlayerWindow = null;

NPR.Player.Action.PLAY_NOW = 1;
NPR.Player.Action.ADD_TO_PLAYLIST = 2;
NPR.Player.Action.LIVE_STREAM = 3;

NPR.Player.Type.STORY = 1;
NPR.Player.Type.TOPIC = 2;
NPR.Player.Type.PROGRAM = 3;
NPR.Player.Type.NEWSCAST = 4;
NPR.Player.Type.PROGRAM_STREAM = 'live1';
NPR.Player.Type.BUTTON = 6;
NPR.Player.Type.MOST_EMAILED = 100;
NPR.Player.Type.MOST_EMAILED_MUSIC = 102;

NPR.Player.Type.LIVE_STREAM_2 = 'live2';
NPR.Player.Type.LIVE_STREAM_3 = 'live3';
NPR.Player.Type.LIVE_STREAM_4 = 'live4';
NPR.Player.Type.LIVE_STREAM_5 = 'live5';

NPR.Player.Type.STATIC_FILE_1 = 'static1';
NPR.Player.Type.STATIC_FILE_2 = 'static2';
NPR.Player.Type.STATIC_FILE_3 = 'static3';
NPR.Player.Type.STATIC_FILE_4 = 'static4';

NPR.Player.Mode.MODEL_FROM_FILE = 0;
NPR.Player.Mode.MODEL_FROM_DATABASE = 1;

NPR.Player.openPlayer = function(id, mediaId, dateString, action, type, mode) {

    try {
        mode = parseInt(mode);
        var islist = false;
        if (id > 0 &&
                (type == NPR.Player.Type.TOPIC 
                    || type == NPR.Player.Type.PROGRAM
                    || type == NPR.Player.Type.MOST_EMAILED
                    || type == NPR.Player.Type.MOST_EMAILED_MUSIC
                )
            ) {
            islist = true;
        }
    
        var cookieString = "?action=" + action + "&t=" + type + "&islist=" + islist
    
        if (id > 0) {
            cookieString += "&id=" + id;
        }
    
        if (mediaId > 0) {
            cookieString += "&m=" + mediaId;
        }   
    
        if (dateString != null) {
            cookieString += "&d=" + dateString;
        }   
        
        if (mode === NPR.Player.Mode.MODEL_FROM_DATABASE) {
            cookieString += "&live=" + NPR.Player.Mode.MODEL_FROM_DATABASE;
        }   
        
        if (action === NPR.Player.Action.ADD_TO_PLAYLIST) {
        	NPR.metrics.pageEvent(NPR.metrics.constants.ADD_TO_PLAYLIST);
        }
    
        diff = 9999999;
        date1 = new Date();
        date2 = null;
    
        if (NPR.Player.readCookie('playerWindowOpen') !== null) {
            date2 = new Date(NPR.Player.readCookie('playerWindowOpen'));
            diff  = date1-date2;
            //alert(diff);
        }
            
    
        // if we have a handle, and it's open, just call the window directly
        if (NPR.Player.MediaPlayerWindow !== null && !NPR.Player.MediaPlayerWindow.closed) {
            // alert("launching via direct");
            // NPR.Player.createCookie('playListItem',cookieString,1);
            try {
                NPR.Player.MediaPlayerWindow.addPlaylistItem(cookieString);
                NPR.Player.MediaPlayerWindow.focus();
            }
            catch (ex) {
                NPR.messaging.exception(ex, 'player.js', 'NPR.Player.openPlayer', NPR.messaging.constants.PLAYER_JS_ERROR);
                NPR.Player.createCookie('playListItem',cookieString,1);
            }
        }
        // if it's not open, and there's no cookie for the playlist, open a new window
        // the window will erase the cookie when it's closed
        else if (NPR.Player.readCookie('playerWindowOpen') === null
        || diff > 5000  ) {
            // alert("launching new window; diff=" + diff);
            // NPR.Player.createCookie('playListItem',cookieString,1);
            NPR.Player.MediaPlayerWindow = window.open('http://www.npr.org/templates/player/mediaPlayer.html' + cookieString, '_player', 'height=590,width=770,status=yes,toolbar=no,menubar=no,location=no');        
            if (NPR.Player && NPR.Player.MediaPlayerWindow) {
                NPR.Player.MediaPlayerWindow.focus();
            }
        }
        // there is a player, but another parent window opened it.
        // we can't get a handle on it.
        // just set the cookie and hope for the best
        else {
            // alert("launching via cookie; diff=" + diff);
            NPR.Player.createCookie('playListItem',cookieString,1);
        }
    }
    catch (e) {
        NPR.messaging.exception(e, 'player.js', 'NPR.Player.openPlayer', NPR.messaging.constants.PLAYER_JS_ERROR);
    }
    return;
};

function RegisterNamespace(namespacePath) {
    var rootObject = window;
    var namespaceParts = namespacePath.split('.');
    
    for (var i = 0; i < namespaceParts.length; i++) {
        var currentPart = namespaceParts[i];
        
        if (!rootObject[currentPart]) {
            rootObject[currentPart] = new Object();
        }
        
        rootObject = rootObject[currentPart];
    }
};

NPR.Player.createCookie = function(thename,thevalue,days) {
    try {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = thename+"="+thevalue+expires+"; path=/";
    }
    catch (e) {
        NPR.messaging.exception(e, 'player.js', 'NPR.Player.createCookie', NPR.messaging.constants.PLAYER_JS_ERROR);
    }    
};

NPR.Player.readCookie = function(thename) {
    var nameEQ = thename + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
};
