/* --------------------- TOPIC INDEX & GLOSSARY --------------------- */

YAHOO.namespace("TopicIndexAndGlossary");
YAHOO.TopicIndexAndGlossary = { activeTab: 'index' };

/*
 * @param integer whichOne switch to this element; 1=Index, 2=Glossary
 * @todo // TODO: change all the links to. probably by toggling a global variable, all leter links then call a function which read the global var
 */
YAHOO.TopicIndexAndGlossary.toggle = function(whichOne) {
    var tab = document.getElementById('TopicIndexTabs');
    var div1 = document.getElementById('TopicIndexTabs1');
    var div2 = document.getElementById('TopicIndexTabs2');
    var a1 = document.getElementById('TopicIndexTabsA1');
    var a2 = document.getElementById('TopicIndexTabsA2');
    
    if (whichOne == 'index')
    {
        YAHOO.TopicIndexAndGlossary.activeTab = whichOne;
        tab.style.backgroundImage = 'url(images/hm_a-to-z_tabs_left.png)';
        a1.style.color = '#377686';
        a2.style.color = 'white';
    }
    else if (whichOne == 'glossary')
    {
        YAHOO.TopicIndexAndGlossary.activeTab = whichOne;
        tab.style.backgroundImage = 'url(images/hm_a-to-z_tabs_right.png)';
        a1.style.color = 'white';
        a2.style.color = '#377686';
    }
    
    // TODO: implement the rest of ToggleTopicIndexGlossary, get A element in element0 & element1, then set style for all 4 pseudo-attributes
    
    if (YAHOO.Sounds && YAHOO.Sounds.flashSound)
        YAHOO.Sounds.flashSound.TGotoAndPlay('/', 'startClick');


    return false;
}

/*
 * Sends the user to the appropriate topic or glossary index page. Should be called directly from an <A onclick=""> handler that returns false to cancel the default action.
 * @param string letter the letter to jump to
 * @return boolean false if parameters are valid, true otherwise (should be passed directly to the onclick handler to allow/cancel the default action)
 */
YAHOO.TopicIndexAndGlossary.go_to = function(letter)
{
    var path = null;
    if (YAHOO.TopicIndexAndGlossary.activeTab == 'index')
        path = 'topics/index/';
    else if (YAHOO.TopicIndexAndGlossary.activeTab == 'glossary')
        path = 'glossary/index/';
        
    if (path != null)
    {
        // set the new url, and return false to cancel the default link action
        window.location = path + letter;
        return false;
    } else {
        // wrong parameters, just allow the link to go to the default HTML-supplied link
        return true;
    }
        
}



/* --------------------- AJAX TIPS --------------------- */

function loadPrevTip(controller, currentId)
{ loadTip(controller, 'previousTip', currentId); }

function loadNextTip(controller, currentId)
{ loadTip(controller, 'nextTip', currentId); }

var TipAnim1;
var TipAnim2;
function loadTip(controller, method, currentId)
{
    var DivId = 'TipData';
    var IndicatorId = 'TipIndicator';
    var tipCompleted = false;
    var tipLoading = true;
    new Ajax.Updater(DivId, controller+method+'?id='+currentId,
        {
            method: 'get',
            onLoading: function() {
                TipAnim1 = new Effect.Opacity(DivId, {
                    duration: 0.5,
                    to: 0.2,
                    queue: { position: 'end', scope: 'busy' }
                    });
                TipAnim2 = new Effect.Opacity(IndicatorId, {
                    duraction: 0.1,
                    from: 0.0,
                    to: 1.0,
                    queue: { position: 'end', scope: 'busy' },
                    beforeStart: function() { $(IndicatorId).show(); }
                    });
            },
            onSuccess: function() {
                if (TipAnim1)
                    TipAnim1.cancel();
                if (TipAnim2)
                    TipAnim2.cancel();
            },
            onComplete: function() {
                // hide busy indicator and fade content back in
                new Effect.Opacity(IndicatorId, {
                    duraction: 0.1,
                    to: 0.0,
                    queue: { position: 'front', scope: 'available' },
                    afterFinish: function() { $(IndicatorId).hide(); }
                    });
                new Effect.Opacity(DivId, {
                    duration: 0.4,
                    to: 1.0,
                    queue: { position: 'end', scope: 'available' }
                    });
//debugger;
//console.trace('here');
                // Get all of the tip and tip overlay elements
                var TipData = YAHOO.util.Dom.get('TipData');
                var TipDataOverlay = YAHOO.util.Dom.get('TeachingTipOverlayContent');
//console.debug(TipData);
                var TipTitle = YAHOO.util.Dom.getElementsByClassName('TipTitle', 'div', TipData);
                var TipTitleOverlay = YAHOO.util.Dom.getElementsByClassName('TipTitle', 'div', TipDataOverlay);
//console.debug(TipTitleOverlay);
                
                var TipContent = YAHOO.util.Dom.getElementsByClassName('TipContent', 'div', TipData);
                var TipContentOverlay = YAHOO.util.Dom.getElementsByClassName('TipContent', 'div', TipDataOverlay);
//console.debug(TipContent[0].innerHTML);
//console.debug(TipContentOverlay[0].innerHTML);
                // Copy content over
                TipTitleOverlay[0].innerHTML = TipTitle[0].innerHTML;
                TipContentOverlay[0].innerHTML = TipContent[0].innerHTML;

            },
            onException: function(AjaxRequest, exception) {
                alert('There was a problem. Please contact the web master.\n\nException: ' + exception.message);
            }
        });
}

/* --------------------- TIP OVERLAY --------------------- */

YAHOO.namespace("TeachingTip");

YAHOO.TeachingTip.init = function() {
    YAHOO.TeachingTip.TeachingTipOverlay = new YAHOO.widget.Overlay('TeachingTipOverlay',
        {
            context: ["LeftSideBarVertical", 'tl', 'tl'], 
            //fixedcenter: true,
            visible: false, width: "278px" }
        );
    YAHOO.TeachingTip.TeachingTipOverlay.render();

    // TeachingTipMoreLink has to be done inline due to the AJAX updates
    // YAHOO.util.Event.addListener("TeachingTipMoreLink", "click", function(e) { YAHOO.TeachingTip.TeachingTipOverlay.show(); YAHOO.util.Event.preventDefault(e); });
    YAHOO.util.Event.addListener("TeachingTipOverlayClose", "click", function(e) { YAHOO.TeachingTip.TeachingTipOverlay.hide(); YAHOO.util.Event.preventDefault(e); });
}

YAHOO.util.Event.addListener(window, "load", YAHOO.TeachingTip.init);