// The global timer that controls whether the active menu is shown
var TIMERID = null;
var TIMEOUT = 250; // timeout in ms

function TopNav()
{
	// This object maps names to arrays of data about the subnavs.
	// The first element in the array is the ID of the div.
	// The second element in the array is the ID of the nav link in the top nav.
	this.subnavs = {
		'estate': ['familyEstateSubNav', 'topNavEstateLink'],
		'wines': ['ourWinesSubNav', 'topNavWinesLink'],
		'organics': ['organicsSubNav', 'topNavOrganicsLink'],
		'people': ['peopleSubNav', 'topNavPeopleLink'],
		'news': ['newsEventsSubNav', 'topNavNewsLink'],
		'tours': ['toursSubNav', 'topNavToursLink'],
		'media': ['mediaTradeSubNav', 'topNavMediaLink']
	};
	
	// The active subnav
	this.sActiveSubNav = '';

	// Called on mouse over of top nav image
	this.MouseOver = function(sName)
	{
		// Stop the timer
		StopTimer();

		// Set the active subnav to the new value
		this.SetSelected(sName);
	}
	
	// Called on mouse out of top nav image
	this.MouseOut = function(sName)
	{
		// Start the timer
		StartTimer();
	}
	
	// Hide/show the appropriate subnav
	this.SetSelected = function(sName)
	{
		// Check if it's different
		if( this.sActiveSubNav != sName )
		{
			// Hide the active sub nav
			var active = null;
			if( this.sActiveSubNav != '' )
			{
				active = document.getElementById(eval('this.subnavs.'+this.sActiveSubNav+'[0]'));
				if( active )
					active.style.display = 'none';
			}
				
			// Show the new one
			if( sName != '' )
			{
				active = document.getElementById(eval('this.subnavs.'+sName+'[0]'));
				if( active )
					active.style.display = 'block';
			}
			
			// Remember the selected subnav
			this.sActiveSubNav = sName;
		}
	}
}

// Global timer methods
function StartTimer()
{
    StopTimer();
    TIMERID = setTimeout("CheckMenuVis()", TIMEOUT);
}

function StopTimer()
{
    if( TIMERID != null )
    {
        clearTimeout(TIMERID);
        TIMERID = null;
    }
}

function CheckMenuVis()
{
    // Clear timer
    StopTimer();

    // Clear menu state
    objTopNav.SetSelected('');
}

function SetActiveMenuItems()
{
	// Get each argument in turn
	for( i = 0; i < arguments.length; i++ )
	{
		// Get link element and change its class
		var linkElem = document.getElementById(arguments[i]);
		if( linkElem )
			linkElem.className = 'selected';
	}
}

// Instantiate the nav object
var objTopNav = new TopNav();

