var currentArtistId;
var xmlData, totalArtists, imgHeight;
var CurrentFeatured = new Array();

$(document).ready(function() {
	
	jQuery.easing.def = "easeInOutCirc";
	
	getData();
	
	$('#artist-list a').click(function(){
		$('#artist-list a').removeClass('active');
		$(this).addClass('active');
		thisClass = $(this).attr('id');
		thisClass = thisClass.split("-");
		currentArtistId = parseInt(thisClass[1]);
		clearTimeout(photoSwitch); //end index randomizer
		$('#index-featured').slideUp();
		$('#artist-list').slideUp(300);
		$('#artist-wrapper').slideDown(600,function(){
			$(this).resetArtistPages(currentArtistId);
		});
	});
	
	$('#artist-controls .artist-list-toggle').click(function(){
		var wasHidden = $('#artist-list').is(':hidden');
		$('#artist-list').slideToggle(500,function(){
			if (wasHidden) {
				$('#artist-controls .artist-list-toggle').html('hide artists');
			} else {
				$('#artist-controls .artist-list-toggle').html('view artists');
			}
		});
	});
	
	$('#index-featured a').click(function(){
		thisClass = $(this).attr('id');
		thisClass = thisClass.split("-");
		currentArtistId = parseInt(thisClass[1]);
		clearTimeout(photoSwitch); //end index randomizer
		$('#index-featured').slideUp(300);
		$('#artist-list').slideUp(300);
		$('#artist-wrapper').slideDown(600,function(){
			$(this).resetArtistPages(currentArtistId);
		});
	});
	
	$('#nextartist').click(function(){ //next artist button
		if(currentArtistId == totalArtists){
			currentArtistId = 1;
		} else{
			currentArtistId++;
		}
		
		$('.artist.current').animate({left:"-960px"},{duration:1500,easing:"easeInOutCirc"});
		$('.artist.next').animate({left:"0px"},1500,function(){
			$('.artist.current').addClass('currentSwitch').removeClass('current');
			$('.artist.next').addClass('current').removeClass('next');
			$('.artist.prev').addClass('next').removeClass('prev').css('left','960px');
			$('.artist.currentSwitch').addClass('prev').removeClass('currentSwitch').css('left','-960px');
			setTimeout(function() { $('.artist.current').resetArtistPages(currentArtistId); }, 30);
		});
	});
	$('#prevartist').click(function(){ //prev artist button
		if(currentArtistId == 1){
			currentArtistId = totalArtists;
		} else{
			currentArtistId--;
		}
		console.log(currentArtistId);
		
		$('.artist.current').animate({left:"960px"},{duration:1500,easing:"easeInOutCirc"});
		$('.artist.prev').animate({left:"0px"},1500,function(){
			$('.artist.current').addClass('currentSwitch').removeClass('current');
			$('.artist.prev').addClass('current').removeClass('prev');
			$('.artist.next').addClass('prev').removeClass('next').css('left','-960px');
			$('.artist.currentSwitch').addClass('next').removeClass('currentSwitch').css('left','960px');	
			setTimeout(function() { $('.artist.current').resetArtistPages(currentArtistId); }, 30);
		});
	});
	
	$('.photo-controls .next').click(function(){
		
		$(this).parent().parent().parent().find('img.currentPhoto').fadeOut(300,function(){
			
			$('.photo-controls a').removeClass('disabled'); //reset prev/next buttons
			
			currentPhoto = $(this).attr('class').split("-");
			currentPhoto = parseInt(currentPhoto[1]);
			$('img.photo-'+currentPhoto).removeClass('currentPhoto');
			$('img',this).addClass('queue');
			photoCount = $(this).parent().find('img').size();
			
			if(currentPhoto < (photoCount - 1))
				currentPhoto++;
			$('.current .photo-controls span').html((currentPhoto+1) + " of " + photoCount);
			$('img.photo-'+currentPhoto).addClass('currentPhoto').removeClass('queue');
			
			if(currentPhoto == 0){
				$('.photo-controls .previous').addClass('disabled');
			} else if(currentPhoto == (photoCount - 1)){
				$('.photo-controls .next').addClass('disabled');
			}
	
			$('img.photo-'+currentPhoto).css('display','inline');
		});
	});
	$('.photo-controls .previous').click(function(){
		$(this).parent().parent().parent().find('img.currentPhoto').fadeOut(300,function(){
			
			$('.photo-controls a').removeClass('disabled'); //reset prev/next buttons
			
			currentPhoto = $(this).attr('class').split("-");
			currentPhoto = parseInt(currentPhoto[1]);
			$('img.photo-'+currentPhoto).removeClass('currentPhoto');
			$('img',this).addClass('queue');
			photoCount = $(this).parent().find('img').size();
			console.log("before: " +currentPhoto);
			
			if(currentPhoto > 0)
			currentPhoto--;
			console.log("after: " +currentPhoto);
			$('.current .photo-controls span').html((currentPhoto+1) + " of " + photoCount);
			$('img.photo-'+currentPhoto).addClass('currentPhoto').removeClass('queue');
			
			if(currentPhoto == 0){
				$('.photo-controls .previous').addClass('disabled');
			} else if(currentPhoto == (photoCount - 1)){
				$('.photo-controls .next').addClass('disabled');
			}
	
			$('img.photo-'+currentPhoto).css('display','inline');
			
		});
	});
});

