// ==============================
// = Initialize page scope vars =
// ==============================
var startPoints = new Array;
var nodes = new Array;
var width;
var browserWidth;
var prev;
var next;
var firstPass;
var currNode;

// =================
// = Handle events =
// =================
$(document).ready(function () {
	firstPass = true; // For browser resizing

	// Set document width
	$('.node-portfolio').css('width', document.documentElement.clientWidth);

	$.localScroll.defaults.axis = 'x';
	//$('#menu').localScroll();
	$('#arrowButtons').localScroll();

	// Initialize node name and start points arrays
	setArrays();


	// Set previous and next links to 0 and 1 initially
	// prev = 0;
	// next = 1;
	updateButtons();

	/*
	setPrevAndNext();

	// Hide previous and next buttons if out of scope
	if ( prev < 0 ) { $('a.leftButton').hide(); next = 1; } else { $('a.leftButton').css('display', 'block'); }
	if ( next >= startPoints.length ) { $('a.rightButton').hide(); } else { $('a.rightButton').css('display', 'block'); }

	// Set the leftButton and rightButton href values
	setHrefAttrs();
	
	

	// setHrefAttrs();
	*/
});

$(window).resize(function () {
	if ($('body').hasClass('node-type-portfolio')) {
		// Only get current node every other resize event
		// Resize seems to get called twice for each resize
		if (firstPass == false) {
			firstPass = true;
			// Scroll to keep current node in view
			$(window).scrollTo({top:0, left:startPoints[currNode]}, 0);
			return;
		}
		firstPass = false;

		// Reset document width
		$('.node-portfolio').css('width', document.documentElement.clientWidth); 

		// Get current node
		currNode = getCurrentNode();
		// Reset startPoints array values
		setStartPointsArray();

		// Scroll to keep current node in view
		$(window).scrollTo({top:0, left:startPoints[currNode]}, 0);
	}
});

$(window).scroll(function (e) {
	// Stop propogating the event
	e.stopPropagation();

	// Update previous and next buttons
	updateButtons();
});

// ====================
// = Useful functions =
// ====================

// Set nodes and startPoints arrays
function setArrays() {
	width = 0;
	browserWidth = document.documentElement.clientWidth - 250;

	var i = 0;
	$('#container .node').each(function() {
		// Initalize array with widths
		if (i == 0) {
			// Position of first element is it's left offset
			width += $(this).offset().left;
		}

		nodes[i] = $(this).attr('id'); // nodes[i] holds the node id of the ith node
		startPoints[i] = width; // startPoints[i] holds the left position of the ith node
		i++;

		// Add the width of the current node to width of all previous nodes
		width += $(this).outerWidth( true );
	});
	$('#container').css('width', width + browserWidth);
}

// Set startPoints array only
function setStartPointsArray() {
	width = 0;
	browserWidth = document.documentElement.clientWidth -250;

	var i = 0;
	$('#container .node').each(function() {
		// Initalize array with widths
		if (i == 0) {
			// Position of first element is it's left offset
			width += $(this).offset().left;
		}

		startPoints[i] = width; // startPoints[i] holds the left position of the ith node
		i++;

		// Add the width of the current node to width of all previous nodes
		width += $(this).outerWidth( true );
	});
	$('#container').css('width', width + browserWidth);
}

// Return current node
function getCurrentNode() {
	// Get scroll position
	var currLoc = $(window).scrollLeft();

	// Determine current node
	var curr;
	for (curr=0; curr < startPoints.length; curr++) {
		if (currLoc < startPoints[curr]) {
			return --curr;
		};
	};
	return startPoints.length - 1;
}

// Set prev and next vars for array access
function setPrevAndNext() {
	// Get scroll position
	var currLoc = $(window).scrollLeft();

	for (prev=0; prev <= startPoints.length; prev++) {
		// Hits on first completely viewable node
		if (currLoc < startPoints[prev]) {
			// Previous node is node before one we can see
			// Next node is the first that can be seen
			next = prev;
			prev--;
			return;
		};
		// Hits on first completely viewable node provided it is pixel perfect
		if (currLoc == startPoints[prev]) {
			// Previous node is the one before the one we're looking at
			// Next node is previous node + 2
			prev--;
			next = prev + 2;
			return;
		};
		// Off the end of the page
		if (prev == startPoints.length) {
			// User is off end of page
			// Set next to an undefined value
			// Set previous to the previous node.
			next = prev + 1;
			prev--;
			return; // Removing this causes infinite loop
		}
	};
}

// Set the leftButton and rightButton href values
function setHrefAttrs() {
	$('.leftButton').attr('href', '#' + nodes[prev]);
	$('.rightButton').attr('href', '#' + nodes[next]);
}

// There has been a change. Update buttons
function updateButtons() {
	// Set prev and next values for array access
	setPrevAndNext();

	// Hide previous and next buttons if out of scope
	if ( prev < 0 ) { $('a.leftButton').hide(); next = 1; } else { $('a.leftButton').css('display', 'block'); }
	if ( next >= startPoints.length ) { $('a.rightButton').hide(); } else { $('a.rightButton').css('display', 'block'); }

	// Set the leftButton and rightButton href values
	setHrefAttrs();	
}
