/*!
 * jQuery Gallery 
 * Copyright (c) 2010 MC
 * Version: 1.0 (20-09-2010)
  */

	$(function () {
			var smallLoader = $('#smallLoader');
			var bigLoader = $('#bigLoader');
			smallLoader.show();
			bigLoader.show();
			$('img').hide();//hide all the images on the page
		});
		
		var i = 0;//initialize
		var int=0;//Internet Explorer Fix
		$(window).bind("load", function() {//The load event will only fire if the entire page or document is fully loaded
			var int = setInterval("doThis(i)",500);//500 is the fade in speed in milliseconds
		
		$('#gallery a:next').addClass("checked");				
	$('#gallery img').each(function(i) {
	
		var imgFile = $(this).attr('src');	
		var preloadImage = new Image();
	var clickImage = new Image();
	  var imgExt = /(\.\w{3,4}$)/;
  preloadImage.src = imgFile.replace(imgExt,'_h$1');
  clickImage.src = imgFile.replace(imgExt,'_c$1');
	
	$(this).hover(
		function() {
			$(this).attr('src', preloadImage.src);
			
			},
		function () {
		var currentSource = $(this).attr('src');
			$(this).attr('src', imgFile);
			}); // end hover
	
	$(this).click(
				function() {
            $('#gallery img').removeClass("checked");
			
		$(this).addClass("checked");		
	}); // end click
	
	
		}); // end each

	
	$('#gallery a').click(function(evt) {
	
	//fade out _over div 
	var oldPrev_over = $('#prev_over img');
    oldPrev_over.hide();
		     		     				
		     		     				
	//make and hide caption
	 $('#gallery').attr('inf', 'a');		     						
	$('#caption').html( $(this).attr('inf') );
	  var Caption = $("#caption");
	  Caption.hide();
	  
	 	evt.preventDefault();	  				
	 	//don't follow link
		//get path to new image
	   var imgPath = $(this).attr('href');
	   var prevPath = $(this).next('a').attr('href');
	   	   		 
	   //get reference to old image
	   var oldImage = $('#photo img');
	   	 var oldPrev = $('#preview img'); 
		 //check to see if they're the same image
				
			 //create HTML for new image
			 var newImage = $('<img src="' + imgPath +'">'); 
			var  newPrev = $('<img src="' + prevPath +'">');
						 			 
			 //make new image invisible
			 newImage.hide();
			newPrev.hide();			 
			 			 
			 //add to the #photo div
			 $('#photo').prepend(newImage);
			 $('#preview').prepend(newPrev);			 			 			 
			 		 
			 //fade in new image
			 newImage.fadeIn(1000);
			 newPrev.fadeIn(850);			 
			 Caption.fadeIn(850);
						 
			 //fade out old image and remove from DOM
			 oldImage.fadeOut(1000,function(){
		     $(this).remove();
		     				});
		     //fade out old Prev and remove from DOM				
		    oldPrev.fadeOut(850,function(){
		     $(this).remove();
		     				});
		 
	}); // end click
		
		$('#gallery a:first').click();
		$('#gallery a img:first').addClass("checked");
				


//fade in _over div 		
$('#gallery a').mouseover(function(evt) {
//var toHide = $(this).attr('href>next');
//toHide.hide(); 
var imgPath = $(this).attr('href');
var prevPath = $(this).next('a').attr('href');	

var  newPrev_over = $('<img src="' + prevPath +'">');

$('#prev_over').prepend(newPrev_over);
newPrev_over.fadeIn(400);

	});
	
	
//fade out _over div
$('#gallery a').mouseout(function(evt) {

var oldPrev_over = $('#prev_over img');
oldPrev_over.fadeOut(400,function(){
		     $(this).remove();
		     });

	}); // end hover	

		
		}); // end load
		
		function doThis() {
			var imgs = $('img').length;//count the number of images on the page
			if (i >= imgs) {// Loop the images
			var smallLoader = $('#smallLoader');
			var bigLoader = $('#bigLoader');
			smallLoader.hide();
			bigLoader.hide();
				clearInterval(int);//When it reaches the last image the loop end
			}
			$('img:hidden').eq(0).fadeIn(500);//fades in the hidden images one by one
			i++;//add 1 to the count
		};













