document.remoteServerUrl = 'http://amazingdiscoveriesministries.net/audio/';

document.track = new Array();
document.mainTitle = 'Audio Player';
document.dictionary = new Dictionary(); // top high value
document.menu = new Menu();
document.library = new Library();

function runPage(artistId, albumId, trackId, urlString)
{
    // gets menu ids
    $.getJSON(document.remoteServerUrl+"library?viewType=menu&callback=?", function(menuJSON) {

        // page is loaded
        $(document).ready(function() {
            document.library.currentTrack = trackId;
            document.menu.generateMenu(menuJSON, artistId, albumId, trackId, urlString);

            $("#player-sidebar").accordion({
                        alwaysOpen: false,
                        autoHeight: false,
                        header: 'h3',
                        active: '.selected',
                        clearStyle: true});
	       
            // load player
            createPlayer();

            $('#audio-player-loading_icon').fadeOut(300);

        }); // end of getting menu IDS
    }); // end of getting dictionaries
}

function isProperHash() {
    try {
        var hash = window.location.hash.split("#")[1];
        hash = hash.split("-");

        if ( isInt(hash[0]) && isInt(hash[1]) && isInt(hash[2]) && (hash.length == 3 || hash.length == 4)) {
            return true;
        }
        else {
           return false
        }
    }
    catch(err) {
        return false;
    }
}

// If the URL is static: http://amazingdiscoveries.org/audio.html/9/8/1
// convert it to hash-based form: http://amazingdiscoveries.org/audio.html#9-8-1
/*function redirectFromStaticURL() {
    var url = window.location.pathname.split('/');

    if(url.length > 3 && isInt(url[url.length-1]) && isInt(url[url.length-2]) && isInt(url[url.length-3])) {
        var newUrl = window.location.protocol+'//'+window.location.host;

        for(i = 1; i < url.length-3; i++) {
            newUrl += '/'+url[i];
        }

        newUrl += '#'+url[url.length-3];
        newUrl += '-'+url[url.length-2];
        newUrl += '-'+url[url.length-1];

        window.location.replace(newUrl);
    }
}*/

// recieve diction ary definitions
$.getJSON(document.remoteServerUrl+"library?viewType=dictionaries&callback=?", function(json) {
    document.dictionary.setDictionary(json);

    if (isProperHash())
    {
	    // hash: ARTIST_ID-ALBUM_ID-TRACK_ID
	    var hash = window.location.hash.split("#")[1];
	    hash = hash.split("-");

	    runPage(hash[0], hash[1], hash[2], hash[3]);
    }
    else
    {
	    var hash = window.location.hash.split("#")[1];

        if (typeof(hash) == "undefined")
        {
            // if URL is static: http://amazingdiscoveries.org/audio.html/9/8/1
            //redirectFromStaticURL();

            // else (http://amazingdiscoveries.org/audio.html):
	        runPage(0,0,0);
        }
        else
        {	
	        hash = hash.split("-");

	        //add spaces instead of _
	        hash[0] = hash[0].replace("_", " ");
	        hash[1] = hash[1].replace("_", " ");

	        runPage(document.dictionary.getArtistId(hash[0]), document.dictionary.getAlbumId(hash[1]), hash[2], hash[3]);
        }
    }

    $("#loading_icon").hide();
});

function isInt(x) { 
    var y=parseInt(x); 
    if (isNaN(y)) return false; 
    return x==y && x.toString()==y.toString(); 
} 

function print_r(theObj) {
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}


