// viewver permet l'affichage dynamique des images d'arebat

viewver = function(target,titre,images,baselines,basehref,num){

	if(titre.length > 20){ this.titre = titre.substr(0,20)+'...'; }else{this.titre = titre;}
	this.myimages = images;
	this.baselines = baselines;
	this.basehref = basehref+num+"/";
	this.dossierThumb = "thumb_100/";
	this.curImg = 0;
	this.target = target;	
	this.masqueur = document.createElement('div');
	this.masqueur.className = "viewver_masqueur";	
	this.masqueur.id = "viewver_masqueur";	
	
	var o = this;
	
	this.imagette = function(id,imgLink,i){
		var imagette = document.createElement('div');
		imagette.className = "viewver_imgette";
		imagette.id = id;
		imagette.identity = i;
		var img = document.createElement('img');
		img.src = imgLink;
		img.style.width = "78px";
		img.style.height = "78px";
		img.alt="";	
		imagette.appendChild(img);
		if (imagette.addEventListener){
		  imagette.addEventListener('click',function(){o.loaded(imagette)}, false);
		} else if (imagette.attachEvent) {
  		imagette.attachEvent('onclick',function(){o.loaded(imagette)});
		}		
		this.element = imagette;
		return this;
	}
	
	this.nbclic = 0;
	this.maxclic = this.myimages.length - 3;
	
	this.scrollingNext = function(){
		if(o.nbclic < o.maxclic){
			o.nbclic++;
			var lim = o.maxclic - o.nbclic;
			var queue = Effect.Queues;
			new Effect.Move($('viewver_floater'),{x:-106, queue:'end', afterFinish: function(){
						if(o.nbclic == o.maxclic){
							$('viewver_next').style.backgroundImage = 'url("img/viewver/next_inac.png")';
						}else{
							$('viewver_next').style.backgroundImage = 'url("img/viewver/next.png")';
						}
						if(o.nbclic == 0){
							$('viewver_prev').style.backgroundImage = 'url("img/viewver/prev_inac.png")';
						}else{
							$('viewver_prev').style.backgroundImage = 'url("img/viewver/prev.png")';
						}
					}
				});				
		}
	}
	this.scrollingBefore = function(){if(o.nbclic > 0){var lim = o.nbclic;o.nbclic--;	var queue = Effect.Queues;new Effect.Move($('viewver_floater'),{x:106, queue:'end', afterFinish: function(){if(o.nbclic == 0){$('viewver_prev').style.backgroundImage = 'url("img/viewver/prev_inac.png")';}else{$('viewver_prev').style.backgroundImage = 'url("img/viewver/prev.png")';}if(o.nbclic == o.maxclic){$('viewver_next').style.backgroundImage = 'url("img/viewver/next_inac.png")';}else{$('viewver_next').style.backgroundImage = 'url("img/viewver/next.png")';}}});}}
	this.nextImg = function(){var j = o.curImg + 1;if(j == o.myimages.length){ j = 0;}o.masqueur.style.height = o.conteneur.clientHeight+'px';o.masking(o.basehref+o.myimages[j]);o.curImg = j;}
	
	
		
	//Lancement initialisation
	this.init();	
}


viewver.prototype =
{
	/*
	*	Initialisation
	*/
	init: function()
	{
		//creation conteneur
		this.conteneur = document.createElement('div');
		this.conteneur.className = "viewver_cont";		
					
		//creation dock
		this.dock = document.createElement('div');
		this.dock.className = "viewver_dock";
		this.dock.id = "viewver_dock";
		//creation img principale
		this.imgPcp = document.createElement('div');
		this.imgPcp.className = "viewver_pcp";
		this.imgPcp.style.backgroundImage = "url('"+this.basehref+this.myimages[0]+"')";
		
		if(this.myimages.length > 1){
			if (this.imgPcp.addEventListener){
			  this.imgPcp.addEventListener('click',this.nextImg, false);
			} else if (this.imgPcp.attachEvent) {
				this.imgPcp.attachEvent('onclick',this.nextImg);
			}
		}	
							
		this.porteImagette = document.createElement('div');
		this.porteImagette.className = "viewver_porteImg";
		this.floater = document.createElement('div');
		this.floater.className = "viewver_floater";
		this.floater.id = "viewver_floater";
		var calc = this.myimages.length * 107;
		this.floater.style.width = calc+'px';
		this.porteImagette.appendChild(this.floater);
		
		//creation imagettes
		if(this.myimages.length > 1){
			for(var i = 0; i < this.myimages.length; i++){			
				var img = this.imagette('imagette'+i,this.basehref+this.dossierThumb+this.myimages[i],i);
				this.floater.appendChild(img.element);
			}
		}
		this.dock.appendChild(this.porteImagette);
		this.conteneur.appendChild(this.imgPcp);
		var div_expl = document.createElement('div');
		div_expl.className = "viewver_infos";
		div_expl.id = "viewver_infos";
		var title = document.createElement('h1');
		var h1 = document.createTextNode(this.titre);		
		title.appendChild(h1);
		var baseline = document.createElement('p');
		baseline.id = "viewver_id_baseline";
		var p = document.createTextNode(this.baselines[0]);
		baseline.appendChild(p);
		div_expl.appendChild(title);
		//div_expl.appendChild(baseline);
		if(this.myimages.length > 3){			
			
			this.nextButton = document.createElement('div');
			this.nextButton.className = "viewver_next";
			this.nextButton.id = "viewver_next";	
			if (this.nextButton.addEventListener){
			  this.nextButton.addEventListener('click', this.scrollingNext, false);
			} else if (this.nextButton.attachEvent) {
	  		this.nextButton.attachEvent('onclick', this.scrollingNext);
			}	
			this.dock.appendChild(this.nextButton);			
			
			this.prevButton = document.createElement('div');
			this.prevButton.className = "viewver_prev";	
			this.prevButton.id = "viewver_prev";	
			if (this.prevButton.addEventListener){
			  this.prevButton.addEventListener('click', this.scrollingBefore, false);
			} else if (this.prevButton.attachEvent) {
	  		this.prevButton.attachEvent('onclick', this.scrollingBefore);
			}	
			this.dock.appendChild(this.prevButton);
			
		}
		this.dock.appendChild(div_expl);		
		this.conteneur.appendChild(this.masqueur);
		this.conteneur.appendChild(this.dock);				
		var element = document.getElementById(this.target);
		while (element.firstChild) {
		  element.removeChild(element.firstChild);
		}

		document.getElementById(this.target).appendChild(this.conteneur);
	},
	
	loaded: function(i){this.masqueur.style.height = this.conteneur.clientHeight+'px';this.masking(this.basehref+this.myimages[i.identity]);this.curImg = i.identity;},
	masking: function(linkImg){var o = this;new Effect.Opacity('viewver_masqueur',{duration:1, from:0.3, to:0.8, beforeStart:function(){$('viewver_masqueur').style.display='block'},afterFinish: function(){new Effect.Opacity('viewver_masqueur',{duration:1, from:0.8,to:0.0, beforeStart: function(){o.imgPcp.style.backgroundImage = 'url("'+linkImg+'")';},afterFinish:function(){$('viewver_masqueur').style.display='none';}});}});}
}




