window.onload = initPage;

// set the cursor to the default
function cursor1() { document.body.style.cursor = 'default'; }

// set the cursor to the pointer
function cursor2() { document.body.style.cursor = 'pointer'; }

// get the named cookie
function getCookie(name) {
	var i;
	var x;
	var y;
	var ARRcookies = document.cookie.split(";");
	for (i=0; i < ARRcookies.length; i++) {
		x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x = x.replace(/^\s+|\s+$/g,"");
		if (x == name) {
			return unescape(y);
		}
	}
}

// run when opening any page
function initPage() {

	// is this a portfolio page?
	var isPortfolio = (document.getElementById('isPortfolio')) ? true : false;

	// set up the sub-navigation
	if (document.getElementById("sub_navigation")) {

		// get the cookie
		var isOpen = getCookie('srsSubNav');

		// does this page force the menu open?
		if (isPortfolio && isOpen == 'closed') {
			isOpen = 'open';
			setCookie('srsSubNav', 'open', 10);

		// if it's undefined, menu is closed
		} else if (isOpen == undefined) {
			isOpen = 'closed';
			setCookie('srsSubNav', 'closed', 10);
		}

		// show/hide based on isOpen setting
		if (isOpen == 'closed') {
			document.getElementById("sub_navigation").style.display = "none";
		} else {
			document.getElementById("sub_navigation").style.display = "block";
		}
	}

	// portfolio pages show an image
	if (isPortfolio) showImage();
}

// scroll the portfolio to the left
function scrollL() {
	if (!window.currentSample) return;
	if (document.getElementById('sample'+ (window.currentSample - 1))) showImage(window.currentSample - 1);
}

// scroll the portfolio to the right
function scrollR() {
	if (!window.currentSample) return;
	if (document.getElementById('sample'+ (window.currentSample + 1))) showImage(window.currentSample + 1);
}

// set the named cookie
function setCookie(name, value, days) {
	var expire = new Date();
	expire.setDate(expire.getDate() + days);
	var value = escape(value) + ((days == null) ? "" : "; expires="+expire.toUTCString())+"; path=/";
	document.cookie = name + "=" + value;
}

// display an image in the portfolio
function showImage(n) {

	// if we weren't passed an image offset, try to get one from window.location
	if (n == null || n == undefined) {
		n = window.location.hash;
		n = Number(n.replace('#', ''));
	}

	// default to sample 1
	if ((n == null || n == 0) && !window.currentSample) n = 1;

	// if the sample doesn't exist, leave now
	if (!document.getElementById('sample'+n)) return;

	// turn off the current sample
	if (window.currentSample && document.getElementById('sample'+window.currentSample)) {
		document.getElementById('sample'+window.currentSample).style.display = 'none';
		if (document.getElementById('num_'+window.currentSample)) document.getElementById('num_'+window.currentSample).className = 'num';
		window.currentSample = 0;
	}

	// turn on the new sample
	document.getElementById('sample'+n).style.display = 'block';
	window.currentSample = n;
	window.location.hash = n;
	if (document.getElementById('num_'+n)) document.getElementById('num_'+n).className = 'numActive';

	// correct the left scroll arrow
	if (document.getElementById('sample'+(n-1))) {
		document.getElementById('scrollLeft').style.opacity = 1;
	} else {
		document.getElementById('scrollLeft').style.opacity = .3;
	}

	// correct the right scroll arrow
	if (document.getElementById('sample'+(n+1))) {
		document.getElementById('scrollRight').style.opacity = 1;
	} else {
		document.getElementById('scrollRight').style.opacity = .3;
	}
}

// reverse the visible state of the subnav div
function toggleNav() {
	if (document.getElementById("sub_navigation")) {
		var isOpen = getCookie('srsSubNav');
		if (isOpen == 'open' || isOpen == undefined) {
			setCookie('srsSubNav', 'closed', 10);
			document.getElementById("sub_navigation").style.display = "none";
		} else {
			setCookie('srsSubNav', 'open', 10);
			document.getElementById("sub_navigation").style.display = "block";
		}
	}
}

