// Code for Gallery
// Thanks to by Valerio Proietti (http://mad4milk.net) MIT-style license for creating these awesome tools.


window.onload = function (){
    //basic gallery function
  	//use moo.dom to select the big image, description and thumbnails
  	var imageHolder = $('display');	
  	var textHolder  = $('caption');	
  	var thumbs = $$('#thumbnails a');
  	var timeFade = 450;
	
  	//create effect
  	var imageFade = $('display').effect('opacity',{duration:timeFade});
    var textFade = $('caption').effect('opacity',{duration:timeFade});

  	//function that sets active thumb
  	function setActiveThumb() {
  	  thumbs.each(function(element) { 			
  			  if (element.href == imageHolder.src){element.addClass("active");}
  			  else {element.removeClass("active");}
  		});
  	}
	
  	//link scroller functions
  	//function that changes image
  	function changeImage(picURL, txtImg){								
  		imgPreloader = new Image();				
  		imgPreloader.onload = function(){ //once the image is loaded:
  			$('img-info').firstChild.nodeValue = txtImg; // Replace Text
  			imageHolder.src = picURL; // replace the img src
  			imageFade.custom(0,1);// fade the image back to 100%
  			textFade.custom(0,1);
  			setActiveThumb();						
  		}
  		imgPreloader.src = picURL;
  		var imgComplete = imgPreloader.complete;				
  	}
	
  	//use moo.dom to assign an action to each a link inside thumbnail nav calling function changeImage:		
    thumbs.addEvent('click', function(e) {
      var picURL = this.href;
      var txtImg = this.title;
      var imageFadeClick = imageHolder.effect('opacity',{duration:timeFade, onComplete:function(){changeImage(picURL, txtImg);}});
  		imageFadeClick.custom(1,0);
  		textFade.custom(1,0); //hide text
    	stopEvent(e);
    });
}

Window.extend({
	stopEvent: function(e){
		if (e.stopPropagation){
			e.stopPropagation();
			e.preventDefault();
		} else {
			e.returnValue = false;
			e.cancelBubble = true;
		}
		return false;
	}
});
