window.log=function(){log.history=log.history||[];log.history.push(arguments);if(this.console){console.log(Array.prototype.slice.call(arguments))}};

gpo.go = {
	left : function () { gpo.mov_pos = -1; $(window).trigger("resize");},
	right : function () { gpo.mov_pos = 1; $(window).trigger("resize");}
}



gpo.keystroke = [];
gpo.keystroke[37] = gpo.go.left;
gpo.keystroke[38] = gpo.go.left;
gpo.keystroke[39] = gpo.go.right;
gpo.keystroke[40] = gpo.go.right;
gpo.keystroke[44] = gpo.go.left;
gpo.keystroke[46] = gpo.go.right;

$(document).ready(function () {

	$("#nojs").remove();

	gpo.init();
	
})

gpo.init = function()
{

	gpo.mov_pos = 0;
	gpo.cur_pos = 0;

	gpo.autoHidden = [];

	gpo.initHash();

	$(window).trigger("resize");

	$("#go_left").click( function() { gpo.go.left(); });
	$("#go_right").click( function() { gpo.go.right(); });

	gpo.preload();

	if (gpo.settings['big_gallery_titles'] == "1") gpo.showGalleryTitle();
	
	if (gpo.settings['auto_hide_controls'] == "1")
	{
		gpo.autoHidden["#go_left"] = true;
		gpo.autoHidden["#go_right"] = true;
	}

	if (gpo.settings['auto_hide_menu'] == "1")
	{
		gpo.autoHidden["#gallery_nav"] = true;
	}

	gpo.initMouseovers();

}

gpo.initMouseovers = function()
{
	for (var i in gpo.autoHidden)
	{
		$(i).mouseover(function()
		{
			$(this).animate({"opacity": 1}, 500);
			gpo.autoHidden["#"+this.id] = false;
		});
		$(i).mouseout(function()
		{
			gpo.autoHidden["#"+this.id] = true;
		});
	}

	setInterval (function() { 
		for (var i in gpo.autoHidden) 
		{
			if (gpo.autoHidden[i] == true)
			{
				$(i).animate({ 'opacity' : 0 });
			}
		}
	}, 3000);
		
}


gpo.jump = function(destination)
{
	gpo.mov_pos = (destination - gpo.cur_pos);
	$(window).trigger("resize");
}

gpo.showGalleryTitle = function ()
{
	$("h1").text( gpo.imgs[gpo.cur_pos].title).hide();
	$("h1").fadeIn(1000).delay(Math.max(gpo.imgs[gpo.cur_pos].title.length * 100, 1000)).fadeOut(1000);
}


/**
 * Preload images
 * Side effects: sets aspect ratios, loads the current image
 */
gpo.preload = function()
{

	gpo.loaded = 0;
	var img;
	for (var i in gpo.imgs)
	{
		img = new Image();
		img.src = gpo.imgs[i].img_src;
		$("#image_"+i).hide().attr("src", img.src);
		
		img.onload = function ()
		{
			for (var i in gpo.imgs)
			{
				if (gpo.imgs[i].img_src == this.src)
				{
					
					gpo.imgs[i].aspect_ratio = $(this).naturalWidth() / $(this).naturalHeight();
					gpo.imgs[i].width = $(this).naturalWidth();
					gpo.imgs[i].height = $(this).naturalHeight();
					if (i == gpo.cur_pos)
					{
						$("#image_"+i).show();
						$(window).trigger("resize");
					}
					break;
				}
			}
			gpo.loaded++;
			gpo.updateLoadDisplay();
		}
	}
}

gpo.updateLoadDisplay = function()
{
	$("div#load_status").html(gpo.loaded + " of " + gpo.imgs.length + ": " +gpo.imgs[gpo.loaded-1].img_title);
	$("div#load_status").css({width: (((gpo.loaded+1) / gpo.imgs.length)*100) + "%"});
	if ((gpo.loaded / gpo.imgs.length) == 1)
	{
		$("div#load_status").remove();
	}
}

gpo.initHash = function()
{
	
	if (window.location.hash != undefined)
	{
		for (var i in gpo.imgs)
		{
			if ("#" + gpo.imgs[i].id == window.location.hash)
			{
				gpo.cur_pos = parseInt(i);
				break;
			}
		}
	} else 
	{
		window.location.hash = gpo.imgs[gpo.cur_pos].id;
	}

	gpo.old_hash = window.location.hash;

}

gpo.checkPosition = function()
{
	gpo.old_pos = gpo.cur_pos;
	
	var new_pos = gpo.cur_pos + gpo.mov_pos;

	gpo.mov_pos = 0;

	if (new_pos == -1) { new_pos = 0; }
	if (new_pos == gpo.imgs.length) { new_pos = gpo.imgs.length - 1; }

	gpo.cur_pos = new_pos;
}

gpo.checkImageSize = function()
{

	var w = { x : $(window).width(), y : $(window).height() },
		
		goWithX = (w.y * gpo.imgs[gpo.cur_pos].aspect_ratio < w.x) && (gpo.settings['fit_image_to_window'] != "1"),
	
		dim = {
			x : goWithX ? w.x : w.y * gpo.imgs[gpo.cur_pos].aspect_ratio,
			y : goWithX ? w.x * (1/gpo.imgs[gpo.cur_pos].aspect_ratio) : w.y
		},
		
		offset = gpo.settings['keep_image_center'] == 1 ?
		{
			x : goWithX ?  0 : (w.x - dim.x)/2,
			y : goWithX ?  (w.y - dim.y)/2 :0
		} : {x:0, y:0}
	;

	if (gpo.old_pos != gpo.cur_pos)
	{
		$("#image_"+gpo.cur_pos).show().css(
		{
			width: dim.x,
			height: dim.y,
			left: offset.x,
			top: offset.y,
			opacity: 1,
			"z-index": 0
		});
		$("#image_"+gpo.old_pos).hide();
	}

	$("#image_"+gpo.cur_pos).css(
	{
		width: dim.x,
		height: dim.y,
		left: offset.x,
		top: offset.y
	});

}

gpo.checkHash = function()
{
	window.location.hash = gpo.imgs[gpo.cur_pos].id;
	
	if (gpo.old_hash != window.location.hash)
	{
		if (gpo.settings['big_gallery_titles'] == "1") gpo.showGalleryTitle();
		gpo.old_hash = window.location.hash;
	}
}

$(window).bind("keydown", function (a, b)
{
	var kc = a.keyCode || b;
	if (gpo.keystroke[kc]) gpo.keystroke[kc].call(this);
});

$(window).resize( function() {

	gpo.checkPosition();

	gpo.checkImageSize();

	gpo.checkHash();

	if (gpo.settings['show_image_thumb'] == 1)
	{
		$("nav a").removeClass("current");
		$("nav a:[href='javascript:gpo.jump("+(gpo.cur_pos)+")']").addClass("current");
	} 

	//$("nav ul").css("margin-left", ((6-gpo.cur_pos)*61));
});

$(".image").click(function() { gpo.go.right(); });
