﻿// JScript File
var helpIDs = null;
var helpConnections = null;
var hideConnections = null;
var helpTimeout = null;
var helpShownonce = [];
var helpCSS = "helptipshow";
var dontShowThis = false;
var dontShowAny = false;

function resetHelpTips()
{
    if(helpConnections!=null && hideConnections!=null)
    {
        for(var hi = helpConnections.length; hi!=0; hi--)
        {
            var con = helpConnections.pop();
            if(con!=null) dojo.disconnect(con);  //if one of the items in the connections array is null then that means this was a showonce tip that has been shown.
        }
        for(var hidei = hideConnections.length; hidei!=0;hidei--)
        {
            dojo.disconnect(hideConnections.pop());
        }
        helpIDs = null;
        helpConnections = null;
        hideConnecions = null;
    }
    
}

function applyHelpTips()
{
  helpCookie = getCookie("help")
  if(helpCookie==null || helpCookie=="")  
  {
    dontShowAny = false;
    if(helpIDs == null)
    {
        helpIDs = [];
        helpConnections = [];
        hideConnections = [];
        var shownonce = "::"+helpShownonce.join("::")+"::";
        var helpItems = document.getElementById("help_helpTipContainer").childNodes;
        var hx;
        for(hx in helpItems)
        {
            var hi = helpItems[hx];
            if (hi.id)
            {
                helpIDs.push(hi.id.slice(10)); //remove the "help_help_" prefix from the id of the helptip and add the id to the array
                
                var hitem = document.getElementById(helpIDs[helpIDs.length-1]); //get the document item using the id just added to the array
                if(hitem && shownonce.indexOf(helpIDs[helpIDs.length-1])==-1)
                {
                    switch (hi.getAttribute('helpevent'))
                    {
                        case "onmouseover":
                            helpConnections[helpIDs.length-1] = dojo.connect(hitem,"onmouseover",showhelpEvent);
                            hideConnections.push(dojo.connect(hitem,"onmouseout", mouseoutHide));
                            break;
                        case "":
                            break;
                        default:
                            helpConnections[helpIDs.length-1] = dojo.connect(hitem,hi.getAttribute('helpevent'),showhelpEvent);
                            break;                        
                    }
                }
            }
         }   
    }
  }
  else dontShowAny = true;  
}
function showhelpEvent(e)
{
    if(helpTimeout!=null)
    {
        clearTimeout(helpTimeout)
        helpTimeout = null;
    }
    if (!e) var e = window.event;
    if(e.type == "mouseover") dojo.stopEvent(e);
    showHelp(e,this.id);
}
function showHelp(e,hitemname)
{

    var helptip = document.getElementById("helpTip");
    var helptipItem = document.getElementById("help_help_"+hitemname);
    helptip.innerHTML = helptipItem.innerHTML;
    
    if(e.type == "mouseover") helptip.onmouseout = mouseoutHide;
    else helptip.onmouseout = null;
    var ax = helptipItem.getAttribute('helpx');
    var ay = helptipItem.getAttribute('helpy');
    var tipx;
    var tipy;
    if(ax) tipx = parseInt(ax);
    if(ay) tipy = parseInt(ay);
    if (!tipx) tipx = e.clientX;
    if (!tipy) tipy = e.clientY;
    
    var tipoffset = 1;
    
    if((GetViewportWidth()>(tipx+300))||(tipx<300))
        helptip.style.left = (tipx+tipoffset) +"px";
    else 
        helptip.style.right = (GetViewportWidth()-tipx+tipoffset) +"px";
    if((GetViewportHeight()>(tipy+300))||(tipy<300))
        helptip.style.top = (tipy+tipoffset) +"px";
    else 
        helptip.style.bottom = (GetViewportHeight()-tipy+tipoffset) +"px";
    
    var adelay = helptipItem.getAttribute('helpdelay');
    var aCSS = helptipItem.getAttribute('helpCSS');
    if(!aCSS) helpCSS = "helptipshow"
    else helpCSS = aCSS;
    
    if(adelay) helpTimeout=window.setTimeout("document.getElementById('helpTip').className = helpCSS;",parseInt(adelay));
    else helptip.className = helpCSS;
    
    helptip.setAttribute('itemname',hitemname)
    dontShowThis = false;
    
    var ashowonce = helptipItem.getAttribute('helpshowonce');
    if(ashowonce)
    {
        var hx;
        for(hx in helpIDs)
        {
            if(helpIDs[hx]==hitemname)
            {
                dojo.disconnect(helpConnections[hx]);
                helpConnections[hx]=null;
                helpShownonce.push(hitemname);
            }
        }
    }
    //alert(hitemname);
}

function showHelpTipOptions()
{
document.getElementById("helpOptionsShow").className = "helpoptionshide";
document.getElementById("helpOptionsHide").className = "helpoptionsshow";
document.getElementById("helpOptions").className = "helpoptionsshow";
}

function hideHelpTipOptions()
{
document.getElementById("helpOptionsShow").className = "helpoptionsshow";
document.getElementById("helpOptionsHide").className = "helpoptionshide";
document.getElementById("helpOptions").className = "helpoptionshide";

}

function mouseoutHide(e)
{
    if (!e) var e = window.event;
    dojo.stopEvent(e);
    
	var tg = (window.event) ? e.srcElement : e.target;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	if (reltg == null) return;
	var helptip = document.getElementById("helpTip");
    while (reltg != tg && reltg != helptip && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return;  // Mouseout took place when mouse actually left layer
	if (reltg == helptip) return;  //Mouseout moved to the tip itself so we don't want to close it
	// Handle event

    hideHelp();	
}

function hideHelp()
{
    if(helpTimeout!=null)
    {
        clearTimeout(helpTimeout)
        helpTimeout = null;
    }
    
    var helptip = document.getElementById("helpTip");
    var hitemname = helptip.getAttribute('itemname');
    helptip.innerHtml = "";
    helptip.className = "helptiphide";
    helptip.style.top = null;
    helptip.style.left = null;
    helptip.style.right = null;
    helptip.style.bottom = null;
    if (dontShowAny)
    {
        resetHelpTips();
        createCookie("help","1",30);
    }
    if(dontShowThis)
    {
        var hx;
        for(hx in helpIDs)
        {
            if(helpIDs[hx]==hitemname)
            {
                dojo.disconnect(helpConnections[hx]);
                helpConnections[hx]=null;
                helpShownonce.push(hitemname);
            }
        }
    }
}

function dontShowAnyHelp(ckbx)
{
    if (ckbx.checked == true)
        dontShowAny = true;
    else
        dontShowAny = false;
        
}
function dontShowThisHelp(ckbx)
{
    if (ckbx.checked == true)
        dontShowThis = true;
    else
        dontShowThis = false;
        
}

