count = 1;
var interval;
$(document).ready(function() {
	$('#main_top_left a:first').addClass('show');

	total = $('#main_top_left > a').size();
	//execute lightBox
	//$('#gallery>a').lightBox();
	
	//make all images invisible
	$('#main_top_left a').css({opacity: 0.0});
	//show preloader
	//$('#preloader').html('Loading images: 1 of '+total);
	//console.log('Loading images: 1 of '+total);


	//preload images
	$('#main_top_left img').imgpreload({
		each: function(){
			// this = dom image object
			// callback executes when each image loads
			//$('#preloader').html('Loading images: '+count+' of '+total);
			//console.log('Loading images: '+count+' of '+total);
			count ++;
		},
		all: function(){
			//console.log("done");
			// this = jQuery image object selection
			// callback executes when all images are loaded
			//$('#preloader').animate({opacity: 0}, 1000, function() {
				//preloader finished fading off
  			//});
			$('#artist_title_bg').css({opacity: 0.0, visibility: 'visible'});
			$('#main_top_left a').css({visibility: 'visible'});
			$('#main_top_left a:first').animate({opacity: 1.0}, 1000, function() {
				$('#preloader').css({visibility: 'hidden'});
				//execute the slideShow
				slideShow();
  			});

		}
	});
	$('#main_top_left a img').hover(
		function(){
			clearInterval(interval);
			$('#artist_title').html($(this).attr('alt'));
			$('#artist_title_bg').stop().animate({opacity: 1.0}, 200, function() {
				//
			});
			//$('#main_top_left a').stop();
		//console.log('in'+' :: '+ $(this).attr('title'));
		},
		function(){
		//console.log('out'+' :: '+ $(this).attr('title'));
			
			$('#artist_title_bg').stop().animate({opacity: 0.0}, 200, function() {
				//$('#artist_title_bg').css({opacity: 0.0, visibility: 'hidden'});
			});

			interval = setInterval('gallery()',5000);
		}
	);
	$('#artist_title_bg').mouseover(function(){
		clearInterval(interval);
		$('#artist_title_bg').stop().css({opacity: 1.0, visibility: 'visible'});
	});

});




/*
function changeSlide(num){
	clearInterval(interval);
	$('#main_top_left a').stop();
	$('#main_top_left a').css({opacity: 0.0});
	$('#image'+num).css({opacity: 1.0});
	//$('#gallery a').animate({opacity: 0.0}, 1000);
	//$('#image'+num).animate({opacity: 1.0}, 1000);
	$('#main_top_left a').removeClass('show');
	$('#image'+num).addClass('show');
	imageCount = num;
	$('#main_top_left img').attr({src: '/userfiles/image/slide_off.gif'});
	$('#slide'+imageCount).attr({src: '/userfiles/image/slide_on.gif'});

	interval = setInterval('gallery()',5000);
	//console.log(interval);
		
};
*/

function slideShow() {

	//Set the opacity of all images to 0
	$('#main_top_left a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#main_top_left a:first').css({opacity: 1.0});
	
	//call the gallery function to run the slideshow, 5000 = change to next image after 5 seconds
	interval = setInterval('gallery()',5000);
	
}
//var imageCount = 1;
function gallery() {
	
	//if no images have the show class, grab the first image
	var current = ($('#main_top_left a.show')?  $('#main_top_left a.show') : $('#main_top_left a:first'));
	
	//get next image, if it reached the end of the slideshow, rotate it back to the first image
	
	if (current.next().length){
		if(current.next().hasClass('caption')){
			var next = $('#main_top_left a:first');
		}else{
			var next = current.next();
			//imageCount ++//= $('#gallery a').index(current) + 1;
		}
		
	}else{
		var next = $('#main_top_left a:first');
		//imageCount =  1;
	}
	//$('#slider img').attr({src: '/userfiles/image/slide_off.gif'});
	//$('#slide'+imageCount).attr({src: '/userfiles/image/slide_on.gif'});
	//var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	//console.log($('#gallery a').length);// number of images
	//console.log($('#gallery a').index(current)); // image number
	//console.log(imageCount);
	//set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
}

