jQuery(document).ready(function($) {  
  
  function activeImg() {
    if ($(this).attr("src").indexOf("_active")==-1) {
      var src = $(this).attr("src");
      var dot = src.lastIndexOf(".");
      var href = src.substring(0, dot);
      $(this).attr("src", href+"_active.png");
      $(this).addClass("hover");
    }
  }
  
  function normalImg() {
    if ($(this).hasClass("hover")) {
      var src = $(this).attr("src");
      var under = src.lastIndexOf("_");
      var slash = src.lastIndexOf("/");
      var name = src.substring(slash+1, under);
      var href = src.substring(0, slash);
      $(this).attr("src", href+"/"+name+".png");
      $(this).removeClass("hover");
    }
  }
  
  $("#nav a img").hover(activeImg, normalImg);
  $("#footer a img").hover(activeImg, normalImg);
  
  var active = $("#nav li.active img").attr("src");
  var dot = active.lastIndexOf(".");
  var href = active.substring(0, dot);
  $("#nav li.active img").attr("src", href+"_active.png");
  
  var ft_active = $("#footer li.active img").attr("src");
  var ft_dot = ft_active.lastIndexOf(".");
  var ft_href = ft_active.substring(0, ft_dot);
  $("#footer li.active img").attr("src", ft_href+"_active.png");
  
  var twitter = '<img src="/wp-content/themes/thurmond/images/twitter.png" alt="Nick\'s Twitter" />';
  $(".widget_twitter a.twitterwidget-title").html(twitter);
  $(".widget_twitter a.twitterwidget-rss").html("");
  $(".widget_twitter .entry-content").css("font-size", "16px").addClass("brown");
  
  $("input.email").click(function() {
  	//$(this).css("background-image", "url(/wp-content/themes/thurmond/images/email_bg_active.jpg)");
  });
  
  // player
  var playItem = 0;
  
  var trackname = 0;
  
  var myPlayList = [
    {name:"We Will Shine",mp3:"/wp-content/themes/thurmond/mp3/we-will-shine.mp3"},
    {name:"Hosanna",mp3:"/wp-content/themes/thurmond/mp3/hosanna.mp3"},
    {name:"Not Alone",mp3:"/wp-content/themes/thurmond/mp3/not-alone.mp3"}
  ];
  
   $("#jquery_jplayer").jPlayer({
    ready: function () {
      displayPlayList();
      playListInit(false);
      $("#trackname").text("PAUSED - CLICK PLAY TO LISTEN");
    },
    swfPath: "/wp-content/themes/thurmond/swf"
  })
  .jPlayerId("play", "play")
  .jPlayerId("pause", "pause")
  .jPlayerId("stop", "stop")
  .jPlayerId("volumeMin", "vmute")
  .jPlayerId("volumeMax", "vmax")
  .jPlayerId("volumeBar", "vhalf")
  //.onProgressChange( function(lp,ppr,ppa,pt,tt) {
    //$("#pcent").text(parseInt(ppa)+"%");
  .onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
    var myPlayedTime = new Date(playedTime);
    var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
    var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
    $("#play_time").text(ptMin+":"+ptSec);

    var myTotalTime = new Date(totalTime);
    var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
    var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
    $("#total_time").text(ttMin+":"+ttSec);
  });
  
  $("#ctrl_prev").click( function() {
    playListPrev();
    return false;
  });

  $("#ctrl_next").click( function() {
    playListNext();
    return false;
  });
  
  function displayPlayList() {
    for (i=0; i < myPlayList.length; i++) {
      $("#playlist_list ul").append("<li id='playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
      $("#playlist_item_"+i).data( "index", i ).hover(
        function() {
          if (playItem != $(this).data("index")) {
            $(this).addClass("playlist_hover");
          }
        },
        function() {
          $(this).removeClass("playlist_hover");
        }
      ).click( function() {
        var index = $(this).data("index");
        if (playItem != index) {
          playListChange( index );
        } else {
          $("#jquery_jplayer").play();
        }
      });
    }
  }

  
  function playListInit(autoplay) {
    if(autoplay) {
      playListChange( playItem );
    } else {
      playListConfig( playItem );
    }
  }
  
  function playListConfig( index ) {
    $("#playlist_item_"+playItem).removeClass("playlist_current");
    $("#playlist_item_"+index).addClass("playlist_current");
    playItem = index;
    $("#jquery_jplayer").setFile(myPlayList[playItem].mp3, myPlayList[playItem].ogg);
    trackname = $("#playlist_item_"+index).text();
    $("#trackname").text(trackname);
  }

  function playListChange( index ) {
    playListConfig( index );
    $("#jquery_jplayer").play();
  }

  function playListNext() {
    var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
    playListChange( index );
  }

  function playListPrev() {
    var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
    playListChange( index );
  }
  
  $("#pause").click(function() {
    $("#trackname").text("PAUSED - CLICK PLAY TO LISTEN");
  });
  
  $("#play").click(function() {
    $("#trackname").text(trackname);
  });
  
  $("#vmute").click(function() {
    $("#vhalf img").attr("src", "/wp-content/themes/thurmond/images/med_off.jpg");
    $("#vmax img").attr("src", "/wp-content/themes/thurmond/images/high_off.jpg");
  });
  
  $("#vhalf").click(function() {
    $("#vhalf img").attr("src", "/wp-content/themes/thurmond/images/med_on.jpg");
    $("#vmax img").attr("src", "/wp-content/themes/thurmond/images/high_off.jpg");
  });
  
  $("#vmax").click(function() {
    $("#vhalf img").attr("src", "/wp-content/themes/thurmond/images/med_on.jpg");
    $("#vmax img").attr("src", "/wp-content/themes/thurmond/images/high_on.jpg");
  });

});