/* 

Twirlie - jQuery animation made simple
Debashis Chakraborty and Qiming Weng
http://www.designerinfusion.com/

Twirlie is licensed under MIT and GPL licenses.

Created: November 13, 2009
Last updated: December 14, 2009

Version 1.1

*/

(function($){
    
    $.fn.twirlie = function(options) {
        
        var defaults = {
            auto        : false,
            duration    : 5000,
            transtime   : 500,
            nextSel     : '.next',
            prevSel     : '.prev'
        };

        var options = $.extend(defaults, options);

        this.each(function() {
            var obj = $(this);
            var next = options.nextSel;
            var prev = options.prevSel;
            var current = 0;
            var total = $('div.slide', obj).size();
            var width = parseInt(($(obj).css("width")).substring(0, ($(obj).css("width")).length-2));

            $(obj).data('current', current);
            $(obj).data('width', width);
            $(obj).data('transtime', options.transtime);

            $(next).click(function() {
                if (options.auto) {
                    clearInterval(timer);
                }
                current = $(obj).data("current");
                current += 1;
                if (current == total) current = 0;
                obj.gotoSlide(current+1);
                return false;
            });

            $(prev).click(function() {
                if (options.auto) {
                    clearInterval(timer);
                }
                current = $(obj).data("current");
                current -= 1;
                if (current < 0) current = total-1;
                obj.gotoSlide(current+1);
                return false;
            });

            if (options.auto) {
                var timer = setInterval(function() {
                    obj.autoSlide();
                }, options.duration);
                if($('.featured-links').length > 0) {
                    $('.featured-links li a').each(function(i) {
                        $(this).click(function(event) {
                            event.preventDefault();
                            $('.featured-links li a').removeClass('active');
                            $(this).addClass('active');
                            obj.gotoSlide($(this).attr('rel'));
                            clearInterval(timer);
                        });
                    });
                }
            }

        });
        
        return this;
        
    }

    $.fn.gotoSlide = function(slide) {
        var width = parseInt($(this).data("width"));
        var transtime = $(this).data("transtime");
        $('div', this).stop().animate({
            left: (slide-1)*(0-width) + "px"
        }, transtime);
        $(this).data("current", slide-1);
        if($('.featured-links').length > 0) {
            $('.featured-links li a').removeClass('active');
            $('.featured-links li a[rel='+(slide)+']').addClass('active');
        }
    }

    $.fn.autoSlide = function() {
        var obj = $(this);
        var total = $('div.slide', obj).size();
        current = $(obj).data('current');
        current += 1;
        if (current == total) current = 0;
        obj.gotoSlide(current+1);
        if($('.featured-links').length > 0) {
            $('.featured-links li a').removeClass('active');
            $('.featured-links li a[rel='+(current+1)+']').addClass('active');
        }
    }

})(jQuery); 
