﻿// requires com.iskitz.ajile v0.7.8 or later
// requires YUI Connection Manager v0.12.1 or later
// suggests com.spamfighter.translate.common v0.1.0 or later

Namespace("com.spamfighter.translate.help");

// Globals
var helpContainer;
var helpContent;

/// Initialize help system
/// returns:    void
com.spamfighter.translate.help.init = function() {
    // Checking for basic DOM manipulation support
    if (!document || !(typeof(document) == "object") || !document.body || !(typeof(document.body) == "object") || !document.body.appendChild || !document.createElement) {
        return false;
    }
    // create container and content holder
    helpContainer = document.createElement("div");
    if (helpContainer.style && typeof(helpContainer.style) == "object" && helpContainer.style.setAttribute && typeof(helpContainer.style.setAttribute) == "object") {
        // spell it out for IE
        helpContainer.style.width = "25em";
        helpContainer.style.backgroundColor = "#fff";
        helpContainer.style.border = "solid 2px #000";
        helpContainer.style.top = 0;
        helpContainer.style.right = 0;
        helpContainer.style.borderWidth = "0 0 2px 2px";
        helpContainer.style.position = "absolute";
        helpContainer.style.zIndex = 5000;
        helpContainer.style.display = "none";
    }
    else {
        helpContainer.setAttribute("style", "width:25em; background-color:#fff; border: solid 2px #000; top: 0; right: 0; border-width: 0 0 2px 2px; position: absolute; z-index: 5000; display: none");
    }
    var helpHeading = document.createElement("div");
    if (helpHeading.style && typeof(helpHeading.style) == "object" && helpHeading.style.setAttribute && typeof(helpHeading.style.setAttribute) == "object") {
        // spell it out for IE
        helpHeading.style.height = "2.1em";
        helpHeading.style.textAlign = "center";
        helpHeading.style.borderBottom = "solid 2px #000";
        helpHeading.style.lineHeight = "2em";
    }
    else {
        helpHeading.setAttribute("style", "height: 2.1em; text-align: center; border-bottom: solid 2px #000; line-height: 2em");
    }
    var closeImg = document.createElement("img");
    closeImg.setAttribute("src", "/images/close.gif");
    if (closeImg.style && typeof(closeImg.style) == "object" && closeImg.style.setAttribute && typeof(closeImg.style.setAttribute) == "object") {
        // spell it out for IE
        closeImg.style.styleFloat = "right";
        closeImg.style.margin = ".4em";
        closeImg.style.cursor = "pointer";
    }
    else {
        closeImg.setAttribute("style", "float:right; margin: .4em; cursor: pointer");
    }
    closeImg.setAttribute("title", "Close online help");
    closeImg.onclick = com.spamfighter.translate.help.hide;
    helpHeading.appendChild(closeImg);
    var helpImg = document.createElement("img");
    helpImg.setAttribute("src", "/images/question.gif");
    if (helpImg.style && typeof(helpImg.style) == "object" && helpImg.style.setAttribute && typeof(helpImg.style.setAttribute) == "object") {
        // spell it out for IE
        helpImg.style.margin = ".25em .35em";
        helpImg.style.styleFloat = "left";
    }
    else {
        helpImg.setAttribute("style", "margin: .25em .35em; float:left");
    }
    helpHeading.appendChild(helpImg);
    var helpTitle = document.createElement("strong");
    if (helpTitle.style && typeof(helpTitle.style) == "object" && helpTitle.style.setAttribute && typeof(helpTitle.style.setAttribute) == "object") {
        // spell it out for IE
        helpTitle.style.fontSize = "larger";
    }
    else {
        helpTitle.setAttribute("style", "font-size: larger");
    }
    helpTitle.appendChild(document.createTextNode("Online help"));
    helpHeading.appendChild(helpTitle);
    helpContainer.appendChild(helpHeading);
    helpContent = document.createElement("div");
    helpContent.setAttribute("id", "helpContent");
    if (helpContent.style && typeof(helpContent.style) == "object" && helpContent.style.setAttribute && typeof(helpContent.style.setAttribute) == "object") {
        // spell it out for IE
        helpContent.style.padding = ".5em 1em";
    }
    else {
        helpContent.setAttribute("style", "padding: .5em 1em");
    }
    var waitContainer = document.createElement("div");
    if (waitContainer.style && typeof(waitContainer.style) == "object" && waitContainer.style.setAttribute && typeof(waitContainer.style.setAttribute) == "object") {
        // spell it out for IE
        waitContainer.style.textAlign = "center";
    }
    else {
        waitContainer.setAttribute("style", "text-align: center");
    }
    var waitImg = document.createElement("img");
    waitImg.setAttribute("src", "/images/wait.gif");
    waitContainer.appendChild(waitImg);
    waitContainer.appendChild(document.createElement("br"));
    waitContainer.appendChild(document.createElement("br"));
    waitContainer.appendChild(document.createTextNode("Loading, please wait..."));
    helpContent.appendChild(waitContainer);
    helpContainer.appendChild(helpContent);
    document.body.insertBefore(helpContainer, document.body.firstChild);
};

com.spamfighter.translate.help.clientError = function(msg) {
    if (com && typeof(com) == "object" && com.spamfighter && typeof(com.spamfighter) == "object" && com.spamfighter.translate && typeof(com.spamfighter.translate) == "object" && com.spamfighter.translate.common && typeof(com.spamfighter.translate.common) == "object" && com.spamfighter.translate.common.clientError && typeof(com.spamfighter.translate.common.clientError) == "function") {
        com.spamfighter.translate.common.clientError(msg);
    }
    else {
        alert(msg);
    }
};

com.spamfighter.translate.help.hide = function() {
    helpContainer.style.display = "none";
    while (helpContent.firstChild) {
        helpContent.removeChild(helpContent.firstChild);
    }
    var waitContainer = document.createElement("div");
    waitContainer.setAttribute("style", "text-align: center");
    var waitImg = document.createElement("img");
    waitImg.setAttribute("src", "/images/wait.gif");
    waitContainer.appendChild(waitImg);
    waitContainer.appendChild(document.createElement("br"));
    waitContainer.appendChild(document.createElement("br"));
    waitContainer.appendChild(document.createTextNode("Loading, please wait..."));
    helpContent.appendChild(waitContainer);
    return false;
};

com.spamfighter.translate.help.show = function(chapter, index) {
    // provide default values for missing params
    if (typeof(chapter) == "undefined") {
        chapter = "";
    }
    if (typeof(index ) == "undefined") {
        index = "";
    }
    
    // check that init has been called
    if (!helpContainer) {
        com.spamfighter.translate.help.clientError("The online help system has not been initialized.");
        return false;
    }
    
    helpContainer.style.display = "block";
    var helpUrl = "/pub/Help.aspx?i=" + escape(index) + "&c=" + escape(chapter);
    var callback = {
        success: com.spamfighter.translate.help.received,
        failure: com.spamfighter.translate.help.failure
    };
    YAHOO.util.Connect.asyncRequest("GET", helpUrl, callback);
    return false;
};

com.spamfighter.translate.help.received = function(o) {
    if (typeof(o.responseText) != "undefined") {
        helpContent.innerHTML = o.responseText;
    }
    else {
        com.spamfighter.translate.help.failure();
    }
};

com.spamfighter.translate.help.failure = function() {
    if (helpContainer.style.display == "block") {
        helpContent.innerHTML = "<p style=\"color:red\">Sorry, the system couldn't answer your help request at this time.</p><p>Please report this to the support team. Thank you!</p>";
    }
};
