function moveLeftLink(ypos) {
	$("#left").css("top", ypos);
}

function updateNavLinks() {
	if($(window).scrollLeft() > 0) {
		$("#left").fadeIn(300);
	}
	else {
		$("#left").fadeOut(300);
	}
	
	//console.log($(document).width() - $(window).scrollLeft() - $(window).width());
	if($(document).width() - $(window).scrollLeft() - $(window).width() < 200 ) {
		$("#right").fadeOut(300);
	}
	else {
		$("#right").fadeIn(300);
	}
}

updateNavLinks();

$(window).scroll(function() {
	updateNavLinks();
});

function getNext() {
	var toReturn = $("html");
	var returnNext = false;
	//console.log("window position: " + $(window).scrollLeft());
	$("#content div").each(function() {
		//console.log($(this).attr("class"));
		//console.log($(this).offset().left);
		//console.log($(this).offset().left - $(window).scrollLeft());
		if(returnNext) {
			toRetun = $(this);
			return false;
		}
		if( ($(this).offset().left - $(window).scrollLeft()) > 101) {
			//console.log($(this).attr("class"));
			
			toReturn = $(this);
			returnNext = true;
		}
		
	});
	return toReturn;
	//return $("#content>div:first-child");
}

function getPrev() {
	var toReturn = $("html");
	
	var returnNext = false;
	//console.log("window position: " + $(window).scrollLeft());
	$("#content div").each(function() {
		
		//console.log($(this).attr("class"));
		//console.log($(this).offset().left);
		//console.log($(this).offset().left - $(window).scrollLeft());
		if( ($(this).offset().left - $(window).scrollLeft()) > -100) {
			//console.log($(this).attr("class"));
			return false;
		}
		toReturn = $(this);
		
	});
	return toReturn;
	//return $("#content>div:first-child");
}

function myScrollTo(x){
	$("html, body").animate({
		scrollLeft: x-100,
	}, 600);
}

function scrollRight() {
	var cur = getNext();
	//console.log("target position: " + cur.offset().left);
	//console.log("window position: " + $(window).scrollLeft());
	myScrollTo(cur.offset().left);
	//console.log(cur.attr("class"));
}

function scrollLeft() {
	var prev = getPrev();
	//console.log("target position: " + prev.offset().left);
	//console.log("window position: " + $(window).scrollLeft());
	if($(window).scrollLeft() == prev.offset().left) {
		myScrollTo(0);
	}
	else {
		myScrollTo(prev.offset().left);
	}
}

$("#right, #left").hover(function() {}, function(e) {
	$(this).find('a').css({"top" : "50%"});
});

$("#right, #left").mousemove(function(e) {
	//console.log(e.pageY);
	link = $(this).find('a');
	link.css({"top" : e.pageY - $(window).scrollTop() - link.height()/2});
});


$("#right>a").click(function() {
	scrollRight();
});

$("#left>a").click(function() {
	scrollLeft();
});
/*
$(window).mousemove(function(e) {
	var pos = e.pageX - $(window).scrollLeft();
	if(pos < 100) {
		moveLeftLink(e.pageY - $(window).scrollTop());
	}
	else if ( pos > $(window).width() - 100) {
		console.log(e.pageY - $(window).scrollTop());
	}
});*/

