$(document).ready(function() {
	
	$('img.thumb').hover( function() {
		var src = $(this).attr( "src" );
		src = src.split( "/" );
		
		var img = $(this).attr( "id" ).replace( "-img", "" );
		var folder = src[src.length - 2];
		var src = 'http://www.ghd-style.com/images/site/stylers/large/' + folder + '/' + img + '.jpg';
		var title =  $("#" + img + "-title").val( );
		var caption =  $("#" + img + "-caption").val( );
		
		$.preloadImages( src );
		
		if( $('img.focus').attr( "src" ) != src )
		{
//			$('img.focus').fadeOut(50, function() {
//				$(this).attr({'src':src});
//				if (this.complete) $(this).fadeIn(500);
//			});
			
			$('img.focus').animate({opacity: 0}, "fast", function() {
				$(this).attr({'src':src});
				if (this.complete) $(this).animate({opacity: 1}, "fast");
			});
			
//			$('div.caption').animate({opacity: 0}, "fast", function() {
				if( $("h3#captionh").html() != title )
				{
					$("h3#captionh").animate({opacity: 0}, "fast").html(title).animate({opacity: 1}, "fast");
				}
				if( $("p#captionp").html() != caption )
				{
					$("p#captionp").animate({opacity: 0}, "fast").html(caption).animate({opacity: 1}, "fast");
				}
//				$(this).animate({opacity: 1}, "fast");
//			});
		}
		
//		$('img.focus').fadeOut().attr( "src", "http://office/ghd-style.com/public_html/images/site/stylers/" + folder + "/" + img );
//		$('img.focus').fadeIn();
		
		$.each( $("img.thumb"), function() {
			$(this).attr( "class", "thumb" );
		});
		
		$(this).attr( "class", "thumb selected" );
	});
	
	jQuery.preloadImages = function( )
	{
	  for(var i = 0; i<arguments.length; i++)
	  {
	    jQuery("<img>").attr("src", arguments[i]);
	  }
	}
});
