//
window.addEvent("domready", init);

function init() {
        sti_config                                                               = new Array();
        sti_global                                                               = new Array();
        /*
        * STI configuration
        * any durations, delays, etc. are in milliseconds
        */
        sti_config["overlay_fadeDuration"]               = 400;
        sti_config["overlay_fadeOpacity"]                = 0.75;
        sti_config["menu_rolloutDuration"]               = 100;
        sti_config["hero_rotateDelay"]                   = 7000;
        sti_config["cssImagePath"]                       = "binx/img/";

        // STI configuration - IE-specific

        if ($defined(window.ie)) {}

        // STI configuration - IE6-specific

        if ($defined(window.ie6)) {}

        // global variables - for internal script use only, do not modify

        sti_global["menu_inputActive"]                   = false;
        sti_global["menu_activeMenu"]                    = null;
        sti();
}

// *****************************************************************************

function sti() {
        /*
        * overlays
        */
        $$(".overlay").each(
                function(element, i) {
                        element.fadeState = 0;
                        //element.setStyle("opacity",0);
                        element.toggle = function() {
                                if ($defined(this.fx))
                                        this.fx.stop();
                                var toggle     = 1 - this.fadeState;
                                var opacity    = toggle * sti_config["overlay_fadeOpacity"];
                                var windowSize = window.getSize();
                                this.setStyles({
                                               width:  windowSize.scrollSize.x,
                                               height: windowSize.scrollSize.y
                                              });
                                this.fx        = new Fx.Style(this, "opacity", {duration: sti_config["overlay_fadeDuration"]}).start(opacity);
                                this.fadeState = toggle;
                        };
                        element.addEvent("click", function() {
                                if (sti_global["menu_activeMenu"])
                                   {
                                   sti_global["menu_activeMenu"].fireEvent("mouseleave");
                                   }
                        });
                }
        );
        /*
        * main menu rollouts & overlay toggle
        */
        $$("#menu li").each(
                function(element, i) {
                        element.addEvent("mouseenter", function() {
                                $("overlay_menu").toggle();
                                $E("a", this).setStyle("backgroundPosition", "0 -33px");
                                var box = $(this.id + "Box");
                                box.setStyle("visibility", "visible");
                                box.setStyle("left","auto");
                                if ($defined(box.fx))
                                        box.fx.stop();
                                box.fx = new Fx.Style(box, "height", {duration: sti_config["menu_rolloutDuration"]}).start(170);
                        });
                        element.addEvent("mouseleave", function() {
                                $("overlay_menu").toggle();
                                $E("a", this).setStyle("backgroundPosition", "");
                                var box = $(this.id + "Box");
                                if ($defined(box.fx))
                                        box.fx.stop();
                                //box.setStyle("visibility","");
                                box.fx = new Fx.Style(box, "height", {duration: sti_config["menu_rolloutDuration"]}).start(0).chain(
                                        function() {
                                                box.setStyle("visibility", "");
                                        }
                                );
                        });
                }
        );
        /*
        * toolbar menu rollouts & overlay toggle
        */
        $$("#toolBar li.actuator").each(
                function(element, i) {
                        var box = $(element.id + "Box");
                        if ($defined(box)) {
                                box.defaultHeight = box.getSize()["size"].y - 6;
                                box.setStyle("height", 0);
                        }
                        box = null;
                        element.addEvent("mouseenter", function() {
                                if (sti_global["menu_inputActive"])
                                        return;
                                sti_global["menu_activeMenu"] = this;
                                var parent = this.getParent();
                                var link = $E("a", this);
                                $("overlay_toolbar").toggle();
                                if ($defined(parent.fx))
                                        parent.fx.stop();
                                var collection = $$("#toolBar li a.actuator");
                                collection.remove(link);
                                link.setStyles({
                                        "color": "black",
                                        "background-color": "#f2bb25"
                                });
                                parent.fx = new Fx.Elements(collection, {duration: sti_config["overlay_fadeDuration"]}).start({
                                        "0": { "color": "#FFFFFF" },
                                        "1": { "color": "#FFFFFF" },
                                        "2": { "color": "#FFFFFF" }
                                });
                                var box = $(this.id + "Box");
                                box.setStyles({
                                        "top": link.getSize()["size"].y,
                                        "visibility": "visible"
                                });
                                if ($defined(box.fx))
                                        box.fx.stop();
                                box.fx = new Fx.Style(box, "height", {duration: sti_config["menu_rolloutDuration"]}).start(box.defaultHeight);
                        });
                        element.addEvent("mouseleave", function() {
                                if (sti_global["menu_inputActive"])
                                        return;
                                sti_global["menu_activeMenu"] = null;
                                var parent = this.getParent();
                                var link = $E("a", this);
                                $("overlay_toolbar").toggle();
                                if ($defined(parent.fx))
                                        parent.fx.stop();
                                var collection = $$("#toolBar li a.actuator");
                                link.setStyles({
                                        "color": "#FFFFFF",
                                        "background-color": ""
                                });
                                parent.fx = new Fx.Elements(collection, {duration: sti_config["overlay_fadeDuration"]}).start({
                                        "0": { "color": "#98948f" },
                                        "1": { "color": "#98948f" },
                                        "2": { "color": "#98948f" },
                                        "3": { "color": "#98948f" }
                                }).chain(
                                        function() {
                                                collection.each(
                                                        function(element, i) {
                                                                element.setStyle("color", "");
                                                        }
                                                );
                                        }
                                );
                                var box = $(this.id + "Box");
                                if ($defined(box.fx))
                                        box.fx.stop();
                                box.fx = new Fx.Style(box, "height", {duration: sti_config["menu_rolloutDuration"]}).start(0).chain(
                                        function() {
                                                box.setStyle("visibility", "");
                                        }
                                );
                        });
                        element.addEvent("click", function() {
                                if (sti_global["menu_activeMenu"] != this) {
                                        sti_global["menu_activeMenu"].fireEvent("mouseleave");
                                        this.fireEvent("mouseenter");
                                }
                        });
                }
        );
        /*
        * inputs in menus, which disable menu collapse when focused
        */
        $$(".menuBox *").each(
                function(element, i) {
                        element.addEvent("focus", function() {
                                sti_global["menu_inputActive"] = true;
                        });
                        element.addEvent("blur", function() {
                                sti_global["menu_inputActive"] = false;
                        });
                }
        );
        /*
        * text input boxes, which remove default value on click
        */
        $$("input.defaultValue").each(
                function(element, i) {
                        element.addEvent("focus", function() {
                                if (!this.changed) this.value = "";
                        });
                        element.addEvent("keypress", function() {
                                if (!this.changed) {
                                        this.value = "";
                                        this.changed = true;
                                }
                        });
                        element.addEvent("blur", function() {
                                if (!this.changed || this.getValue() == "") {
                                        var classes = this.className.split(" ");
                                        classes.each(
                                                function(c, i) {
                                                        if (c.substr(0,13) == "defaultValue_") {
                                                                if ($defined(sti_config[c])) {
                                                                        element.value = sti_config[c];
                                                                }
                                                        }
                                                }
                                        );
                                        this.changed = false;
                                }
                        });
                }
        );
        /*
        * expandable menus, ported from expandable_menu.js, which used the Prototype library
        */
        $$("ul.dynamic li a.item").each(
                function(element, i) {
                        element.addEvent("click", function(e) {
                                new Event(e).stop();
                                this.getParent().toggleClass("active");
                        });
                }
        );
        /*
        * tabs
        */
        $$(".js_tabSwitch").each(
                function(element, i) {
                        element.addEvent("click", function() {
                                element.className = element.className.replace("TSTabSel", "TSTab");
                                element.className = element.className.replace("TSTab", "TSTabSel");
                                $(element.id + "Contents").setStyle("display", "block");
                                $$(".js_tabSwitch").each(
                                        function(item, n) {
                                                if (item != element) {
                                                        item.className = item.className.replace("TSTabSel", "TSTab");
                                                        $(item.id + "Contents").setStyle("display", "none");
                                                }
                                        }
                                );
                        });
                }
        );
        /*
        * column height normalizer, ported from column2.js, which used the Prototype library (Event.observe)
        */
        function height_normalizer() {
                var divs = $ES("#middle_column, #right_column, #left_column");
                if (!$defined(divs))
                        return false;
                var tallestHeight = 0;
                divs.each(
                        function(element, i) {
                                if (element.offsetHeight > tallestHeight)
                                        tallestHeight = element.offsetHeight;
                        }
                );
                divs.each(
                        function(element, i) {
                                var box                 = $E(".box", element);
                                var box_top             = $E(".box_top", element);
                                var box_mid             = $E(".box_mid", element);
                                var box_end             = $E(".box_end", element);
                                var newHeight   = 0;
                                if ($defined(box) && $defined(box_top) && $defined(box_mid) && $defined(box_end)) {
                                        newHeight = tallestHeight - box_top.offsetHeight - box_end.offsetHeight - box.offsetTop;
                                        if (window.ie6)
                                                box_mid.style.height = newHeight + "px";
                                        else
                                                box_mid.style.minHeight = newHeight + "px";
                                        if (box_mid.offsetHeight > newHeight) {
                                                if (window.ie6)
                                                        box_mid.style.height = (newHeight - (box_mid.offsetHeight - newHeight)) + 'px';
                                                else
                                                        box_mid.style.minHeight = (newHeight - (box_mid.offsetHeight - newHeight)) + 'px';
                                        }
                                }
                        }
                );
        }
        height_normalizer();
}
