﻿MenuManager = function()
{
	this._menuId = "";
	this._menuContainer = null;
	this._moveTarget = null;
	this._inAnimation = false;
	this._operation = "";
	this._animationCompleteHandler = null;
}
MenuManager.prototype =
{
	get_menuId: function() { return this._menuId; },
	get_isAnimating: function() { return this._inAnimation; },
	
	// Shows the specified menu
	showMenu: function(menuId)
	{
		var moveTarget = null;
		var targetBounds = null;
		var container = null;
		
		// Save the id of the menu to be displayed
		this._menuId = menuId;
		
		// Get the menu container
		if ((this._menuContainer = $get(menuId)) == null || this._menuContainer.childNodes.length == 0)
			return;
		
		// Get the first child that is not text
		for (var index = 0; index < this._menuContainer.childNodes.length; index ++)
		{
			if ((this._moveTarget = this._menuContainer.childNodes[index]) != null && this._moveTarget.id != undefined)
				break;
		}
		
		// Ensure we have an item to move
		if (this._moveTarget == null || this._moveTarget.id == undefined)
			return;
		
		// Stop the animation
		this._moveTarget.style.display = "block";
		this._menuContainer.style.display = "block";
		return;
		
		// Show the container and the target item but keep the target to be moved out of view
		this._menuContainer.style.display = "block";
		this._moveTarget.style.left = "-1000px";
		this._moveTarget.style.display = "block";
		
		// Get the dimensions of the item to be moved
		targetBounds = Sys.UI.DomElement.getBounds(this._moveTarget);
		
		// Ensure our move target is moved to its starting position
		this._moveTarget.style.left = -targetBounds.width + "px";
		
		// Save the current operation and mark us as animating
		this._operation = "show";
		this._inAnimation = true;
		
		// Create our delegate
		this._animationCompleteHandler = Function.createDelegate(this, this._onAnimationComplete);
		
		// Move the element into view
		var slider = new Crown.Web.Ajax.Controls.Slider();
		slider.add_animationComplete(this._animationCompleteHandler);
		slider.slideComponent(this._moveTarget.id, -targetBounds.width, 0, 5);
	},
	
	// Hides the currently displayed menu
	hideMenu: function()
	{
		// Do nothing if we don't have a currently displayed menu
		if (this._moveTarget == null)
			return;
		
		// Stop the animation
		this._moveTarget.style.display = "none";
		this._menuContainer.style.display = "none";
		this._menuId = "";
		return;
			
		// Get the dimensions of the item to be moved
		targetBounds = Sys.UI.DomElement.getBounds(this._moveTarget);
		
		// Save the current operation and mark us as animating
		this._operation = "hide";
		this._inAnimation = true;
		
		// Create our delegate
		this._animationCompleteHandler = Function.createDelegate(this, this._onAnimationComplete);
		
		// Move the element out of view
		var slider = new Crown.Web.Ajax.Controls.Slider();
		slider.add_animationComplete(this._animationCompleteHandler);
		slider.slideComponent(this._moveTarget.id, 0, -targetBounds.width, 5);
		
		// Clear the menu id as we are now not showing a menu
		this._menuId = "";
	},
	
	// Called when the animation completes
	_onAnimationComplete: function (eventArgs)
	{
		// Delete our delegate
		delete this._animationCompleteHandler;
		
		// If the operation that has completed was a hide then hide the menu
		if (this._operation == "hide")
		{
			this._moveTarget.style.display = "none";
			this._menuContainer.style.display = "none";
		}
		
		// Mark us as not animating any more
		this._inAnimation = false;
	}
}

// Create our menu manager
var _menuManager = new MenuManager();
var _hideTimerHandle = null;

// Shows the requested menu item
function ShowMenu (menuId)
{
	// Cancel any request to hide a menu
	CancelHideMenu();
	
	// If the menu to be displayed is already displayed, do nothing
	if (_menuManager.get_menuId() == menuId)
		return;
	
	// Make sure we are not currently animating
	if (_menuManager.get_isAnimating() == true)
	{
		window.setTimeout("ShowMenu('" + menuId + "')", 100);
		return;
	}
		
	// If a menu is already shown, hide it
	if (_menuManager.get_menuId() != "")
	{
		_menuManager.hideMenu();
		window.setTimeout("ShowMenu('" + menuId + "')", 100);
		return;
	}
	
	// Show the requested menu
	_menuManager.showMenu(menuId);
}

// Requests that the currently displayed menu item is hidden
function HideMenu()
{
	// Set a timer after which time we hide the menu
	_hideTimerHandle = window.setTimeout("HideMenuInternal()", 500);
}

// Cancels the hide menu request
function CancelHideMenu()
{
	// Do nothing if there isn't a hide menu request
	if (_hideTimerHandle == null)
		return;
	
	// Cancel the hide menu request
	window.clearTimeout(_hideTimerHandle);
	_hideTimerHandle = null;
}

// Hides the currently displayed menu item
function HideMenuInternal()
{
	// Clear our hide timer
	_hideTimerHandle = null;
	
	// Make sure we are not currently animating
	if (_menuManager.get_isAnimating() == true)
	{
		window.setTimeout("HideMenuInternal()", 100);
		return;
	}
	
	// Hide the currently shown menu
	_menuManager.hideMenu();
}

// Called when the user moves the mouse off our menu item
function OnMenuUnhover(sender, eventArgs)
{
	var anchor;
	var hash;
	
	if ((hash = location.href.indexOf("#")) != -1)
		anchor = location.href.substring(hash + 1);
	
	if (anchor) 
	{
		anchorElements = anchor.split("/");
	
		if (anchorElements[1]) 
		{
			if ((anchorElements[1] == "our-work") && (sender.get_element().id.indexOf("_OurWork") != -1)) eventArgs.set_cancel(true);
			if ((anchorElements[1] == "our-clients") && (sender.get_element().id.indexOf("_OurClients") != -1)) eventArgs.set_cancel(true);
			if ((anchorElements[1] == "our-expertise") && (sender.get_element().id.indexOf("_OurExpertise") != -1)) eventArgs.set_cancel(true);
			if ((anchorElements[1] == "the-agency") && (sender.get_element().id.indexOf("_TheAgency") != -1)) eventArgs.set_cancel(true);
			if ((anchorElements[1] == "press-room") && (sender.get_element().id.indexOf("_PressRoom") != -1)) eventArgs.set_cancel(true);
		} 
	}
	HideMenu();
}

// Called when the user clicks on our menu
function OnMenuCommand(sender, eventArgs)
{
	if (typeof(RaisePageCommand) != "undefined")
		FlashTell(eventArgs.get_commandArgument());
	else
		window.location = "/Showcase/#" + eventArgs.get_commandArgument();
}