(function($) {

    $.readyToWearDetail = function(options) {
        return $.readyToWearDetail.impl.init(options);
    };

    $.fn.readyToWearDetail = function(options) {
        return $.readyToWearDetail.impl.init(this, options);
    };

    /*
    * default options
    */
    $.readyToWearDetail.defaults = {
        pageSize: 6,
        pageIndex: 0
    };

    $.readyToWearDetail.impl = {

        /*
        * options
        */
        opts: null,

        /*
        * helper
        */
        helper: {},

        /*
        * Initializes the page
        */
        init: function(options) {

            var self = this;

            this.opts = $.extend({}, $.readyToWearDetail.defaults, options);

            // helper objects
            self.helper.container = $('div.page-readytoweardetail');
            self.helper.list = self.helper.container.find('ul.rtwitem-collection');
            self.helper.image = self.helper.container.find('div.rtw-imagery img');

            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
            });

            // show the image
            $(new Image()).load(function() {

                self.helper.image
                    .fadeIn(1000)
                    ;

            }).attr('src', self.helper.image.attr('src'));

            return self;
        },

        /*
        * Carousel init callback
        */
        carouselInit: function(carousel, state) {

            var self = $.readyToWearDetail.impl;

            self.helper.container.find('.rtw-items')
		        .css({
		            display: 'none',
		            position: 'relative',
		            left: 'auto'
		        })
		        .fadeIn(500)
		        ;

            // 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')
	                    ;
		        }
		    );

            // 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-items'))
		        ;

            // 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-items'))
		        ;

            return;
        }

    };
})(jQuery);
