(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)			

//bind the forward/backward icons
function bindNav()
{
	$(".next").bind("click", function()
	{
		navImage('next');
	});
	
	//previous image
	$(".back").bind("click", function()
	{
		navImage('prev');
	});			
}

function showImage(GLR_id)
{
	$.post("/view/gallery_ajax.php", { show: 1, GLR_id: GLR_id }, function(data)
	{
		$("div#overlay").css("opacity", 0.7).css("height", "100%").show();
		$("#holder").css("top", 300).show();
		
		img_html = "<img src='/img/gallery/preview/"+data.image+"' class='full' height='"+data.height+"' width='"+data.width+"' />";
		desc_html = "<div class='desc'><h1>"+data.name+"</h1><p>"+data.desc+"</p></div>";
		loader = "<img src='/img/gallery/loader.gif' class='loader' />";
		
		section = "<div class='section'><p>"+data.section+"</p></div>";
		
		current_img = "<div class='current'><p>"+data.this_image+"/"+data.max_image+"</p></div>";
		
		corners = "<img src='/img/gallery/corner.jpg' class='corner' /><img src='/img/gallery/back.jpg' class='back' /><img src='/img/gallery/next.jpg' class='next' />";
		
		screen_w = $("div#overlay").width();
		
		$("#holder").css("left", (screen_w - 60)/2 );
		$("#holder").html(loader+img_html+desc_html+current_img+section);
		//$(".current").css("width", data.width);
		
		//hold
		$("#holder").animate({
			opacity: 1
		}, 1500, function()
		{
			total_h = parseInt(80) + parseInt(data.height) + parseInt(110);
			//resize
			$("#holder").animate({
				height: total_h,
				width: data.width,
				left: '-='+(data.width/2),
				top: '-='+(total_h/2)
			}, 500, function()
			{
				//show image
				$("#holder").append(corners);
				
				//bind nav icons
				bindNav();
				
				$(".loader").fadeOut();
				$(".full, .desc, .current, .section").fadeIn();
			});
		});
		
	}, "json");	
	
}

function hideImage()
{
	screen_w = $("div#overlay").width();
	
	//unbind nav icons
	$(".next, .back").unbind("click");
	
	$("#holder").html("").animate({
				height: 60,
				width: 60,
				left: (screen_w - 60)/2,
				top: 300,
				opacity: 0.05
			}, 500, function()
			{
				$(this).fadeOut();
				$(this).html("");
			});
	$("#overlay").fadeOut();
}

function navImage(dir)
{
	$(".next, .back").unbind("click").fadeOut();
	$.post("/view/gallery_ajax.php", { dir: dir }, function(data)
	{
		$(".full, .desc").fadeOut();
		
		//resize
		total_h = parseInt(80) + parseInt(data.height) + parseInt(110);
		
		//calculate new positions
		curr_w = $("#holder").width();
		curr_h = $("#holder").height();
		diff_w = parseInt(curr_w) - parseInt(data.width);
		diff_h = parseInt(curr_h) - parseInt(total_h);
		
		$("#holder").delay(500).animate({
				height: total_h,
				width: data.width,
				left: '+='+diff_w/2,
				top: '+='+diff_h/2
			}, 750, function()
			{
				//bind nav icons
				bindNav();
				
				$(".desc").html("<h1>"+data.name+"</h1><p>"+data.desc+"</p>")
				$(".full").attr("src", "/img/gallery/preview/"+data.image).attr("height", data.height).attr("width", data.width);
				$(".full, .next, .back, .desc").fadeIn();
				$(".current").html("<p>"+data.this_image+"/"+data.max_image+"</p>");
			});
	}, "json");
}
