/*
Script Galerie 4.2.1
©2010 djm Web sprl

Need jQuery 1.3.2
And slimbox 2
*/

class_galerie = function(vId, vWidth, vHeight, vThWidth, vThHeight, vThBorder, vThPadding, vThNbreVisi, vitesse, vitesseOne, vitesseTwo) {
	
	this.vBox = true;
	this.vId = document.getElementById(vId);
	this.vWidth = vWidth;
	this.vHeight = vHeight;
	this.vThWidth = vThWidth;
	this.vThHeight = vThHeight;
	this.vThBorder = vThBorder;
	this.vThPadding = vThPadding;	
	this.vThNbreVisi= vThNbreVisi;
	this.vThGalWidth = 0;
	this.vThTailleImg = this.vThWidth + (this.vThBorder*2) + (this.vThPadding*2);
	this.totalImg;
	this.vitesse = vitesse;
	this.location = 0;
	this.position = 0;
	this.posimg = 0;
	this.thPos = 0;
	this.thNbreImg=0;
	this.thMov=0;
	this.thMaxwidth=0;
	this.gThPadding = 0;
	
	var vitesseOne = vitesseOne;
	var vitesseTwo = vitesseTwo;
	
	this.Init_galerie = function() {
		this.construct_photobox();
		}

	this.get_photo = function() {
		var divGalerie = this.vId.getElementsByTagName("div");
		var tabGalerie = new Array();
		var idx = 0;
		var attValue;
		for (var i=0;i<divGalerie.length;i++) {
			if (divGalerie[i].getAttributeNode('class')) {
				attValue = divGalerie[i].getAttributeNode('class').value; 
				} else {
					attValue = divGalerie[i].getAttribute('class');
					}
			if (attValue == 'gal_cont') {
				tabGalerie[idx]=divGalerie[i];
				idx++;
				}
			}
		this.put_image(tabGalerie);
		}
		
	this.put_image = function(images) {
	this.photoboxcache.style.backgroundImage = 'none';
	
	var count = images.length;
	this.totalImg = count;
	
	if (count>1) {
		this.btnR(1);
		}
	if (count>this.vThNbreVisi) {
		this.btnThR(1);
		}
	
	var vNum;
	var vUrl;
	var vTitle;
	var vDesc;
	var vLink;
	var vUrlTh;
		
	for (i=0;i<count;i++) {
		if (images[i].getElementsByTagName("a")[1].getAttribute("href")) {
			vNum = i;
			}
			else {
				vNum = false;
				}
		if (images[i].getElementsByTagName("a")[1].getAttribute("href")) {
			vUrl = images[i].getElementsByTagName("a")[1].getAttribute("href");
			}
			else {
				vUrl = false;
				}
		if (images[i].getElementsByTagName("a")[2].getAttribute("href")) {
			vUrlTh = images[i].getElementsByTagName("a")[2].getAttribute("href");
			}
			else {
				vUrlTh = 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 (this.vBox) {
			if (images[i].getElementsByTagName("a")[0].getAttribute("href")) {
				vLink = images[i].getElementsByTagName("a")[0].getAttribute("href");
				}
				else {
					vLink = false;
					}
			}
		
		this.create_imagebox(vUrl,vNum,vTitle,vDesc,vLink);
		this.create_thumbbox(vUrlTh,vNum);
		this.location = this.location + this.vWidth;
		this.vThGalWidth = this.vThGalWidth + (this.vThPadding*2)+ this.vThWidth + (this.vThBorder*2) + this.gThPadding;
		this.vId.removeChild(images[i]);
		this.thNbreImg = this.thNbreImg + 1;
		}
	
	
	
	this.photobox.style.width = this.location+'px';	
	this.photoThumbGal.style.width = this.vThGalWidth+'px';
	this.thMaxwidth = this.vThGalWidth;
	
	};
	
this.photoboxcache = false;

this.construct_photobox = function () {
	
	if (!this.photoboxcache) {
		
			var obj = this;
		
			this.photolinkleft = document.createElement('div');
			this.photolinkleft.style.cssFloat = 'left';
			this.photolinkleft.style.styleFloat = 'left';
			this.photolinkleft.style.cursor = 'pointer';
			this.photolinkleft.className = 'photolinkleft';
			this.photolinkleft.setAttribute('id','photolinkleft');
			this.photolinkleft.onclick = function () {obj.move_photo_left();};	
			this.photolinkleft.innerHTML = '&nbsp;';
			this.vId.appendChild(this.photolinkleft);
			this.btnL(0);
			
			this.photoboxcache = document.createElement('div');
			this.photoboxcache.style.height = this.vHeight+'px';
			this.photoboxcache.style.width = this.vWidth+'px';
			this.photoboxcache.style.backgroundImage = 'url(./images/waiting.gif)';
			this.photoboxcache.style.backgroundRepeat = 'no-repeat';
			this.photoboxcache.style.backgroundPosition = 'center center';
			this.photoboxcache.style.overflow = 'hidden';
			this.photoboxcache.style.whiteSpace = 'nowrap';
			this.photoboxcache.style.position = 'relative';	
			this.photoboxcache.style.cssFloat = 'left';
			this.photoboxcache.style.styleFloat = 'left';
			this.photoboxcache.setAttribute('id','photoboxcache');
			this.vId.appendChild(this.photoboxcache);
		
			this.photolinkright = document.createElement('div');
			this.photolinkright.style.cssFloat = 'left';
			this.photolinkright.style.styleFloat = 'left';
			this.photolinkright.style.cursor = 'pointer';
			this.photolinkright.className = 'photolinkright';
			this.photolinkright.setAttribute('id','photolinkright');
			this.photolinkright.onclick = function () {obj.move_photo_right();};	
			this.photolinkright.innerHTML = '&nbsp;';
			this.vId.appendChild(this.photolinkright);
			this.btnR(0);
			
			this.photoclear = document.createElement('div');			
			this.photoclear.style.height = '0px';
			this.photoclear.style.width = '0px';
			this.photoclear.style.clear = 'both';
			this.photoclear.style.fontSize = '0px';
			this.photoclear.className = 'photoclear';
			this.photoclear.setAttribute('id','photoclear');
			this.vId.appendChild(this.photoclear);
		
			this.photobox = document.createElement('div');
			this.photobox.style.height = this.vHeight+'px';
			this.photobox.style.position = 'relative';
			this.photobox.style.left = '0px';
			this.photobox.className = 'photobox';
			this.photobox.setAttribute('id','photobox');
			this.photoboxcache.appendChild(this.photobox);
			
			this.photoThumbLeft = document.createElement('div');
			this.photoThumbLeft.style.cssFloat = 'left';
			this.photoThumbLeft.style.styleFloat = 'left';
			this.photoThumbLeft.style.cursor = 'pointer';
			this.photoThumbLeft.className = 'photoThumbLeft';
			this.photoThumbLeft.setAttribute('id','photoThumbLeft');
			this.photoThumbLeft.innerHTML = '&nbsp;';
			this.vId.appendChild(this.photoThumbLeft);
			this.btnThL(0);
			
			this.photoThumbCache = document.createElement('div');
			this.photoThumbCache.style.cssFloat = 'left';
			this.photoThumbCache.style.styleFloat = 'left';
			this.photoThumbCache.style.width = (((this.vThPadding*2) + this.vThWidth + (this.vThBorder*2))*this.vThNbreVisi) - ((this.vThPadding*2)) + 'px';
			this.photoThumbCache.style.height = this.vThHeight+(this.vThBorder*2)+'px';
			this.photoThumbCache.style.overflow = 'hidden';
			this.photoThumbCache.style.whiteSpace = 'nowrap';
			this.photoThumbCache.style.position = 'relative';	
			this.photoThumbCache.className = 'photoThumbCache';
			this.photoThumbCache.setAttribute('id','photoThumbCache');
			this.vId.appendChild(this.photoThumbCache);
			
			this.photoThumbRight = document.createElement('div');
			this.photoThumbRight.style.cssFloat = 'left';
			this.photoThumbRight.style.styleFloat = 'left';
			this.photoThumbRight.style.cursor = 'pointer';
			this.photoThumbRight.className = 'photoThumbRight';
			this.photoThumbRight.setAttribute('id','photoThumbRight');
			this.photoThumbRight.innerHTML = '&nbsp;';
			this.vId.appendChild(this.photoThumbRight);
			this.btnThR(0);
			
			this.photoThumbGal = document.createElement('div');
			this.photoThumbGal.style.position = 'relative';	
			this.photoThumbGal.style.left= '0px';
			this.photoThumbGal.className = 'photoThumbGal';
			this.photoThumbGal.setAttribute('id','photoThumbGal');			
			this.photoThumbCache.appendChild(this.photoThumbGal);
			
	}
	
	this.get_photo();
}

this.create_imagebox = function (img,nbre,title,txt,vLink) {
	this.imagebox = document.createElement('div');
	this.imagebox.style.width = this.vWidth+'px';
	this.imagebox.style.height = this.vHeight+'px';
	this.imagebox.style.backgroundImage = 'url(./images/waiting.gif)';
	this.imagebox.style.backgroundRepeat = 'no-repeat';
	this.imagebox.style.backgroundPosition = 'center center';
	this.imagebox.style.cssFloat = 'left';
	this.imagebox.style.styleFloat = 'left';
	this.imagebox.className = 'imagebox';
	this.imagebox.setAttribute('id','imagebox_'+nbre);
	this.photobox.appendChild(this.imagebox);
	
	this.imageImg = document.createElement('div');
	this.imageImg.className = 'imageImg';
	this.imageImg.setAttribute('id','imageImg_'+nbre);	
	this.imageImg.style.backgroundImage= 'url('+img.replace(/ /g,'%20').replace(/\(/g,'%28').replace(/\)/g,'%29')+')';
	this.imageImg.style.backgroundRepeat= 'no-repeat';
	this.imageImg.style.backgroundPosition= 'center center';
	this.imageImg.style.width = this.vWidth+'px';
	this.imageImg.style.height = this.vHeight+'px';
	this.imagebox.appendChild(this.imageImg);
	if (this.vBox) {
		if (vLink) {
			this.imageImg.style.cursor = 'pointer';	
			this.imageImg.onclick = function() {
				var photoLegend = '';
				if (title){
					photoLegend = title;
					if (txt) {
						photoLegend += '<br /><br />' + txt;
						}
					}
					else {
					if (txt){
						photoLegend = txt;
						}
					}
				jQuery.slimbox(vLink.replace(/ /g,'%20').replace(/\(/g,'%28').replace(/\)/g,'%29'), photoLegend);	
				}
			}
		}
	
	if (title) {	
		this.txtbox = document.createElement('div');
		this.txtbox.style.width = this.imageImg.offsetWidth-52+'px'; 
		this.txtbox.style.marginLeft = '23px';
		this.txtbox.style.paddingLeft = '3px';		
		this.txtbox.style.paddingRight = '3px';
		this.txtbox.style.position = 'absolute';
		this.txtbox.style.whiteSpace = 'normal';
		this.txtbox.style.zIndex = 3;
		this.txtbox.className = 'txtbox';
		this.txtbox.setAttribute('id','txtbox_'+nbre);
		this.imagebox.appendChild(this.txtbox);
		
		this.titlebox = document.createElement('h1');
		this.titlebox.style.margin = '0px';
		this.titlebox.style.padding = '0px';
		this.titlebox.setAttribute('id','titlebox_'+nbre);
		this.titlebox.className = 'titlebox';
		this.titlebox.innerHTML = title;	
		this.txtbox.appendChild(this.titlebox);
	
		if (txt) {		
			this.descbox = document.createElement('div');
			this.descbox.style.margin = '0px';
			this.descbox.style.padding = '0px';
			this.descbox.setAttribute('id','descbox_'+nbre);
			this.descbox.className = 'descbox';
			this.descbox.innerHTML = txt;	
			this.txtbox.appendChild(this.descbox);
		}

		this.txtbox.style.marginTop = (-(this.txtbox.offsetHeight)-22)+'px';
		
		this.txtback = document.createElement('div');
		this.txtback.className = 'txtback';
		this.txtback.style.width = this.imageImg.offsetWidth-46+'px'; 
		this.txtback.style.height = this.txtbox.offsetHeight+'px';
		this.txtback.style.marginTop = '-'+(this.txtbox.offsetHeight+25)+'px';		
		this.txtback.style.marginLeft = '20px';
		this.txtback.style.paddingTop = '3px';
		this.txtback.style.paddingBottom = '3px';
		this.txtback.style.paddingLeft = '3px';
		this.txtback.style.paddingRight = '3px';
		this.txtback.style.background = '#000000';	
		this.txtback.style.position = 'absolute';	
			// IE
			this.txtback.style.filter = 'alpha(opacity=50)';
			// Safari<1.2, Konqueror
			this.txtback.style.KHTMLOpacity = '0.50';
			// Mozilla and Firefox
			this.txtback.style.MozOpacity = '0.50'; 
			// Safari 1.2, newer Firefox and Mozilla, CSS3
			this.txtback.style.opacity = '0.50';
		this.txtback.style.zIndex = 2;
		this.imagebox.appendChild(this.txtback);
	}
	
}

this.create_thumbbox = function(vUrlTh,vNum) {
	var obj = this;
	this.thumbbox = document.createElement('div');
	this.thumbbox.style.width = this.vThWidth+'px';
	this.thumbbox.style.height = this.vThHeight+'px';
	this.thumbbox.style.backgroundImage = 'url(./images/waiting.gif)';
	this.thumbbox.style.backgroundRepeat = 'no-repeat';
	this.thumbbox.style.backgroundPosition = 'center center';
	this.thumbbox.style.cssFloat = 'left';
	this.thumbbox.style.styleFloat = 'left';
	this.thumbbox.style.overflow = 'hidden';		
	this.thumbbox.style.cursor = 'pointer';
	this.thumbbox.style.borderWidth = this.vThBorder+'px';
	this.thumbbox.style.marginRight = this.vThPadding+'px';
	this.thumbbox.style.marginLeft = this.vThPadding+'px';
	this.thumbbox.className = 'thumbbox';
	this.thumbbox.setAttribute('id','thumbbox_'+vNum);
	this.thumbbox.onclick = function () {obj.showThumb(vNum)};
	this.thumbbox.onmouseover = function () {
		this.className += ' thumbImg_hover';
		}
	this.thumbbox.onmouseout = function () {
		this.className = this.className.replace('thumbImg_hover','');
		}
	this.photoThumbGal.appendChild(this.thumbbox);
	
	switch (vNum) {
		case (this.totalImg-1) : 
			this.thumbbox.style.marginRight = '0px';
			break;
		case 0 :
			this.thumbbox.style.marginLeft = '0px';
			break;
		}
	
	this.thumbImg = document.createElement('div');
	this.thumbImg.className = 'thumbImg';
	this.thumbImg.setAttribute('id','thumbImg_'+vNum);
	this.thumbImg.style.backgroundImage= 'url('+vUrlTh.replace(/ /g,'%20').replace(/\(/g,'%28').replace(/\)/g,'%29')+')';
	this.thumbImg.style.backgroundRepeat= 'no-repeat';
	this.thumbImg.style.backgroundPosition= 'center center';
	this.thumbImg.style.width = this.vThWidth+'px';
	this.thumbImg.style.height = this.vThHeight+'px';
	this.thumbbox.appendChild(this.thumbImg);
	}
	
this.showThumb = function(num) {
	var pictId = num;
	this.position = -(this.vWidth * pictId);
	this.photobox.style.left = (this.position)+'px';
	if (this.position<0) {
			this.btnR(1);
			}
	if (Math.abs(this.position)==(this.location-this.vWidth)) {
			this.btnR(0);
			}	
	if (Math.abs(this.position)<(this.location-this.vWidth)) {
			this.btnL(1);
			}	
	if (this.position==0) {
			this.btnL(0);
			}
	if ((this.position==0) && (this.thNbreImg > 1)) {
			this.btnR(1);
		}
	if ((this.position<0) && (Math.abs(this.position)==((this.thNbreImg*this.vWidth)-this.vWidth))) {
			this.btnL(1);
			}
	}

this.timeleft;
this.move_photo_left = function () {
	if (this.position<0) {
		clearTimeout(this.timeright);	
		var mod1 = this.position%this.vWidth;
		var mod2 = (this.position+this.vitesse)%this.vWidth;
		if ((mod1>mod2) && !(mod1==0)) {
			this.position = this.position+Math.abs(mod1);
			}
			else {
				this.position = this.position+this.vitesse;
				}
		if ((mod2==Math.abs(mod2)) && (!(mod2==0) || (Math.abs(this.position)<this.vWidth))) {
			this.position = 0;
			}
		this.photobox.style.left = this.position+'px';
		this.btnR(1);
		if (!((this.position%this.vWidth)==0)) {
			var obj = this;
			this.timeleft = setTimeout(function() {obj.move_photo_left()},20);
			}
			else 
			{
				clearTimeout(this.timeleft);
				}
		}
	if (this.position==0) {
			this.btnL(0);
			}
	}

this.timeright;
this.move_photo_right = function () {
	if (Math.abs(this.position)<(this.location-this.vWidth)) {
		clearTimeout(this.timeleft);
		var mod1 = Math.abs(this.position%this.vWidth);
		var mod2 = Math.abs((this.position-this.vitesse)%this.vWidth);
		if (mod1>mod2) {
			this.position = this.position-(this.vWidth - mod1);
			}
			else {
				this.position = this.position-this.vitesse;
				}
		
		this.photobox.style.left = this.position+'px';
		this.btnL(1);
		if (!((this.position%this.vWidth)==0)) {
				var obj = this;
				this.timeright = setTimeout(function() {obj.move_photo_right()},20);
				}
				else 
				{
					clearTimeout(this.timeright);
					}
			}
		if (Math.abs(this.position)==(this.location-this.vWidth)) {
			this.btnR(0);
			}
	}

this.cInt = function (Nbre) {
	Nbre = Nbre.toString();
	var tableau=Nbre.split('.');
	if (tableau[1]) {
		return false;
		}
		else {
			return true;
			}
	}

this.fullVisible = function(obj) {
	obj.style.visibility='visible';
	}

this.noneVisible = function(obj) {
	obj.style.visibility='hidden';
	}

this.btnL = function(flag) {
	if (flag==0) {
		this.noneVisible(this.photolinkleft);
		this.photolinkleft.style.cursor = 'auto';
		this.photolinkleft.onclick = function () {};
		}
	if (flag==1) {
		this.fullVisible(this.photolinkleft);
		var obj = this;
		this.photolinkleft.style.cursor = 'pointer';
		this.photolinkleft.onclick = function () {obj.move_photo_left();};
		}
	}

this.btnR = function(flag) {
	if (flag==0) {
		this.noneVisible(this.photolinkright);
		this.photolinkright.style.cursor = 'auto';
		this.photolinkright.onclick = function () {};
		}
	if (flag==1) {
		this.fullVisible(this.photolinkright);
		var obj = this;
		this.photolinkright.style.cursor = 'pointer';
		this.photolinkright.onclick = function () {obj.move_photo_right();};
		}
	}

this.getTopPos = function (inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

this.getLeftPos = function (inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}

/*
Fonctions de mouvement pour les vignettes
*/
var timer;
	/*
	this.thPos = 0;
	this.thNbreImg=0;
	this.thMov=0;
	this.thMaxwidth=0;
	this.photoThumbRight;
	this.photoThumbLeft;
	this.photoThumbGal;
	this.vThNbreVisi;
	this.vThTailleImg;
	*/
this.btnThL = function(flag) {
	if (flag==0) {
		this.noneVisible(this.photoThumbLeft);
		this.photoThumbLeft.style.cursor = 'auto';
		this.photoThumbLeft.onmouseover = function () {
				};
		this.photoThumbLeft.onmouseout = function () {
				};	
		this.photoThumbLeft.onmousedown = function () {
				};
		this.photoThumbLeft.onmouseup = function () {
				};
		}
	if (flag==1) {
		this.fullVisible(this.photoThumbLeft);
		var obj = this;
		this.photoThumbLeft.style.cursor = 'pointer';
		this.photoThumbLeft.onmouseover = function () {
				obj.cataPreviousSlide(vitesseOne);
				};
		this.photoThumbLeft.onmouseout = function () {
				obj.stopSlide();
				};	
		this.photoThumbLeft.onmousedown = function () {
				obj.stopSlide();
				obj.cataPreviousSlide(vitesseTwo);
				};
		this.photoThumbLeft.onmouseup = function () {
				obj.stopSlide();
				obj.cataPreviousSlide(vitesseOne);
				};	
		}
	}

this.btnThR = function(flag) {
	if (flag==0) {
		this.noneVisible(this.photoThumbRight);
		this.photoThumbRight.style.cursor = 'auto';
		this.photoThumbRight.onmouseover = function () {
				};
		this.photoThumbRight.onmouseout = function () {
				};	
		this.photoThumbRight.onmousedown = function () {
				};
		this.photoThumbRight.onmouseup = function () {
				};		
		}
	if (flag==1) {
		this.fullVisible(this.photoThumbRight);
		var obj = this;
		this.photoThumbRight.style.cursor = 'pointer';
		this.photoThumbRight.onmouseover = function () {
				obj.cataNextSlide(vitesseOne);
				};
		this.photoThumbRight.onmouseout = function () {
				obj.stopSlide();
				};	
		this.photoThumbRight.onmousedown = function () {
				obj.stopSlide();
				obj.cataNextSlide(vitesseTwo);
				};
		this.photoThumbRight.onmouseup = function () {
				obj.stopSlide();
				obj.cataNextSlide(vitesseOne);
				};
		}
	}
	
/***mouvement next on mouse hover***/
this.cataNextSlide = function (vitesse) {
	var obj = this;
	if (((this.thPos)==-((this.thMaxwidth)-(this.vThTailleImg*this.vThNbreVisi))) || ((this.thPos)<-((this.thMaxwidth)-(this.vThTailleImg*this.vThNbreVisi)))  || ((this.thMaxwidth)<(this.vThTailleImg*this.vThNbreVisi)))
	{
		this.btnThR(0);
		timer='';
	}
	else
	{
		this.btnThL(1);		
		if ((this.thPos-vitesse)<-((this.thMaxwidth)-(this.vThTailleImg*this.vThNbreVisi))) {
			this.thPos = -((this.thMaxwidth)-(this.vThTailleImg*this.vThNbreVisi));
			}
			else {
				this.thPos -= vitesse;
				}
		this.photoThumbGal.style.left=(this.thPos)+'px';
		timer=setTimeout(function() {obj.cataNextSlide(vitesse);},20);
		
	};
}

/***Mouvement previous on mouse over***/
this.cataPreviousSlide = function (vitesse) {
	var obj = this;
	if ((this.thPos==0) || (this.thPos>0))
	{
		this.btnThL(0);
		timer='';
	}
	else
	{
		this.btnThR(1);		
		if ((this.thPos+vitesse)>0) {
			this.thPos = 0;
			}
			else {
				this.thPos += vitesse;
				}	
		if (((this.thPos+vitesse)==0)||((this.thPos+vitesse)>0)) {
			this.thPos = 0;
			this.photoThumbGal.style.left='0px';
			}
			else {
				this.photoThumbGal.style.left=(this.thPos)+'px';
				}
		timer=setTimeout(function() {obj.cataPreviousSlide(vitesse);},20);	
	};
}

/***Stop slide***/
this.stopSlide = function () {
	clearTimeout(timer);
}

	this.statut = function () {
	var obj = this;	
	window.status=this.thPos + ' || ' + this.position + ' || ' + this.position;
	setTimeout(function() {obj.statut()},500);
	};
	
	return this.Init_galerie();

}
