/**
 * @author Marco Rogers
 *
 *  * Built on top of the jQuery library
 *   http://jquery.com
 *
 */

(function($) {
    /**
     * Creates a carousel for all matched elements.
     *
     * @name jcarousel
     * @type jQuery
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/jCarousel
     * 
     * This is a jQuery plugin: http://docs.jquery.com/Plugins/Authoring
     * It is based on the jCarousel jquery plugin: http://sorgalla.com/projects/jcarousel/
     * 
     * All of the code is wrapped in a self executing function to give it a private scope so that
     * the jQuery $ will not collide with $ keywords from other libraries.
     */
    $.fn.cfc_carousel = function(o) {
        return this.each(function() {
            new $.cfc_carousel(this, o);
        });
    };

    // Default configuration properties.
    var defaults = {
    };

	// 
    $.cfc_carousel = function(e, o) {
        this.list = $(e).find('li');

        if ($.browser.safari) {
            $(window).bind('load', function() { self.setup(); });
        } else
            this.setup(o);
    };

    // Create shortcut for internal use
    var $cc = $.cfc_carousel;

    $cc.fn = $cc.prototype = {
        jcarousel: '0.2.3'
    };

    $cc.fn.extend = $cc.extend = $.extend;

    $cc.fn.extend({
                       /**
                        * Setups the carousel.
                        *
                        * @name setup
                        * @type undefined
                        * @cat Plugins/jCarousel
                        */
                       setup: function(opts) {
                           if(this.list.length < 2) return;

                           this.timer = null;
                           this.auto = 10;
                           this.ctrls = $('#c-carouselCtrls');
                           this.ctrlsImg = this.ctrls.children('img').get(0);
                           this.pauseCtrl = this.ctrlsImg.src;
                           this.playCtrl = this.pauseCtrl.replace('pause', 'play');
                           (new Image()).src = this.playCtrl;
                           this.ctrls = this.ctrls.get(0);
                           this.locked = false;
                           this.cur = 0;
                           this.next = this.cur+1;
                           this.prev = this.list.length-1;

                           this.onFirst = true;
                           this.onLast = false;

                           if(opts && opts.initCallback) opts.initCallback(this);
                           var self = this;
                           setTimeout(function() {
                                          self.startAuto();
                                      }, this.auto * 1000);
                       },

                       goNext: function() {
                           //this.stopAuto();

                           var from = this.list[this.cur];
                           if (this.cur == (this.list.length-1)) {
                               this.prev = this.cur;
                               this.cur = 0;
                               this.next = this.cur+1;
                           } else {
                               this.prev = this.cur;
                               this.cur = this.next;
                               this.next = (this.next==(this.list.length-1)) ? 0 : this.next+1;
                           }
                           var to = this.list[this.cur];

                           this.animate(from, to);
                       },

                       goPrev: function() {
                           //this.stopAuto();

                           var from = this.list[this.cur];
                           if (this.cur == 0) {
                               this.cur = this.prev;
                               this.prev = this.prev-1;
                               this.next = 0;
                           } else {
                               this.next = this.cur;
                               this.cur = this.prev;
                               this.prev = (this.prev==0) ? this.list.length-1 : this.cur-1;
                           }
                           var to = this.list[this.cur];

                           this.animate(from, to);
                       },

                       animate: function(from, to) {
                           var self = this;
                           $(from).fadeOut(this.speed,
                                           function() {
                                               $(to).fadeIn( self.speed );
                                           });

                       },

                       autoAnimate: function() {
                           var self = this;
                           this.goNext();
                           this.timer = setTimeout(function(){
                                                       self.autoAnimate();
                                                   }, this.auto * 1000);
                       },

                       startAuto: function(s) {
                           var self = this;
                           if(!this.timer) {
                               this.timer = setTimeout(function() {
                                                           self.autoAnimate();
                                                       }, 100);
                           }
                           this.ctrlsImg.src = this.pauseCtrl;
                       },

                       stopAuto: function() {
                           if (this.timer == null)
                               return;

                           clearTimeout(this.timer);
                           this.timer = null;
                           this.ctrlsImg.src = this.playCtrl;
                       }
                   });


})(jQuery);

//start carousel on page load and bind actions to page events
$(function(){
    $('#c-carousel ul.c-carouselList').cfc_carousel({
        initCallback: function(c){
            $('#c-pauseCtrl').click(function(ev){
                $('#c-carouselCtrls').toggleClass('c-carouselPlay').toggleClass('c-carouselPause');
                if (c.timer) 
                    c.stopAuto();
                else 
                    c.startAuto();
                ev.preventDefault();
            });
            
            $('#c-nextCtrl').click(function(ev){
                c.stopAuto();
                c.goNext();
                ev.preventDefault();
            });
            
            $('#c-prevCtrl').click(function(ev){
                c.stopAuto();
                c.goPrev();
                ev.preventDefault();
            });
        }
    });
});


