// ==============================
// SET VARIABLES
// ==============================


// Extract the URL minus the domain name.
// Find the index of the first '/' AFTER the 'http://' and extract everything that follows
var url = document.URL.substr(document.URL.indexOf('/',7));

// Handle any '#' or '?' in the url. They have to be removed for the on-state detection to work.
if (url.indexOf('#') != -1) {
	var urlSplit = url.split('#');
	url = urlSplit[0];
} else if (url.indexOf('?') != -1) {
	var urlSplit = url.split('?');
	url = urlSplit[0];
}

// Extract the first directory of the URL
// This is used to activate the main button on states
// Extract everthing up to but not including the 2nd '/'
var urlMainDir = url.substr(0, url.indexOf('/',1));

// This is used in the news page when arriving from the news spotlight box
var args = getArgs(); // From function at bottom
var newsOpenOnLoad = args.expand;

// ==============================
// RUN ALL ONLOAD JQUERY COMMANDS
// ==============================
$(document).ready(function() {
						   
	// ========================================================
	// SET THE ON CLASS TO THE APPROPRIATE MENU BUTTONs
	// ========================================================	
	// Set the on state for the left margion buttons
	$("ul#sidesubnav li a[href='" + url + "']").parent().addClass('on');
	
	// Set the on state for the main menu buttons
	// Match any href that begins with the loaded main directory
	// We test first for an empty varible to prevent on-states on the home page
	if (urlMainDir != '') {
		$("ul#nav li a[href^='" + urlMainDir + "']").parent().parent().parent().addClass('on');
	}
	
	
	
	// ========================================================
	// THE NEWS PAGE FUNCTIONALITY
	// ========================================================	
	// Turn off all the news items on load
	$('div.news .newsItem').hide();
	// Turn off the collapse all button on load
	$('p#newsControls span#collapse').addClass('grayedOut');
	
	// If we've arrived from the new highlights box we want to open that detail
	// And scroll the window to it. *Using the jquery.scrollTo plugin, the only page on the site to do so.
	if (newsOpenOnLoad) {
		$('div.news .newsItem:eq(' + (newsOpenOnLoad - 1) + ')').show();
		$.scrollTo('div.news h2:eq(' + (newsOpenOnLoad - 1) + ')');
		
		// Ungray the control buttons
		$('p#newsControls span#expand').removeClass('grayedOut');
		$('p#newsControls span#collapse').removeClass('grayedOut');
	}
	
	// The news headlines
	// When a headline is clicked, do a bunch of stuff
	$('div.news h2.newsTitle').click(function() {
		// Toggle the selected news item
		$(this).parent().each(function(){
			$(this).find('.newsItem').toggle();
			$(this).find('.newsTeaser').toggle();
			$(this).find('.readmore').toggle();
		});
		
		// Ungray the control buttons
		$('p#newsControls span#expand').removeClass('grayedOut');
		$('p#newsControls span#collapse').removeClass('grayedOut');
		
		// NOW TURN THE CONTROL BUTTONS ON OR OFF DEPENDING ON WHETHER EVERYTHING'S OPEN OR CLOSED.
		// Fill some variables with booleans
		var someClosed = $("div.news .newsItem").is(".newsItem:hidden");
		var someOpen = $("div.news .newsItem").is(".newsItem:visible");
		
		if (!someClosed) {
			$('p#newsControls span#expand').addClass('grayedOut');
			$('p#newsControls span#collapse').removeClass('grayedOut');
		}
		
		// if all items are closed, turn off the collapse all button
		if (!someOpen) {
			$('p#newsControls span#expand').removeClass('grayedOut');
			$('p#newsControls span#collapse').addClass('grayedOut');
		}
	});
	
	$('div.news div.readmore').click(function() {
		// Toggle the selected news item
		$(this).parent().each(function(){
			$(this).find('.newsItem').toggle();
			$(this).find('.newsTeaser').toggle();
			$(this).find('.readmore').toggle();
		});
		
		// Ungray the control buttons
		$('p#newsControls span#expand').removeClass('grayedOut');
		$('p#newsControls span#collapse').removeClass('grayedOut');
		
		// NOW TURN THE CONTROL BUTTONS ON OR OFF DEPENDING ON WHETHER EVERYTHING'S OPEN OR CLOSED.
		// Fill some variables with booleans
		var someClosed = $("div.news .newsItem").is(".newsItem:hidden");
		var someOpen = $("div.news .newsItem").is(".newsItem:visible");
		
		if (!someClosed) {
			$('p#newsControls span#expand').addClass('grayedOut');
			$('p#newsControls span#collapse').removeClass('grayedOut');
		}
		
		// if all items are closed, turn off the collapse all button
		if (!someOpen) {
			$('p#newsControls span#expand').removeClass('grayedOut');
			$('p#newsControls span#collapse').addClass('grayedOut');
		}
	});
	
	// When clicking the news controls
	$('p#newsControls span#expand').click(function() {
		$('div.news .newsItem').show();	
		$(this).addClass('grayedOut');
		$('p#newsControls span#collapse').removeClass('grayedOut');
		
		$('div.news .newsTeaser').hide();
		$('div.news .readmore').hide();
	});
	
	$('p#newsControls span#collapse').click(function() {
		$('div.news .newsItem').hide();	
		$(this).addClass('grayedOut');
		$('p#newsControls span#expand').removeClass('grayedOut');
		
		$('div.news .newsTeaser').show();
		$('div.news .readmore').show();
		$('div.news .close').hide();
	});
});



/*
 * This function parses comma-separated name=value 
 * argument pairs from the query string of the URL. 
 * It stores the name=value pairs in 
 * properties of an object and returns that object.
 */
function getArgs() {
    var args = new Object(  );
    var query = location.search.substring(1);     
      // Get query string
    var pairs = query.split("&");
     // Break at comma
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
          // Look for "name=value"
        if (pos == -1) continue;
          // If not found, skip
        var argname = pairs[i].substring(0,pos);
          // Extract the name
        var value = pairs[i].substring(pos+1);
          // Extract the value
        args[argname] = unescape(value);
         // Store as a property
       // In JavaScript 1.5, use decodeURIComponent(  ) 
       // instead of escape(  )
    }
    return args;     // Return the object
}

