﻿var helpPopupObject;
function ngd_HelpDisplayer(url) {
    this.helpDisplayerSpan = document.getElementById('helpDisplayerSpan');
    this.xLocation = 0;
    this.yLocation = 0;
    this.resourceId = "";
    this.helpBodyText = "";
    this.contentUrl = url;
    this.className = "";
    this.isOpen = false;
    
    helpPopupObject = this;
    
    if (this.helpDisplayerSpan == null) {
        //alert("There was a problem loading the Help Displayer");
    }
}

ngd_HelpDisplayer.prototype.Open = function(sender, id, positionCode) {

    var topAdjustment = -17;
    var leftAdjustment = 0;
    if (positionCode == 'left') {
        this.className = 'helpPopupLeft';
        leftAdjustment = 15;
    }
    else {
        this.className = 'helpPopupRight';
        leftAdjustment = -185;
    }
    this.isOpen = true;
    
    // Add mouse event click to whole page to detect if click outside help popout (if so then close popup)
    this.AddEvent();

    this.xLocation = this.GetElementLeft(sender) + leftAdjustment;
    this.yLocation = this.GetElementTop(sender) + topAdjustment;
    this.resourceId = id;
    loadXMLDoc(this, this.contentUrl, null, null, null);
}

ngd_HelpDisplayer.prototype.Close = function() {
    if (this.helpDisplayerSpan != null) {
        this.helpDisplayerSpan.innerHTML = "";
    }

    // Remove mouseclick event from page
    this.RemoveEvent();
}


ngd_HelpDisplayer.prototype.Setup = function() {
    if (this.helpDisplayerSpan != null) {
        var html = "<div class=\"helpPopup " + this.className + "\" style=\"left:" + this.xLocation + "px;top:" + this.yLocation + "px\"><div class=\"minihelpclosebutton\" onClick=\"JavaScript:helpDisplayer.Close()\" />" + this.helpBodyText + "</div>";
        this.helpDisplayerSpan.innerHTML = html;
    }

}

ngd_HelpDisplayer.prototype.GetElementLeft = function(eElement) {
    var nLeftPos = eElement.offsetLeft;          // initialize var to store calculations
    var eParElement = eElement.offsetParent;     // identify first offset parent element  
    while (eParElement != null) {                                            // move up through element hierarchy
        nLeftPos += eParElement.offsetLeft;      // appending left offset of each parent
        eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    return nLeftPos;                             // return the number calculated
}

ngd_HelpDisplayer.prototype.GetElementTop = function(eElement) {
    var nTopPos = eElement.offsetTop;            // initialize var to store calculations
    var eParElement = eElement.offsetParent;     // identify first offset parent element  
    while (eParElement != null) {                                            // move up through element hierarchy
        nTopPos += eParElement.offsetTop;        // appending top offset of each parent
        eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    return nTopPos;                              // return the number calculated
}


ngd_HelpDisplayer.prototype.AddEvent = function() {
    if (document.addEventListener) {
        document.addEventListener('click', this.MouseClicked, false);
    } else if (document.attachEvent) {
    document.attachEvent('onclick', this.MouseClicked);
    }
}


ngd_HelpDisplayer.prototype.RemoveEvent = function() {
    if (document.removeEventListener) {
        document.removeEventListener('click', this.MouseClicked, false);
    } else if (document.detachEvent) {
    document.detachEvent('onclick', this.MouseClicked);
    }
}


ngd_HelpDisplayer.prototype.MouseClicked = function(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    if (helpPopupObject != null && targ.name != 'helpDisplayerButtonImage') {
        helpPopupObject.Close();
    }
}


ngd_HelpDisplayer.prototype.ProcessXML = function(xmlDOM) {
    var allResourceNodes = xmlDOM.getElementsByTagName('resource');
    for (i = 0; i < allResourceNodes.length; i++) {
        var id = allResourceNodes[i].getAttribute("key");

        if (id == this.resourceId) {
            this.helpBodyText = allResourceNodes[i].childNodes[0].nodeValue;
        }
    }
    this.Setup();
}


var xmlDoc;
var xmlDOM;
var _targetContainer;
var _headHTML;
var _callBackObj;
var _menuSystemObj;


function loadXMLDoc(returnObj, url, target, html, mso) {
    _callBackObj = returnObj;
    _targetContainer = target;
    _headHTML = html;
    _menuSystemObj = mso;


    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        xmlDoc = new XMLHttpRequest();
        xmlDoc.onreadystatechange = processReqChange;
        xmlDoc.open("GET", url, true);
        xmlDoc.send(null);
        // branch for IE/Windows ActiveX version
    }
    else if (window.ActiveXObject) {
        xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlDoc) {
            xmlDoc.onreadystatechange = processReqChange;
            xmlDoc.open("GET", url, true);
            xmlDoc.send();
        }
    }
}

function processReqChange() {
    // only if req shows "loaded"
    if (xmlDoc.readyState == 4) {
        // only if "OK"
        if (xmlDoc.status == 200) {
            // this will show the loaded XML file
            //alert(xmlDoc.responseText);

            try {

                // Load the HTTPXML Request into an XMLDOM Object
                xmlDOM = new ActiveXObject("Microsoft.XMLDOM");
                xmlDOM.async = "false";
                xmlDOM.validateOnParse = "true";
                xmlDOM.loadXML(xmlDoc.responseText);
            }
            catch (e) {
                try {
                    parser = new DOMParser();
                    xmlDOM = parser.parseFromString(xmlDoc.responseText, "text/xml");
                }
                catch (e) {
                    alert(e.message);
                }
            }

            if (xmlDOM.documentElement.hasChildNodes) {
                // get the root element
                var root = xmlDOM.documentElement;
                var rootName = root.tagName;


                // Check we have results
                if (rootName == "Resources") {
                    _callBackObj.ProcessXML(xmlDOM);
                }
                else {
                    alert("There was an error with the returned AJAX XML");
                }

            }
            else {
                alert("XML Root contains no child elements");
            }

        }
        else {
            alert("There was a problem retrieving the XML data:\n" +
 			xmlDoc.statusText);
        }
    }
}

