/*
TimothyHumphrey.WebControls
Copyright (c) 2003 - 2004 Timothy Humphrey

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

//-------------------- thwcHighlightCI_Hover --------------------\\
thwcHighlightCI_Hover = function() {
	this.base = Initiator;
	this.base();
	delete this.base;
}
thwcHighlightCI_Hover.prototype = new Initiator;
thwcHighlightCI_Hover.prototype.constructor = thwcHighlightCI_Hover;

thwcHighlightCI_Hover.prototype.Load = function() {
	if(this.Control.constructor == Item) {
		Delegate.Add(this.Control.Element, "mouseover", this, this.MouseOver);
		Delegate.Add(this.Control.Element, "mouseout", this, this.MouseOut);
		this.Installed = true;
	}
}

thwcHighlightCI_Hover.prototype.MouseOut = function(e) {
	this.MainMenuEvent.Fire(this, false);
}

thwcHighlightCI_Hover.prototype.MouseOver = function(e) {
	this.MainMenuEvent.Fire(this, true);
}

thwcHighlightCI_Hover.prototype.Unload = function() {
	if(this.Installed) {
		Delegate.Remove(this.Control.Element, "mouseover", this, this.MouseOver);
		Delegate.Remove(this.Control.Element, "mouseout", this, this.MouseOut);
		this.Installed = false;
	}
}

//-------------------- thwcHotCH_Display --------------------\\
thwcHotCH_Display = function() {
	this.base = Handler;
	this.base();
	delete this.base;
}
thwcHotCH_Display.prototype = new Handler;
thwcHotCH_Display.prototype.constructor = thwcHotCH_Display;

thwcHotCH_Display.prototype.Play = function(initiator, hot) {
	var item = this.Control;

	if(hot) {
		switch(item.Display) {
		case Display.None:
			break;
		case Display.Text:
			window.status = item.DisplayText;
			break;
		case Display.Url:
			window.status = item.ActionUrl;
			break;
		}
	}
	else {
		if(item.Display != Display.None)
			window.status = window.defaultStatus ? window.defaultStatus : "";
	}
}

//-------------------- thwcHotCI_Hover --------------------\\
thwcHotCI_Hover = function() {
	this.base = Initiator;
	this.base();
	delete this.base;
}
thwcHotCI_Hover.prototype = new Initiator;
thwcHotCI_Hover.prototype.constructor = thwcHotCI_Hover;

thwcHotCI_Hover.prototype.Load = function() {
	Delegate.Add(this.Control.Element, "mouseover", this, this.MouseOver);
	Delegate.Add(this.Control.Element, "mouseout", this, this.MouseOut);
	this.Installed = true;
}

thwcHotCI_Hover.prototype.MouseOut = function(e) {
	this.MainMenuEvent.Fire(this, false);
}

thwcHotCI_Hover.prototype.MouseOver = function(e) {
	if(this.Control.constructor == Menu) {
		if(this.Control.Visible)
			this.MainMenuEvent.Fire(this, true);
	}
	else
		this.MainMenuEvent.Fire(this, true);
}

thwcHotCI_Hover.prototype.Unload = function() {
	if(this.Installed) {
		Delegate.Remove(this.Control.Element, "mouseover", this, this.MouseOver);
		Delegate.Remove(this.Control.Element, "mouseout", this, this.MouseOut);
		this.Installed = false;
	}
}

//-------------------- thwcSelectedH_Action --------------------\\
thwcSelectedH_Action = function() {
	this.base = Handler;
	this.base();
	delete this.base;
}
thwcSelectedH_Action.prototype = new Handler;
thwcSelectedH_Action.prototype.constructor = thwcSelectedH_Action;

thwcSelectedH_Action.prototype.Play = function(initiator, eventData) {
	var item = this.Control;
	var targetWindow;

	if(item.Action != Action.None)
		MainMenu.HideAll();

	switch(item.Action) {
	case Action.None:
		return;
	case Action.Link:
		if(item.ActionUrl != "") {
			targetWindow = item.ActionTarget == "" ? window : window.top.frames[item.ActionTarget];
			if(targetWindow)
				targetWindow.location.href = item.ActionUrl;
		}
		break;
	case Action.Postback:
		if(__doPostBack)
			__doPostBack(item.MainMenu.UniqueID, Control.Type.Item + MainMenu.PostBackDelimiter + item.ID);
		break;
	}
}

//-------------------- thwcSelectedI_Click --------------------\\
thwcSelectedI_Click = function() {
	this.base = Initiator;
	this.base();
	delete this.base;
}
thwcSelectedI_Click.prototype = new Initiator;
thwcSelectedI_Click.prototype.constructor = thwcSelectedI_Click;

thwcSelectedI_Click.prototype.Load = function() {
	if(this.Control.constructor == Item) {
		Delegate.Add(this.Control.Element, "click", this, this.Click);
		this.Installed = true;
	}
}

thwcSelectedI_Click.prototype.Click = function(e) {
	this.MainMenuEvent.Fire(this, null);
}

thwcSelectedI_Click.prototype.Unload = function() {
	if(this.Installed) {
		Delegate.Remove(this.Control.Element, "click", this, this.Click);
		this.Installed = false;
	}
}

//-------------------- thwcVisibleCH_Visible --------------------\\
thwcVisibleCH_Visible = function() {
	this.base = Handler;
	this.base();
	delete this.base;
}
thwcVisibleCH_Visible.prototype = new Handler;
thwcVisibleCH_Visible.prototype.constructor = thwcVisibleCH_Visible;

thwcVisibleCH_Visible.prototype.Play = function(initiator, visible) {
	var element = this.Control.Element;

	if(visible) {
		Browser.Move(element, this.Control.GetAlignmentPoint());
		element.style.visibility = "";
	}
	else
		element.style.visibility = "hidden";
}

//-------------------- thwcVisibleCI_Context --------------------\\
thwcVisibleCI_Context = function() {
	this.base = Initiator;
	this.base();
	delete this.base;

	this.ContextElement = null;
}
thwcVisibleCI_Context.prototype = new Initiator;
thwcVisibleCI_Context.prototype.constructor = thwcVisibleCI_Context;

thwcVisibleCI_Context.prototype.Click = function(e) {
	if(this.Control.Visible && !this.Control.Hot)
		this.MainMenuEvent.Fire(this, false);
}

thwcVisibleCI_Context.prototype.ContextMenu = function(e) {
	MainMenu.HideAll();
	this.MainMenuEvent.Fire(this, true);
	return false;
}

thwcVisibleCI_Context.prototype.Load = function() {
	var element;

	if(this.Control.constructor == Menu) {
		element = this.Control.Data.thwcVisibleCI_Context[0]
			? Browser.GetElement(this.Control.Data.thwcVisibleCI_Context[0])
			: document;

		if(element && !element.thwcVisibleCI_Context) {
			Delegate.Add(document, "click", this, this.Click);
			Delegate.Add(element, "contextmenu", this, this.ContextMenu);
			element.thwcVisibleCI_Context = true;
			this.ContextElement = element;
			this.Installed = true;
		}
	}
}

thwcVisibleCI_Context.prototype.Unload = function() {
	if(this.Installed) {
		Delegate.Remove(document, "click", this, this.Click);
		Delegate.Remove(this.ContextElement, "contextmenu", this, this.ContextMenu);
		delete this.ContextElement.thwcVisibleCI_Context;
		this.Installed = false;
	}
}

//-------------------- thwcVisibleCI_ParentItemClick --------------------\\
thwcVisibleCI_ParentItemClick = function() {
	this.base = Initiator;
	this.base();
	delete this.base;
}
thwcVisibleCI_ParentItemClick.prototype = new Initiator;
thwcVisibleCI_ParentItemClick.prototype.constructor = thwcVisibleCI_ParentItemClick;

thwcVisibleCI_ParentItemClick.prototype.DocumentClick = function(e) {
	var menu = this.Control;

	if(menu.Visible && !(menu.Parent.Hot || menu.IsDescendentOrSelfHot()))
		this.Initiate(false);
}

thwcVisibleCI_ParentItemClick.prototype.Initiate = function(visible) {
	this.MainMenuEvent.Fire(this, visible);
}

thwcVisibleCI_ParentItemClick.prototype.Load = function() {
	if(this.Control.constructor == Menu && this.Control.Parent) {
		Delegate.Add(this.Control.Parent.Element, "click", this, this.ItemClick);
		Delegate.Add(document, "click", this, this.DocumentClick);
		this.Installed = true;
	}
}

thwcVisibleCI_ParentItemClick.prototype.ItemClick = function(e) {
	this.MainMenuEvent.Fire(this, true);
}

thwcVisibleCI_ParentItemClick.prototype.Unload = function() {
	if(this.Installed) {
		Delegate.Remove(this.Control.Parent.Element, "click", this, this.ItemClick);
		Delegate.Remove(document, "click", this, this.DocumentClick);
		this.Installed = false;
	}
}

//-------------------- thwcVisibleCI_ParentItemHover --------------------\\
thwcVisibleCI_ParentItemHover = function() {
	this.base = Initiator;
	this.base();
	delete this.base;

	this.TimeoutID = 0;
}
thwcVisibleCI_ParentItemHover.prototype = new Initiator;
thwcVisibleCI_ParentItemHover.prototype.constructor = thwcVisibleCI_ParentItemHover;

thwcVisibleCI_ParentItemHover.prototype.Initiate = function(controlType, visible) {
	this.TimeoutID = 0;
	if(visible)
		this.MainMenuEvent.Fire(this, true);
	else {
		if(controlType == Control.Type.Menu)
			this.Control.HideTopInactiveAncestorOrSelf();
		else
			this.MainMenuEvent.Fire(this, false);
	}
}

thwcVisibleCI_ParentItemHover.prototype.Load = function() {
	var menu = this.Control;

	if(menu.constructor == Menu && menu.Parent) {
		Delegate.Add(menu.Element, "mouseover", this, this.MenuMouseOver);
		Delegate.Add(menu.Element, "mouseout", this, this.MenuMouseOut);
		Delegate.Add(menu.Parent.Element, "mouseover", this, this.ItemMouseOver);
		Delegate.Add(menu.Parent.Element, "mouseout", this, this.ItemMouseOut);
		this.Installed = true;
	}
}

thwcVisibleCI_ParentItemHover.prototype.ItemMouseOut = function(e) {
	this.Notify();
	if(this.Control.Visible) {
		if(this.Control.HideDelay > 0)
			this.TimeoutID = window.setTimeout(this.GetTimeoutString("VisibleChanged", "Initiate", Control.Type.Item, false), this.Control.HideDelay);
		else
			this.Initiate(Control.Type.Item, false);
	}
}

thwcVisibleCI_ParentItemHover.prototype.ItemMouseOver = function(e) {
	this.Notify();
	if(!this.Control.Visible) {
		if(this.Control.ShowDelay > 0)
			this.TimeoutID = window.setTimeout(this.GetTimeoutString("VisibleChanged", "Initiate", Control.Type.Item, true), this.Control.ShowDelay);
		else
			this.Initiate(Control.Type.Item, true);
	}
}

thwcVisibleCI_ParentItemHover.prototype.MenuMouseOut = function(e) {
	this.Notify();
	if(this.Control.HideDelay > 0)
		this.TimeoutID = window.setTimeout(this.GetTimeoutString("VisibleChanged", "Initiate", Control.Type.Menu, false), this.Control.HideDelay);
	else
		this.Initiate(Control.Type.Menu, false);
}

thwcVisibleCI_ParentItemHover.prototype.MenuMouseOver = function(e) {
	this.Control.NotifyAncestorsAndSelf();
}

thwcVisibleCI_ParentItemHover.prototype.Notify = function() {
	if(this.TimeoutID != 0) {
		window.clearTimeout(this.TimeoutID);
		this.TimeoutID = 0;
	}
}

thwcVisibleCI_ParentItemHover.prototype.Unload = function() {
	var menu = this.Control;

	if(this.Installed) {
		Delegate.Remove(menu.Element, "mouseover", this, this.MenuMouseOver);
		Delegate.Remove(menu.Element, "mouseout", this, this.MenuMouseOut);
		Delegate.Remove(menu.Parent.Element, "mouseover", this, this.ItemMouseOver);
		Delegate.Remove(menu.Parent.Element, "mouseout", this, this.ItemMouseOut);
		this.Installed = false;
	}
}

