/*
Script banner 2.0.0
2010 djm Web sprl

Need jQuery 1.3.2
*/
jQuery.noConflict();

banner_djm = function(banner,time,padding,bottom) {
	this.banner = banner;
	this.time = time*1000;
	this.pad = padding;
	this.bot=bottom;
	this.init();
	};
jQuery.extend(banner_djm.prototype, {
	init: function() {
		var obj = this;
		var nbreImg = jQuery(this.banner + ' a').length;
		jQuery(this.banner).addClass('waiting_banner');
		jQuery(this.banner + ' a').css('display','none');
		jQuery(this.banner + ' a').each(function() {
			var bW = jQuery(obj.banner).width();	
			var bH = jQuery(obj.banner).height();
												 
			obj.img = document.createElement("div");
			jQuery(obj.img).css({
				'background' : 'url(' + jQuery(this).attr('href').replace(/ /g,'%20').replace(/\(/g,'%28').replace(/\)/g,'%29') + ') no-repeat center center',
				'width' : bW+'px',
				'height' : bH+'px',
				'position' : 'absolute',
				'z-index' : '0'
				}).appendTo(obj.banner);
			
			obj.bg_txt = document.createElement("span");
			jQuery(obj.bg_txt).addClass("banner_back");
			jQuery(obj.bg_txt).css({
				'display':'block',
				'background-color':'#000',
				'opacity':'0.5',
				'position':'absolute',
				'padding':obj.pad+'px',
				'padding-right':obj.pad*3+'px'
				}).appendTo(obj.img);
			
			obj.txt = document.createElement("p");
			jQuery(obj.txt).addClass("banner_txt");
			jQuery(obj.txt).css({
				'color':'#fff',
				'position':'absolute',
				'padding':obj.pad+'px',
				'padding-right':obj.pad*3+'px',
				'margin':'0px'
				}).appendTo(obj.img);
			jQuery(obj.txt).text(jQuery(this).text());
			
			var wTxt=jQuery(obj.txt).width();
			var hTxt=jQuery(obj.txt).height();
			
			var mW=bW-(wTxt+(obj.pad*2)+(obj.pad*2));
			var mH=bH-(hTxt+(obj.pad*2))-obj.bot;
			
			jQuery(obj.bg_txt).css({
				'width':wTxt+'px',
				'height':hTxt+'px',
				'margin-left':mW+'px',
				'margin-top':mH+'px'
				});
			
			jQuery(obj.txt).css({
				'width' : wTxt+'px',
				'height' : hTxt+'px',
				'margin-left':mW+'px',
				'margin-top':mH+'px'
				});
			
			jQuery(obj.img).css('display','none');
			jQuery(this).remove();	
			});
		if (nbreImg>1) {
			this.init_anime();
			}
			else {	
				jQuery(obj.banner + ' div').fadeIn(500, function() {
					jQuery(obj.banner).removeClass('waiting_banner');			
					});		
				}
		},
	init_anime: function() {
		var obj = this;
		//Defini la derniere
		jQuery(this.banner + ' div:last').addClass('last');
		//Commence aleatoirement
		var nombreAlea = Math.floor(Math.random() * jQuery(this.banner + ' div').length)+1;
		this.alea_start(jQuery(this.banner + ' div:first'),jQuery(this.banner + ' div:first').next(),0,nombreAlea);		
		},
	anime_ban: function(objP, objN, objD) {
		var obj = this;
		//Applique le niveau des images
		objP.css('z-index','2');
		objN.css('z-index','1');
		if (objD!=0) {
			objD.css('z-index','0');
			objD.css('display','block');
			}
		//Anime les images
		objP.fadeOut(500);
		//Attribue les images suivantes
		objD = objP;
		objP = objN;
		if (objN.attr('class') == 'last') {
			objN = jQuery(this.banner + ' div:first');
			}
			else {
				objN = objN.next();
				}
		//Boucle l'animation
		setTimeout(function() {
			obj.anime_ban(objP, objN, objD);
			}, this.time);
		},
	alea_start: function(objP, objN, objD, alea) {		
		var obj = this;
		//Boucle un nombre aleatoire
		for (i=1; i<=alea;i++) {			
			if (i==alea) {
				//Applique le niveau des images
				objP.css('z-index','2');
				objN.css('z-index','1');
				if (objD!=0) {
					objD.css('z-index','0');
					}
				//Anime le premier
				objP.fadeIn(500, function() {
					jQuery(obj.banner + ' div').css('display','block');
					jQuery(obj.banner).removeClass('waiting_banner');
					setTimeout(function() {
						obj.anime_ban(objP,objN,objD);
						}, obj.time);
					});	
				}
				else {
					//Attribue les images suivantes
					objD = objP;
					objP = objN;
					if (objN.attr('class') == 'last') {
						objN = jQuery(this.banner + ' div:first');
						}
						else {
							objN = objN.next();
							}
					}				
			}			
		}
	});
