//----------------------------------------------------------------------------
//	Copyright (C) 2008 The BIGGEST LLC. All Rights Reserved.
//----------------------------------------------------------------------------

/*
 * This script relies on the YUI 2.5.2 Global, Dom, and Event scripts
 * found here: http://developer.yahoo.com/yui/
 * 
 * @author jON bEEBE
 * @history Created June 06 2008
 * 
 */

// Establish a more convenient reference to the YUI Dom object
var Dom = YAHOO.util.Dom;

/**
 * Initialize the application
 */
function init() {
	// Hide all the topic divs
	showContentDiv(null);
	// Highjack all the navigation buttons
	initButtons();
	// Establish the default state of the page
	establishDefaultState();
}

/**
 * Initialize all the navigation buttons by
 * highjacking their links.
 */
function initButtons() {
	// Find all the navigation anchors
	var $anchors = Dom.getElementsByClassName ( "mainNavLink" , 'a' );
	
	// For each anchor highjack its link
	var max = $anchors.length;
	for (var i = 0; i < max; i++) {
		var $anchor = $anchors[i];
		//log($anchor);
		YAHOO.util.Event.addListener($anchor, "click", onNavbuttonClicked);
		//YAHOO.util.Event.addListener($anchor, "mouseover", onNavbuttonClicked);
	}
}

/**
 * The clicked handler for the main nav buttons
 * 
 * @param {Object} e The event object
 */
function onNavbuttonClicked(e) {
	// Prevent the default action that the link would have performed
	// had javascript been unavailable
	YAHOO.util.Event.preventDefault(e);
	
	// Find the topic div to show by using the value of 
	// the href for this anchor
	var $id = this.getAttribute('href');
	$id = $id.slice($id.indexOf('#')+1);
	
	var $div = Dom.get($id);
	// Show the target div and set the link as active
	showContentDiv($div);
	setActiveLink(this);
}

/**
 * Show the target content div and hide all the rest
 * Pass in a null target to hide all the divs
 * 
 * @param {Element} $target The div to show
 */
function showContentDiv($target) {
	// Find all the topic divs on the page
	var $divs = Dom.getElementsByClassName ( "topic" , 'div' );

	// Hide each topic div found
	var max = $divs.length;
	for (var i = 0; i < max; i++) {
		var $div = $divs[i];
		Dom.setStyle($div, 'display', 'none');
	}
	
	// If the target is defined then show it
	if ($target) {
		Dom.setStyle($target, 'display', 'block');
	}
}

/**
 * Set the active link by styling it
 * 
 * @param {Element} $target The link to set as active
 */
function setActiveLink($target) {
	// Find every main nav link on the page
	var $anchors = Dom.getElementsByClassName ( "mainNavLink" , 'a' );

	// Deactivate each anchor found
	var max = $anchors.length;
	for (var i = 0; i < max; i++) {
		var $anchor = $anchors[i];
		$anchor.className = "mainNavLink";
	}
	
	// If the target is defined then make it the active link
	if ($target) {
		$target.className = "mainNavLink active";
	}
}

/**
 * Put this page in its default state by showing the first
 * topic and setting its anchor to the active link.
 */
function establishDefaultState() {
	showContentDiv(Dom.get('features'));
	setActiveLink(Dom.get('button_features'));
}

/**
 * log a message to the console
 * 
 * @param {String} $message
 */
function log($message) {
	try {
		console.log($message);
	} 
	catch (e) {
		//alert(e);
	}
}

// When the dom becomes ready initialize the application
YAHOO.util.Event.onDOMReady(init);