(function ($) {
	
	$.brandList = function (options) {
		return $.brandList.impl.init(options);
	};
	
	$.fn.brandList = function (options) {
		return $.brandList.impl.init(this, options);
	};
	
	/*
	 * default options
	 */
	$.brandList.defaults = {
	    
	};
	
	$.brandList.impl = {
		
		/*
		 * options
		 */
		opts:           null,
		
		/*
		 * helper
		 */
		helper:         {},
		
		/*
		 * Initialize the page
		 */
		init: function (options) {
            
            var self = this;
         
            this.opts = $.extend({}, $.brandList.defaults, options);
            
            // helpers
            self.helper.container = $('div.page-brandlist');
            self.helper.background = self.helper.container.find('ul.background-collection img');
            self.helper.aligner = self.helper.container.find('.aligner');
            self.helper.list = self.helper.container.find('ul.brand-collection');
            self.helper.campaign = self.helper.container.find('div.brand-campaign');
            
            // options
            self.opts.length = self.helper.list.children().length;
            
            // grid
            self.grid();
            
            // background
            //self.background();
            
			return self;
		},
		
		/*
		 * Background
		 */
		background: function () {
		    
		    var self = this;
		    
		    $(new Image()).load(function(){
                
                self.helper.background
                    .fadeIn(1000)
                    ;
                
            }).attr('src', self.helper.background.attr('src'));
		    
		    return;
		},
		
		/*
		 * Grid
		 */
		grid: function () {
		    
		    var self = $.brandList.impl;
		    var cellW;
            var cellH;
            var listW;
            var listH;
            var listOffset;
            var containerOffset;
            
            cellW = $(self.helper.list.children()[0]).width() + 2;
            cellH = $(self.helper.list.children()[0]).height() + 2;
            
            // append the "brand" cell if odd number of items
            if (self.opts.length%2 != 0 && self.opts.length != 15 || self.opts.length == 14)
            {
                // odd number of items, append the title block
                var $li = $('<li class="brandlist-title-container"><div class="brandlist-title-bg"></div></li>');
                var $title = self.helper.container.find('.brandlist-title');
                
                $li
                    .append($title)
                    .prependTo(self.helper.list)
                    ;
                
                // initialize cufon
                Cufon.replace($title, {
                    fontFamily: 'Helvetica Neue LT Std'
                });
                Cufon.now();
            }
            
            // size and position
            if (self.opts.length >= 13 && self.opts.length <= 15)
            {
                listW = 5*cellW;
                listH = 3*cellH;
            }
            else if (self.opts.length == 12 || self.opts.length == 11)
            {
                listW = 4*cellW;
                listH = 3*cellH;
            }
            else if (self.opts.length == 10 || self.opts.length == 9)
            {
                listW = 5*cellW;
                listH = 2*cellH;
            }
            else if (self.opts.length == 8 || self.opts.length == 7)
            {
                listW = 4*cellW;
                listH = 2*cellH;
            }
            else if (self.opts.length == 6 || self.opts.length == 5)
            {
                listW = 3*cellW;
                listH = 2*cellH;
            }
            
            self.helper.list
                .css({
                    width:      listW + 'px',
                    height:     listH + 'px'
                })
                ;
            
            // load
            self.load();
		    
		    // events
		    self.events();
		    
		    return;
		},
		
		/*
		 * Load
		 */
		load: function () {
		    
		    var self = this;
		    var count = self.helper.list.children(':not(".brandlist-title-container")').length;
		    var loaded = 0;
		    
		    var interval = setInterval(function(){
                
                if (loaded == count)
                {
                    clearInterval(interval);
                    
                    // show the grid
                    self.show();
                }
                
            }, 80);
		    
		    self.helper.list.children().each(function(i, item){
                
                if ($(item).hasClass('brandlist-title-container'))
                    return;
                
                $(item)
                    .prepend('<div class="brand-bg"></div>')
                    ;
                
                var src = $(item).find('img.brand-logo').attr('src');
                
                // load the images
                $(new Image()).load(function(){
                    
                    loaded++;
                    
                    self.fixIE(item);
                    
                }).attr('src', src);
                
            });
		    
		    return;
		},
		
		/*
		 * Show
		 */
		show: function () {
		    
		    var self = this;
		    var marginL = self.helper.list.width()/2 - self.helper.campaign.width() - 4;
            var marginT = self.helper.list.height()/2 + 10;
		    
		    self.helper.aligner
                .css({
                    display:    'none',
                    left:       '0'
                })
                ;
		    
            // campaign link
            self.helper.campaign
                .css({
                    display:    'block',
                    marginLeft: marginL + 'px',
                    marginTop:  marginT + 'px'
                })
                ;
		    
		    // IE6 HACK: this is redundant, dunno what's going on here
		    self.helper.list.find('.brand-logo')
                .css({
                    marginLeft: '-75px'
                })
                ;
		    
		    self.helper.aligner
                .fadeIn(1000, function(){
                    
                    // background
                    self.background();
                    
                })
                ;
		    
		    return;
		},
		
		/*
		 * Events
		 */
		events: function () {
		    
		    var self = this;
		    
		    // bind hover
		    self.helper.list.children('.brand')
		        .hover(
		            function () {
    		            
		                $(this).find('img.brand-image')
		                    .css({
		                        opacity:    '1.00'
		                    })
		                    .fadeIn(250)
		                    ;
    		            
		            },
		            function () {
    		            
		                $(this).find('img.brand-image')
		                    .queue('fx', [])
		                    .fadeOut(250)
		                    ;
    		            
		            }
		        )
		        .tooltip({
		            id:         'brand-tooltip',
		            track:      true,
		            showURL:    false,
		            delay:      0,
		            top:        30,
			        left:       30,
		            bodyHandler: function() {
                        return $(this).find('.brand-description').clone();
                    }
		            })
		        ;
		    
		    return;
		},
		
		/*
		 * Fix IE
		 */
		fixIE: function (item) {
		    
		    if (!$.browser.msie || ($.browser.version >= 7))
				return;
		    
		    var self = this;
		    var $logo = $(item).find('img.brand-logo');
		    
		    $logo 
		        .pngFix()
                ;
		    
		    return;
		}
		
	};
})(jQuery);
