/**
 2DayFM Custom Functionality
 CHANGELOG:
 20080118 - Born
 20080121 - Fixed Nav functionality so all dropdowns are independant
 - Added onload functionality to ammend the background gradient
 depending on which (primary or secondary) is taller.
 20080122 - Updated background gradient resize to use scroll event on FF and IE7 (only once)
 load was not firing reliably in IE7
 20080204 - Removed onmouseout behaviour to restart feature loop to improve readability.
 20080205 - Updated _initPrimaryHeight to use scroll event for ie6 and remove scroll events once fired.
 20080222 - Fixed _initAutoInputs not to let event bubble into submit button.
 20080222 - Updated _initAutoInputs to add class userInput when there is a value
 20080318 - Updated _initControlTabs to accept multiple classes rather than single id element. (Jens)
 20080411 - Added _initValidateForm and _initRegistrationForm (show/hide form elements) (Jens)
 20080502 - Added a maxlength3 form validation rule (nick)
 20080502 - Added a function to enable/disable buttons in profile form (Bhabani)
 20080811 - Updated _initControlList function to set the height of each control panel show, for the case when the "On Air Today" tab is not selected on page load (Claire)
 20080926 - Removed non todayNetwork specific JS.
 20081002 - Moved some of the JS back into this file as it actually was TodayNetwork specific.
 20081210 - Re-adding showControlPanelHeight to allow for scrolling in the music widget.
 20081224 - Pass through cat2 to AREA
 20090518 - Don't Pass through cat2 to AREA - only required when uri prefix
 **/

if(window["FD"]) {

    FD.TodayNetwork = function(fdOptions) {

        /**
         * Default options for a <code>FD.ChallengeSecurity</code>.
         *
         * @private
         */
        var defaultOptions = {
        };
        /**
         * The final, merged version of the toggle text definition.
         * <p/>
         * This toggle text definition is the result of extending the <code>defaultOptions</code> object with our custom
         * parameters passed in via <code>fdOptions</code>.
         *
         * @private
         */
        var options = $.extend(true, defaultOptions, fdOptions);

        /**
         *
         * @constructor
         * @private
         */
        (function() {
            _initNav();
        })();


        /* START NAV */
        function _initNav(){
            var navigation = $('#navigation');
            if(navigation.length) {
                navigation.bind("click", togglePlusNav);
                var lastNavElements = navigation.find('li.expand');
                if(lastNavElements.length) {
                    lastNavElements.bind("click", openAllNav);
                }
            }
        }

        function togglePlusNav(event) {
            if(event) {
                var expanded;
                var ele = $(event.target);
                // Make sure we get the LI and not the A:
                if(ele.attr('tagName').toLowerCase() == "a") {
                    ele = ele.parent();
                }
                if(!ele.hasClass("plus")) {
                    return true;
                }
                // Exit if user clicked on 'open all' LI:
                if (ele.hasClass('expand')) return false;

                var parentUL = ele.parent();
                if(parentUL.attr('tagName').toLowerCase() == "ul") {
                    // Is the nav totally expanded?
                    var expand = parentUL.find("li.expand");
                    if(expand.length) {
                        expanded = expand.hasClass("expanded");
                    }
                    // Determine whether we are opening or closing
                    var add;
                    if(!ele.hasClass("open")) {
                        add = true;
                    }
                    // Remove class 'open' from all LI:
                    if(!expanded) {
                        parentUL.find("li.plus").removeClass("open");
                    }
                    if(add) {
                        ele.addClass("open");
                        // Check for other plus elements within this dropdown.
                        // If they all are open, add the expanded classes to the expander
                        var exitnreturnfalse=false;
                        var siblings = parentUL.find("li.plus");
                        siblings.each(function(idx, tsib) {
                            var sib = $(tsib);
                            if (!sib.hasClass("open")) {
                                exitnreturnfalse = true;
                            }
                        });
                        if(exitnreturnfalse) {
                            return false;
                        }

                        // All of the siblings are open
                        // Set the expander to be expanded.
                        expand.addClass("expanded");
                    } else if(expanded) {
                        ele.removeClass("open");
                        // Check for other open elements within this dropdown.
                        // If none, remove the expanded classes from the expander
                        var siblings = parentUL.find("li.open");
                        if(siblings.length == 0) {
                            var expanded = parentUL.find("li.expanded");
                            if(expanded.length) {
                                expanded.removeClass("expanded");
                            }
                        }
                    }
                    return false; // Return false to prevent reloading of page.
                }
            }
        }

        function openAllNav(event) {
            if(event) {
                FD.stopEvent(event);
                var ele = $(this);
                // Make sure we get the LI and not the A:
                if(ele.attr('tagName').toLowerCase() == "a") {
                    ele = ele.parent();
                }
                var parent = ele.parent();
                var navElements = parent.find("li.plus");
                navElements.each(function(idx, tnavElement) {
                    var navElement = $(tnavElement);
                    if(!ele.hasClass("expanded")) {
                        // Open all li.plus'
                        navElement.addClass("open");
                    } else {
                        // Close all li.plus'
                        navElement.removeClass("open");
                    }
                });
                if(!ele.hasClass("expanded")) {
                    // Open all li.plus'
                    ele.addClass("expanded");
                } else {
                    // Close all li.plus'
                    ele.removeClass("expanded");
                }
            }
        }
        /* END NAV */


    }

    $(function() {
        var todayNetwork = new FD.TodayNetwork();
    });

}

