trellum = {
    init : function() {
        this.tooltip();
        
        // Load <body> class name and load views.
        this.views._global.call(this);
        var classNames = document.body.className.split(" ");
        for (var i=0; classNames[i]; i++) {
            if (this.views[classNames[i]]) {
                this.views[classNames[i]].call(this);
            }
        }
    },
    views : {

        _global : function() {
            
        },
        home : function() {
            
            $(".market").click(function() {
              
               window.location =  $(this).find(".cover").attr("href");
                
            });
            
        },
        product : function() {
            
            this.productNav();
            this.gallery();
            this.productSlider.init();
            
        },
        flashbg : function() {
            
        }
    },
    productSlider : {
        
        setProperties : function() {
            
            this.filterItems();
            this.steps = Math.ceil(this.itemContainer.find(".item:visible").length / 8);
            this.navLeft = $("#nav-left").addClass("inactive");
            if((this.steps - this.currentStep) < 2) {
                this.navRight.addClass("inactive");
            }
            else {
                this.navRight.removeClass("inactive");
            }
            if(this.currentStep > 0 ) {
                this.navLeft.removeClass("inactive");
            }
            $.cookie("genderFilter", this.genderFilter, { path: '/', expires: 10 });
            $.cookie("collectionFilter", this.collectionFilter, { path: '/', expires: 10 });
            this.position();
        },
        filterItems : function() {
            var items = this.itemContainer.find(".item").hide();
            items.filter("."+ this.genderFilter +"."+ this.collectionFilter).show();
            items.filter(".U."+ this.collectionFilter).show();
            if(this.genderFilter == "U") {
              items.filter(".F."+ this.collectionFilter).show();  
              items.filter(".M."+ this.collectionFilter).show();
            }
        },  
        position : function() {
            var distance = this.currentStep * this.distance;
            this.itemContainer.css("margin-left","-" + distance + "px");
        },
        init : function () {

            this.speed = 500
            this.distance = 888
            this.itemContainer = $("#slider-items");
            this.navRight = $("#nav-right");
            this.genderFilter = $.cookie('genderFilter') || "U"
            this.collectionFilter = $.cookie('collectionFilter') || "A";
            this.currentStep = $.cookie('currentStep') || 0
            this.filterButtons = $("#slide-filter a");
            this.filterButtons.filter("."+this.genderFilter).addClass("active");
            this.collectionNav = $("#product-nav a");
            var currentCollection = this.collectionNav.filter("."+this.collectionFilter);
            currentCollection.filter("."+this.collectionFilter).addClass("active").parent().prev("h2").text(currentCollection.text());

            this.setProperties();
            var proxy = this;
            this.navLeft.bind("click", function() {
                if(proxy.currentStep > 0) {
                    proxy.currentStep--;
                    if((proxy.currentStep - 1) < 0) {
                        $(this).addClass("inactive");
                    }
                    proxy.itemContainer.animate({
                       marginLeft: '+=' + proxy.distance 
                       }, 
                       proxy.speed
                    );
                    proxy.navRight.removeClass("inactive")
                }
                $.cookie("currentStep", proxy.currentStep, { path: '/', expires: 10 });
            });
            this.navRight.bind("click", function() {
                if($(this).hasClass("inactive")) {
                    return
                }
                proxy.currentStep++;
                if((proxy.currentStep + 1) == proxy.steps) {
                    $(this).addClass("inactive");
                }
                proxy.itemContainer.animate({
                    marginLeft: '-=' + proxy.distance
                    }, 
                    proxy.speed
                );
                proxy.navLeft.removeClass("inactive")
                $.cookie("currentStep", proxy.currentStep, { path: '/', expires: 10 });
            });
            this.filterButtons.bind("click", function(e) {
                e.preventDefault();
                proxy.filterButtons.removeClass("active");
                proxy.genderFilter = this.className;
                $(this).addClass("active");
                proxy.currentStep = 0
                $.cookie("currentStep", proxy.currentStep, { path: '/', expires: 10 });
                proxy.setProperties();
            });
            this.collectionNav.bind("click", function(e) {
                e.preventDefault();
                proxy.collectionNav.removeClass("active");
                proxy.collectionFilter = this.className;
                $(this).parent().prev("h2").text($(this).text());
                $(this).addClass("active");
                proxy.currentStep = 0
                $.cookie("currentStep", proxy.currentStep, { path: '/', expires: 10 });
                proxy.setProperties();
            });
        }
    },
    gallery : function() {
        
        var items = $("#gallery-items");
        var images = items.find(".full");
        var desc = items.find(".desc");
        var canvas = $("#gallery-canvas").append(desc).append(images);
        items.find(".item:first-child").addClass("active");
        canvas.find("img:first, .desc:first").show();
        items.bind("click", function(e) {
            if($(e.target).is("img")) {
                if(!$(e.target).parent().hasClass("active")) {
                    items.children().removeClass("active");
                    images.hide();
                    desc.hide();
                    $(e.target).parent().addClass("active");
                    var i = $(items.find(".thumb")).index(e.target);
                    $(images[i]).fadeIn(800);
                    $(desc[i]).show();
                }
            }
        });
    },
    tooltip : function() {
        var items = $(".tooltip .item");
        $.each(items, function() {
            var obj = $(this),
                h = obj.find("h2"),
                i = obj.find("img.zoom");
            
            obj.bind("mouseenter", function() {
                i.stop(true, true).fadeIn(1000);
                h.stop(true, true).fadeIn(500);
            });     
            obj.bind("mouseleave", function() {
                i.stop(true, true).fadeOut(500);
                h.stop(true, true).fadeOut(50);
            });
        });
    },
    productNav : function() {
      
      var label = $("#product-nav h2");
      label.bind("click", function() {
          $(this).next("div.drop").fadeIn(500);
      });
      $("#product-nav div").bind("mouseleave", function(){
          
          $(this).find("div.drop").fadeOut(50);
      });
    }
}