// JavaScript Document

jQuery(function( $ ){
	/**
	 * 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.
	 */
	var current_ISD_Section = 1;
	// The default axis is 'y', but in this demo, I want to scroll both
	// You can modify any default like this
	$.localScroll.defaults.axis = 'x';
	
	// Function to update the arrows -FP
	$.updateWidget = function(){
		// Updating the arrows
		var left_btn = $("#ISD .goLeft a")
		if(current_ISD_Section>1){
			left_btn.attr("href", "#section"+(current_ISD_Section-1));
		}else{
			left_btn.attr("href", "#section1");
		}
		
		var right_btn = $("#ISD .goRight a")
		if(current_ISD_Section<4){
			right_btn.attr("href", "#section"+(current_ISD_Section+1));
		}else{
			right_btn.attr("href", "#section4");
		}
	}
	
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: '#ISD_widget', // Could be a selector or a jQuery object too.
		queue:true,
		duration:1500,
		onBefore:function( e, anchor, $target ){
			updateTabs(anchor)
			$.updateWidget(anchor)
			// The 'this' is the settings object, can be modified
		}
	});
	
	/**
	 * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
	$('#ISD').localScroll({
		target: '#ISD_widget', // could be a selector or a jQuery object too.
		queue:true,
		duration:1000,
		hash:true,
		onBefore:function( e, anchor, $target ){
			updateTabs(anchor)
			// The 'this' is the settings object, can be modified
		},
		onAfter:function( anchor, settings ){
			$.updateWidget(anchor)
			// The 'this' contains the scrolled element (#content)
		}
	});
	
	function getAnchorNo(anchor){
		return (anchor.id.substring(anchor.id.length-1)*1)
	}
	
	function updateTabs(anchor){
		var old_ISD_Section = current_ISD_Section;
		current_ISD_Section = getAnchorNo(anchor);
		
		$("#ISD .tabs .tab"+old_ISD_Section).removeClass("selected");
		$("#ISD .tabs .tab"+current_ISD_Section).addClass("selected");	
	}
});
