var Utils = {

    customerLoginShowHide : function() {

        var maxWidth = 0;


        var sizecheck = $$('#account-navigation .subHeaderMenu li a').each(function(item, index){

            var lisize = item.getSize().x;


            if(maxWidth < lisize || index === 0){

                maxWidth = lisize;
            }

        });

        $$('#account-navigation .subHeaderMenu li a') .setStyle('width', maxWidth);
        $$('#account-navigation .subHeaderMenu li span') .setStyle('width', maxWidth);
        $$('#account-navigation .subHeaderMenu li') .setStyle('width', maxWidth + 25);
        $$('#account-navigation .subHeaderMenu').setStyle('width', maxWidth + 50);


        Element.implement({
                fancyShow: function() {
                    this.fade('in');
                },
                fancyHide: function() {
                    this.fade('out');
                }
            }),

            $('customerLoginMenu').setStyle('opacity','0');
            $$('#customerLoginMenu a span').setStyle('opacity','0');

            $('login-link').addEvent('mouseenter', function(e){
                e = new Event(e);
                $('customerLoginMenu').setStyle('opacity','1');
                $$('#customerLoginMenu a span').fade("in");
                $('customerLogin').set('class', 'active');

                if (Browser.Engine.trident && !document.querySelectorAll) {
                    $$('#nav_primary') .setStyle('z-index', '-10');
                    $$('#country-select').setStyle('z-index','-10');
                }

                if (Browser.Engine.trident && Browser.Engine.version < 5 ){
                    $$('#languageSwitch') .setStyle('display', 'none');
                }

            e.stop();
            });

            $('customerLoginMenu').addEvent('mouseleave', function(e){
                e = new Event(e);
                $('customerLoginMenu').setStyle('opacity','0');
                $$('#customerLoginMenu a span').setStyle('opacity','0');
                $('customerLogin').set('class', '');

                if (Browser.Engine.trident && !document.querySelectorAll) {
                    $$('#nav_primary') .setStyle('z-index', '0');
                }

                if (Browser.Engine.trident && Browser.Engine.version < 5 ){
                    $$('#languageSwitch') .setStyle('display', 'block');
                }

            e.stop();

            });

            $('customerLogin').addEvent('mouseleave', function(e){
                e = new Event(e);
                $('customerLoginMenu').setStyle('opacity','0');
                $$('#customerLoginMenu a span').setStyle('opacity','0');
                $('customerLogin').set('class', '');

                if (Browser.Engine.trident && !document.querySelectorAll) {
                    $$('#nav_primary') .setStyle('z-index', '0');
                }        

                if (Browser.Engine.trident && Browser.Engine.version < 5 ){
                    $$('#languageSwitch') .setStyle('display', 'block');
                }

            e.stop();

            });
    },


    replaceAnchors : function () {

       var anchors = $$("a");

       anchors.each(function(item,index){

           if (item.getProperty("href").test("#")){


               var urlString =  item.getProperty("href").split('#')[1];

               item.addEvent('click', function(e){

                   e.stop();

                   document.location.hash = '#' + urlString;

               });



           }

       });


   },



    // id = the tab to display
    // tabs = array with all the tabs
    // extraClass = to add to the active tab

    showHideTabs : function (id, tabs,extraClass) {

        //Make all the tabs nonactive

        for (var i = 0; i < tabs.length; i++) {
            if (document.getElementById(tabs[i] + "Area") != null) {
                document.getElementById(tabs[i] + "Area").className = "none";
                document.getElementById(tabs[i] + "Button").className = "";
            }

        }

        if (extraClass != null && extraClass != "") {
            document.getElementById(id + "Area").className = "block " + extraClass;

        } else {
            document.getElementById(id + "Area").className = "block";
        }

        document.getElementById(id + "Button").className = "active";

    },

    changeLocale : function (id) {

        var value = document.getElementById(id).value;
        if (value != "select") {
            window.location = value;
        }

    },

    navTimeout : null,

    sfHide : function() {

        this.removeClass("sfhover").removeClass("active");

    },

    sfHover : function() {

        if (Browser.Engine.trident && Browser.Engine.version < 5 ) {

            var sfEls = $$('#nav_primary li');

            var sfTimer;

            sfEls.each( function(item) {

                item.addEvents({

                    'mouseenter' : function(e) {

                        item.addClass("sfhover").addClass("active");

                    },

                    'mouseleave' : function(event) {

                        item.removeClass("sfhover").removeClass("active");
                        event.stopPropagation();
                    }

                });


                var ulElm = undefined;

                if (item.getChildren('ul').length > 0) {

                    ulElm = item.getChildren('ul')[0];

                    ulElm.addEvents( {

                        'mouseover' : function(event) {

                            sfTimer= $clear(sfTimer);

                            item.addClass("sfhover").addClass("active");

                            sfTimer = Utils.sfHide.delay(3000,item);



                        }

                    });

                }

            });

        }

    },

    /*highlightNavParent : function () {
        $$('#nav_primary li ul').each(function(item) {
            item.addEvents({
                'mouseover': function() {
                    if (Utils.navTimeout) $clear(Utils.navTimeout);
                    this.getParent().addClass("active");
                }.bind(item),
                'mouseout' : function() {
                    Utils.navTimeout = Utils.highlightTimeout.delay(2000, item);
                }.bind(item)
            });
        });
    },


    highlightTimeout : function() {
        this.getParent().removeClass("sfhover").removeClass("active");
        Utils.navTimeout = null;
        if (Browser.Engine.trident4 && $("region-ie6")) $("region-ie6").setStyle("visibility", "visible");
    },*/

    heightHackShonk : function (className) {

        var shonkArray = $$("a."+className)

        for ( var i = 0 ; i < shonkArray.length ; i++ ) {

            var thisElm = shonkArray[i];
            var previousElm = thisElm.getPrevious();

            thisElm.setStyle('top', previousElm.getStyle('height').toInt()-25 );
            thisElm.setStyle('display', 'inline' );

        }


    },

    submitHelpForm : function (form) {
        var selectHelpForm = $(form);
        var helpTopic = $('helpTopic');
        var value = helpTopic.options[helpTopic.options.selectedIndex].value;
        if (value.length > 0) {
            selectHelpForm.action = value;
            selectHelpForm.submit();
        }
        return false;

    },

    tabNavFix : function () {

        var navElms = $$('li.tabNavElm');

        for ( var i = 0 ; i < navElms.length ; i++ ) {

            navElms[i].addEvents({

                'mouseover': function() {

                    if ( Browser.Engine.trident && Browser.Engine.version == 4 ) {

                        this.setStyles({
                            background: 'url( ../images/productnavbar-left.gif) no-repeat top left transparent'
                        });

                        this.getChildren('a').setStyles({
                            background: 'url(../images/productnavbar-right.gif) no-repeat top right transparent'
                        })

                    }


                },

                'mouseout' : function() {

                    if ( !this.hasClass("active")) {

                        if ( Browser.Engine.trident && Browser.Engine.version == 4 ) {

                            this.setStyles({

                                background: 'url(../images/productnavbar-leftInactive.gif) no-repeat top left transparent'
                            });

                            this.getChildren('a').setStyles({
                                background: 'url(../images/productnavbar-rightInactive.gif) no-repeat top right transparent'
                            })

                        }

                    }
                },

                'click' : function(event) {

                    event.stop();

                    if ( typeof inlineTabArray != 'undefined'  ) {

                        var tabArray = inlineTabArray;
                        var tabElm = this.getChildren('a')[0].getProperty('id');

                        if ( Browser.Engine.trident && Browser.Engine.version == 4 ) {

                            for ( var j = 0 ; j < tabArray.length ; j++ ) {

                                $(tabArray[j]).setStyles({
                                    background: 'none'
                                });

                                $(tabArray[j]).getParent().setStyles({
                                    background: 'none'
                                });
                            }


                            $(tabElm).getParent().setStyles({
                                background: 'url( ../images/productnavbar-left.gif) no-repeat top left transparent'
                            });


                            $(tabElm).setStyles({
                                background: 'url(../images/productnavbar-right.gif) no-repeat top right transparent'
                            });

                        }


                        Utils.showHideTabs( tabElm, tabArray ,'tabArea');

                    }

                }

            });

        }

    },

    multiBoxCreator: {
        mbInstances: {},

        findParentAndAdd: function(parent, el, index) {
            if (parent.retrieve('mbParentId')) {
                p = this.mbInstances[parent.retrieve('mbParentId')].children.push(el);
            } else {
                parent.store('mbParentId', 'mbParentId-' + index);
                this.mbInstances['mbParentId-' + index] = {parent:parent, children: [el]};
            }
        },

        findMultiBoxes: function() {
            $$('.lightboxItem').each(function(el, index){
                var groupParent = el.getParent('.lightboxGroup');
                groupParent ? this.findParentAndAdd(groupParent, el, index):this.mbInstances['mbParentId-' + index] = {children: [el]};
            }, this);

            for (var x in this.mbInstances) {
                var o = this.mbInstances[x];
                for (var y = 0; y < o.children.length; y++) {
                    o.children[y].addClass('lightboxItem' + x);
                }
                new MultiBox('lightboxItem' + x, {useOverlay: true, ajax:true});
            }
        }
    }
};
