/* 

	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
	
*/

/*
	Modification by Pau Baiget for banner creation at www.fluendo.com

*/

(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({'list-style':'none', 'position':'relative','height':config.height+config.cheight+'px','width':config.width+'px','overflow':'hidden'});
		$(config.bannerContainer+" li").each(
			function(i){
				$(this).css({'position':'absolute','top':'0','left':0});
				//$(this).attr("title",i+1);
				$(this).children("a").after("<p>"+$(this).children("a").children("img").attr("alt")+"</p>");
			});
		$(config.bannerContainer+" li:first").addClass("selected");

		setTimeout( 
			function(){
				next(config)
			}, config.delay
		);
	};
	function next(config) {
		
		var i = parseInt($(config.bannerContainer+" li.selected").attr("title"));

		$(config.bannerContainer+" li[title='"+i+"']").removeClass("selected");
		$(config.bannerContainer+" li[title='"+i+"']").fadeOut();
		$(config.bannerContainer+" li[title='"+(NextElement(config,i))+"']").addClass("selected");
		$(config.bannerContainer+" li[title='"+(NextElement(config,i))+"']").fadeIn();
		/*alert('' + i + ': ' + NextElement(config,i))*/
		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;}
	};

})(jQuery);

