var SelectableItem = new Class({
    Implements: [Options, Events],
    options: {
        selectionClass: "selected"
    },
    initialize: function (itemEl, options) {
        if (!itemEl) {
            return;
        }
        this.itemEl = itemEl;
        this.setOptions(options);
        var select = this.select.bind(this);
        itemEl.addEvents({
            "click": select,
            "keypress": function (event) {
                if (event.key == "enter") {
                    select();
                }
            }
        });
    },
    select: function () {
        if (this.isSelected()) {
            return;
        }
        this.itemEl.addClass(this.options.selectionClass);
        this.fireEvent("selectItem", this);
    },
    unselect: function () {
        if (!this.isSelected()) {
            return;
        }
        this.itemEl.removeClass(this.options.selectionClass);
        this.fireEvent("unselectItem", this);
    },
    isSelected: function () {
        return this.itemEl.hasClass(this.options.selectionClass);
    },
    getSubMenuContainerId: function () {
        return this.itemEl.getElement("a").get("rel");
    },
    hasSubMenu: function () {
        return !!this.getSubMenuEl();
    },
    getSubMenuEl: function () {
        var subMenuContainer = $(this.getSubMenuContainerId());
        if (!subMenuContainer) {
            return null;
        }
        if (subMenuContainer.hasClass("js_menu")) {
            return subMenuContainer;
        }
        return subMenuContainer.getElement(".js_menu");
    }
});
var Selectable = new Class({
    Implements: [Options, Events, CHKOverrides],
    selectedItem: null,
    hasSubMenu: null,
    options: {
        itemsSelector: null
    },
    initialize: function (menu, options) {
        this.setOptions(options);
        this.setOverrides();
        this.menuItems = this.getItemElements(menu).map(function (itemEl) {
            var item = this.createMenuItem(itemEl, this.options.item);
            item.addEvent("selectItem", this.onSelectItem.bind(this));
            if (item.isSelected()) {
                this.selectedItem = item;
            }
            if (item.hasSubMenu()) {
                this.hasSubMenu = true;
            }
            return item;
        }.bind(this));
    },
    onSelectItem: function (item) {
        var event = {
            previousItem: this.selectedItem,
            currentItem: item
        };
        if (this.selectedItem) {
            this.selectedItem.unselect();
        }
        this.selectedItem = item;
        this.fireEvent("selectItem", event);
    },
    getItemElements: function (menuEl) {
        if (this.options.itemsSelector) {
            return menuEl.getElements(this.options.itemsSelector);
        } else {
            return menuEl.getChildren();
        }
    },
    createMenuItem: function (itemEl, options) {
        return new SelectableItem(itemEl, options);
    }
});
var AnimatedContainer = new Class({
    Implements: [Options, CHKOverrides],
    options: {
        opening: {
            state: "opening",
            before: null,
            beforeAction: null,
            effect: null,
            after: null,
            afterAction: null
        },
        closing: {
            state: "closing",
            before: null,
            beforeAction: null,
            effect: null,
            after: null,
            afterAction: null
        },
        effect: {
            duration: 500,
            transition: Fx.Transitions.Sine.easeOut,
            link: 'cancel'
        }
    },
    initialize: function (cnt, options) {
        this.setOptions(options);
        this.setOverrides();
        cnt.effect = new Fx.Morph(cnt, this.options.effect);
        cnt.effect.addEvent("complete", function () {
            cnt.inprogress = false;
            cnt.open = cnt.effectState == "opening";
            var afterStyles = cnt.fxOptions.after;
            if (afterStyles) {
                cnt.setStyles(afterStyles);
            }
            var action = cnt.fxOptions.afterAction;
            if (action) {
                action(cnt)
            }
        });
        cnt.effectShow = this._effectFunction(this.options.opening);
        cnt.effectHide = this._effectFunction(this.options.closing);
    },
    _effectFunction: function (options) {
        return function () {
            if (this.inprogress) {
                this.effect.cancel();
            }
            this.fxOptions = options;
            if (options.before) {
                this.setStyles(options.before);
            }
            var action = this.fxOptions.beforeAction;
            if (action) {
                action(this)
            }
            this.inprogress = true;
            this.effectState = options.state;
            this.effect.start(options.effect);
        }
    }
});
AnimatedContainer.init = function (cntSelector, options) {
    return $(cntSelector).map(function (cnt) {
        return new AnimatedContainer(cnt, options);
    });
};

