var toolTipManager =
{
    gfxFolder: "http://themes.scor.dk/Themes/Common/Gfx/Tooltip/",

	direction:
	{
		down: 0,
		up: 1
	},

	currentFormToolTip: null,
	
	toolTipsIDMapping: new Hashtable(),
	textEditorIDMapping: new Hashtable(),
	currentTinyMCEToolTip: null,
	
	showTinyMCEToolTip: function(textEditorID)
	{	    
	    if (this.currentTinyMCEToolTip != null)
	    {
	        this.currentTinyMCEToolTip.close();
	        this.currentTinyMCEToolTip = null;
	    }
	
	    var toolTip = this.textEditorIDMapping.get(textEditorID);
	    
        if (toolTip != null)
        {
            this.currentTinyMCEToolTip = toolTip;
            this.focus(null, toolTip.id);
        }
	},
	
	handleTinyMCEEvent: function(event, textEditorID)
	{	    
	    // IE does not fire the focus event type, so here we use mouseup.
	    if (event.type == "focus" || event.type == "mouseup")
	    {
	        var toolTip = this.textEditorIDMapping.get(textEditorID);
	            	    
	        if (toolTip != null)
	        {
	            this.focus(null, toolTip.id); 
	        }
	    }
	},

	add: function(toolTip)
	{    
		// Update tool tip display text.
		if (this.contains(toolTip))
		{
			currentToolTip = this.toolTipsIDMapping.get(toolTip.id);
			currentToolTip.displayText = toolTip.displayText;
		}
		else
		{
			this.toolTipsIDMapping.set(toolTip.id, toolTip);
			
			if (toolTip.isTextEditorToolTip)
			{
			    this.textEditorIDMapping.set(toolTip.textEditorId, toolTip);
			}
		}
	},

	initialize: function()
	{
		this.toolTipsIDMapping.each(function(key, value)
		{
		    value.initialize();
		});
	},

	contains: function(toolTip)
	{
		return (this.toolTipsIDMapping.get(toolTip.id) != null);
	},
	
	getById: function(id)
	{
	    return this.toolTipsIDMapping.get(id);
	},

	remove: function(id)
	{
		var toolTip = this.toolTipsIDMapping.get(id);

		if (toolTip != null)
		{
			toolTip.hidedisplayElement();
			toolTip.hide();
			this.toolTipsIDMapping.remove(id);
		}
	},

	displayFormValidation: function()
	{
		if (this.currentFormToolTip != null)
		{
			this.currentFormToolTip.hide();
		}

		var firstToolTip = null;
		this.currentFormToolTip = null;

        this.toolTipsIDMapping.each(function(key, value)
        {
			if (value instanceof GenericFormToolTip)
			{
				if (firstToolTip == null)
				{
					value.show();
					this.currentFormToolTip = value;
					firstToolTip = value;
				}
				
				value.showDisplayElement();
			}
			else if (value instanceof ToolTip)
			{
				value.show();
			}
		}.bind(this));
	},

	focus: function(event, toolTipId)
	{
		if (this.currentFormToolTip != null)
		{
			this.currentFormToolTip.hide();
		}

		var toolTip = this.toolTipsIDMapping.get(toolTipId);

		if (toolTip != null)
		{
			this.currentFormToolTip = toolTip;
			toolTip.show();
		}
	},

	closeClick: function(event)
	{
		if (this.currentFormToolTip != null)
		{
			this.currentFormToolTip.close();
			this.currentFormToolTip = null;
		}
	}
};

function ToolTip(id, title, displayText, displayElement)
{
	this.id = id;
    this.title = title;
	this.displayText = displayText;
	this.toolTipHtml = null;

	this.showing = false;
	this.closed = false;

    this.inactivateID = null;
    this.inactivateText = null;
	
	this.autoClose = false;
	this.autoCloseTimeoutInSeconds = 4;
	this.autoCloseTimer = null;

    this.initialized = false;
	this.displayElement = displayElement;
}

ToolTip.prototype.initialize = function()
{
    if (!this.initialized)
    {
        this.initializeHtml();
        this.initializeEvents();
        this.initializeAutoClose();

        this.initialized = true;
    }
}

