// Pop Up Profile Display
// Each profile link ([span class="profileLink"][a]username[/a][/span])
// has an event attached that displays a div (popProfileHover) with
// the user's profile information.
YAHOO.namespace('forumsPopProfile');
YAHOO.forumsPopProfile = function() {
    var timer;      // timer id for a pending display
    var profileDiv; // popup display object
    var triggerSpan;// username span triggering popup
    var loadingText = '<p style="text-align:center;padding:20px;">Loading User...<' + '/p>';

    // OnMouseOver, set a timer to display this in half a second.
    // If we OnMouseOut, we'll cancel the timer.
    function profileOver(e) {
        if (timer) { clearTimeout(timer); timer = "";}
        triggerSpan = this;
        timer = setTimeout(function(){profileOverGuts();},500);
    }

    // Set the popup's position and invoke the ajax call.
    function profileOverGuts() {
        var el = triggerSpan;
        // Objects must be displayed in order to set their XY (grrr).
        profileDiv.style.opacity = 0;
        profileDiv.style.display = '';
        // Position box to the right and slightly centered.
        var XY = YAHOO.util.Dom.getXY(el);
        XY[0] += el.offsetWidth + 5;
        XY[1] -= 27;
        YAHOO.util.Dom.setXY(profileDiv, XY);
        // Issue request to load username ([span class="profileLink"][a]username[/a][/span])
        var sUrl = "/WebX?ajax_displayUserProfile";
        var args = "vuserName=" + YAHOO.util.Dom.getFirstChild(el).innerHTML;
        var callback = {
            success:handleSuccess,
            failure:handleFailure
        };

        YAHOO.util.Connect.setDefaultPostHeader(true);
        var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, args);
    }

    // Display the popup.
    function handleSuccess(o) {
        profileDiv.innerHTML = o.responseText;
        profileDiv.style.opacity = 0;
        profileDiv.style.display = '';
        var animIn = new YAHOO.util.Anim( profileDiv, { opacity: { to: 1 } }, 0.25, YAHOO.util.Easing.easeIn);
        animIn.animate();
    }

    function handleFailure(o) {
        profileHide();
    }

    function profileOut(e) {
        if (timer) { clearTimeout(timer); timer = "";}
        if (profileDiv.style.display != 'none') {
            var animOut = new YAHOO.util.Anim( profileDiv, { opacity: { to: 0 } }, 0.25, YAHOO.util.Easing.easeOut);
            animOut.animate();
            animOut.onComplete.subscribe( profileHide );
        }
    }

    // Turn off the display once opacity animation is done.
    function profileHide() {
        profileDiv.style.display = 'none';
        profileDiv.innerHTML = loadingText;
    }

return({
    init: function() {
        // Add the popup div.
        profileDiv = document.createElement('div');
        profileDiv.id = "popProfileHover";
        YAHOO.util.Dom.addClass(profileDiv, 'popProfile');
        profileDiv.innerHTML = loadingText;
        profileDiv.style.display = 'none';
        profileDiv.style.position = 'absolute';
        document.body.appendChild(profileDiv);

        // Add the events.
        var els = YAHOO.util.Dom.getElementsByClassName('profileLink', 'span');
        for (var i = 0; i < els.length; i++) {
            YAHOO.util.Event.addListener( els[i], "mouseover", profileOver );
            YAHOO.util.Event.addListener( els[i], "mouseout", profileOut );
        }
    }
});

}();

