/* 

	jBnnr  - 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 jBnnr object with some default config settings
	$.jBnnr = {
		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({
		jBnnr:function(options) {
			
			//use defaults or properties supplied by user
			var config = $.extend({}, $.jBnnr.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.selectedID = 0;
		$(config.bannerContainer).css({'position':'relative','height':config.height+config.cheight+'px','width':config.width+'px','overflow':'hidden'});
		$(config.bannerContainer+" li").each(function(i){
			config.count=i;
			$(this).css({'position':'absolute','top':'0','marginLeft':i*(config.width+config.padding)+'px'});
		});
		config.random = Math.round(Math.random()*config.count);
		config.random_pos = 1;
		$(config.bannerContainer+" li:first").addClass("selected");
		$(config.bannerContainer+" li img").css({'height':config.height,'width':config.width});

		if ( config.random_pos++ < config.random )
		{
			next(config);
		}
		else
		{
			setTimeout( function(){next(config)}, config.delay);
		}
	};
	function next(config) {
		i = 0;

		if (LastElement(config,i)) { $(config.bannerContainer+" li:eq("+NextElement(config,i)+")").css('marginLeft',(config.width+config.padding)+'px'); }
		$(config.bannerContainer+" li:eq("+config.selectedID+")").removeClass("selected");
		$(config.bannerContainer+" li:eq("+(NextElement(config,i))+")").addClass("selected");
		config.selectedID = NextElement(config,i);

		if ( config.random_pos <= config.random )
		{
			config.random_pos++;
			$(config.bannerContainer+" li").each(function(i){ var newLeft = parseInt( $(this).css("marginLeft").replace(/px/i, '') )-(config.width+config.padding); $(this).css({'marginLeft':newLeft+'px'}); });

			if ( config.random_pos > config.random )
			{
				setTimeout( function(){next(config)}, config.delay);
			}
			else
			{
				next(config);
			}
		}
		else
		{
			$(config.bannerContainer+" li").each(function(i){ var newLeft = parseInt( $(this).css("marginLeft").replace(/px/i, '') )-(config.width+config.padding); $(this).animate({'marginLeft':newLeft+'px'},config.speed+250); });
			setTimeout( function(){next(config)}, config.delay);
		}
	};
	function NextElement(config,index) {
		if(config.selectedID < config.count ) {return config.selectedID+1;} else {return 0;}
	};
	function LastElement(config,index) {
		return ( parseInt($(config.bannerContainer+" li:eq("+NextElement(config,index)+")").css("marginLeft").replace(/px/i, '')) == (0 - config.count)*(config.width+config.padding) );
	};
})(jQuery);