(function ($) {
	
	$.readyToWearList = function (options) {
		return $.readyToWearList.impl.init(options);
	};
	
	$.fn.readyToWearList = function (options) {
		return $.readyToWearList.impl.init(this, options);
	};
	
	/*
	 * default options
	 */
	$.readyToWearList.defaults = {
	    pageSize:   6,
	    pageIndex:  0
	};
	
	$.readyToWearList.impl = {
		
		/*
		 * options
		 */
		opts:           null,
		
		/*
		 * helper
		 */
		helper:         {},
		
		/*
		 * Initializes the page
		 */
		init: function (options) {
            
            var self = this;
            
            this.opts = $.extend({}, $.readyToWearList.defaults, options);
            
            // helper objects
            self.helper.container = $('div.page-readytowearlist');
            self.helper.list = self.helper.container.find('ul.rtwitem-collection');
            self.helper.pages = $('<ul class="page-collection"></ul>');
            
            self.opts.length = self.helper.list.children().length;
            
            while (self.opts.length%self.opts.pageSize != 0)
            {
                var $dummy = $('<li class="rtwitem"></li>');
                
                self.helper.list.append($dummy);
                self.opts.length++
            }
            
            // initialize the carousel
            self.helper.list.jcarousel({
                scroll:                 self.opts.pageSize,
                animation:              0,
                buttonPrevHTML:         '<div></div>',
                buttonNextHTML:         '<div></div>',
                initCallback:           self.carouselInit,
                itemLastInCallback:     self.carouselIndexChanged
            });
            
            // bind the events
            self.bind();
            
			return self;
		},
		
		/*
		 * Binds the events
		 */
		bind: function () {
		    
		    var self = this;
		    
		    // hover
		    self.helper.list.find('a.rtwitem-link').hover(
		        function(){
		            
		            $(this).parent().siblings().find('img')
		                .queue('fx', [])
	                    .fadeTo(300, '0.40')
	                    ;
		        },
		        function(){
		            
		            $(this).parent().siblings().find('img')
		                .queue('fx', [])
	                    .fadeTo(300, '1.00')
	                    ;
		        }
		    );
		    
		    return;
		},
		
		/*
		 * Carousel init callback
		 */
		carouselInit: function (carousel, state) {
		    
		    var self = $.readyToWearList.impl;
		    var pageCount = Math.ceil(self.opts.length/self.opts.pageSize);
		    
		    for (var i = 0; i < pageCount; i++)
		    {
		        var $li = $('<li class="page"><a class="page-' + i + '" href="#">' + (i + 1) + '</a></li>');
		        
		        if (i == self.opts.pageIndex)
		            $li.addClass('page-active');
		        
		        self.helper.pages.append($li);
		    }
		    
		    self.helper.pages
		        .css({
		            left:       '50%',
		            marginLeft: -self.helper.pages.width()/2 + 'px'
		        })
		        ;
		    
		    self.helper.pages.find('a').click(function(e){
		        e.preventDefault();
		        
		        var p = $(this).attr('class').split('-')[1];
		        
		        if (p == self.opts.pageIndex)
		            return;
		        
		        self.page(p, carousel);
		        
		    });
		    
		    carousel.container.parent().append(self.helper.pages);
		    
		    // load the images
//		    self.helper.list.children()
//		        .each(function(i, item){
//		            
//		            var $img = $(item).find('img.rtwitem-image');
//		            
//		            $(new Image())
//		                .load(function(){
//		                    
//		                    $img
//		                        .fadeIn(1000)
//		                        ;
//		                    
//		                })
//		                .attr('src', $img.attr('src'))
//		                ;
//		            
//		        })
//		        ;
		    
		    // bind the tooltip
		    carousel.list.find('a.rtwitem-link').tooltip({
		        id:         'rtwitem-tooltip',
		        track:      true,
		        showURL:    false,
		        delay:      0,
		        bodyHandler: function() {
                    return $(this).siblings('.rtwitem-info').clone();
                }
		    });
		    
		    // bind next hover
		    carousel.buttonNext
		        .hover(
		            function(){$(this).addClass('jcarousel-next-hover')},
		            function(){$(this).removeClass('jcarousel-next-hover')}
		        )
		        .appendTo(self.helper.container.find('div.rtw-container'))
		        ;
		    
		    // bind prev hover
		    carousel.buttonPrev
		        .hover(
		            function(){$(this).addClass('jcarousel-prev-hover')},
		            function(){$(this).removeClass('jcarousel-prev-hover')}
		        )
		        .appendTo(self.helper.container.find('div.rtw-container'))
		        ;
		    
		    return;
		},
		
		/*
		 * Carousel index changed callback
		 */
		carouselIndexChanged: function (carousel, item, i, action) {
		    
		    var self = $.readyToWearList.impl;
		    
		    self.opts.pageIndex = i/self.opts.pageSize - 1;
		    
		    if (action == 'prev')
		    {
	            self.helper.pages.find('a.page-' + self.opts.pageIndex).parent()
                    .addClass('page-active')
                    .siblings()
                    .removeClass('page-active')
                    ;
		    }
		    else if (action == 'next')
		    {
                self.helper.pages.find('a.page-' + self.opts.pageIndex).parent()
                    .addClass('page-active')
                    .siblings()
                    .removeClass('page-active')
                    ;
		    }
		    
		    return;
		},
		
		/*
		 * Updates the current page
		 */
		page: function (p, carousel) {
		    
		    var self = this;
		    
		    self.opts.pageIndex = parseInt(p);
		    
		    var scroll = self.opts.pageIndex*self.opts.pageSize + 1;
		    
		    carousel.scroll(jQuery.jcarousel.intval(scroll));
		    
		    return;
		}
		
	};
})(jQuery);
