var gigCounter = 0;
var thumbCounter = 0;
var imgCounter = 0;
var imgTotal = 0;
var fbGigID = "";
var mySpaceGigID = "";
var lastFMGigID = "";
var iLikeGigID = "";
var messID = "";
var reverbID = "";
var flickrImages = [];
var flickrIds = [];

function setEventListeners()
{
	$("#playSong1").click(function(event){
		niftyplayer('niftyPlayer1').loadAndPlay('mp3/diary.mp3');
		$("#musictitle").html("Music (Diary of a Falling Man, Pt 1)");
	});

	$("#playSong2").click(function(event){
		niftyplayer('niftyPlayer1').loadAndPlay('mp3/fool.mp3');
		$("#musictitle").html("Music (Fool Me Once)");
	});

	$("#playSong3").click(function(event){
		niftyplayer('niftyPlayer1').loadAndPlay('mp3/house.mp3');
		$("#musictitle").html("Music (In a House Rebuilding)");
	});

	$("#songdl1").click(function(event){
	});

	$("#songdl2").click(function(event){
	});

	$("#songdl3").click(function(event){
	});

	$("#mail").click(function(event){
		if($(this).val() == "your@email.address.here")
		{
			$(this).val("");
		}
	});

	$("#facebookgig").click(function(event){
		var gigURL = "http://www.facebook.com/event.php?eid=" + gigList[gigCounter].fbID;
		window.open(gigURL);
	});

	$("#myspacegig").click(function(event){
		var gigURL = "http://events.myspace.com/Event/" + gigList[gigCounter].myspaceID;
		window.open(gigURL);
	});

	$("#lastfmgig").click(function(event){
		var gigURL = "http://www.last.fm/event/" + gigList[gigCounter].lastfmID;
		window.open(gigURL);
	});

	$("#ilikegig").click(function(event){
		var gigURL = "http://www.ilike.com/concerts/venues/" + gigList[gigCounter].ilikeID;
		window.open(gigURL);
	});

	$("#messgig").click(function(event){
		var gigURL = "http://www.messandnoise.com/events/" + gigList[gigCounter].messID;
		window.open(gigURL);
	});

	$("#reverbgig").click(function(event){
		var gigURL = "http://www.reverbnation.com/show/" + gigList[gigCounter].reverbID;
		window.open(gigURL);
	});

}

/* JQuery document ready function */
$(document).ready(function(){

	/* Start with first gig */
	if(gigList.length > 0)
	{
		$("#upcoming_gigs").html(gigList[gigCounter].date + "<br>" + gigList[gigCounter].venue + ", " + gigList[gigCounter].time + "<br>");
		var loopCounter = 0;

		$("#upcoming_gigs_container").everyTime(5000, function() {
			if(gigList.length > 1)
			{
				$(this).fadeOut(1000, function() {
					loopCounter++;
					gigCounter = loopCounter % gigList.length;
					$("#upcoming_gigs").html(gigList[gigCounter].date + "<br>" + gigList[gigCounter].venue + ", " + gigList[gigCounter].time + "<br>");
					$(this).fadeIn(1000);
				});
			}
		});
	}
	else
	{
		$("#upcoming_gigs").html("no upcoming gigs");
	}
	
	/* Function to get the latest 20 photos from flickr and add them to the images div */

	/* http://api.flickr.com/services/rest/?method=	*/


	$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=8038f7cadc610eb84f4cc69c2f39389d&photoset_id=72157623430653140&extras=url_sq&per_page=500&format=json&jsoncallback=?", function(data){
			$.each(data.photoset.photo, function(i,item){
				flickrImages[imgCounter] = item.url_sq;
				flickrIds[imgCounter] = item.id;
				imgCounter++;
			});

			imgTotal = parseInt(data.photoset.total);

			$("#img1").attr('src', flickrImages[0]);
			$("#img2").attr('src', flickrImages[Math.floor(imgCounter/6)]);
			$("#img3").attr('src', flickrImages[Math.floor(imgCounter/4)]);
			$("#img4").attr('src', flickrImages[Math.floor(imgCounter/3)]);
			$("#img5").attr('src', flickrImages[Math.floor(imgCounter/2)]);
			$("#img6").attr('src', flickrImages[Math.floor((imgCounter/8)*5)]);
			$("#img7").attr('src', flickrImages[Math.floor((imgCounter/8)*7)]);
			$("#img8").attr('src', flickrImages[imgTotal- 1]);

			$("#img1").parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[0]);
			$("#img2").parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[Math.floor(imgCounter/6)]);
			$("#img3").parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[Math.floor(imgCounter/4)]);
			$("#img4").parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[Math.floor(imgCounter/3)]);
			$("#img5").parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[Math.floor(imgCounter/2)]);
			$("#img6").parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[Math.floor((imgCounter/8)*5)]);
			$("#img7").parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[Math.floor((imgCounter/8)*7)]);
			$("#img8").parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgTotal- 1]);
			
	});

	/* image fading timers */

	$("#img1").everyTime(12000, function() {
		$(this).fadeOut(2000, function() {
			imgCounter = Math.floor(Math.random()*(imgTotal + 1));
			$(this).attr('src', flickrImages[imgCounter]);
			$(this).parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgCounter]);
			$(this).fadeIn(2000);
		});
	});

	$("#img2").everyTime(10000, function() {
		$(this).fadeOut(2000, function() {
			imgCounter = Math.floor(Math.random()*(imgTotal + 1));
			$(this).attr('src', flickrImages[imgCounter]);
			$(this).parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgCounter]);
			$(this).fadeIn(2000);
		});
	});

	$("#img3").everyTime(11000, function() {
		$(this).fadeOut(2000, function() {
			imgCounter = Math.floor(Math.random()*(imgTotal + 1));
			$(this).attr('src', flickrImages[imgCounter]);
			$(this).parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgCounter]);
			$(this).fadeIn(2000);
		});
	});
	
	$("#img4").everyTime(8000, function() {
		$(this).fadeOut(2000, function() {
			imgCounter = Math.floor(Math.random()*(imgTotal + 1));
			$(this).attr('src', flickrImages[imgCounter]);
			$(this).parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgCounter]);
			$(this).fadeIn(2000);
		});
	});
	
	$("#img5").everyTime(15000, function() {
		$(this).fadeOut(2000, function() {
			imgCounter = Math.floor(Math.random()*(imgTotal + 1));
			$(this).attr('src', flickrImages[imgCounter]);
			$(this).parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgCounter]);
			$(this).fadeIn(2000);
		});
	});
	
	$("#img6").everyTime(14000, function() {
		$(this).fadeOut(2000, function() {
			imgCounter = Math.floor(Math.random()*(imgTotal + 1));
			$(this).attr('src', flickrImages[imgCounter]);
			$(this).parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgCounter]);
			$(this).fadeIn(2000);
		});
	});
	
	$("#img7").everyTime(9000, function() {
		$(this).fadeOut(2000, function() {
			imgCounter = Math.floor(Math.random()*(imgTotal + 1));
			$(this).attr('src', flickrImages[imgCounter]);
			$(this).parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgCounter]);
			$(this).fadeIn(2000);
		});
	});
	
	$("#img8").everyTime(13000, function() {
		$(this).fadeOut(2000, function() {
			imgCounter = Math.floor(Math.random()*(imgTotal + 1));
			$(this).attr('src', flickrImages[imgCounter]);
			$(this).parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgCounter]);
			$(this).fadeIn(2000);
		});
	});

	/* set the event listeners */
	setEventListeners();

});

