//<script>

if (typeof BEEP == "undefined") {
    /**
     * This is BEEP global namespace object. If already defined it will
     * not be overwritten thereby preserving defined namespaces.
     * @class BEEP
     * @static
     */
    var BEEP = {};
}

/**
 * This class provides navigation functions for top list.
 * 
 * @author Bojan Popadic <bopo@tv2.dk>
 * @module toplist
 * @namespace BEEP
 * @class toplist
 * @static
 */
BEEP.toplist = function() {
    /**
     * Animating indicator flag.
     * We don't want to do multiple animations, so this flag is used to control that.
     * @property animating
     * @private
     * @type Boolean
     */
    var animating = false;

    var toplist = null;

    var heights = Array();
    
    var SPEED_ANIM_HEIGHT = 0.25;

    /*******************************
     *  Public fields and methods  *
     *******************************/
    return {
        init: function() {
            toplist = document.getElementById('toplist');
            var ol = toplist.getElementsByTagName('ol');
            for (var i = 0; i < ol.length; i++) {
                var ael = YAHOO.util.Dom.getPreviousSibling(ol[i]);
                if(ael.id.length < 1) {
                    ael.id = 'beep-toplist-'+i;
                }

                var r = YAHOO.util.Dom.getRegion(ol[i]);
                heights[ol[i].parentNode.className] = (r.bottom - r.top);
                if (i > 0) {
                    ol[i].style.height = '0px';
                }
            }

            // TODO: check the id of the cookie (if set), and load that
            var selectedList = getCookie('beep-sel-top-list');
            if(selectedList != null) {
                BEEP.toplist.toggleFold(document.getElementById(selectedList));
            }
        },

        toggleFold: function(elm) {
            if (animating == true || elm.className == 'topListButton selected') {
                return;
            } else {
                animating = true;
            }
            animating = true;

            var toplist = document.getElementById('toplist');
            var activeLink = YAHOO.util.Dom.getElementsByClassName('topListButton selected', 'a', toplist)[0];
            var activeList = activeLink.parentNode.getElementsByTagName('ol')[0];
            var aReg = YAHOO.util.Dom.getRegion(activeList);

            var li = elm.parentNode;
            var selectLink = elm;
            var selectList = li.getElementsByTagName('ol')[0];
            var h = heights[li.className];
            
            var onComplete = function() {
                activeLink.className = 'topListButton';
                selectLink.className = 'topListButton selected';
                setCookie('beep-sel-top-list',selectLink.id);
                animating = false;
            }
            
            var cls = new YAHOO.util.Anim(activeList, {height : { to : 0 }, opacity: {to : 0}}, SPEED_ANIM_HEIGHT, YAHOO.util.Easing.easeOut);
            var exp = new YAHOO.util.Anim(selectList, {height : { to : h }, opacity: {to : 1}}, SPEED_ANIM_HEIGHT, YAHOO.util.Easing.easeOut);
            exp.onComplete.subscribe(onComplete);
            cls.animate();
            exp.animate();
        }
    };
}();