ToolTip.prototype.initializeHtml = function()
{
    var mainTableID = "MainTable" + this.id;
    var closeAnchorID = "CloseAnchor" + this.id;
    var textDivID = "TextDiv" + this.id;
    var inactivateAnchorID = "InactivateAnchor" + this.id;

    var sb = new StringBuilder();
    sb.append("<table class=\"ToolTip\" cellpadding=\"0\" cellspacing=\"0\" id=\"{0}\">".format(mainTableID));
    sb.append("    <tr>");
    sb.append("        <td></td>");
    sb.append("        <td><span class=\"ToolTipTopLeft\"><img alt=\"\" src=\"{0}TopLeft.png\" /></span></td>".format(toolTipManager.gfxFolder));
    sb.append("        <td class=\"TopBorder\"></td>");
    sb.append("        <td><span class=\"ToolTipTopRight\"><img alt=\"\" src=\"{0}TopRight.png\" /></span></td>".format(toolTipManager.gfxFolder));
    sb.append("        <td></td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td><span class=\"ToolTipArrow\"><img src=\"{0}Arrow.png\" class=\"Arrow\" /></span></td>".format(toolTipManager.gfxFolder));
    sb.append("        <td class=\"LeftBorder\"><img alt=\"\" src=\"{0}LeftBorder.png\" /></td>".format(toolTipManager.gfxFolder));
    sb.append("        <td class=\"Content\">");
    sb.append("            <table class=\"Header\">");
    sb.append("                <tr>");
    sb.append("                    <th>");
    sb.append("                        {0}".format(this.title));
    sb.append("                    </th>");
    sb.append("                    <td>");
    sb.append("                        <a id=\"{0}\" href=\"javascript: void(null);\"><img alt=\"\" src=\"{1}CloseButton.png\" /></a>".format(closeAnchorID, toolTipManager.gfxFolder));
    sb.append("                    </td>");
    sb.append("                </tr>");
    sb.append("            </table>");
    sb.append("            <div id=\"{0}\" class=\"Text\">".format(textDivID));
    sb.append("                {0}".format(this.displayText));
    sb.append("            </div>");
    sb.append("        </td>");
    sb.append("        <td class=\"RightBorder\"><img alt=\"\" src=\"{0}RightBorder.png\" /></td>".format(toolTipManager.gfxFolder));
    sb.append("        <td></td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td></td>");
    sb.append("        <td class=\"LeftBorder\"></td>");
    sb.append("        <td class=\"ContentBottom\">");

    if (this.inactivateID != null && this.inactivateText != null)
    {
        sb.append("            <div class=\"BottomText\">");
        sb.append("                <a id=\"{0}\" href=\"javascript: void(null);\">{1}</a>".format(inactivateAnchorID, this.inactivateText));
        sb.append("            </div>");
    }

    sb.append("        </td>");
    sb.append("        <td class=\"RightBorder\"></td>");
    sb.append("        <td></td>");
    sb.append("    </tr>");
    sb.append("    <tr>");
    sb.append("        <td></td>");
    sb.append("        <td><span class=\"ToolTipBottomLeft\"><img alt=\"\" src=\"{0}BottomLeft.png\" /></span></td>".format(toolTipManager.gfxFolder));
    sb.append("        <td class=\"BottomBorder\"></td>");
    sb.append("        <td><span class=\"ToolTipBottomRight\"><img alt=\"\" src=\"{0}BottomRight.png\" /></span></td>".format(toolTipManager.gfxFolder));
    sb.append("        <td></td>");
    sb.append("    </tr>");
    sb.append("</table>");

    var div = document.createElement("div");
    div.innerHTML = sb.getValue();
    document.body.appendChild(div);

    this.toolTipHtml = new ToolTipHtml(mainTableID, textDivID, closeAnchorID, inactivateAnchorID);
}

ToolTip.prototype.initializeAutoClose = function()
{
    if (this.autoClose && this.autoCloseTimeoutInSeconds > 1)
    {
        this.autoCloseTimer = window.setTimeout(this.autoCloseTimerEnded.bindAsEventListener(this), this.autoCloseTimeoutInSeconds * 1000);
    }
}

ToolTip.prototype.autoCloseTimerEnded = function()
{
    this.autoCloseTimer = null;
    this.hide();
}

ToolTip.prototype.clearAutoCloseTimer = function()
{
    window.clearTimeout(this.autoCloseTimer);
}

ToolTip.prototype.initializeEvents = function()
{
    this.initializeBaseEvents();
}

