/*

	BILLY The Carousel jquery plugin v 0.3
	By. Jason Howmans (jasonhowmans@me.com)
	
	-- Options
	
	- scrollSpeed : The time a single transition will take
	- slidePause : Amount of time between scrolls
	- indicators : The target <ul> for displaying the indicators (indicators arent required for basic functionality)
	- indicatorLinks : Clicking on indicators jumps to slide, true by default
	- activeClass : Class to attach to the active indicator. default is 'active'
	- scrollAmount : Amount to scroll by on each transition. Set to 'auto' by default
	- nextLink : The element which will scroll next on click
	- prevLink : The element which will scroll back on click
	- autoAnimate : Do you want the carousel to play without the user pressing the next/prev buttons
	- loop : Loops back to the beginning after final slide
	
*/

(function($) {
    $.fn.extend({

        billy: function(options) {

            // Defaults
            var defaults = {
                scrollSpeed: 500,
                slidePause: 5000,
                indicators: $('ul#billy_indicators'),
                indicatorLinks: true,
                activeClass: 'active',
                scrollAmount: 'auto',
                nextLink: $('#billy_next'),
                prevLink: $('#billy_prev'),
                autoAnimate: true,
                loop: true
            };
            // Set Options
            var options = $.extend(defaults, options);

            // Loop throuch Carousels
            return this.each(function() {

                // Set Options
                var option = options;
                // Set currently selected
                var object = $(this);
                // Sets up slide size
                var slides = object.find('li');
                if (option.scrollAmount == 'auto') {
                    var slidewidth = slides.width();
                } else {
                    var slidewidth = option.scrollAmount;
                }
                // Other vars
                var slidecount = Math.round((slides.width() * slides.length) / slidewidth);
                var currentslide = 0;

                // If there's slides, continue
                if (slides.length > 0) {

                    // Loop / no. of slides
                    for (var i = 0; i < slidecount; i++) {
                        // -- Insert Indicators
                        if (!option.indicatorLinks) {
                            option.indicators.append('<li></li>');
                        } else {
                            option.indicators.append('<li><a href="#' + i + '">' + (i + 1).toString() + '</a><img class="tip" src="' + object.find('img.thumb:eq(' + i.toString() + ')').attr("src") + '"/></li>');
                        }
                    };

                    //DG: append caption bar
                    option.indicators.append('<li id="billy_caption"><span id="billy_caption_msg"></span></li>');



                    // Indicator Functionality
                    option.indicators.find('li:eq(0)').addClass(option.activeClass);
                    option.indicators.find('span#billy_caption_msg').text(object.find('span.caption:eq(' + currentslide + ')').text());

                    // On Click
                    var the_indicators = option.indicators.find('li a');

                    // Thanks to Tomas Nikl for the below fix
                    the_indicators.click(function() {

                        var hreftaginfo = $(this).attr('href').split('#');

                        var hreftag = hreftaginfo[hreftaginfo.length - 1];
                        jumptospecific(hreftag);
                        if (option.autoAnimate) {
                            clearInterval(period);
                            period = window.setInterval(function() {
                                if (currentslide >= (slidecount - 1)) {
                                    jumptostart();
                                } else {
                                    jumpnext();
                                }
                            }, option.slidePause);
                        }
                        return false;
                    });


                    // -- Jump Functions
                    var jumptostart = function() {
                        currentslide = 0;
                        object.animate({ 'marginLeft': "0" }, option.scrollSpeed);
                        option.indicators.find('li').removeClass();
                        option.indicators.find('li:eq(' + (currentslide) + ')').addClass(option.activeClass);
                        option.indicators.find('span#billy_caption_msg').text(object.find('span.caption:eq(' + currentslide + ')').text());
                    };
                    var jumptoend = function() {
                        currentslide = slidecount - 1;
                        object.animate({ 'marginLeft': "-" + currentslide * slidewidth }, option.scrollSpeed);
                        option.indicators.find('li').removeClass();
                        option.indicators.find('li:eq(' + (currentslide) + ')').addClass(option.activeClass);
                        option.indicators.find('span#billy_caption_msg').text(object.find('span.caption:eq(' + currentslide + ')').text());
                    };
                    var jumpnext = function() {
                        if (currentslide < (slidecount - 1)) {
                            currentslide++;
                            object.animate({ 'marginLeft': "-" + (slidewidth * currentslide) }, option.scrollSpeed);
                            option.indicators.find('li').removeClass();
                            option.indicators.find('li:eq(' + (currentslide) + ')').addClass(option.activeClass);
                            option.indicators.find('span#billy_caption_msg').text(object.find('span.caption:eq(' + currentslide + ')').text());
                            if (option.autoAnimate) {
                                clearInterval(period);

                                //Update to manually set the pause time of a slide the rel attribute
                                //of the li
                                var sp = object.find("li:eq(" + currentslide + ")").attr("rel");
                                period = window.setInterval(function() {
                                    if (slidecount == 2 && currentslide >= (slidecount - 1)) {
                                        jumpback();
                                    }
                                    else if (currentslide >= (slidecount - 1)) {
                                        jumptostart();
                                    } else {
                                        jumpnext();
                                    }
                                }, (sp != null) ? sp : option.slidePause);
                            }
                        } else {
                            if (option.loop)
                                jumptostart();
                        }
                    };
                    var jumpback = function() {
                        if (currentslide > 0) {
                            currentslide--;
                            object.animate({ 'marginLeft': "-" + (slidewidth * currentslide) }, option.scrollSpeed);
                            option.indicators.find('li').removeClass();
                            option.indicators.find('li:eq(' + (currentslide) + ')').addClass(option.activeClass);
                            option.indicators.find('span#billy_caption_msg').text(object.find('span.caption:eq(' + currentslide + ')').text());
                            if (option.autoAnimate) {
                                clearInterval(period);

                                //Update to manually set the pause time of a slide the rel attribute
                                //of the li
                                var sp = object.find("li:eq(" + currentslide + ")").attr("rel");

                                period = window.setInterval(function() {
                                    if (slidecount == 2 && currentslide >= (slidecount - 1)) {
                                        jumpback();
                                    }
                                    else if (currentslide >= (slidecount - 1)) {
                                        jumptostart();
                                    } else {
                                        jumpnext();
                                    }
                                }, (sp != null) ? sp : option.slidePause);
                            }
                        } else {
                            if (option.loop)
                                jumptoend();
                        }
                    };
                    var jumptospecific = function(frame) {
                        if (currentslide !== frame) {
                            currentslide = frame;
                            object.animate({ 'marginLeft': "-" + (slidewidth * currentslide) }, option.scrollSpeed);
                            option.indicators.find('li').removeClass();
                            option.indicators.find('li:eq(' + (currentslide) + ')').addClass(option.activeClass);
                            option.indicators.find('span#billy_caption_msg').text(object.find('span.caption:eq(' + currentslide + ')').text());
                        }
                    };

                    // -- Click next/prev
                    option.nextLink.click(function() {
                        jumpnext();
                    });

                    option.prevLink.click(function() {
                        jumpback();
                    });
                    if (option.autoAnimate) {
                        // -- Periodical
                        var period;
                        // Run
                        //Update to manually set the pause time of a slide the rel attribute
                        //of the li
                        var sp = object.find("li:eq(" + currentslide + ")").attr("rel");
                        
                        period = window.setInterval(function() {
                            if (slidecount == 2 && currentslide >= (slidecount - 1)) {
                                jumpback();
                            }
                            else if (currentslide >= (slidecount - 1)) {
                                jumptostart();
                            } else {
                                jumpnext();
                            }

                        }, (sp != null) ? sp : option.slidePause);
                    }
                }

            });

        }

    });
})(jQuery);

