MO.Pullldown = function($root){
    var self = this;
    this.categories = $('#categories');
    this.isOpen = false;
    this.activeCat = $('#container').attr('data-category');
    this.activeSubCat = $('#container').attr('data-subcategory');

    $('#categories .item h5').hide();

    if (!this.activeCat) {
        this.activeCat = 'skis';
    };

    if(!this.activeSubCat) {
        this.activeSubCat = 'jibber';
    }

    this.activeCatIndex = $('#category_'+this.activeCat).index('#categories_wrap > div');
    this.activeSubCatIndex = $('.cat_nav [data-subcategory='+this.activeSubCat+']').index('#category_' + this.activeCat + ' .cat_nav > li');

    $('#categories_wrap .category').css('width', $(window).width());

    // When you hover over a ski, fade it out and pop it up.
    $('#categories_wrap .item').hover(function(){
        $(this).find('h5').stop(true).show();
        $(this).stop(true).animate({
            top: '-5px'
        });

        if(!$('html').hasClass('csstransitions')){
            $(this).find('img').stop(true).animate({
                opacity: '.1'
            });            
        }

    }, function(){
        $(this).find('h5').stop(true).hide();
        $(this).stop(true).animate({
            top: '0px'
        });

        if(!$('html').hasClass('csstransitions')){
            $(this).find('img').stop(true).animate({
                opacity: '1'
            });            
        }

    });


    // Remove the child categories so the tabs plugin doesn't get confused.
    $('#menu_container ul ul').remove();
    $('#menu_container > .inner > ul').tabs('#categories_wrap > div', {
        effect: 'fade'
    });

    // Make the category of the product you're viewing active
    this.tabsApi = $("#menu_container > .inner > ul").data("tabs");
    this.tabsApi.click(this.activeCatIndex);


    // Opens the dropdown
    this.open = function(){
        // this.categories.clearQueue();
        this.categories.animate({
            height: 'toggle'
        }, function(){
            self.categories.clearQueue();
        });

        this.isOpen = true;
    };


    // Closes the dropdown
    this.close = function(){
            this.categories.animate({
                height: 'toggle'
            }, function(){
                self.categories.clearQueue();
            });

            this.isOpen = false;
    };


    $.easing.expo = function (x, t, b, c, d) {
        return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
    };


    // add new effect to the tabs
    jQuery.tools.tabs.addEffect("slide", function(i, done) {
        var leftOffset = $('body').width() * (i);

        $('.category_outer:visible .category_wrap').stop(true).animate({
            left: '-'+leftOffset
        }, 500, 'expo');
    });


    $('#nav_products a, #category_pulldown h4').click(function(){
        if(self.isOpen){
            self.close();
        } else{
            self.open();            
        }
        
        return false; 
    });


    // Disable forward/backward link event bubbling
    this.categories.find('#cat_nav_wrap .forward, #cat_nav_wrap .backward').click(function(){
        return false;
    });


    // Create a tabbed interface for subcategories
    this.categories.find(".category_outer").each(function(){

        var elemId = $(this).attr('id');
        $(this).find(".cat_nav").tabs("#" + elemId + " .category_wrap > div", {
            effect: 'slide',
            circular: true,
            rotate: true

        }).slideshow({
            clickable: false,
            circular: true,
            rotate: true
        });

    });



    // set the active subcategory
    $('#category_'+self.activeCat+' .cat_nav a:eq('+self.activeSubCatIndex+')').click();
    

    // when the window gets resized, resize the subcategory divs
    $(window).resize(function(){
        var bodyWidth = $('body').width();
        if( bodyWidth < 970){
            bodyWidth = 970;
        }

        self.activeSubCatIndex = $('#categories_wrap > div:visible').find('.cat_nav .current').parent().index('#categories_wrap .category_outer:visible .cat_nav li');

        $('#categories_wrap .category').css('width', bodyWidth);
        $('.category_outer:visible .category_wrap').css('left', (bodyWidth * self.activeSubCatIndex) * -1);
    });


};
