$(document).ready(function(){
	var sections = ['header','#homeSlide','#awards','#aboutUs','#clients','#accolades','#ee','footer'];
	var classes = ['.home','.work','.award','.about','.clients','.acco','.ee','.foot'];
	
	var sec=getUrlVars(window.location.href)["l"];
	if(sec !== undefined && sec > 0 && sec < sections.length){
		var v = getSection(sec);
		$('html, body').animate({ scrollTop: v }, 'slow', 'swing');
	}else{
		$('html, body').animate({ scrollTop: 0 }, 'slow', 'swing');
	}
	
	$('aside#sideNav').rulerscroll({
		sections: sections,
		rulerClasses: classes,
		homeFunc: fromSidenavActive
	});
	
	setUpDownHandlers();
	
	function getUrlVars(url){
		var vars = [], hash;
		var hashes = url.slice(url.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	}
	
	function getSection(s) {
		var v;
		if(s == sections.length-1){
			v = $(document).height()-$(window).height();
		}else{
			v = $(sections[s]).offset().top;
		}
		return v;
	}
	
	function setUpDownHandlers(){
		$('div.up').each(function(index) {
			$(this).click(function(){
				$('html, body').animate({ scrollTop: $(sections[index]).offset().top }, 'slow', 'swing');
			});
		});
		
		$('div.down').each(function(index) {
			$(this).click(function(){
				//alert(index + ":" +sections.length);
				if((index+3) == sections.length){
					var m = $(document).height()-$(window).height();
					$('html, body').animate({ scrollTop: m }, 'slow', 'swing');	
				}else{
					$('html, body').animate({ scrollTop: $(sections[index+2]).offset().top }, 'slow', 'swing');	
				}				
			});
		});
	}
});
