$(function() {
	var gallery_thumb_set_top = "-100px";
	var animate = true;
	var current_image = 0;
	var next_image = 0;
	var images = null;
	var in_animation = false;
	var element_height = $(window).height()-45;
	var element_width = $(window).height();
	var gallery_large = $("#gallery-large");
	
	$("#gallery-large img").css("height", element_height+'px');
	$(window).resize(function() {
		var element_height = $(window).height()-45;
		var element_width = $(window).height();
		$("#gallery-large img").css("height", element_height+'px');
	});
	$("#gallery-toggle-thumbs-close").click(function() {
		gallery_hide();
	});
	$("#gallery-toggle-thumbs").click(function() {
		var parent = $(this).parent("div");
		if($("#gallery-loader").css("display") != 'block') {
			if(parent.css("top") == gallery_thumb_set_top) {
				gallery_show_thumbs();
			} else {
				gallery_hide_thumbs(gallery_thumb_set_top);
			}
		}
		
	});
	$("#gallery-toggle-right").live('click', function() {
		change_image("right");
	});
	$("#gallery-toggle-left").live('click', function() {
		change_image("left");
	});
	
	
	$("#thumb-data-li li img").live('click' , function() {
		$("#gallery-large img").attr("src", $(this).attr("src"));

		current_image = ($(this).attr("id"))-0;

		//gallery_hide_thumbs(gallery_thumb_set_top);
	});
	$(".gallery-open").click(function() {
		$("#gallery-thumbs").css("top", gallery_thumb_set_top);
		$("#gallery-toggle-thumbs").html('<img src="/images/gallery-open.gif">');
		$("#gallery-large img").attr("src", "/images/none.png");
		var href = $(this).attr("href");
		$("#gallery").css("display", "block");
		$("#gallery-loader").css("display", "block");
		$.ajax({
		  url: href,
		  success: function(data) {
		  	images = data;
		  	current_image = 0;
		  	var thumb_data = $("#gallery-thumb-data");
		  	thumb_data.html('<ul id="thumb-data-li"></ul>');
		  	var thumb_li   = $("#thumb-data-li");
		  	$.each(data, function(index, e) { 
			 thumb_li.append('<li><img height="100" id="'+index+'" src="/images/activities/images/'+e['file']+'"></li>');
			});
			$("#gallery-large img").attr("src", "/images/activities/images/"+data[0]['file']);
				$("#gallery-loader").css("display", "none");
		  }
		});
	});
	
	function change_image(direction) {

		if(direction == 'left') {
			next_image = (current_image)-1;
			next_image = next_image
			if(next_image < 0) {
				next_image = (images.length-1);
			}

			current_image = next_image;
			if(animate) {
				if(!in_animation) { 
					in_animation = true;
					gallery_large.animate({"left":"-"+(element_width+500)+"px"}, 400);
					gallery_large.queue(function() {
						$("#gallery-large img").attr("src", "/images/activities/images/"+images[next_image]['file']);
						$(this).dequeue();

						gallery_large.css("left", (element_width+500)+"px");
						gallery_large.animate({"left":"0px"}, 400);
						gallery_large.queue(function() {
							in_animation = false;
							$(this).dequeue();
						});
					});
				}
			} else {
				$("#gallery-large img").attr("src", "/images/activities/images/"+images[next_image]['file']);
			}
		} else {
			next_image = current_image+1;
			if(next_image >= images.length) {
				next_image = 0;
			}
			current_image = next_image;


			if(animate) {
				if(!in_animation) { 
					in_animation = true;
					gallery_large.animate({"left":(element_width+500)+"px"}, 400);
					gallery_large.queue(function() {
						$("#gallery-large img").attr("src", "/images/activities/images/"+images[next_image]['file']);
						$(this).dequeue();

						gallery_large.css("left", "-"+(element_width+500)+"px");
						gallery_large.animate({"left":"0px"}, 400);
						gallery_large.queue(function() {
							in_animation = false;
							$(this).dequeue();
						});
					});
				}
			} else {
				$("#gallery-large img").attr("src", "/images/activities/images/"+images[next_image]['file']);
			}
			
			
			

		}
		
	}
	
});



function gallery_hide_thumbs(top) {
	$("#gallery-thumbs").animate({"top": top}, 300);
	$("#gallery-toggle-thumbs").html('<img src="/images/gallery-open.gif">');

}

function gallery_show_thumbs() {
	$("#gallery-thumbs").animate({"top":"0px"}, 300);
	$("#gallery-toggle-thumbs").html('<img src="/images/gallery-close.gif">');
}

function gallery_hide() {
	$("#gallery").css("display", "none");
}

