/******
  Context-sensitive help for IRDA-Online v5

*/

var help = new Object; // namespace

/**
  Show/Hide Online Help  
**/

help.toggleOnlineHelp = function() {
    var elems = $w('helpContentBox helpControlHideBox helpControlShowBox');
    elems.each(function (e) {
        //Effect.toggle($(e), 'blind', {duration:0.4});
        $(e).toggle();
    });
    return false;

};

help.injectOnlineHelpBehavior = function() {
    var elems = $('helpControlHideTrigger', 'helpControlShowTrigger');
    for (var i = 0; i < elems.length; i++) {
        if (elems[i]) {
	    elems[i].onclick = help.toggleOnlineHelp;
        }
    }
};

onloadHooks.addHook(help.injectOnlineHelpBehavior);


/**
  Tooltip-style floating help
**/

help.delay = 1500; // milliseconds

help.intID = -1;
help.target = '';
help.idCounter = 0;
help.bindList = new Array(); // List of element IDs to bind to onmouseover handlers AFTER page has laoded
help.messages = new Array();
help.msgByElemId = new Array();

help.register = function(helpid, message) {
    help.messages[helpid] = message;
};

// Associate a control with a help message
// Third arg is optional, and if absent, assigns the 
// help message whose helpid matches the form control name
//  If the element does not have an ID, one is generated and assigned
//
help.attachByName = function(formName, elemName, helpid) {
    if (! helpid) { helpid = elemName; }
    var frm = document.forms[formName];
    if (! frm) { throw('help.attachByName: form name ' + formName + ' is invalid'); }
    var elem = frm.elements[elemName];
    if (! elem) { throw('help.attachByName: elem name ' + elemName + ' is invalid'); }
    var elemid = elem.id;
    if (! elemid) {
	elemid = elemName + '-' + ++help.idCounter;
	elem.id = elemid;
    }
    help.attachById(elemid, helpid);
};

// Associate anything that can have a mouseover with a help message.
// 2nd arg optional, assumed equal to elemid if missing.
// 3rd arg is bool, default false, if true immediately bind the control 
help.attachById = function(elemid, helpid, immediateBind) {
    if (! helpid) { helpid = elemid; }
    
    var elem = $(elemid);
    if (! elem) { throw('help.attachById: elem id ' + elemid + ' is invalid'); }

    // Associate message with this control
    help.msgByElemId[elemid] = helpid;

    // Schedule installation of onmouseover handler
    if (immediateBind) {
        var elem = $(elemid);
        Event.observe(elem, 'mouseover', help.handleMouseOver);
	Event.observe(elem, 'mouseout', help.handleMouseOut);
    } else {
        help.bindList[help.bindList.length] = elem;    
    }
};

// Drop attachement.
help.releaseById = function(elemid) {
    var elem = $(elemid);
    Event.stopObserving(elem, 'mouseover', help.handleMouseOver);
    Event.stopObserving(elem, 'mouseout', help.handleMouseOut);
    delete help.msgByElemId[elemid];
}

/** 
  Event Handlers
**/
help.handleMouseOver = function(evt) {
    var elem = Event.element(evt);
    // Start timer for revealing help box
    if (help.intID != -1) {
	clearTimeout(help.intID);
    }
    help.intID = setTimeout('help.showHelp("' + elem.id + '",' + Event.pointerX(evt) + ',' + Event.pointerY(evt)+ ');', help.delay);
};

help.handleMouseOut = function(evt) {
    $('help_floater').hide();

    // Clear timer for revealing help box
    if (help.intID != -1) {
	clearTimeout(help.intID);
    }
    help.intID = -1;
};

help.showHelp = function(elemID, mouseX, mouseY) {
    var floater = $("help_floater");
    var elem = $(elemID);

    // Set message
    var msg = help.messages[help.msgByElemId[elemID]];
    if (! msg) { return; } // Never show an empty message
    $("help_floater_text").innerHTML = msg;

    var pos = help.calcFloaterPos(elem, mouseX, mouseY);
    floater.style.top = pos.y;
    floater.style.left = pos.x;

    // Show floating help
    floater.show();

};


help.calcFloaterPos = function(src, mouseX, mouseY) {

    var fl = $("help_floater");

    // Calculate rightmost and bottommost pixel in doc coords
    var rightmost = Geometry.getViewportWidth() + Geometry.getHorizontalScroll();
    var bottommost = Geometry.getViewportHeight() + Geometry.getVerticalScroll();

    // Determine width & height of floater box
    var dim = Element.getDimensions(fl);
    var fw = dim.width;
    var fh = dim.height;

    // Start with the mouse position in doc coords
    var x = mouseX;
    var y = mouseY;
    var padding = 10;

    // Draw above or below?
    if (mouseY + fh + 2*padding > bottommost) {
        // Would overflow, better draw above
        y = mouseY - fh - padding;
    } else {
        y = mouseY + padding;
    }
    
    // Draw to left or right?
    if (mouseX + fw + 2*padding > rightmost) {
        // Would overflow, better draw to left
        x = mouseX - fw - padding;
    } else {
        x = mouseX + padding;
    }

    var pos = new Object;
    pos.y = y + 'px';
    pos.x = x + 'px';
    return pos;
}


// Walk over bindList, attaching onmouseover handlers
help.installMouseHandlers = function() {
    help.bindList.each(function(elem) {
	Event.observe(elem, 'mouseover', help.handleMouseOver);
	Event.observe(elem, 'mouseout', help.handleMouseOut);
    });
    help.bindList = null;
};
onloadHooks.addHook(help.installMouseHandlers);

// Cancel any pending (or already present) hover
help.abortHover = function() {  help.handleMouseOut(); }
