﻿function getCurrentCatalog()
{
    Debug("in getCurrentCatalog",1);

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=FULL" + "&WL=" + glGet("glWhiteLabel") + "&UID=" + glGet("glUserID") + "&PLAT=" + glGet("glPlatformName") + "&VER=" + glGet("glVersionNumber"),
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("guide loaded successfully",1);
                hideLoadingMessage();
                document.getElementById("mainBody").innerHTML += parseCatalog(msg.childNodes[0].childNodes[0],true);
           },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
           }
         });
    }
    catch(e) { Debug("error in getCurrentCatalog: " + e.description); }
}
    


//********************************************

function getCurrentFavorites(userID)
{
    Debug("in getCurrentFavorites",1);

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=FAV&UID=" + userID + "&WL=" + glGet("glWhiteLabel"),
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("favorites loaded successfully",1);
                try { getCurrentFavoritesCallback(true, msg); } catch(e) {}
                
           },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
              try { getCurrentFavoritesCallback(false, msg); } catch(e) {}
           }
         });
    }
    catch(e) { Debug("error in getCurrentFavorites: " + e.description); }
}



//********************************************

function getCurrentHistory(userID)
{
    Debug("in getCurrentHistory",1);

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=HIS&UID=" + userID + "&WL=" + glGet("glWhiteLabel"),
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("history loaded successfully",1);
                try { getCurrentHistoryCallback(true, msg); } catch(e) {}

           },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
           }
         });
    }
    catch(e) { Debug("error in getCurrentHistory: " + e.description); }
}

//*******************************************

function getSearchResults(strKeyword, strSearchType)
{
    Debug("in getSearchResults",1);

    try
    {
        $.ajax({
           type: "GET",
           url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
           data: "FEED=" + strSearchType + "&SSTR=" + strKeyword + "&WL=" + glGet("glWhiteLabel") + "&UID=" + glGet("glUserID") + "&NOSHOUTCAST=1",
           processData: false,
           dataType: "xml",
            
           success: function(msg)
           {
                
                hideLoadingMessage();
                if (msg.childNodes[0].childNodes[0].nodeName != "FAIL")
                {
                    Debug("search results loaded successfully",1);
                    document.getElementById("home").style.display = "none";
                    pageHistory.push(currentPage);
                    document.getElementById("backButton").style.display = "inline";
                    document.getElementById("pageTitle").innerText = "Search Results";
                    
                    Debug(msg.childNodes[0].childNodes[0].childNodes.length + " stations found");
                    var currentPageNumber = glGet("glPGID");  
                    currentPage = "PG" + currentPageNumber;
                    document.getElementById("mainBody").innerHTML += parseCatalog(msg.childNodes[0].childNodes[0],true,currentPageNumber);

                                    
                    if (glGet("glPlatformName").toUpperCase() != "DESKTOP")
                    {
                        $("ul").css({'top' : '0px'});
                    }
                
                }
                else
                {
                    Debug("error loading search results:" + msg.childNodes[0].childNodes[0].getAttribute("message"));
                }
           },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
           }
         });
    }
    catch(e) { Debug("error in getSearchResults: " + e.description); }
}

//***************

var objStationInformation = "";
function getStation(NetStationID, UserID)
{
    //Debug("in getStation");

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=DIR&ID="+ NetStationID + "&UID=" + UserID + "&WL=" + glGet("glWhiteLabel"),
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("station loaded successfully",1);
                try { getStationCallback(true, msg); } catch(e) {}
                

               
            },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
              try { getStationCallback(false); } catch(e) {}
           }
         });
    }
    catch(e) { Debug("error in getStation: " + e.description); }
}

//***************

function getStartupInstructions(userID, platformName, versionNumber)
{
    Debug("in getStartupInstructions",1);

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=START&UID=" + userID + "&PLAT=" + platformName + "&VER=" + versionNumber + "&WL="+ glGet("glWhiteLabel"),
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("startup instructions received successfully",1);
                try { getStartupInstructionsCallback(true, msg); } catch(e) { }
            },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
                Debug("textstatus: " + textStatus,1);
                Debug("errorThrown: " + errorThrown,1);
                Debug("XMLHttpRequest: " + XMLHttpRequest,1);
                Debug("startup instructions failed");
                try { getStartupInstructionsCallback(false, ""); } catch(e) {}
           }
         });
    }
    catch(e) { Debug("error in getStartupInstructions: " + e.description); }
}

//***************

