var currPost = 0;
var posts = [];
var locked = false;
var paddingTop = 20;

function slideRight(id) {
	if (locked) {
		return;
	}
	
	var post = getCurrentPost(id);
	
	if (post.images.length < 2) {
		return;	
	}
	
	locked = true;
	
	var curr = post.images[post.current];
	post.current = (post.current + 1) % post.images.length;
	var next = post.images[post.current];
	
	curr.animate({
		opacity: 0,
		left: -600
	}, 
	{
		complete: function() {
			curr.addClass("inactive");
			locked = false;
		}
	});	
	
	next.css("left", 600);
	next.css("opacity", 0);
	next.removeClass("inactive");
	next.animate({
		opacity: 1,
		left: 0
	});	
}

function slideLeft(id) {
	if (locked) {
		return;
	}
	
	var post = getCurrentPost(id);
	
	if (post.images.length < 2) {
		return;	
	}
	
	locked = true;
	
	var curr = post.images[post.current];
	post.current = (post.current - 1);
	if (post.current < 0) {
		post.current = post.images.length - 1;	
	}
	var next = post.images[post.current];
	
	curr.animate({
		opacity: 0,
		left: 600
	}, 
	{
		complete: function() {
			curr.addClass("inactive");
			locked = false;
		}
	});	
	
	next.css("left", -600);
	next.css("opacity", 0);
	next.removeClass("inactive");
	next.animate({
		opacity: 1,
		left: 0
	});		
}

function slideDown() {
	var post = getCurrentPost();
	if (post.index + 1 < posts.length) {
		var next = posts[post.index + 1];
		var top = $("#" + posts[next.index].id).position().top - paddingTop;
		$("html,body").animate({
			scrollTop: top
		});
	}
}

function slideUp() {
	var post = getCurrentPost();
	var index = post.index - 1;
	if (index < 0) {
		index = 0;
	}

	var next = posts[index];
	var top = $("#" + posts[next.index].id).position().top - paddingTop;
	$("html,body").animate({
		scrollTop: top
	});
}

function getCurrentPost(id) {
	if (id != undefined) {
		for (var i = 0; i < posts.length; ++i) {
			if (posts[i].id == id) {
				return posts[i];	
			}
		}
	}
	
	var scrollPos = $(window).scrollTop();
	var distance = Math.abs(scrollPos - $("#" + posts[0].id).position().top);
	for (var i = 1; i < posts.length; ++i) {
		var d = Math.abs(scrollPos - $("#" + posts[i].id).position().top);
		//console.log("  " + d + " vs " + distance);
		if (d > distance) {
			return posts[i - 1];	
		}
		distance = d;
	}
	return posts[posts.length - 1];	
}

function initArrowButtons() {
	$(".article").each(function() {
		var post = {
			id: $(this).attr("id"),
			images: [],
			current: 0,
			index: posts.length
		};
	
		var imageContainer = $(this).find(".image-container");		
		imageContainer.find("img").each(function() {
			post.images.push($(this));											   
		});		
		
		if (post.images.length > 1) {
			imageContainer.append('<div class="arrow-left" onclick="slideLeft(\'' + post.id + '\');"></div>');
			imageContainer.append('<div class="arrow-right" onclick="slideRight(\'' + post.id + '\');"></div>');
		}
		
		posts.push(post);
	});
}

function initBasicVersion() {
	
	$("#categories").change(function(event) {
		var url = $(this).val();
		if (url != "") {
			document.location.href = url;	
		}
	});
	
	$(".nav-info").hide();
}

function initFullVersion() {
	
	initArrowButtons();
	
	$(document).keydown(function(event) {
		if (document.activeElement.nodeName.toLowerCase() == "input") {
			return;
		}
		if (event.which == 37) { // left
			slideLeft();
		}
		else if (event.which == 39) { // right
			slideRight();
		}
		else if (event.which == 38) { // up
			event.preventDefault();
			slideUp();	
		}
		else if (event.which == 40) { // down
			event.preventDefault();
			slideDown();	
		}
	});
	
	$("#categories").change(function(event) {
		var url = $(this).val();
		if (url != "") {
			document.location.href = url;	
		}
	});
}

$(document).ready(function() {
	
	if ($.browser.msie) {
		var v = parseInt($.browser.version, 10);
		if (v < 8) {
			initBasicVersion();
			return;
		}
	}
	
	initFullVersion();
	
});
