// JS Classe - Gestion d'une image de fond

var BackgroundImage = {
	screenW : $(window).width(),
	screenH : $(window).height(),
	screenRatio : this.screenW / this.screenH,
	puceList : new Array,
	//---- Préchargement d'une image
	preloadImg : function(Img){
		var preloadImg = new Image;
		preloadImg.src = Img;
	},
	//---- Chargement de l'image
	loadImg : function(Img, IdImg, Position, forceLoad){
		!Position ? Position = 'bottom' : false;
		!forceLoad ? forceLoad = false : false;
		if ( $.browser.msie ){
			parseInt($.browser.version) > 8 ? BackgroundImage.Wait(true) : false;
		} else {
			BackgroundImage.Wait(true);
		}
		if ( $('#'+IdImg).size()==0 || forceLoad==true ){
			var myPreloadImg = new Image;
			var Infos;
			myPreloadImg.src = Img;
			myPreloadImg.onload = function(){
				Infos = new Array;
				Infos[0] = myPreloadImg.width;
				Infos[1] = myPreloadImg.height;
				Infos[2] = Infos[0] / Infos[1];
				BackgroundImage.showImg(Img, IdImg, Infos, Position);
				window.setTimeout( function(){
					if ( BackgroundImage.puceList.length!=0 ){
						for (i=0; i<BackgroundImage.puceList.length; i++){
							var IdImg 		= BackgroundImage.puceList[i][0];
							var IdPuce 		= BackgroundImage.puceList[i][1];
							var ratioWidth  = BackgroundImage.puceList[i][2];
							var ratioHeight = BackgroundImage.puceList[i][3];
							BackgroundImage.positionnePuce(IdImg, IdPuce, ratioWidth, ratioHeight, Position);
						}
					}
				},500);
			}
		} else{
			BackgroundImage.Wait(false);
			return false
		}
	},
	//---- Affichage de l'image
	showImg : function(Img, IdImg, infosImg, Position){
		Position == 'top' ? Position = 'top:0; left:0;' : Position = 'bottom:0; right:0;';
		var Style;
		BackgroundImage.Wait(false);
		$('.BgImage').remove();
		this.screenW = $(window).width();
		this.screenH = $(window).height();
		this.screenRatio = this.screenW / this.screenH;
		if ( this.screenRatio >= infosImg[2] ){
			Style = 'position:absolute; ' + Position + ' z-index:0; display:none; width:' + this.screenW + 'px; height:auto';
		} else {
			Style = 'position:absolute; ' + Position + ' z-index:0; display:none; height:' + this.screenH + 'px; width:auto';
		}
		if ( $('#'+IdImg).length==0  )
			$('body').append('<img id="' + IdImg + '" name="' + IdImg + '" src="' + Img + '" alt="" style="' + Style + '" class="BgImage" />');
		$('#'+IdImg).fadeIn(1000);
		$('.BgImage').size()>1 ? $('.BgImage').eq(0).fadeOut(1500).remove() : false;
	},
	//---- Redimensionne de l'image
	resizeImg : function(Img, IdImg){
		if ( $('#' + IdImg).size()!=0 ){
			var Infos = new Array;
			this.screenW = $(window).width();
			this.screenH = $(window).height();
			this.screenRatio = this.screenW / this.screenH;
			Infos[0] = document.images[IdImg].width;
			Infos[1] = document.images[IdImg].height;
			Infos[2] = Infos[0] / Infos[1];
			if ( this.screenRatio >= Infos[2] ){
				$('#'+IdImg).css({
					'width':this.screenW+'px',
					'height':'auto'
				});
			} else {
				$('#'+IdImg).css({
					'width':'auto',
					'height':this.screenH+'px'
				});
			}
		}
	},
	//---- Positionne la puce
	positionnePuce : function(IdImg, IdPuce, ratioWidth, ratioHeight, Position){
		!Position ? Position='bottom' : false;
		if ( $('#'+IdImg).size()==0 ) return false;
		var Infos = new Array;
		var i = 0;
		var posX = 0;
		var posY = 0;
		$('#'+IdPuce).hide();
		Infos[0] = document.images[IdImg].width;
		Infos[1] = document.images[IdImg].height;
		if ( !ratioWidth && !ratioHeight ){
			ratioWidth  = Infos[0] / parseFloat($('#'+IdPuce).css('left').replace('px', ''));
			ratioHeight = Infos[1] / parseFloat($('#'+IdPuce).css('top').replace('px', ''));
		}
		posX = Infos[0] / ratioWidth;
		posY = Infos[1] / ratioHeight;
		if ( Position=='bottom' ){
			if ( Infos[1]>$(window).height() ){
				posY = posY - (Infos[1] - $(window).height());
			} else if ( Infos[0]>$(window).width() ){
				posX = posX - (Infos[0] - $(window).width());
			}
		}
		$('#'+IdPuce).css({
			'left' : posX + 'px',
			'top'  : posY + 'px'
		}).fadeIn();
	},
	Wait : function(Affich){
		var url_img=URL_THEME_DIR;
		window.setTimeout( function(){
			if ( $('#BackgroundImageWait').size()==0 )
				$('body').append('<img id="BackgroundImageWait" src="'+url_img+'ajax-loader.gif" alt="Wait" style="position:absolute; z-index:100; top:20px; right:20px; height:16px; width:auto; display:none" />');
			if ( Affich==true ) $('#BackgroundImageWait').show();
			else $('#BackgroundImageWait').hide();
		}, 500);
		return false;
	}
}

