_timer = null;
_project = null;
_sideNavTween = null;

// ---------------------------------------------------------------------------

function filterNav (className) {
	// reset content
	$('#sideNavMenu ul').css({top:0});
	// Dont animate IE or Safari, box model troubles
	if($.browser.msie || $.browser.safari) {
		if (className != 'all') {
			var activeLinks = $('#sideNavMenu ul li.'+className);
			$('#sideNavMenu ul li.'+className).show();
			$('#sideNavMenu ul li:not(.'+className+')').hide();														 
		}
		else {
			var activeLinks = $('#sideNavMenu ul li');
			$('#sideNavMenu ul li').show();		
		}
	}
	//Animate only for Firefox
	else {
		if (className != 'all') {
			var activeLinks = $('#sideNavMenu ul li.'+className);
			$('#sideNavMenu ul li.'+className).slideDown(400);
			$('#sideNavMenu ul li:not(.'+className+')').slideUp(400);														 
		}
		else {
			var activeLinks = $('#sideNavMenu ul li');
			$('#sideNavMenu ul li').slideDown(400);		
		}
	}
	//$('#sideNavMenu/ul').fadeIn('normal'); <- this breaks the filter results in Safari
	// build pagination
	var totalPages = Math.ceil(activeLinks.size() / 15);
	$('#sideNavPager').empty();
	if (totalPages > 1) {
		var result = 'Page: ';
		for (var i = 0; i < totalPages; i++) {
			var dist = -(315*i);
			var id = (i == 0) ? 'id="sideNavActivePager"' : '';
			result += '<a href="#" '+id+' onclick="sideNavScroll(this, '+dist+');return false;">'+(i+1)+'</a>';
			if (result.length > 0 && i < totalPages - 1) result += ' | ';
		}
		$('#sideNavPager').html(result);
	}
}
function loadPage (anchor, category) {
	resetScroll();
	// get project name
	var href = $(anchor).attr('href');
	if (window.urchinTracker) urchinTracker(href);
	var parts = cleanSplit(href, '/');
	_project = parts[parts.length-1];
	// deactivate old active project
	$('#sideNavActiveLink').removeAttr('id');
	$('#sideNavActiveProjArrow').remove();
	// activate current project link
	$(anchor).attr('id', 'sideNavActiveLink');
	$(anchor).append('<img src="_img/shared/sidenav_arrow.gif" id="sideNavActiveProjArrow" />');
	// clear content
	$('#mainContent').css({backgroundImage:'url(_img/projects/'+_project+'/bg.jpg)'});
	$('#sideNavLegend').attr('className', category);
	$('#backToTop').css('display', 'none');
	$('#content').html('<p id="loadingPage"><img src="_img/shared/spinner.gif" width="14" height="14" /> Loading Page</p>');
	// load page
	clearTimeout(_timer);
	_timer = setTimeout('doLoadPage()', 100);
}
function doLoadPage(){
	var pg = 'projects/'+_project+'.php';
	$('#content').load(pg, null, function(){
		loadCallback();
	});
}
function loadCallback(){
	$('#backToTop').css('display', 'inline');
}
function sideNavScroll (anchor, y) {
	$('#sideNavActivePager').removeAttr('id');
	$(anchor).attr('id', 'sideNavActivePager');
	if (_sideNavTween != null) _sideNavTween.stop();
	
	$('#sideNavMenu ul').animate({ 
		top: y
	}, 600, "easeOutCirc" );
	_sideNavTween = new Tween($('#sideNavMenu ul').get(0), 'top');
	//_sideNavTween.start(null, y, 15, 'easeout');
	//$('#sideNavMenu/ul').animate({top:y}, 800, 'easeout', function(){
		//$(this).css('top', y);
	//});
}

// ---------------------------------------------------------------------------
// Old School Nav Scroller

function initScroll(){
	_nav = document.getElementById('sideNav');
	_scrolltimer = null;
	_scrolling = false;
	if (document.all) {
		if (document.documentElement && document.documentElement.clientHeight) {
			var doc = document.documentElement;
		} else {
			var doc = document.body;
		}
	} else {
		var doc = document;
	}
	doc.onscroll = startScroll;
	window.onresize = startScroll;
}
function resetScroll(){
	stopScroll();
	_nav.style.top = '0px';
	window.scrollTo(0,0);
}
function startScroll(){
	if ( ! _scrolling) {
		_scrolling = true;
		doScroll();
	}
}
function stopScroll(){
	_scrolling = false;	
	clearTimeout(_scrolltimer);
}
function doScroll(){
	if (document.documentElement && document.documentElement.clientHeight) {
		var windowHeight = document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		var windowHeight = document.body.clientHeight;
	} else {
		var windowHeight = window.innerHeight;
    }
	if (window.pageYOffset) {
		var scrollTop = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		var scrollTop = document.documentElement.scrollTop;
	} else if (document.body && document.body.scrollTop) {
		var scrollTop = document.body.scrollTop;
	} else {
		scrollTop = 0;
	}
	if (windowHeight >= 550) {
		var yTarget = scrollTop;
		var yCurrent = parseInt(_nav.style.top) || 0;
		var y = yCurrent + (yTarget - yCurrent) / 2;
		_nav.style.top = y + 'px';
		if (Math.abs(yTarget - y) <= 2) {
			stopScroll();
			_nav.style.top = yTarget + 'px';
		} else {
			_scrolltimer = setTimeout('doScroll()', 50);
		}
	} else {
		stopScroll();
		_nav.style.top = '0px';
	}
}

// ---------------------------------------------------------------------------

$(document).ready(function(){
	initScroll();
	// activate nav filter
	filterNav('all');
	$('#sideNavFilter select').change(function(){
		filterNav($(this).val());
	})
	// activate nav menu
	$('#sideNavMenu a').click(function(){
		loadPage(this, this.parentNode.className);
		return false;
	});
	
	$('#sideNavMenu li a').hover(function(){
		$(this).prev().stop();
		$(this).prev().attr("style","opacity: 1;");
	}, function(){
		$(this).prev().animate({ 
        	opacity: "0.01"
      	}, 100);
	});
	
	$('#sideNavPager a').click(function() {
		return false;										
	});
	
	// back to top
	$('#backToTop').click(function(){//a[@href=#top]
		resetScroll();
		return false;
	});
	
	// load the first project
	var a = (hasPageParams()) ? $('#sideNavMenu a[@href*=work/'+getLastPageParam()+']').get(0) : null;
	var activeAnchor = a ? a : $('#sideNavMenu a').get(0);
	loadPage(activeAnchor, activeAnchor.parentNode.className);
});

// ---------------------------------------------------------------------------

