﻿// PlaylistPicker.js
// PlaylistPicker javascript class definition

// Chooses a playlist for a video player component based on the day of the week

function PlaylistPicker()
{

    
    ///////////////////////////////////////
    // Private member data
    ///////////////////////////////////////
    
    var VERSION_STRING = "v0.1.0";

    // Convention for self-reference
    var _instance = this;

    // Used to request playlist information from the server
    var m_strPlaylistInfoURL = "";
    var m_objPlaylistInfoRequest = null;
    var m_objPlaylistInfoXML = null;
    
    var m_objPlaylistArray = null;
    
    // Error details
    var m_strErrorMessage = "";
    
    // Used to hide a necessarily public function from external scripts
    var m_bFetchingInfo = false;
    
    ///////////////////////////////////////
    // Expose public interfaces
    ///////////////////////////////////////
    this.SetPlaylistInfoURL = SetPlaylistInfoURL;
    this.GetPlaylistInfoURL = GetPlaylistInfoURL;
    
    this.LoadInfo = LoadInfo;
    
    this.GetPlaylist = GetPlaylist;
    
    this.GetLastErrorMessage = GetLastErrorMessage;
    
    // User-supplied callback when XML info is loaded.
    this.OnInfoLoaded = null;
    
    // For internal use only, should not be called externally
    this.OnResult = OnResult;
    
    ///////////////////////////////////////
    // Accessors and manipulators
    ///////////////////////////////////////
    
    function SetPlaylistInfoURL(strPlaylistInfoURL)
    {
        m_strPlaylistInfoURL = strPlaylistInfoURL;    
    }
    
    function GetPlaylistInfoURL()
    {
        return m_strPlaylistInfoURL;
    }
    
    function GetLastErrorMessage()
    {
        return m_strErrorMessage;
    }
    
    ///////////////////////////////////////
    // Operations
    ///////////////////////////////////////
    
    function LoadInfo()
    {
        // Verify params
        if("" == m_strPlaylistInfoURL)
        {
            m_strErrorMessage = "You must call SetPlaylistInfoURL() before calling LoadInfo().";
            return false;
        }
        
        //
        // Create XML HTTP Request object
        //        
        try
        {
            // Mozilla and Safari browsers
            if(window.XMLHttpRequest)
            {
                m_objPlaylistInfoRequest = new XMLHttpRequest();
            }
            // Internet Explorer
            else if(window.ActiveXObject)
            {
                m_objPlaylistInfoRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else
            {
                throw "ERROR!";            
            }
        }
        catch(err)
        {
            m_objPlaylistInfoRequest = null;
            m_strErrorMessage = "The browser does not support or allow the XMLHttpRequest object.";
            return false;
        }
        
        m_objPlaylistArray = new Array();
        
        RequestInfo();
    
        return true;
    
    }
    
    function GetPlaylist(nDayIndex)
    {
        // Check param and assign default value
        if(null == nDayIndex)
        {
            var objToday = new Date();
            nDayIndex = objToday.getDay();
        }
        
        if(null != m_objPlaylistArray)
        {
            if(null == m_objPlaylistArray[nDayIndex] || "" == m_objPlaylistArray[nDayIndex])
            {
                return null;
            }
            else
            {
                return m_objPlaylistArray[nDayIndex];
            }        
        
        }
        else
        {
            return null;
        }
    
    }
    
    
    ///////////////////////////////////////
    // Internal utility functions
    ///////////////////////////////////////
    
    function RequestInfo()
    {
        if(null == m_objPlaylistInfoRequest)
        {
            return;
        }
        
        m_bFetchingInfo = true;
        
        // Make sure scope of the handler is same as class scope       
        var _this = _instance;
        m_objPlaylistInfoRequest.onreadystatechange = function(){_this.OnResult();};
            
        m_objPlaylistInfoRequest.open("GET", m_strPlaylistInfoURL, true);    
        m_objPlaylistInfoRequest.send(null);
        
    }
    
    function OnResult()
    {        
        // State "complete"
        if(4 == m_objPlaylistInfoRequest.readyState)
        {
             // Hide this call from external scripts
            if(!m_bFetchingInfo)
            {    
                return;
            }
            m_bFetchingInfo = false;
    
            // HTTP return code "OK"
            if(200 == m_objPlaylistInfoRequest.status)
            {
                m_objPlaylistInfoXML = m_objPlaylistInfoRequest.responseXML;
                OnPlaylistFetched();
            }
            else
            {
                m_strErrorMessage = "HTTP Request returned with status code: " + m_objPlaylistInfoRequest.status;
                OnPlaylistFetched();
            }
        }
    }
    
    function OnPlaylistFetched()
    {
        // On success, parse the playlist info
        if(null != m_objPlaylistInfoXML)
        {
            ParseInfo();
            if(null != _instance.OnInfoLoaded)
            {
                _instance.OnInfoLoaded(true);
            }
        }
        else
        {
            if(null != _instance.OnInfoLoaded)
            {
                _instance.OnInfoLoaded(false);
            }
        }    
    
    }
    
    function ParseInfo()
    {
        var objPlaylists = m_objPlaylistInfoXML.getElementsByTagName("Playlist");
        
        for(var i = 0; i < objPlaylists.length; i++)
        {
            // Insert playlist name into array / hash table based on day index
            m_objPlaylistArray[objPlaylists[i].getAttribute("day_index")] = objPlaylists[i].getAttribute("playlist_name");
        }
            
    
    }
}

