/*
 *  Plugin of Simple Never End Spin Carousel
 *  ----------------------------------------
 *  12/03/2009 - Westfield 2009
 *  version: 0.5
 *  Author: Agus Daud
 *  Email: agusdaud@gmail.com
 *  ----------------------------------------
 *
 *  The simple mod of the current carousel
 *  by replacing the left pixel once it reach most left or right
 *  so it look like unlimited spinned.
 *  Note: for the countless spin the number item show need
 *  to be smaller then total number in carousel
 *
 */

(function($) {

    jQuery.fn.premiumCarousel = function (options) {

        var opts = jQuery.extend({}, jQuery.fn.premiumCarousel.defaults, options);

        // Core function
        return this.each(function() {

            var $this = jQuery(this);
            var o = jQuery.meta ? jQuery.extend({}, opts, $this.data()) : opts;

            var carouselSpinOriginalSize = $this.find("li:not(.border)").size();
            var carouselSpinShow = o.carouselItemShow;
            var carouselSpinWidth = o.carouselItemWidth;
            var carouselSpinValue = 0;
            var carouselMaxSize = carouselSpinOriginalSize - carouselSpinShow;
            var anime = false;

            if (o.carouselLoop == 'yes') {

                if (carouselSpinOriginalSize <= carouselSpinShow) {
                    alert('Never End Spin Carousel is not going to work the total number of item need to be bigger than total view item');
                }

                // Automatic insert the clone of other item to make the never end spinned trick works
                for (i=0; i <carouselSpinShow; i++) {
                    copyComp = $this.find("li:not(.border):eq("+i+")");
                    copyCompBorder = $this.find("li.border:eq("+i+")");
                    $this.find("li:last").after(copyComp.clone());
                    $this.find("li:last").after(copyCompBorder.clone());
                }

            }

            var carouselSpinSize =  $this.find("li:not(.border)").size();
            var carouselValue = 0;
            var maxSpinSize = carouselSpinSize - carouselSpinShow;

            $this.parent().css("width",carouselSpinShow * carouselSpinWidth-2);
            $this.css({width: (carouselSpinSize*carouselSpinWidth)+"px"});
            resetSpinCarousel = carouselSpinWidth * maxSpinSize ;

            // insert the left and right button
            $this.parent().parent().after('<div class="carousel-left-button" id="carousel-spin-left-button"><div class="alpha-container"></div><a href="#" class="carousel-button">left-button</a></div>');
            $this.parent().parent().after('<div class="carousel-right-button" id="carousel-spin-right-button"><div class="alpha-container"></div><a href="#" class="carousel-button">right-button</a></div>');

            if (o.carouselLoop == 'no') {
                jQuery.fn.premiumCarousel.hideCarouselButton(carouselMaxSize, carouselSpinWidth);
            }

            jQuery("#carousel-spin-right-button").click(function() {

                carouselSpinValue =  parseFloat($this.css("left"),10);
                if (o.carouselLoop != 'yes') {

                }
                if ((carouselSpinValue > (carouselSpinWidth * carouselMaxSize * -1)) && (anime == false) || (o.carouselLoop == 'yes' && anime == false)) {
                        anime = true;
                        $this.stop().animate({
                            left: "-=290px"
                        }, 700, function() {
                            anime = false;
                            carouselValue =  parseFloat($this.css("left"),10);
                            if (resetSpinCarousel+carouselValue == 0 && o.carouselLoop == 'yes') {
                               $this.css("left","0");
                            } else if (o.carouselLoop == 'no') {
                               jQuery.fn.premiumCarousel.hideCarouselButton(carouselMaxSize, carouselSpinWidth);

                            }
                        });

                }
                return false;
            });

            jQuery("#carousel-spin-left-button").click(function() {

                carouselSpinValue =  parseFloat($this.css("left"),10);
                if (carouselSpinValue == 0 && o.carouselLoop == 'yes') {
                    $this.css("left",0-resetSpinCarousel);
                }

                if (((carouselSpinValue < 0 ) && (anime == false)) || (o.carouselLoop == 'yes' && anime == false)) {
                        anime = true;
                        $this.stop().animate({
                            left: "+=290px"
                        }, 700, function() {
                            anime = false;
                            if (o.carouselLoop == 'no') {
                                jQuery.fn.premiumCarousel.hideCarouselButton(carouselMaxSize ,carouselSpinWidth);
                            }
                        });

                }

                return false;

             });

            if (o.carouselGrowth == 'yes') {
                $this.find("li .cont").hover(function() {
                    if (jQuery(this).parent().parent().attr("class") != "static") {
                        jQuery(this).parent().parent().find("div.carousel-container").stop().animate({
                            top: "0px"
                        }, 700);
                        jQuery(this).parent().find(".hover-image").stop().css({
                            display: "block"
                        }).fadeTo("slow", 1);
                    }
                },function() {
                    if (jQuery(this).parent().parent().attr("class") != "static") {
                        jQuery(this).parent().parent().find("div.carousel-container").stop().animate({
                            top: "80px"
                        }, 700);
                        jQuery(this).parent().find(".hover-image").stop().fadeTo("slow", 0);
                    }
                });
             }

        });

    };

    jQuery.fn.premiumCarousel.hideCarouselButton  = function(maxSize,carouselWidth) {

        carouselValue =  parseFloat(jQuery("#carousel-list").css("left"),10);

        if ((maxSize) == 0) {
            jQuery(".carousel-left-button a").hide();
            jQuery(".carousel-right-button a").hide();
        } else if ((carouselValue) == 0) {
            jQuery(".carousel-left-button a").hide();
            jQuery(".carousel-right-button a").show();
        } else if ((carouselValue) == (carouselWidth * maxSize * -1)) {
            jQuery(".carousel-right-button a").hide();
            jQuery(".carousel-left-button a").show();
        } else {
            jQuery(".carousel-right-button a").show();
            jQuery(".carousel-left-button a").show();
        }

        return true;

    };

    jQuery.fn.premiumCarousel.defaults = {
        carouselItemShow : 4,
        carouselItemWidth : 290,
        carouselLoop : 'yes',
        carouselGrowth: 'yes'
    };

})(jQuery);