function getCustomFeatures(strWhiteLabel)
{
    Debug("in getCustomFeatures",1);

    if (strWhiteLabel)
    {
        glSet("glWhiteLabel",strWhiteLabel);
    }

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=CUSTOM&WL=" + glGet("glWhiteLabel"),
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("custom features received successfully",1);
                try { getCustomFeaturesCallback(true, msg); } catch(e) { }
            },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
                Debug("textstatus: " + textStatus,1);
                Debug("errorThrown: " + errorThrown,1);
                Debug("XMLHttpRequest: " + XMLHttpRequest,1);
                try { getStartupInstructionsCallback(false, ""); } catch(e) {}
           }
         });
    }
    catch(e) { Debug("error in getCustomFeatures: " + e.description); }
}

//***************
                           
function getStationByNetStationID(iNetStationID,UserID)
{
    Debug("in getStationByNetStationID",1);
    var UIDString = "";
    if (UserID)
    {
        UIDString = "&UID=" + UserID;
    }

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=DIR&ID=" + iNetStationID + "&WL="+ glGet("glWhiteLabel") + UIDString,
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("station loaded successfully",1);
                try { getStationByNetStationIDCallback(true, msg, iNetStationID); } catch(e) {}

           },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
                //!!! error going to the server for this data
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
              
              try { getStationByNetStationIDCallback(false, "",""); } catch(e) {}

           }
         });
    }
    catch(e) { Debug("error in getStationByNetStationID: " + e.description); }

}

//***************

function getPodcastByNetStationID(iNetStationID,iEpisode,UserID)
{
    Debug("in getPodcastByNetStationID",1);
    var UIDString = "";
    if (UserID)
    {
        UIDString = "&UID=" + UserID;
    }

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=PODCAST&ID=" + iNetStationID + "&EP=" + iEpisode + UIDString,
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("podcast loaded successfully",1);
                try { getPodcastByNetStationIDCallback(true, msg, iNetStationID); } catch(e) {}

           },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
              //!!! error going to the server for this data
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
              
              try { getPodcastByNetStationIDCallback(false, "",""); } catch(e) {}

           }
         });
    }
    catch(e) { Debug("error in getPodcastByNetStationID: " + e.description); }

}

//***************

function deleteFavorite(userID, strNetStationIdentifier, strFavoriteType)
{
    Debug("in deleteFavorite",1);

    try
    {
        var dataLine = "FEED=DLFAV&UID=" + userID + "&ID=" + strNetStationIdentifier + "&WL="+glGet("glWhiteLabel");
        if (strFavoriteType)
        {
            if (strFavoriteType == "TITLE")
            {
                dataLine = "FEED=DLFAV&UID=" + userID + "&TITLE=" + strNetStationIdentifier;
            }
            if (strFavoriteType == "SHOUTCAST")
            {
                dataLine = "FEED=DLFAV&UID=" + userID + "&SHOUT=" + strNetStationIdentifier;
            }
        } 
    
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: dataLine,
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("favorite deleted successfully",1);
                try { deleteFavoriteCallback(true, msg); } catch(e) {}
                
           },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
              try { deleteFavoriteCallback(false, msg); } catch(e) {}
           }
         });
    }
    catch(e) { Debug("error in deleteFavorite: " + e.description); }
}

function getConnectionStatus()
{
    Debug("in getCurrentCatalog",1);

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=CONN",
            processData: false,
            dataType: "xml",
            timeout: 3000,
            
            success: function(msg)
            {
                Debug("connection loaded successfully",1);
                try { getConnectionStatusCallback(true, msg); } catch(e) {}
           },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
              try { getConnectionStatusCallback(false, msg); } catch(e) {}
           }
         });
    }
    catch(e) { Debug("error in getCurrentCatalog: " + e.description); }
}
//***************
function hideLoadingMessage()
{
    try
    {
        //Debug("hiding the loading message",1);
        document.getElementById("loadingMessage").style.display="none";
    }
    catch(e) {}
}

function showLoadingMessage()
{
    try
    {
        //Debug("showing the loading message",1);
        document.getElementById("loadingMessage").style.display="inline";
    }
    catch(e) {}
}
//************

function checkForAd(adID)
{
    Debug("in checkForAd",1);

    try
    {
        $.ajax({
           type: "GET",
            url: "http://" + glGet("glClientServer") + "/external/ClientServices.aspx",
            data: "FEED=CHECKAD" + "&ADID=" + adID,
            processData: false,
            dataType: "xml",
            
            success: function(msg)
            {
                Debug("checkForAd loaded successfully",1);
                try { checkForAdCallback(true, msg); } catch(e) {}
           },
           
           error: function (XMLHttpRequest, textStatus, errorThrown) 
           {
              Debug("textstatus: " + textStatus,1);
              Debug("errorThrown: " + errorThrown,1);
              Debug("XMLHttpRequest: " + XMLHttpRequest,1);
              checkForAdCallback(false, msg);
           }
         });
    }
    catch(e) { Debug("error in checkForAd: " + e.description); checkForAdCallback(false, msg); }

}
