function shuffle(o) {
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

var mp3root = "http://s3.amazonaws.com/www.massyfergusonband.com/mp3/";
var songIndex = 0;

var playlist = shuffle(
[
    { name: "Long Time No See",             path: "hard_water/01-Long_Time_No_See.mp3"                },
    { name: "Pretty Plain Jane",            path: "hard_water/02-Pretty_Plain_Jane.mp3"               },
    { name: "What You Decide",              path: "hard_water/03-What_You_Decide.mp3"                 },
    { name: "Wenatchee Eyes",               path: "hard_water/04-Wenatchee_Eyes.mp3"                  },
    { name: "Sparks And Shrapnel",          path: "hard_water/05-Sparks_And_Shrapnel.mp3"             },
    { name: "Freedom County",               path: "hard_water/06-Freedom_County.mp3"                  },
    { name: "Idle Threats And Cigarettes",  path: "hard_water/07-Idle_Threats_And_Cigarettes.mp3"     },
    { name: "Dreams Of St Petersburg",      path: "hard_water/08-Dreams_Of_St_Petersburg.mp3"         },
    { name: "Good Enough",                  path: "hard_water/09-Good_Enough.mp3"                     },
    { name: "Aspartame",                    path: "hard_water/10-Aspartame.mp3"                       }
]);

function playSong() {
  var song = playlist[songIndex];
  $("#jplayer").jPlayer("setFile", mp3root + song.path).jPlayer("play");
  $('#jplayer_title').text(song.name);
}

$(function() {
  $("#jplayer").jPlayer( {
    ready: function () {
      playSong();
    },
    nativeSupport: true,
    errorAlerts: true,
    warningAlerts: true
  });
  
  $('#jplayer_next').click(function() {
    ++songIndex;
    
    if (songIndex == playlist.length)
      songIndex = 0;
    
    playSong();
    return false;
  });
  
  $('#jplayer_previous').click(function() {
    --songIndex;
    
    if (songIndex < 0)
      songIndex = playlist.length - 1;
      
    playSong();
    return false;
  });
  
  $("#twitter").liveTwitter('massyferguson', {limit: 7, refresh: false, mode: 'user_timeline'}, function() {
    $("#topTweet").html($('.tweet:first p').html());
  });
	
	if (useArtistData) {
	  $("#showList").empty();
	  
    loadShows($('#showList'), function() {
      $("#nextShow").html($('.show:first').html());
    });
  } else {
    $('#showList').load('show_list.html');
  }

  setInterval('nextBandPhoto()', 30000);
  nextBandPhoto();
});

var bandPhotoIndex = 0;
var numBandPhotos  = 12;

function nextBandPhoto() {
  do
    newBandPhotoIndex = Math.floor(Math.random() * numBandPhotos) + 1;
  while (newBandPhotoIndex == bandPhotoIndex);

  bandPhotoIndex = newBandPhotoIndex;
  setHeaderBackgroundImg('images/band/band' + bandPhotoIndex + '.jpg');
}

function setHeaderBackgroundImg(src) {
    var tempImage = new Image();

    $(tempImage).load(function() {
        $('#header').css('background', 'url(' + src + ') right no-repeat');
    });

    tempImage.src = src;
}

