$(document).ready(function() {
  $("#jplayer").jPlayer({
    ready: function () {
      if (music_tracks > 0) {
        current_track = 1;
        $("#track_1").trigger("click");
      }
    },
    customCssIds: true,
    swfPath: 'swf'
  })
  .jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) { 
      $('#stereo_time').text($.jPlayer.convertTime(pt));
      
      var lpInt = parseInt(lp);
      var ppaInt = parseInt(ppa);

      global_lp = lpInt;
      $('#stereo_load_bar').css('width', lpInt + '%')
      $('#stereo_play_bar').css('width', ppaInt + '%')
    })
  .jPlayer("onPlayingChanged", function(state) { if (state) { playTape() } else{ stopTape() }; })
  .jPlayer("onSoundComplete", function() { if (current_track < music_tracks) { playTrack(current_track + 1) }});
  
  $.jPlayer.timeFormat.padMin = true;
  $.jPlayer.timeFormat.padSec = true;
  $.jPlayer.timeFormat.sepMin = ":";
  $.jPlayer.timeFormat.sepSec = "";
  
  $("#tracks li a").click( changeTrack );
  
  function playTape() {
    $('#tape').css('display', 'inline').css('visibility', 'visible');
    $('#amp').css('display', 'inline').css('visibility', 'visible');
    $('#amp-off').css('display', 'inline').css('visibility', 'hidden');
  }
  
  function stopTape() {
    $('#tape').css('display', 'none').css('visibility', 'hidden');    
    $('#amp').css('display', 'none').css('visibility', 'hidden');
    $('#amp-off').css('display', 'inline').css('visibility', 'visible');
  }

  function changeTrack(e) {
    $("#stereo_display_text").text($(this).text());
    $("#jplayer").jPlayer("setFile", $(this).attr("href")).jPlayer("play");
    $(this).blur();
    return false;
  }
  
  $(".player-box AREA").hover(function() {
    stereoHover($(this).attr('rel'), true);
  }, function(){
    stereoHover($(this).attr('rel'), false);
  });
  
  $(".player-box AREA").click(function() {
    button = $(this).attr('rel');
    if (music_tracks == 0) {
      $('#stereo_display_text').html("NO MUSIC")
    } else {
      if (button == 'fwd') {
        if (current_track < music_tracks) {
          playTrack(current_track + 1);
        }
      } else if (button == 'rew') {
        if (current_track > 1) {
          playTrack(current_track - 1);
        }
      } else if (button == 'play') {
        $("#jplayer").jPlayer("play");
      } else if (button == 'stop') {
        $("#jplayer").jPlayer("pause");
      }
    }
    $(this).blur();
    return false;
  });
  
  function playTrack(track) {
    current_track = track;
    $("#track_" + track).trigger("click");
  }
  
  function stereoHover(button, state) {
    if (state) {
      if ($('#stereo_hover_' + button).is(":hidden")) {
        $('#stereo_hover_' + button).css('display', 'inline');
        $('#stereo_hover_' + button).css('visibility', 'visible');
      }
    } else {
      if ($('#stereo_hover_' + button).is(":visible")) {
        $('#stereo_hover_' + button).css('display', 'none');
        $('#stereo_hover_' + button).css('visibility', 'hidden');
      }
    }
    return false;
  }
  
  $('.expand').click(function() {
    id = $(this).attr('rel');
    if ($('#news-expand-' + id).is(":hidden")) {
      $('#news-expand-' + id).fadeIn("slow");
      $('#expand-' + id).html("Collapse").addClass("collapse");
    } else {
      $('#news-expand-' + id).fadeOut("slow");
      $('#expand-' + id).html("Expand").removeClass("collapse");;
    }
    return false;
  });
  
  function showNewsArticle(p) {
    $('#news-box-' + p).css('display', 'inline');
    $('#news-box-' + p).css('visibility', 'visible');
    $('#news-expand-' + p).css('display', 'inline');
    $('#news-expand-' + p).css('visibility', 'visible');
    $('#expand-' + p).html("Collapse");
  }
  
  function hideNewsArticle(p) {
    $('#news-box-' + p).css('display', 'none');
    $('#news-box-' + p).css('visibility', 'hidden');
    $('#news-expand-' + p).css('display', 'none');
    $('#news-expand-' + p).css('visibility', 'hidden');
  }
  
  function changeNewsArticle(from, to) {
    if (to != from) {
      $('#news-expand-' + to).css("left", 0);
      showNewsArticle(to);
      hideNewsArticle(from); 
      current_news_article = to;
    }
  }
  
  $('.news_article_link').click(function() {
    changeNewsArticle(current_news_article, $(this).attr('rel'));    
    return false;
  });
  
  $('.news_article_link_previous').click(function() {
    if (current_news_article > 1) {
      changeNewsArticle(current_news_article, current_news_article - 1);    
    }
    return false;
  });
  
  $('.news_article_link_next').click(function() {
    if (current_news_article < news_articles) {
      changeNewsArticle(current_news_article, current_news_article + 1);    
    }
    return false;
  });
  
  function showGigPage(p) {
    $('#gig_page_' + p).css('display', 'inline');
    $('#gig_page_' + p).css('visibility', 'visible');
    $('#gig_page_link_' + p).addClass('active');
  }
  
  function hideGigPage(p) {
    $('#gig_page_' + p).css('display', 'none');
    $('#gig_page_' + p).css('visibility', 'hidden');
    $('#gig_page_link_' + p).removeClass('active');
  }
  
  function changeGigPage(from, to) {
    if (to != from) {
      showGigPage(to);
      hideGigPage(from); 
      current_gig_page = to;
    }
  }
  
  $('.gig_page_link').click(function() {
    changeGigPage(current_gig_page, $(this).attr('rel'));    
    return false;
  });
  
  $('#gig_page_link_previous').click(function() {
    if (current_gig_page > 1) {
      changeGigPage(current_gig_page, current_gig_page - 1);    
    }
    return false;
  });
  
  $('#gig_page_link_next').click(function() {
    if (current_gig_page < gig_pages) {
      changeGigPage(current_gig_page, current_gig_page + 1);    
    }
    return false;
  });
  
  $('.flash').delay(5000).slideUp("slow")
  
  $("#books AREA").hover(function() {
    bookHover($(this).attr('rel'), true);
  }, function(){
    bookHover($(this).attr('rel'), false);
  });
  
  function bookHover(button, state) {
    if (state) {
      if ($('#img-books').attr("src") != "/images/img-books-" + button + '.gif') {
        $('#img-books').attr('src', '/images/img-books-' + button + '.gif');
      }
    } else {
      if ($('#img-books').attr("src") != "/images/img-books-0.gif") {
        $('#img-books').attr('src', '/images/img-books-0.gif');
      }
    }
    return false;
  }
  
  $(".mix_link").hover(function() {
    mixHover($(this).attr('rel'), true);
  }, function(){
    mixHover($(this).attr('rel'), false);
  });
  
  function mixHover(link, state) {
    if (state) {
      $('.mics-box').addClass("mix_link_" + link);
    } else {
      $('.mics-box').removeClass("mix_link_" + link);
    }
    return false;
  }
  
  var cache = [];
  // Arguments are image paths relative to the current page.
  function preLoadImages() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = "/images/" + arguments[i];
      cache.push(cacheImage);
    }
  }
  
  preLoadImages("img-books-1.gif", "img-books-2.gif", "img-books-3.gif", "img-books-4.gif", "img-books-5.gif", "img-books-6.gif");
  preLoadImages("bg-mics-1.png", "bg-mics-2.png", "bg-mics-3.png", "bg-mics-4.png", "bg-mics-5.png", "bg-mics-6.png", "bg-mics-7.png", "bg-mics-8.png")
});
