﻿$(window).load(InitHelp);

var _currentHelpId = "";
var _helpTimeout = null;

/*
 * Registers the help popups with the controls that request them
 */
function InitHelp() {
    var isHelpShown = true; //$("__help").value == "True";
    var elements = $("a[rel=help]");
    
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        
        if (isHelpShown) {
            var helpId = GetHelpTextId(element);
            if (helpId != null) {
                
                $("a[name=" + helpId + "]").mouseover(function(event) { ShowHelpText(this, false); });
                $("a[name=" + helpId + "]").mouseout(function() { HideHelpText(); });
                $("a[name=" + helpId + "]").click(function(event) { ShowHelpText(this, true); });
            }
            
        } else {
            element.style["display"] = "none";
        }
    }
}

function GetHelpTextId(element) {
    var rel = element != null ? element.attributes["rel"] : null;
    return !rel || rel == null || rel.value != "help" ? null : element.name;
}

function ShowHelpText(element, isSticky) {
    // Don't show help if help is already showing for a previous click
    if (_helpTimeout != null) return;
    
    if (_currentHelpId != "") {
        HideHelpText();
    }
    
    // Find out where to move the element to if we're not already showing the help for this element.
    if (element != null && _currentHelpId != element.name) {
        var popup = $("#divHelp");    
        var helpId = element.name;
        _currentHelpId = helpId;

        // Show the popup and the help message. We MUST show the help before we reposition it otherwise
        // the getHeight() method below fails (the height cannot be calculated unless the element is visible).
        popup.css({display: "block"});
        $("span[id=" + helpId+ "]").css({display: "inline"});

        var offset = $(element).offset();
        var leftOffset = offset.left - (popup.width() / 4);
        var topOffset = (offset.top - popup.height() - 25);
        
        if(topOffset < 0)
        {
            topOffset = offset.top + popup.height() + 10;
        }
        
        popup.css({left: leftOffset + "px"});
        popup.css({top: topOffset + "px"});
        
        if (isSticky) {
            _helpTimeout = setTimeout(function() {
                _helpTimeout = null;
                HideHelpText();
            }, 3000);
        }
    }
}

function HideHelpText() {
    // Don't hide help if we are showing it in response to a click, unless the timer has expired
    if (_helpTimeout != null) return;
    
    if(_currentHelpId != "")
    {
        $("span[id=" + _currentHelpId + "]").css({display: "none"});
        $("#divHelp").css({display:"none"});
    }
    
    _currentHelpId = "";
/*    
    var element = GetHelpElement(Event.element(event));
    
    if (element != null) {
        var helpId = GetHelpTextId(element);
        $(helpId).style["display"] = "none";    
        $("divHelp").style["display"] = "none";
    }
*/
}

/*
 * Find the element to display the help on
 */
function GetHelpElement(element) {
    if (GetHelpTextId(element) != null) {
        return element;
    }
    
    var parentNode = element.parentNode;
    return parentNode != null ? GetHelpElement(parentNode) : null;
}

function popitup(url, width, height) {
	newwindow=window.open(url,'name','width='+width+'height='+height+', scrollbars=yes,dependent=yes');
//	if (window.focus) {newwindow.focus()}	
}
 

function CaptureEnter()
{
	if(event.keyCode==13){
	alert ('Dont Press enter !');
	}
}

