if( typeof jQuery == 'function' ) // Checks to be sure jQuery is loaded
{
    jQuery(function($){ //  // Wordpress loads jQuery in no conflict mode, this allow you to use the $ method of calling jQuery, this is shorthand for $(document).ready(function()

        var menuDropDownStyle = {
            init: function() {
                this.bind();
            },
            bind: function() {
                $('#menu li ul a').not(':first').mouseover(function() {
                    var self = $(this);
                    var parentLi = self.parent();
                    var parentLiPosition = parentLi.position();
                    var parentUl = self.parents("ul");
                    var parentUlHeight = parentUl.height();
                    
                    self.siblings('ul').css({
                        'top' : - parentLiPosition.top,
                        'left' : 230
                    });
                    
                    if (self.siblings('ul').height() > parentUlHeight) {
                        self.parents("ul").css("height", self.siblings('ul').height());
                    } else {
                        self.siblings("ul").css("height", parentUlHeight);
                    };
                });
                
                /*$('#menu li ul a').not(':first').mouseout(function() {
                    var self = $(this);
                    var parentLi = self.parent();
                    var parentLiPosition = parentLi.position();
                    var parentUl = self.parents("ul");
                    var parentUlHeight = parentUl.height();
                    
                    self.siblings("ul").css("height", "auto");
                    self.parents("ul").css("height", "auto");
                });*/
            }
        }

        var customSelectStyle = {
            init: function() {
                this.bind();
            },
            bind: function() {
                if (!$.browser.opera) {
                    $('select.select').each(function(){
                        var title = $(this).attr('title');
                        if ( $('option:selected', this).val() != '' ) title = $('option:selected',this).text();
                        if ( $('option:selected', this).val() == '' ) title = 'Geographical Market';
                        $(this)
                            .css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
                            .after('<span class="select">' + title + '</span>')
                            .change(function(){
                                val = $('option:selected',this).text();
                                $(this).next().text(val);
                            })
                    });

                };
            }
        }

        // needed as long as the wordpress menu is created manually
        var hightlightCurrentPage = {
            init: function() {
                var loc = window.location.href;
                $("#menu-item-23620 a").each(function() {
                    if(this.href == loc) {
                        $(this).parent().addClass('current-menu-item');
                    }
                });
            }
        }

        var callouts = {
            init: function() {
                this.bind();
                delete this.init;
            },
            bind: function() {
                $boxes = $('.callout-box');
                $boxes.bind('mouseenter', showCallout);
                $boxes.bind('mouseleave', hideCallout);

                function showCallout(e) {
                    $box = $(this);
                    $box.css('overflow', 'visible');
                    $box.find('.mode-flyout').show();
                    $box.find('.callout-background').show();
                    $box.css('z-index', '99999');
                }

                function hideCallout(e) {
                    $box = $(this);
                    $box.css('overflow', 'hidden');
                    $box.find('.mode-flyout').hide();
                    $box.find('.callout-background').hide();
                    $box.css('z-index', '1');
                }
                
                $('.callout-close').bind('click', function(e) {
                    hideCallout.apply($(this).closest('.callout-box'), [e]);
                });
                
            }
        };
        
        var largeListClear = {
            init: function() {
                $(".largelist li:odd").each(function() {
                    var self = $(this);
                    self.after("<div class='clearing-div'>");
                });
                $(".largelist-alt li:nth-child(3n)").each(function() {
                    var self = $(this);
                    self.after("<div class='clearing-div'>");
                });
            }
        }

        var largeListAltClear = {
            init: function() {
                $(".largelistalt li:nth-child(3n)").each(function() {
                    var self = $(this);
                    self.after("<div class='clearing-div'>");
                });
            }
        }
        
        var industriesRollover = {
            init: function() {
                this.bind();
            },
            bind: function() {
                $(".industries-served- .pagetitle").css("opacity", 0.9);
                $(".section-industries-content-overlay").css("opacity", 0.8);
                
                $(".section-industries-img").mouseenter(function() {
                    var self = $(this);
                    var hiddenContent = self.parents(".section-industries-wrap").find(".section-industries-content-wrap");
                    
                    if (hiddenContent.is(":visible")) {
                    } else {
                        $(".section-industries-content-wrap").hide();
                        hiddenContent.show();
                    };
                });
                
                $(".section-industries-content-wrap").mouseleave(function() {
                    var self = $(this);
                    self.hide();
                    
                    if (self.is(":visible")) {
                        self.hide();
                    } else {
                    };
                });
            }
        }
        
        $("#tabs").tabs();
        
        var accordians = {
            init: function() {
                this.bind();
            },
            bind: function() {
                $('.productinfo').accordion({
                    autoheight:false,
                    header:"a.toggle",
                    collapsible:true,
                    active:".productinfo",
                    autoHeight: false,
                    alwaysOpen: false,
                    icons: { 'header': 'toggle', 'headerSelected': 'selected' }
                });
        
                $('.sub-productinfo').accordion({
                    autoheight:false,
                    header:"a.toggle-sub",
                    collapsible:true,
                    active:".productinfo",
                    alwaysOpen: false,
                    icons: { 'header': 'toggle', 'headerSelected': 'selected' }
                });
            }
        }
        
        var verticalCarousel = {
            init: function() {
                this.bind();
            },
            bind: function() {
                $('#myverticalcarousel').jcarousel({
                    vertical: true,
                      auto: 0,
                    scroll: 2,
                    wrap: 'last',
                    initCallback: mycarousel_initCallback
                });
                $('#applications').jScrollPane({showArrows:true});
                $('#term-list-markets').jScrollPane({showArrows:true});
                $('#term-list-region').jScrollPane({showArrows:true});
                $('#term-list-brands').jScrollPane({showArrows:true});
                $('#term-list-product-type').jScrollPane({showArrows:true});
            }
        };
        
        menuDropDownStyle.init();
        customSelectStyle.init();
        hightlightCurrentPage.init();
        callouts.init();
        largeListClear.init();
        industriesRollover.init();
        accordians.init();
        verticalCarousel.init();
    });
}

