jQuery(function( $ ){
	/**
	 * Demo binding and preparation, no need to read this part
	 */
		//borrowed from jQuery easing plugin
		//http://gsgd.co.uk/sandbox/jquery.easing.php
		$.easing.elasout = function(x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		};
		$('a.back').click(function(){
			$(this).parents('div.pane').scrollTo( 0, 800, { queue:true } );
			$(this).parents('div.section').find('span.message').text( this.title );
			return false;
		});
		//just for the example, to stop the click on the links.
		$('ul.links').click(function(e){
			e.preventDefault();
			var link = e.target;
			link.blur();
			if( link.title )
				$(this).parent().find('span.message').text(link.title);
		});
	
	//by default, the scroll is only done vertically ('y'), change it to both.
	$.scrollTo.defaults.axis = 'y'; 			
	//this one is important, many browsers don't reset scroll on refreshes
	$.scrollTo( 0 );//reset the screen to (0,0)
	
	//TOC, shows how to scroll the whole window
	$('#home_nav a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 800);
		//$(this.hash).find('span.message').text( this.title );
		$('#local_content').attr({scrollTop:0,scrollLeft:0});
		return false;
	});

	$('#home_sub_nav a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 800);
		//$(this.hash).find('span.message').text( this.title );
		$('#local_content').attr({scrollTop:0,scrollLeft:0});
		return false;
	});



	$('#work_nav a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 800);
		//$(this.hash).find('span.message').text( this.title );
		$('#local_content').attr({scrollTop:0,scrollLeft:0});
		return false;
	});

	$('#contact_nav a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 800);
		//$(this.hash).find('span.message').text( this.title );
		$('#local_content').attr({scrollTop:0,scrollLeft:0});
		return false;
	});	

	$('#footer a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 800);
		//$(this.hash).find('span.message').text( this.title );
		$('#local_content').attr({scrollTop:0,scrollLeft:0});
		return false;
	});	
	
	function reset_local()
	{
		$('#local_content').attr({scrollTop:0,scrollLeft:0});
		//alert($('#local_content').attr.scrollTop);
	}
	
	/**
	 * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://flesler.demos.com/jquery/scrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
	 */
	
	/**
	 * Restart the scroll position to ( 0, 0 ) (Firefox doesn't reset it)
	 * could use $(target).scrollTo( 0, {axis:'xy'));
	 * but this needs to be quick(synchronous), to reset before $.localScroll.hash() begins
	*/

	$('#local_content').attr({scrollTop:0,scrollLeft:0});
	
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: '#work', //could be a selector or a jQuery object too.
		duration:1500
	});
	
	var $last = $([]);//save the last link
	 
	/**
	 * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
	$.localScroll({
		target: '#local_content', //could be a selector or a jQuery object too.
		axis:'xy', //the default is 'y'
		queue:true,
		duration:1000,
		hash:false,
		margin:false,
		onBefore:function( e, anchor, $target ){//'this' is the clicked link
			//$last.removeClass('scrolling');
			//$last = $(this).addClass('scrolling');
			//if( this.blur )
			//	this.blur();//remove the awful outline
		},
		onAfter:function( anchor ){
			if($('#local_content').attr('scrollTop') % 540)
			{
				//$('#local_content').attr({scrollTop:0});
			}
			//window.status = $('#local_content').attr('scrollLeft');
			if($('#local_content').attr('scrollLeft') == 1)
			{
				//$('#local_content').attr({scrollLeft:0});
				//window.status = window.status + ' | ' + $('#local_content').attr('scrollLeft');
			}

		}
	});

});

//resets srolling to 0 if close but not quite there
function local_content_x()
{
	//window.status = document.getElementById('section1') ? document.getElementById('section1').offsetTop + ' | ' + $('#local_content').attr('scrollTop') : '';
			if($('#local_content').attr('scrollLeft') == 1)
			{
				$('#local_content').attr({scrollLeft:0});
				//window.status = window.status + ' | ' + $('#local_content').attr('scrollLeft');
			}

setTimeout("local_content_x()",100);
}
local_content_x();