ToolTip.prototype.initializeBaseEvents = function()
{
    var closeMethod = this.close.bindAsEventListener(this);
    
    if (this.toolTipHtml.close != null)
    {
	    S.Event.add(this.toolTipHtml.close, "click", closeMethod);
	}
	
	S.Event.add(this.toolTipHtml.table, "click", closeMethod);

	if (this.toolTipHtml.inactivationAnchor != null && CurrentProfile != null)
	{
	    S.Event.add(this.toolTipHtml.inactivationAnchor, "click", this.inactivate.bindAsEventListener(this), false);
	}
}

ToolTip.prototype.inactivate = function(event)
{
    try
    {
        // Do not listen to success or failure.
        var serverRequest = new ServerRequest("GET");
        serverRequest.send("/Global/Tooltip/Inactivate.aspx", "ProfileID={0}&TooltipID={1}".format(CurrentProfile.ID, this.inactivateID));
    }
    catch(ex) { }
    
    this.close();
}

ToolTip.prototype.show = function()
{
    this.initialize();

	if (this.toolTipHtml != null && !this.closed)
	{
		this.toolTipHtml.displayTextContainer.innerHTML = this.displayText;
		
		if (this.setToolTopPosition())
		{
		    this.toolTipHtml.table.style.visibility = "visible";
		    this.showing = true;
		}
	}
}

ToolTip.prototype.setToolTopPosition = function()
{
    var position = S.Element.getPosition(this.displayElement);
            
    if (position.x == 0 && position.y == 0)
        return false;
        
    this.toolTipHtml.table.style.left = (position.x + this.displayElement.offsetWidth + 5) + "px";
	this.toolTipHtml.table.style.top = (position.y - 21) + "px";
	
	return true;
}

ToolTip.prototype.close = function()
{
    this.clearAutoCloseTimer();
    this.closed = true;
	this.hide();
}

ToolTip.prototype.hide = function()
{
	if (this.toolTipHtml != null)
	{
	    this.showing = false;
	    
		this.toolTipHtml.table.style.visibility = "hidden";
	}
}

ToolTip.prototype.onResize = function()
{
	if (this.showing)
	{
		this.setToolTopPosition();
	}
}

function GenericFormToolTip(id, title, displayText, container, displayElement)
{
	this.base = ToolTip;
	this.base(id, title, displayText, displayElement)

    this.isTextEditorToolTip = false;
    this.textEditorId = null;
    
	this.container = container;
}

GenericFormToolTip.prototype = new ToolTip;

// Override the inherited initializeEvents method.
GenericFormToolTip.prototype.initializeEvents = function()
{
    this.initializeBaseEvents();
	
	var focusFunction = new Function("event", "toolTipManager.focus(event, '" + this.id + "');");
	S.Event.add(this.displayElement, "click", focusFunction);
	
    if (!this.isTextEditorToolTip)
    {
        var resizeFunction = this.onResize.bindAsEventListener(this);

        // Bind to <input>.
	    var inputs = this.container.getElementsByTagName("input");

	    for (var i = 0; i < inputs.length; i++)
	    {
	        S.Event.add(inputs[i], "click", focusFunction);
		    S.Event.add(inputs[i], "focus", focusFunction);
		    S.Event.add(window, "resize", resizeFunction);
	    }
    	
	    // Bind to <select>.
	    var selects = this.container.getElementsByTagName("select");

	    for (var i = 0; i < selects.length; i++)
	    {
	        S.Event.add(selects[i], "click", focusFunction);
		    S.Event.add(selects[i], "focus", focusFunction);
		    S.Event.add(window, "resize", resizeFunction);
	    }
    	
	    // Bind to <textarea>.
	    var textAreas = this.container.getElementsByTagName("textarea");

	    for (var i = 0; i < textAreas.length; i++)
	    {
	        S.Event.add(textAreas[i], "click", focusFunction);
		    S.Event.add(textAreas[i], "focus", focusFunction);
		    S.Event.add(window, "resize", resizeFunction);
	    }
    }
}

GenericFormToolTip.prototype.showDisplayElement = function()
{
	if (this.displayElement != null)
	{
		this.displayElement.style.visibility = "visible";
	}
}

GenericFormToolTip.prototype.hideDisplayElement = function()
{
	if (this.displayElement != null)
	{
		this.displayElement.style.visibility = "hidden";
	}
}

function ToolTipHtml(mainTableID, textDivID, closeAnchorID, inactivateAnchorID)
{
	this.table = S.Get(mainTableID);
	this.displayTextContainer = S.Get(textDivID);
	this.close = S.Get(closeAnchorID);
	this.inactivationAnchor = S.Get(inactivateAnchorID);
}