var currentimage = 0; 
var image_list = new Array();
var image_description = new Array();
var image_holder;
var number_of_images = 0;
var previous_btn,next_btn;
var maxHeight = 0;
var temp_image = new Image();
$(document).ready(function() { 
	var i = 0;
	image_holder = $("#image_holder");
	// get the img urls
    $("#images_list img").each(function(){ 
		image_list[i] = $(this).attr("src"); 
		image_description[i] = $(this).attr("alt"); 
		i++;
	}); 	
	
	// set the height of the image wrapper to the height of the highest image
	 $(window).load(function(){ 
		 $("#images_list img").each(function(){ 
			if(image_list[0] == $(this).attr('src'))
			{
				image_holder.fadeOut('fast',function(){image_holder.attr('src', image_list[0]);image_holder.fadeIn('fast');});
				$("#image_description").html(image_description[currentimage]);
			}
			temp_image = $(this);
			if(maxHeight < $(this).attr('height')){ 
					maxHeight = $(this).height();  
					$("#gallery_image").height(maxHeight)
				}  
			})
		})
	 
	
	number_of_images = image_list.length;
	$("#number_of_pictures").text(number_of_images);
	next_btn = $(".gallery_next_btn");
	previous_btn = $(".gallery_previous_btn");
	previous_btn.toggleClass('gallery_previous_btn'); 
	previous_btn.toggleClass('gallery_previous_disabled_btn'); 
	$("#gallery_previous_btn").click(function(){openImage(false,$(this),next_btn);return false;});
	$("#gallery_next_btn").click(function(){openImage(true,$(this),previous_btn);return false;});
 });
 
function openImage(flag,switcher,second_switch){ 
	  
	if(flag){ 
		if(currentimage >= number_of_images - 1)
		{	 
			if(switcher.hasClass('gallery_next_btn')){
				switcher.toggleClass('gallery_next_btn'); 
				switcher.toggleClass('gallery_next_disabled_btn');  
			} 
			return false
		}
		else if(currentimage == number_of_images - 2)
		{	 
			if(switcher.hasClass('gallery_next_btn')){
				switcher.toggleClass('gallery_next_btn'); 
				switcher.toggleClass('gallery_next_disabled_btn');  
			}  
		}
		else{  
			if(switcher.hasClass('gallery_next_disabled_btn')){
				switcher.toggleClass('gallery_next_disabled_btn'); 
				switcher.toggleClass('gallery_next_btn');  
			} 
			if((currentimage >= 0) && second_switch.hasClass('gallery_previous_disabled_btn')){ 
				second_switch.toggleClass('gallery_previous_btn'); 
				second_switch.toggleClass('gallery_previous_disabled_btn'); 	 
			}
		} 
		currentimage++;
		}
	else{
		if(currentimage <= 0)
		{		
			if(switcher.hasClass('gallery_previous_btn')){
				switcher.toggleClass('gallery_previous_btn'); 
				switcher.toggleClass('gallery_previous_disabled_btn'); 		
			} 	 
			return false
		}
		else if(currentimage == 1)
		{		
			if(switcher.hasClass('gallery_previous_btn')){
				switcher.toggleClass('gallery_previous_btn'); 
				switcher.toggleClass('gallery_previous_disabled_btn'); 		
			} 	  
		}
		else{ 	 
			if(switcher.hasClass('gallery_previous_disabled_btn')){
				switcher.toggleClass('gallery_previous_btn'); 
				switcher.toggleClass('gallery_previous_disabled_btn'); 
			}
			if((currentimage <= number_of_images) && second_switch.hasClass('gallery_next_disabled_btn')){ 
				second_switch.toggleClass('gallery_next_btn'); 
				second_switch.toggleClass('gallery_next_disabled_btn'); 	 
			}
		} 	
		currentimage--; 
	}
	image_holder.fadeOut('fast',function(){image_holder.attr('src', image_list[currentimage]);image_holder.fadeIn('fast');});
	$("#current_picture").text(currentimage + 1 - 0);
	$("#image_description").html(image_description[currentimage]);
	return false;
	} 
