var gigCounter = 0;
var thumbCounter = 0;
var imgCounter = 0;
var imgTotal = 0;
var fbGigID = "";
var mySpaceGigID = "";
var lastFMGigID = "";
var iLikeGigID = "";
var flickrImages = [];
var flickrIds = [];

function setEventListeners()
{
	/* Event Listener - Show the lyrics for a song */
	$("#lyric1").click(function(event){

		/* Dialog - Call the lyrics dialog to open */
		$("#lyrics1").dialog("open");
	});

	/* Event Listener - Show the lyrics for a song */
	$("#lyric2").click(function(event){

		/* Dialog - Call the lyrics dialog to open */
		$("#lyrics2").dialog("open");
	});

	/* Event Listener - Show the lyrics for a song */
	$("#lyric3").click(function(event){

		/* Dialog - Call the lyrics dialog to open */
		$("#lyrics3").dialog("open");
	});

	$("#playSong1").click(function(event){
		niftyplayer('niftyPlayer1').loadAndPlay('mp3/diary.mp3');
		$("#musictitle").html("Diary of a Falling Man, Pt 1");
	});

	$("#playSong2").click(function(event){
		niftyplayer('niftyPlayer1').loadAndPlay('mp3/fool.mp3');
		$("#musictitle").html("Fool Me Once");
	});

	$("#playSong3").click(function(event){
		niftyplayer('niftyPlayer1').loadAndPlay('mp3/house.mp3');
		$("#musictitle").html("In a House Rebuilding");
	});

	$("#songdl1").click(function(event){
	});

	$("#songdl2").click(function(event){
	});

	$("#songdl3").click(function(event){
	});

	$("#upcoming_gigs").click(function(event){
		window.open('http://www.facebook.com/pages/Autumn-Gray/6452133198?v=app_2344061033');
	});

	$("#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);
	});

}

/* JQuery document ready function */
$(document).ready(function(){

	$("#lyrics1").dialog({
		bgiframe: true,
		autoOpen: false,
		hide: "blind",
		width: 450,
		draggable: false,
		resizable: false,
		modal: true,
		position: "center",
		title: "<img src='images/AG_lyrics.gif'>"
	});

	$("#lyrics2").dialog({
		bgiframe: true,
		autoOpen: false,
		hide: "blind",
		width: 450,
		draggable: false,
		resizable: false,
		modal: true,
		position: "center",
		title: "<img src='images/AG_lyrics.gif'>"
	});

	$("#lyrics3").dialog({
		bgiframe: true,
		autoOpen: false,
		hide: "blind",
		width: 450,
		draggable: false,
		resizable: false,
		modal: true,
		position: "center",
		title: "<img src='images/AG_lyrics.gif'>"
	});

	/* Start with first gig */
	$("#upcoming_gigs").html(gigList[gigCounter].date + " - " + gigList[gigCounter].venue + ", " + gigList[gigCounter].town);
	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 + " - " + gigList[gigCounter].venue + ", " + gigList[gigCounter].town);
				$(this).fadeIn(1000);
			});
		}
	});

	/* 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/2)]);
			$("#img3").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/2)]);
			$("#img3").parent().attr('href', "http://www.flickr.com/photos/autumngray/"+flickrIds[imgTotal- 1]);

			//imgCounter = 3;
	});

	/* 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);
		});
	});

	/* set the event listeners */
	setEventListeners();

});
