//  Copyright ©2009 Paul Barrett - www.pbarrett.co.uk
//-----------------------------------------------------------------------------
//  Filename                : pb.slider.js
//-----------------------------------------------------------------------------
//  Project                 : Website Development
//  Version                 : 1.00
//  Author                  : Paul Barrett
//  Date Created            : 06 July 2009
//-----------------------------------------------------------------------------
//  Description:
//  Provides automatic scrolling functionality when hyperlinjs are clicked on 
//  the website.
//
//  Requires the following libraries/plugins:
//    jquery-1.3.2.min.js
//    jquery.easing.1.3.js
//    jquery.scrollTo-1.4.2.min.js
//    jquery.localScroll-1.2.7.min.js
//    jquery.serialScroll-1.2.2.min.js
//  
//  
//  Revisions:
//  06 July 2009 - No revisions yet.
//-----------------------------------------------------------------------------

$(document).ready(function () {

	// If true, panels scroll from top to bottom. 
	// If false, floats all panels left and fix width of the container. Scrolls from left to right.
    var bHorizScroll = true;
	
	// Grab the panel class as a var.
	var $panels = $('#slider .panel');
	
	// Grab the container class as a var.
	var $container = $('#slider .scrollContainer');
	
	// Hides the scroll bar.
	// Also allows page still to be readable on browsers where jscript is turned off.
	var $scroll = $('#slider .scroll').css('overflow', 'hidden');
	
	// Determines scroll settings.
	var aScrollOptions = {
		target: $scroll,
		items: $panels,
		navigation: '.nav a',
		prev: '#main-left',
		next: '#main-right',
		axis: 'xy',
		duration: 1000,
		easing: 'easeOutBounce',
		onAfter: setMenuTab		
	};

    // Float the panels left if going horizontal.
    if (bHorizScroll) {
        $panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
        });

        // calculate a new width for the container (so it holds all panels)
        $container.css('width', $panels[0].offsetWidth * $panels.length);
    }
	
	// Calls function to style the clicked main menu item.
	$('#main-menu .nav a').click(selectNav);	
	
	// Fixes menu styling issues with discrepencies between selecting via menu tab and via hyperlinks.
	if (window.location.hash) {
		setMenuTab({ id: window.location.hash.substr(1) });
	} else {
		$('#main-menu .nav a:first').click();
	}
	
	// Make scroll effect work for the main menu.
	$('#slider').serialScroll(aScrollOptions);
	
	// Make scroll effect work for hyperlinks contained on the page (not just the main menu).
    $.localScroll(aScrollOptions);
	
	
	
	//-----------------------------------------------------------------------------
	//  FUNCTIONS
	//-----------------------------------------------------------------------------
	
	//-----------------------------------------------------------------------------
	// summary:
	//		Removes 'selected' class from all menu list items and then applies 
	//		'selected' class (and therefore associated CSS styling) to the 
	//		currently clicked item.
	//-----------------------------------------------------------------------------
	function selectNav() {
		$(this)
			.parents('ul:first')
				.find('a')
					.removeClass('selected')
				.end()
			.end()
			.addClass('selected');
	}
	
	//-----------------------------------------------------------------------------
	// summary:
	//		Selects the correct main-menu tab when a non-menu hyperlink is clicked.
	// sData: String
	//		Panel div string.
	//-----------------------------------------------------------------------------
	function setMenuTab(sData){
		var el = $('#main-menu .nav').find('a[href$="' + sData.id + '"]').get(0);
		selectNav.call(el);
	};
});