$(document).ready (function() {

	var scrolldiv = $('.scroll');
	var panels = $('.scroll .panel');
	var container = $('.scrollContainer');
	var subNav = $('.subnav');
	var allNavItems = 0;
	var selectedNavID = 0;
	
	var scrollOptions = {
		target: scrolldiv,
		items: panels,
		navigation: '.subnav li a',
		cycle: false,
		prev:'.last-page',
		next:'.next-page',
		axis:'x',
		onAfter:trigger,
		duration:'fast',
		easing:'swing',
		reset:true,
		hash:true
	};
		
	//console.log($("li.subnav-item").size());
	
	// Cycle subNavItems
	$("li.subnav-item").each(
		// Assign ID
		function(index, element){
			$(element).attr("id", index);
		}
	);

	// Show the buttons in correct locales
	function showButtons() {
		if (selectedNavID == 0) {   
			$('#back').fadeOut('slow');
		} else {
			$('#back').fadeIn('slow');
		}
		
		if (selectedNavID == allNavItems-1) {
			$('#continue').fadeOut('slow');
		} else {
			$('#continue').fadeIn('slow');
		}
	};

	// Run selected actions
	function selectNav() {
		// Change the selected Nav
		subNav.find('li').removeClass('selected');
		$(this).parents('li:first').addClass('selected');
		// Set the variables
		allNavItems = $('li.subnav-item').size();
		selectedNavID = $("ul.subnav li.selected").attr('id');
		// Show the buttons function if exists
		
		// Exclude mobiles
		if ( (navigator.userAgent.match(/Android/i)) ||
			(navigator.userAgent.match(/webOS/i)) ||
			(navigator.userAgent.match(/iPhone/i)) ||
			(navigator.userAgent.match(/iPod/i))
			) {
			// Mobile action
		} else {
			showButtons();
		}

	};
	
	
    // Apply selectNav to any clicked subNav link
	subNav.find('a').click(selectNav);
	

	// Resize all panels to fit content of each
	var heights = {};
	panels.each(function() {
		heights[this.id] = $(this).height() + 40;
	});
	
	
	// Find the nav element that has same ID and run selectNav
	function trigger(data) {
		var el = subNav.find('a[href$="'+data.id+'"]').get(0);
		selectNav.call(el);	
		// Animate the height of the scroller div
		scrolldiv.animate({height: heights[data.id]},'fast', function () {
		
			// Exclude mobiles
			if ( (navigator.userAgent.match(/Android/i)) ||
				(navigator.userAgent.match(/webOS/i)) ||
				(navigator.userAgent.match(/iPhone/i)) ||
				(navigator.userAgent.match(/iPod/i))
				) {
				// Mobile action
			} else {
				//$('body.csp').animate({scrollTop:0},100);
			};
		
		});		
	}
	
	// If hashtag in URL then pass ID
	if (window.location.hash) {
		trigger({ id : window.location.hash.substr(1)});
		scrolldiv.height(heights[id]);
	// If no hash then assume page home
	} else {
		//$('#continue').fadeIn('slow');
		// Runs SelectNav for first option on load
		$('.subnav li a:first').click();
		var id = $('.subnav li a:first').attr('href');
		id = id.substr(1,id.length);
		scrolldiv.height(heights[id]);
	};

	// Previous button action
	$(".last-page").click(function () {
		var previousItemID = ".subnav li#"+(parseFloat(selectedNavID)-1);
		/* alert(previousItemID); */
		$(previousItemID+" a:first").click();
		// Track Analytics
		try {
		var pageTracker = _gat._getTracker("UA-10350860-1");
		//pageTracker._trackPageview();
		var gapathname = window.location.pathname, gasearch = window.location.search, gahash = escape(window.location.hash);
		pageTracker._trackPageview(gapathname + gasearch + gahash);
		} catch(err) {}	
    });	
	
	// Next button action
	$(".next-page").click(function () {
		var nextItemID = ".subnav li#"+(parseFloat(selectedNavID)+1);
		/* alert(nextItemID); */
		$(nextItemID+" a:first").click();
		// Track Analytics
		try {
		var pageTracker = _gat._getTracker("UA-10350860-1");
		//pageTracker._trackPageview();
		var gapathname = window.location.pathname, gasearch = window.location.search, gahash = escape(window.location.hash);
		pageTracker._trackPageview(gapathname + gasearch + gahash);
		} catch(err) {}	
    });

	// Load localScroll
	$.localScroll(scrollOptions);
	

});
