/* 

 

        jBanner  - a jQuery Banner Plugin

        

        Copyright (c) 2009 Morgan Showman

        

        Dual licensed under the MIT and GPL licenses:

        http://www.opensource.org/licenses/mit-license.php

        http://www.gnu.org/licenses/gpl.html

        

*/

 

(function($) {

 

        //define jBanner object with some default config settings

        $.jBanner = {

               defaults: {

                       bannerContainer: "",

                       height: 295,           // height of images

                       width: 685,            // width of images

                       padding: 5,            // amount of padding (in pixels) between images

                       caption: false,        // display caption? true or false

                       cheight: 35,           // caption height

                       delay: 5000,           // delay to next element

                       speed: 1000            // transition speed

               }

        };

        

        //extend jquery with the plugin

        $.fn.extend({

               jBanner:function(options) {

                       

                       //use defaults or properties supplied by user

                       var config = $.extend({}, $.jBanner.defaults, options);

 

                       if (!config.caption){config.cheight=0;}

 

                       config.bannerContainer = "#"+this.attr("id");

 

                       banner(config);

 

                       //return the jquery object for chaining

                       return this;

               }

        });

        function banner(config) {

                $(config.bannerContainer).css({'position':'relative','height':config.height+config.cheight+'px','width':config.width+'px','overflow':'hidden'});

               $(config.bannerContainer+" li").each(function(i){$(this).css({'position':'absolute','left':'0','bottom':i*(config.height+config.padding)+'px'});$(this).attr("title",i+1);$(this).children("a");});

               $(config.bannerContainer+" li:first").addClass("selected");

               $(config.bannerContainer+" li img").css({'height':config.height,'width':config.width});

               setTimeout( function(){next(config)}, config.delay);

        };

        function next(config) {

               var i = parseInt($(config.bannerContainer+" li.selected").attr("title"));

               if (LastElement(config,i)){$(config.bannerContainer+" li[title='"+NextElement(config,i)+"']").css('bottom',(config.height+config.padding)+'px');}

               $(config.bannerContainer+" li[title='"+i+"']").removeClass("selected");

               $(config.bannerContainer+" li[title='"+(NextElement(config,i))+"']").addClass("selected");

               $(config.bannerContainer+" li").each(function(i){var newLeft = parseInt( $(this).css("bottom").replace(/px/i, '') )-(config.height+config.padding);$(this).animate({'bottom':newLeft+'px'},config.speed+250);});

               setTimeout( function(){next(config)}, config.delay);

        };

        function NextElement(config,index) {

               if(index < parseInt($(config.bannerContainer+" li:last").attr("title"))) {return index + 1;} else {return 1;}

        };

        function LastElement(config,index) {

               return (parseInt($(config.bannerContainer+" li[title='"+NextElement(config,index)+"']").css("bottom").replace(/px/i, '')) == (1 - parseInt($(config.bannerContainer+" li:last").attr("title")))*(config.height+config.padding));

        };

})(jQuery);
