///////////////////////////////////////////////////////////
///	Form Elements Focus Behaviour
///////////////////////////////////////////////////////////

$(document).ready(function() {
	
	// Give Form Elements class of 'idle-field'
	$('input, select, textarea').addClass("idle-field");
	
	// Change class 'idle-field' to 'focus-field' when Form Element gets focus
	$('input[type=text], select, textarea').focus(function() {
		$(this).removeClass("idle-field").addClass("focus-field");
	});
	
	// Reset class to 'idle-field' once Form Element loses focus
	$('input[type=text], select, textarea').blur(function() {
		$(this).removeClass("focus-field").addClass("idle-field");
	});
	
	// Set Search Input default value on focus/blur
	$('#site-search-field').focus(function() {
		if (this.value == this.defaultValue){
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('#site-search-field').blur(function() {
		if ( $.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
});

///////////////////////////////////////////////////////////
///	Give Navigation LIs Class of 'hover' on hover
///////////////////////////////////////////////////////////

$(document).ready(function() {
	$('#site-navigation li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
});


///////////////////////////////////////////////////////////
///	FWK Menu
///////////////////////////////////////////////////////////

function getItem ( tID )
{
	try {
		return document.getElementById( tID );
	}catch(e){
		/*remove*/ try{ console.warn( "Element:" + tID + ", not found" ); }catch(e){ alert( "can't find it" ); };
		return false;
	}
}

function isString ( tObject )
{return ( typeof tObject == "String" || typeof tObject == "string" ) ? true : false;}

function newElement ( tType )
{return document.createElement( tType );}

function getClassNamedItemWithin ( tClassName, tObject )
{
	var theObject;
	
	/// cycle through its children and get the one with the classname
	for ( var tItem = 0; tItem <  tObject.childNodes.length; tItem ++ )
	{
		/// check to see if its the one were looking for
		if ( tObject.childNodes[ tItem ].className == tClassName )	
		{
			/// return a reference to the object
			theObject = tObject.childNodes[ tItem ];
		}
	}
	
	if ( theObject != null )
	{
		return theObject;
	}
	else
	{
		/// obviously it wasn't found, so pass back false
		/*remove*/ try{ console.warn( "Element with the class name " + tClassName + ", not found" ); }catch(e){ alert( "Element with the class name " + tClassName + ", not found" ); };
		return false;
	};
};

var FWK_Menu = function ( tObject )
{
	if ( isString ( tObject ) )
	{
		tObject = getItem ( tObject );
	}
	
	document._zzzIndex = 0;
	
	this.me = tObject;
	this.hasSelected = false;
}

FWK_Menu.prototype.add = function ( tSection )
{
	this.me.appendChild ( tSection.build() );
}

FWK_Menu.prototype.init = function ()
{
	/// cycle through the list items
	/// this part is IE only
	if ( document.isIE )
	{
		var tItems = this.me.getElementsByTagName( "li" );
		for ( var i = 0; i < tItems.length; i ++ )
		{
			/// make sure the one were manipulating is an item
			if ( tItems[i].className == "item" && !tItems[i].attributes.getNamedItem( "emptyMenu" ) )
			{
				/// on mouse over, show the drop down
				tItems[i].onmouseover = function ()
				{
					document._zzzIndex ++;
					 
					this.className    = "open";
					//this.style.zIndex = 9999999 + document._zzzIndex;
				};
				
				/// on mouse out, put it away
				tItems[i].onmouseout = function ()
				{
					this.className = "";
				};
			}
		}
	}
	else
	{
		var tItems = this.me.getElementsByTagName( "li" );
		for ( var i = 0; i < tItems.length; i ++ )
		{
			/// make sure the one were manipulating is an item
			if ( tItems[i].className == "item" && !tItems[i].attributes.getNamedItem( "emptyMenu" ) )
			{
				/// on mouse over, show the drop down
				tItems[i].onmouseover = function ()
				{
					document._zzzIndex ++;
					
					//this.style.zIndex = 9999999 + document._zzzIndex;
				};
			}
		}
	}
	
	
	/// Now go through and get all the sub items
	/// this part is for all browsers, and only required if we want messages to display
	var tItems = this.me.getElementsByTagName( "li" );
	
	for ( var i = 0; i < tItems.length; i ++ )
	{
		/// make sure its a menu item
		if ( String ( tItems[i].className ) == "item" )
		{
			/// cycle through its children
			var tItemsSub = tItems[i].getElementsByTagName( "li" );
			for ( var j = 0; j < tItemsSub.length; j++ )
			{
				/// make sure the object were talking to is a HTML element
				/// this fix is required for IE, it passes through function calls as part of the object
				if ( tItemsSub[j].nodeName && tItemsSub[j].className != "description" )
				{
					tItemsSub[j].messageArea = getClassNamedItemWithin( "description", tItems[i].childNodes[1] );
					
					/// setup the mouse over function
					/// when the user puts their mouse over the sub item, it will change the message to the one set to the nav item
					/// if theres none, nothing will be displayed
					tItemsSub[j].onmouseover = function ()
					{							
						if ( this.attributes.getNamedItem( "message" ) )
						{
							this.messageArea.innerHTML = this.attributes.getNamedItem( "message" ).nodeValue;
						}
						else
						{
							this.messageArea.innerHTML = "";
						};
					};
				}
			}
		}
	}
	
	try
	{
		menuComplete();
	}
	catch(e){};
}

FWK_Menu.prototype.select = function ( tText )
{
	if ( this.hasSelected == false )
	{
		var tItems = this.me.getElementsByTagName( "li" );
		for ( var i = 0; i < tItems.length; i ++ )
		{
			/// make sure the one were manipulating is an item
			if ( tItems[i].rootTitle == tText )
			{
				// select it
				tItems[i].className="selected";
			}
		}
		
		this.hasSelected = true;
	}
}

var FWK_Menu_Section = function ( tName, tURL, tMenuID )
{
	this.data = new Array ();
	this.name = tName;
	this.url  = tURL;
	this.menuID = tMenuID;
}

FWK_Menu_Section.prototype.add = function ( tName, tURL, tDescription,  tHighlighted )
{
	this.data.push ( Array ( tName, tURL, tDescription, tHighlighted ) );
}

FWK_Menu_Section.prototype.build = function (  )
{
	// Create the Drop Down
	tItem = newElement ( "li" );
	tItem.className = "item";
	tItem.id = "siteNavigation-" + this.menuID;
	tItem.rootTitle = this.name;
	
	// Create the main link
	tLink = newElement ( "a" );
	tLink.href = this.url;
	tLink.innerHTML = this.name;
	
	// add the link to the drop down
	tItem.appendChild ( tLink );
	
	// create the menu items
	tSub = newElement ( "ul" );
	for ( var i in this.data )
	{
		if ( this.data[i][0] )
		{
			tEntry = newElement ( "li" );
			
			// is it highlighted?
			if ( this.data[i][3] )
			{
				tEntry.className = "highlighted";
			}
			
			tLink = newElement ( "a" );
			tLink.href = this.data[i][1];
			tLink.innerHTML = this.data[i][0];
			
			tEntry.setAttribute ( "message", this.data[i][2] );
			tEntry.appendChild ( tLink );
			
			tSub.appendChild ( tEntry );
		}
	}
	
	tEntry = newElement ( "li" );
	tEntry.className = "description";
	tSub.appendChild ( tEntry );
	
	tItem.appendChild( tSub );
	
	return tItem;
}