(function ($) {
	
	$.discover = function (options) {
		return $.discover.impl.init(options);
	};
	
	$.fn.discover = function (options) {
		return $.discover.impl.init(this, options);
	};
	
	/*
	 * default options
	 */
	$.discover.defaults = {
	    
	};
	
	$.discover.impl = {
		
		/*
		 * options
		 */
		opts:           null,
		
		/*
		 * helper
		 */
		helper:         {},
		
		/*
		 * Initialize the page
		 */
		init: function (options) {
            
            var self = this;
            
            this.opts = $.extend({}, $.discover.defaults, options);
            
            // helpers
            self.helper.container = $('div.page-discover');
            self.helper.background = self.helper.container.find('ul.background-collection img');
            self.helper.list = self.helper.container.find('ul.griditem-collection');
            
            // cufon
            Cufon.replace(self.helper.list.find('.griditem-title'), {
                fontFamily: 'Helvetica Neue LT Std'
            });
            Cufon.now();
            
            // show the background
            $(new Image()).load(function(){
                
                self.helper.background
                    .fadeIn(1000, self.grid)
                    ;
                
            }).attr('src', self.helper.background.attr('src'));
            
			return self;
		},
		
		/*
		 * Grid
		 */
		grid: function () {
		    
		    var self = $.discover.impl;
		    
		    self.helper.list.children().each(function(i, item){
                
                var $img = $(item).find('.griditem-image');
                var $bg = $(item).find('.griditem-bg');
                
                $img
                    .insertAfter(self.helper.background)
                    ;
                
                $bg
                    .animate({
                        backgroundColor:    '#fff',
                        opacity:            '0.10'
                    }, 1)
                    ;
                
                $(new Image()).load(function(){
                    
                    $(item)
                        .hover(
                            function(){
                                
                                $bg
                                    .animate({
                                        backgroundColor:    '#000',
                                        opacity:            '0.50'
                                    }, 500)
                                    ;
                                
                                $img
                                    .css({
                                        opacity:            '1.00'
                                    })
                                    .fadeIn(500)
                                    ;
                                
                            },
                            function(){
                                
                                $bg
                                    .queue('fx', [])
                                    .animate({
                                        backgroundColor:    '#fff',
                                        opacity:            '0.10'
                                    }, 500)
                                    ;
                                
                                $img
                                    .queue('fx', [])
                                    .fadeOut(500)
                                    ;
                                
                            }
                        )
                        .click(function(e){
                            e.preventDefault()
                            
                            window.location = $(this).find('a').attr('href');
                        })
                        ;
                    
                }).attr('src', $img.attr('src'));
                
            });
		    
		    return;
		},
		
		/*
		 * Events
		 */
		events: function () {
		    
		    var self = $.discover.impl;
		    
		    self.helper.list.find('.griditem-link').hover(
		        function(){
		            
		        },
		        function(){
		            
		        }
		    );
		    
		    return;
		}
		
	};
})(jQuery);
