$().ready(function() {
	// assume javascript is off until we are notified otherwise
	// document.write('<?php $scriptEnabled = true; ?>');
		$('#portfolio ul li a').show();
	
	
	setupUI();
	
		if (typeof(photos) != "undefined") {
			setupFunctions();
			setupEvents();
			createThumbs(photos);	
		}
});

function setupUI(){
	$('#portfolio').accordion({
		active: false,
		autoHeight: true,
		animated: 'slide'
	});
	
	$('#portfolio').blur();
	$('#portfolio ul li a').show();
	$('#main').hide();
	// Put the loading message here.
	$('#main').html("<h1>Loading Image <img id='loader' src='/images/load/loader.gif'/></h1>");	
}

function setupFunctions(){
	$.fn.image = function(src, func){ 
	   return this.each(function(){ 
	     var image = new Image(); 
	     image.src = src; 
	     image.onload = func; 
	     this.appendChild(image);
		
	   }); 
	 }
}

function setupEvents(){
	// Back to thumbs
	$('#main').click(function(){
		$(this).fadeOut("slow");
		$('#thumbs').fadeIn('slow');
		$('#main').html('');
	});
}

function createThumbs(data){
	// get gallery name
	$.each(data, function(i,item){
		
		$("#thumbs >ul >li").image("/images/galleries/"+galleryName.toLowerCase()+"/thumbs/"+data[i].filename,function(){
			$(this).click(function () {

				$('#thumbs').hide();
				$('#main').fadeIn();
				$("#main").image("/images/galleries/"+galleryName.toLowerCase()+"/images/"+data[i].filename,function(){
					$('#main h1').remove();
					$('#main img').fadeIn("slow");
				});
			});
			$(this).fadeIn("slow");
		 });
		// $("#thumbs-large >ul").append(img);
	});
	$("#thumbs >h1").html('');
}//end function