function initSecondaryNav(options) {
    $$(".secondary_nav").each(function (snav) {
        var fxOptions = {
            opening: {
                state: "opening",
                before: {
                    opacity: 0,
                    height: 0,
                    'padding-top': 0,
                    display: 'block'
                },
                effect: {
                    opacity: 1,
                    height: 294,
                    'padding-top': 18
                },
                afterAction: function (cnt) {
                    cnt.getElement("a").focus();
                    var parent = $(cnt).getParent(".secondary_nav");
                    parent.addClass("js-opened");
                    parent.removeClass("js-closed");
                }
            },
            closing: {
                state: "closing",
                effect: {
                    opacity: 0,
                    height: 0,
                    'padding-top': 0
                },
                after: {
                    opacity: 1,
                    height: null,
                    'padding-top': null,
                    display: 'none'
                },
                afterAction: function (cnt) {
                    var parent = $(cnt).getParent(".secondary_nav");
                    parent.addClass("js-closed");
                    parent.removeClass("js-opened");
                }
            },
            options: {
                duration: 500,
                transition: Fx.Transitions.Sine.easeOut,
                link: 'cancel'
            }
        };
        var menu = initMenuWithContainer(snav.getElement(".level2 .js_menu"), options);
        snav.addEvent("mouseleave", function () {
            if (snav.hasClass("js-opened")) {
                var subMenuEl = $(menu.selectedItem.getSubMenuContainerId());
                if (subMenuEl) {
                    subMenuEl.effectHide();
                }
            }
        });
        menu.menuItems.each(function (item) {
            var container = $(item.getSubMenuContainerId());
            if (!container) {
                return;
            }
            new AnimatedContainer(container, fxOptions);
            item.itemEl.addEvent("click", function () {
                if (item.isSelected() && snav.hasClass("js-closed")) {
                    container.effectShow();
                }
            });
            var subMenu = container.getElement(".level3");
            initMenuRecursive(subMenu, $extend({
                itemsSelector: ".js_menu li"
            }, options));
            container.getElement(".snav_arr").addEvent("click", function () {
                container.effectHide();
            });
        });
    })
}

function initMenuWithContainer(menuEl, options) {
    var menu = new Selectable(menuEl, options);
    menu.addEvent("selectItem", function (event) {
        var prevContainer = event.previousItem && event.previousItem.getSubMenuContainerId();
        var currentContainer = event.currentItem.getSubMenuContainerId();
        var currCntWrapper = $(currentContainer);
        var prevCntWrapper = $(prevContainer);
        if (currCntWrapper) {
            var parent = currCntWrapper.getParent(".secondary_nav");
            if (parent && parent.hasClass("js-opened") && prevContainer && currentContainer) {
                prevCntWrapper.hide();
                currCntWrapper.show();
                return;
            }
        }
        if (prevCntWrapper) {
            if (prevCntWrapper.effectHide) {
                prevCntWrapper.effectHide();
            } else {
                prevCntWrapper.hide();
            }
        }
        if (currCntWrapper) {
            if (currCntWrapper.effectShow) {
                currCntWrapper.effectShow();
            } else {
                currCntWrapper.show();
                /*avoiding opened tab link to get the focues and probably making the page jump to the middle when first load
				var link = currCntWrapper.getElement("a");
                if (link) {
                    link.focus();
                }
				*/
            }
        }
    });
    if (menu.selectedItem) {
        var cnt = menu.selectedItem.getSubMenuContainerId();
        var cntWrapper = $(cnt);
        if (cntWrapper) {
            cntWrapper.show();
        }
    }
    return menu;
}

function initMenuRecursive(menuEl, options) {
    var menu = initMenuWithContainer(menuEl, options);
    menu.menuItems.each(function (item) {
        var subMenuEl = item.getSubMenuEl();
        if (subMenuEl) {
            initMenuRecursive(subMenuEl, options);
        }
    });
    return menu;
}

function initTabs(tabsSelector, options) {
    function setSelectedTabFromUrlParam() {
        var tabContentMather = /#tab=([^=&$]+)/i.exec(window.location.href);
        if (!tabContentMather) {
            return;
        }
        var link = $$(".tabs_nav .js_menu li a[rel='" + tabContentMather[1] + "']");
        if (!link) {
            return;
        }
        link.getParent("li").addClass("selected");
    }
    return $$(tabsSelector || ".tabs_nav .js_menu").map(function (menuEl) {
        setSelectedTabFromUrlParam();
        var menu = initMenuWithContainer(menuEl, options || {});
        menu.addEvent('selectItem', function (event) {
            if (event.previousItem) {
                updateScreenReading(event.previousItem.itemEl, '.js_unselectedTab');
            }
            if (event.currentItem) {
                updateScreenReading(event.currentItem.itemEl, '.js_selectedTab');
            }
        });
        if (!menu.selectedItem) {
            var items = menu.menuItems;
            if (items && items.length > 0) {
                items[0].select()
            }
        }
        return menu;
    })
}

/*
Date: 11/15/2011 10:15:27 PM
All images published
*/

/* UnCompressed - Reason: DISABLED_TARGET-LIVECWADEPLOYER# */

/*
Date: 12/15/2011 9:54:10 PM
All images published
*/
