/* JQuery Gallery
 * Copyright (c) 2006 Miek Dunbar (miek AT collabo DOT net || http://www.miek.com.au)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * $Id$
 */

jQuery.fn.gallery = function() {

var element = this;  
	
	if($(element).length != 0){
		var imageSet = new Array();
		var imageSet = $(element).children("img");
		$(element).addClass("image-gallery");
		var containerHeight = 654;
		var containerWidth = 723;
		var currimage = imageSet.length-1;
		$(imageSet).hide();
		
		function highlightThumb(){
			$("#thumb_series_cntr img").removeClass("active");
			$("#thumb_series_cntr img").css('opacity', 1);
			$("#thumb_series_cntr img").eq(currimage).addClass("active");
			$("#thumb_series_cntr img").eq(currimage).css('opacity', 0.75);
		}
		
		$("#thumb_series_cntr img").click(function(){
			changeImage($("#thumb_series_cntr img").index(this));
		});
		
		$("#thumb_series_cntr img").hover(
			function(){
				if($(this).hasClass("active")==false){
					$(this).css('opacity', 0.75);
				}
			},
			function(){
				if($(this).hasClass("active")==false){
					$(this).css('opacity', 1);
				}
			}
		);
	
		
		//Give this the image no (0 index), and it will fade it in
		function changeImage(nextImage){
			if(nextImage!=currimage){
				$(imageSet[nextImage]).show();
				$(imageSet[currimage]).css({'z-index' : '51'})
				$(imageSet[nextImage]).css({'z-index' : '50'})
				$(imageSet[currimage]).hide();
				currimage = nextImage;
				centreImage();
				highlightThumb();
			}
		};
		
		$('div#arrow_next').hover(function(){ $(this).addClass('hover') },function(){ $(this).removeClass('hover') });
		$('div#arrow_prev').hover(function(){ $(this).addClass('hover') },function(){ $(this).removeClass('hover') });
		
		$("div#arrow_next").click(function(){
			var x = currimage - 1;
			if( currimage > 0){
				changeImage(x);
			}else{
			changeImage(imageSet.length-1);
			}
			return false;
		});
		
		$("div#arrow_prev").click(function(){
			nextSlide();
			return false;
		});
		
		//If we are already viewing the last image, it will go back to 0
		function nextSlide(){
			var x = currimage + 1;
			if( currimage < imageSet.length-1){
				changeImage(x);
			}else{
				changeImage(0);
			};
		};
		
		function centreImage(){
			 
			var targetWidth = $(imageSet[currimage]).width();
			var targetHeight = $(imageSet[currimage]).height();
			var newTop = containerHeight/2-targetHeight/2;
			var newLeft = containerWidth/2-targetWidth/2;
			$(imageSet[currimage]).css("margin-top", newTop).css("margin-left", newLeft);
			$(imageSet[currimage]).show();
		};
		
		highlightThumb();
		
		//does the centering for the first image only
		
		
		//var ignoreMe = document.body.offsetWidth;
		var targetWidth = $(imageSet[currimage]).width();
		var targetHeight = $(imageSet[currimage]).height();
		var newTop = containerHeight/2-targetHeight/2;
		var newLeft = containerWidth/2-targetWidth/2;
		$(imageSet[currimage]).css("margin-top", newTop).css("margin-left", newLeft);
		$(imageSet[currimage]).show();
		
		
	}
  
};