$(document).ready(function()
{
	// Init Player
	current_active = null;
	current_zoom = null;
	backup_pager = $("#player span.pager").html();
	
	$("#player-content div.inner a.thumb").click(function(){
		ZoomImg($("#player-content a.thumb").index(this));
		return false;
	});
	
	$(document).keydown(function(event) {
		if ($("#player").is(":visible")) {
			if (event.keyCode == '37') {
				if ($("#player div.command a.cmd-prev").is(".off")) player_prev($("#player div.command a.cmd-prev"));
			} else if (event.keyCode == '39') {
				if ($("#player div.command a.cmd-next").is(".off")) player_next($("#player div.command a.cmd-next"));
			} else if (event.keyCode == '38') {
				player_back($("#player div.command a.cmd-back"));
			}
		}
	});
	$("#player div.command a.cmd-back").click(function(){
		if ($(this).is(".n2")) {
			player_back($(this));
			return false;
		}
	});
	$("#player div.command a.cmd-next").click(function(){
		if ($(this).is(".off")) return false;
		else {
			player_next($(this));
			return false;
		}
	});
	$("#player div.command a.cmd-prev").click(function(){
		if ($(this).is(".off")) return false;
		else {
			player_prev($(this));
			return false;
		}
	});
	
	// Function
	function player_prev(target) {
		if (target.is(".n2")) {
			tmp_current_zoom = $("#player-content div.inner li img").eq(current_zoom - 1);
			if (current_zoom > 0) ZoomImg(current_zoom - 1);
		} else {
			tmp_current_active = $("#" + current_active.prevAll(".project").attr("id"));
			if (tmp_current_active.length > 0) {
				current_active = tmp_current_active;
				$("#player-content").html(sHtmlPlayerLoading);
				GetStoryboard(current_active.attr("storyboard"));
			}
		}
	}
	function player_next(target) {
		if (target.is(".n2")) {
			tmp_current_zoom = $("#player-content div.inner li img").eq(current_zoom + 1);
			if (tmp_current_zoom.length > 0) ZoomImg(current_zoom + 1);
		} else {
			tmp_current_active = $("#" + current_active.nextAll(".project").attr("id"));
			if (tmp_current_active.length > 0) {
				current_active = tmp_current_active;
				$("#player-content").html(sHtmlPlayerLoading);
				GetStoryboard(current_active.attr("storyboard"));
			}
		}
	}
	function player_back(target) {
		$("#player span.pager").html(backup_pager);
		$("#player div.command a").removeClass("n2");
		$("#player-zoom").hide();
		$("#player-content").show();
	}
	function ZoomImg(i) {
		current_zoom = i;
		$("#player-zoom").addClass("loading");
		$("#player-zoom img").css({ opacity: 0 });
		$("#player span.pager").html((current_zoom+1) + " &mdash; " + $("#player-content div.inner li").length);
		s = String($("#player-content div.inner li img").eq(current_zoom).attr("src")).replace("/-thumbnails/", "/");
		if (!$("#player-zoom").is(":visible")) {
			$("#player div.command a").addClass("n2");
			$("#player-zoom").show();
			$("#player-content").hide();
		}
		$("#player-zoom img").attr("src", s).load(function(){
			$("#player-zoom").removeClass("loading");
			$(this).animate(
				{opacity: 1},
				{queue:false, duration:"fast"}
			);
		});
		if ((current_zoom+1) == $("#player-content div.inner li").length) $("#player div.command a.cmd-next").addClass("off");
		else $("#player div.command a.cmd-next").removeClass("off");
		
		if ((current_zoom+1) == 1) $("#player div.command a.cmd-prev").addClass("off");
		else $("#player div.command a.cmd-prev").removeClass("off");
	}
});