/*Collect artist data from XML*/
function getData(){
	$.ajax({
		type: "GET",
		url: "data/data.xml",
		dataType: "xml",
		async: "false",
		success: function(xml) {
			xmlData = xml;
			photoPreviews();
			totalArtists = $(xmlData).find('artist').size(); //find artist count
			totalPhotos = $(xmlData).find('photo').size(); //find artist count
		}
	});
}

$.fn.resetArtistPages = function(id) {
	$('#artist-list a').removeClass('active');
	$('#artist-'+id).addClass('active');
	$('.current').returnArtist(id);
	
	if(id == totalArtists){
		nextId = 1;
	} else{
		nextId = id + 1;
	}
	$('.next').returnArtist(nextId);
	
	if(id == 1){
		prevId = totalArtists;
	} else{
		prevId = id - 1;
	}
	$('.prev').returnArtist(prevId);
};

$.fn.returnArtist = function(id,animate) {
		
	var animSpeed = 0;
	if(animate){
		animSpeed = 300;
	}
	
	var firstName,lastName,setTitle,website,statement,imgDirectory,photoSrc,downloadHref;
	var photoSrc = new Array();
	var photoCount;
	
	$(xmlData).find('artist[ id = ' + id + ' ]').each(function(){
		firstName = $(this).find('firstname').text();
		lastName = $(this).find('lastname').text();
		setTitle = $(this).find('setTitle').text();
		website = $(this).find('website').text();
		statement = $(this).find('statement').text();
		downloadHref = $(this).find('bookDownload').text();
		downloadHref = downloadHref.replace(/ /g,'');
		
		imgDirectory = $(this).find('photos').attr('directory');
		
		photoCount = $(this).find('photo[ role = profile ]').size();
		$(this).find('photo[ role = profile ]').each(function(){
			src = $(this).find('src').text();
			photoSrc.push(src);
		});
		
	});
	
	$('.photos',this).fadeOut(animSpeed,function(){
		$('img',this).addClass("currentPhoto").attr('src','photos/' + imgDirectory + '/' + photoSrc[0]).addClass('photo-0').load(function(){
			console.log("LOADED");	
		});
		$('img.temp',this).remove();
		for(var i = 1;i<photoSrc.length;i++){
			path = "photos/" + imgDirectory + "/" + photoSrc[i]
			$(this).append('<img src="' + path + '" alt="" class="photo-' + i + ' queue temp" />');
		}
		
		$('.photo-controls span',this).html('1 of ' + photoCount);
		$('.download a',this).attr('href','photos/'+imgDirectory+'/'+downloadHref)
			.attr('onClick','javascript: pageTracker._trackPageview(\'/downloads/'+firstName+lastName+'\');');
		$(this).fadeIn(animSpeed);
	});

	$('.content',this).fadeOut(animSpeed,function(){
		$(this).empty();
		$('<h2></h2>').html(firstName + " " + lastName).appendTo(this);
		$('<h3></h3>').html("" + setTitle).appendTo(this);
		$('<ul></ul>').append('<li><a href="' + website + '" target="_blank">' + website + '</a></li>').appendTo(this);
		$(this).append(statement);
		$(this).fadeIn(animSpeed);
	});
	
};

function photoPreviews(artistId) {
	var indexPreviewId = new Array();
	var indexPreviewSrc = new Array();
	var indexPreviewTitle = new Array();
	var indexPreviewArtist = new Array();
	var indexPreviewArtistName = new Array();
	var indexPreviewSetTitle = new Array;

	$(xmlData).find('artist').each(function(){
		var artistId = $(this).attr('id');
		var firstname = $(this).find('firstname').text();
		var lastname = $(this).find('lastname').text();
		var setTitle = $(this).find('setTitle').text();
		if(setTitle.length > 40){
			setTitle = setTitle.substring(0,35);
			setTitle = setTitle + "...";
		}
		indexPreviewArtistName.push(firstname + " " + lastname);
		indexPreviewSetTitle.push(setTitle);
		var directory = $('photos',this).attr("directory");
		$('photo[ role = "index" ]',this).each(function(){
			var src = $(this).find('src').text();
			var title = $(this).find('title').text();
			indexPreviewId.push(artistId);
			indexPreviewSrc.push(directory + "/" + src);
			indexPreviewTitle.push(title);
			indexPreviewArtist.push(artistId);
		});
	});
	
	var randoms = new Array();
	while(randoms[0] == randoms[1] || randoms[1] == randoms[2] || randoms[2] == randoms[0]){
		maxVal = indexPreviewArtist.length;
		randoms[0] = Math.floor((maxVal-0)*Math.random());
		randoms[1] = Math.floor((maxVal-0)*Math.random());
		randoms[2] = Math.floor((maxVal-0)*Math.random());
	}
	
	$('#index-featured li').fadeOut(800,function(){
		$('#index-featured li').each(function(i){
			arrayNum = randoms[i];
			$(this).find('img').attr('src',"photos/" + indexPreviewSrc[arrayNum]);
			$(this).find('a').attr('id',"indexartist-" + indexPreviewId[arrayNum]);
			$(this).find('.artist').html(indexPreviewArtistName[arrayNum]);
			$(this).find('.photo-title').html(indexPreviewSetTitle[arrayNum]);
			$(this).fadeIn(1500);
			i++;
		});
	});
	
	photoSwitch = setTimeout(photoPreviews, 8000);
	
};