$(document).ready(function() {
  // Load the history plugin
  $.historyInit(nags_loadpage);
  
  // Javascriptify navigation links
  $('.navigationLink').click(function(){
    $thisText = $(this).text();
    $.historyLoad($thisText.toLowerCase().replace(" ",""));
    _gaq.push(['_trackEvent', 'asyncPages', $thisText ]);
    return false;
  });
    
  musicLoaded = false;
    
  nags_loadMusic();
  
  $('#refreshMusic').click(function(){
    if ($(this).attr('src').match('refresh.png')) {
      nags_loadMusic();
      setTimeout(function(){
        $('#refreshMusic').attr('src','img/refresh.png').attr('title','Refresh track list').attr('aria:disabled','false');
      },
      30000);
      $('#refreshMusic').attr('src','img/refreshbw.png').attr('title','Please wait to refresh the track list again').attr('aria:disabled','true');
      _gaq.push(['_trackEvent', 'musicRefresh', 'refreshed']);
    }
  });  
});

function nags_loadpage(hash) {
  // hash doesn't contain the first # character.
  if(hash) {
    // restore ajax loaded state
    $('#contentPane').load(hash+".php?ajax=true");
    hash[0] = hash[0].toUpperCase();
    document.title = "The Nag's Head - "+hash;
    if (hash == "map")
    {
      $('#contentPane').css('overflow','hidden');
      $('#contentPane').css('overflow-y','hidden');
      window.scroll(0,0);
    }
    else
    {
        $('#contentPane').css('overflow-y','scroll');
    }
  } else {
    if (document.location.href.substring(27) == "map.php")
    {
      $('#contentPane').css('overflow','hidden');
      $('#contentPane').css('overflow-y','hidden');
      window.scroll(0,0);
    }
  }
}

function nags_loadMusic() {
  if (musicLoaded){
    queryString = "music.php?ajax=true";
  }
  else
  {
    queryString = "music.php";
  }
  $('#trackList').load(
    queryString+" #contents",
    "",
    function(responseText, textStatus, XMLHttpRequest) {
      if (textStatus == "success")
      {
        musicLoaded = true;
        /*$('.latestTrack').mouseenter(
          function(e){
            var artist = $(this).children('.artist').text();
            var track = $(this).children('.track').text();
            $(this).append("<div style=\"background:#fff;border:1px solid #000;position:absolute;z-index:3;!important\" id=\"trackInfo\"><img src=\"\" alt=\"Album Art for "+track+"\"/></div>");
            $('#trackInfo').css('top',$(this).css('top'));
            $('#trackInfo').css('left',10);
          }
        );
        $('.latestTrack').mouseleave(
          function() {
            $('#trackInfo').remove();
          }
        );*/
      }
    }
  );
}
