/*
Script Ambiance 1.0.2
©2009 djm Web sprl
*/

class_ambiance = function(vId, vWidth, vHeight) {
	
	this.vId = document.getElementById(vId);
	this.vWidth = vWidth;
	this.vHeight = vHeight;
	this.vLegendWidth = vWidth;
	this.vLegendHeight = 60;
	this.totalImg;
	this.startImg;
	this.compteur;
	this.vitesse = 5000;
	this.posSlide = 100;
	this.vitSlide = 2;
	this.vIndex = 10;	
	var obj = this;
	
	this.Init_ambiance = function() {
		this.construct_ambBox();
		}
	
	this.get_photo = function() {
		var divAmb = this.vId.getElementsByTagName("div");
		var tabAmb = new Array();
		var idx = 0;
		var attValue;
		for (var i=0;i<divAmb.length;i++) {
			if (divAmb[i].getAttributeNode('class')) {
				attValue = divAmb[i].getAttributeNode('class').value; 
				} else {
					attValue = divAmb[i].getAttribute('class');
					}
			if (attValue == 'amb_cont') {
				tabAmb[idx]=divAmb[i];
				idx++;
				}
			}
		this.put_image(tabAmb);
		}	
	
	this.put_image = function(images) {
		
		var count = images.length;
		this.totalImg = count;
			
		var vNum;
		var vUrl;	
		var vTitle;
		var vDesc;
		var vLink;
		
		for (i=0;i<count;i++) {
			if (images[i].getElementsByTagName("img")[0].getAttribute("src")) {
				vNum = i;
				}
				else {
					vNum = false;
					}
			if (images[i].getElementsByTagName("img")[0].getAttribute("src")) {
				vUrl = images[i].getElementsByTagName("img")[0].getAttribute("src");
				}
				else {
					vUrl = false;
					}
			if (images[i].getElementsByTagName("h3")[0].firstChild) {
				vTitle = images[i].getElementsByTagName("h3")[0].firstChild.nodeValue;
				}
				else {
					vTitle = false;
					}
			if (images[i].getElementsByTagName("p")[0].firstChild) {
				vDesc = images[i].getElementsByTagName("p")[0].firstChild.nodeValue;
				}
				else {
					vDesc = false;
					}
			if (images[i].getElementsByTagName("a")[0].getAttribute("href")) {
					vLink = images[i].getElementsByTagName("a")[0].getAttribute("href");
					}
					else {
						vLink = false;
						}
					
			this.create_ambBox(vUrl,vNum,vTitle,vDesc,vLink);
			this.vId.removeChild(images[i]);
			
			/*Over sur l'ensemble des ambiances*/
			this.vId.onmouseover = function () {
				obj.setFade(70,this);
				for (i=0;i<count;i++) {
					obj.vId.getElementsByTagName('h3')[i].style.color = 'red';
					obj.vId.getElementsByTagName('p')[i].style.color = '#C49569';
					}
				}
			this.vId.onmouseout = function () {
				obj.setFade(100,this);
				for (i=0;i<count;i++) {
					obj.vId.getElementsByTagName('h3')[i].style.color = '#FFFFFF';
					obj.vId.getElementsByTagName('p')[i].style.color = '#FFFFFF';
					}
				}
			}
				
		this.initAnime();
		}
	
	this.ambBoxCache = false;
	
	this.construct_ambBox = function () {
	
		if (!this.ambBoxCache) {
			
			this.ambBoxTrans = document.createElement('div');
			this.ambBoxTrans.style.width = this.vLegendWidth+'px';
			this.ambBoxTrans.style.height = this.vLegendHeight+'px';
			this.ambBoxTrans.style.marginTop = (this.vHeight-this.vLegendHeight)+'px';
			this.ambBoxTrans.style.overflow = 'hidden';
			this.ambBoxTrans.style.whiteSpace = 'nowrap';
			this.ambBoxTrans.style.position = 'absolute';
			this.ambBoxTrans.style.zIndex = this.vIndex+4;
			this.ambBoxTrans.setAttribute('id','ambBoxTrans');
			this.vId.appendChild(this.ambBoxTrans);	
			this.setFade(60,this.ambBoxTrans);
			
			this.ambBoxLegend = document.createElement('div');
			this.ambBoxLegend.style.width = this.vLegendWidth+'px';
			this.ambBoxLegend.style.height = this.vLegendHeight+'px';
			this.ambBoxLegend.style.marginTop = (this.vHeight-this.vLegendHeight)+'px';
			this.ambBoxLegend.style.overflow = 'hidden';
			this.ambBoxLegend.style.whiteSpace = 'nowrap';
			this.ambBoxLegend.style.position = 'absolute';
			this.ambBoxLegend.style.zIndex = this.vIndex+5;
			this.ambBoxLegend.setAttribute('id','ambBoxLegend');
			this.vId.appendChild(this.ambBoxLegend);	
			
			this.ambBoxCache = document.createElement('div');
			this.ambBoxCache.style.height = this.vHeight+'px';
			this.ambBoxCache.style.width = this.vWidth+'px';
			this.ambBoxCache.style.overflow = 'hidden';
			this.ambBoxCache.style.whiteSpace = 'nowrap';
			this.ambBoxCache.setAttribute('id','ambBoxCache');
			this.vId.appendChild(this.ambBoxCache);	
								
			}
		
		this.get_photo();
		}
	
	this.create_ambBox = function (vUrl,vNum,vTitle,vDesc,vLink) {
		
		this.imageBox = document.createElement('div');
		this.imageBox.style.height = this.vHeight+'px';
		this.imageBox.style.width = this.vWidth+'px';		
		this.imageBox.style.width = this.vWidth+'px';
		this.imageBox.style.backgroundImage = 'url('+vUrl+')';
		this.imageBox.style.backgroundRepeat = 'no-repeat';
		this.imageBox.style.backgroundPosition = '0px center';
		this.imageBox.style.cursor = 'pointer';		
		this.imageBox.style.position = 'absolute';
		this.imageBox.style.zIndex = this.vIndex;
		this.imageBox.onclick = function () {document.location.href = vLink;};
		this.imageBox.className = 'imageBox';
		this.imageBox.setAttribute('id','imageBox_'+vNum);
		this.ambBoxCache.appendChild(this.imageBox);
		
		this.txtBox = document.createElement('div');
		this.txtBox.style.width = (this.vLegendWidth-10)+'px';
		this.txtBox.style.height = (this.vLegendHeight-3)+'px';
		this.txtBox.style.paddingLeft = 10+'px';
		this.txtBox.style.paddingTop = 3+'px';
		this.txtBox.style.position = 'absolute';
		this.txtBox.style.zIndex = this.vIndex+6;
		this.txtBox.style.cursor = 'pointer';		
		this.txtBox.onclick = function () {document.location.href = vLink;};
		this.txtBox.className = 'txtBox';
		this.txtBox.setAttribute('id','txtBox_'+vNum);
		this.ambBoxLegend.appendChild(this.txtBox);
		
		this.titleBox = document.createElement('h3');
		this.titleBox.className = 'titleBox';
		this.titleBox.innerHTML = vTitle;
		this.txtBox.appendChild(this.titleBox);
		
		this.descBox = document.createElement('p');
		this.descBox.className = 'descBox';
		this.descBox.innerHTML = vDesc;
		this.txtBox.appendChild(this.descBox);
				
		this.setFade(0,this.imageBox);
		this.setFade(0,this.txtBox);
		
		}
	
	this.animeTime;
	
	this.initAnime = function () {
		this.startImg = Math.floor(Math.random() * this.totalImg);
		
		this.setFade(100,document.getElementById('imageBox_'+this.startImg));
		this.setFade(100,document.getElementById('txtBox_'+this.startImg));
		document.getElementById('txtBox_'+this.startImg).style.zIndex = this.vIndex+7;
		document.getElementById('imageBox_'+this.startImg).style.zIndex = this.vIndex+1;
		
		this.slideAmb('imageBox_'+this.startImg,0);
		
		this.animeTime = setTimeout(function () {obj.anime(obj.fctNbre(obj.startImg),obj.startImg);}, this.vitesse);		
		}
	
	this.anime = function (newId,oldId) {
				
		document.getElementById('imageBox_'+newId).style.zIndex = this.vIndex+2;		
		document.getElementById('imageBox_'+newId).style.backgroundPosition = '0px center';
		document.getElementById('txtBox_'+newId).style.zIndex = this.vIndex+8;
		this.fade(0,100,newId,oldId);
		
		this.animeTime = setTimeout(function () {obj.anime(obj.fctNbre(newId),newId);}, this.vitesse);	
		}
	
	this.fctNbre = function (nbre) {
		if (nbre<(this.totalImg-1)) {
			nbre++;
			}
			else {
				nbre=0;
				}
		return nbre;
		}
		
	this.fade = function (amt,to,objA,objB) {
		var fadeTime;
		if(amt <= to) {
			this.setFade(amt,document.getElementById('imageBox_'+objA));
			this.setFade(amt,document.getElementById('txtBox_'+objA));
			this.setFade(to-amt,document.getElementById('txtBox_'+objB));
			amt += 10;
			fadeTime = setTimeout(function () {obj.fade(amt,to,objA,objB);}, 50);
		}
		else {
			clearTimeout(fadeTime);
			document.getElementById('imageBox_'+objA).style.zIndex = this.vIndex+1;
			document.getElementById('txtBox_'+objA).style.zIndex = this.vIndex+7;
			document.getElementById('imageBox_'+objB).style.zIndex = this.vIndex;
			document.getElementById('txtBox_'+objB).style.zIndex = this.vIndex+6;	
			this.slideAmb('imageBox_'+objA,0);
			}
		}
		
	
	this.setFade = function (amt,obj) {		
		amt = (amt == 100)?100:amt;
	  
		// IE
		obj.style.filter = "alpha(opacity:"+amt+")";	  
		// Safari<1.2, Konqueror
		obj.style.KHTMLOpacity = amt/100;	  
		// Mozilla and Firefox
		obj.style.MozOpacity = amt/100;	  
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		obj.style.opacity = amt/100;
		}
		
	this.slideAmb = function (img,pos) {
		var slideTime;
		if (pos<=this.posSlide) {
			document.getElementById(img).style.backgroundPosition = '-'+pos+'px center';
			pos += this.vitSlide;
			slideTime = setTimeout(function () {obj.slideAmb(img,pos);}, 60);
			}
			else {
				clearTimeout(slideTime);
				}
		
		}
	
	return this.Init_ambiance();

	}
