/********************/
/*		UTILS		*/
/********************/
String.prototype.capitalize = function(){ //v1.0
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.substr(1);//.toLowerCase();
    });
};


function getElementsBySelector(selector){
//Extracted from nifty.js
	var i,selid="",selclass="",tag=selector,f,s=[],objlist=[];
	if(selector.indexOf(" ")>0){  //descendant selector like "tag#id tag"
		s=selector.split(" ");
		var fs=s[0].split("#");
		if(fs.length==1) return(objlist);
		f=document.getElementById(fs[1]);
		if(f) return(f.getElementsByTagName(s[1]));
		return(objlist);
	}
	if(selector.indexOf("#")>0){ //id selector like "tag#id"
		s=selector.split("#");
		tag=s[0];
		selid=s[1];
    }
	if(selid!=""){
		f=document.getElementById(selid);
		if(f) objlist.push(f);
		return(objlist);
    }
	if(selector.indexOf(".")>0){  //class selector like "tag.class"
		s=selector.split(".");
		tag=s[0];
		selclass=s[1];
    }
	var v=document.getElementsByTagName(tag);  // tag selector like "tag"
	if(selclass=="")
		return(v);
	for(i=0;i<v.length;i++){
		if(v[i].className.indexOf(selclass)>=0)
			objlist.push(v[i]);
    }
	return(objlist);
}

/****************************************/
/*		GESTION DU VISUEL PRODUIT		*/
/****************************************/
function placePic(elt) {
	// fonction de centrage d'une image dans un bloc
	elt.style.marginLeft = -Math.round(elt.width/2) + "px";
	elt.style.marginTop = -Math.round(elt.height/2) + "px";
	createTitleImg(elt);
}

function showPic(whichpic, conteneur, placeholder) {
	// ajout d'une class au lien actif
	var tabVPdt = conteneur.getElementsByTagName('a');
	for (var iLVP=0; iLVP<tabVPdt.length; iLVP++) {
		tabVPdt[iLVP].className = tabVPdt[iLVP].className.replace(new RegExp(" lienActif\\b"),"");
	}
	whichpic.className += " lienActif";
	
	var imgTemp = new Image(); var zoomTemp = new Image();
	var zoompic = whichpic.urlImg.replace(/_vignette/, "_zoom");// creation de l'url du zoom
	zoomTemp.src = zoompic;
	var titlepic = whichpic.getAttribute('title');
	
	imgTemp.onload = function() {
		placeholder.setAttribute('src', whichpic.urlImg);		// modification de l'image a afficher
		// TODO : traitement du rapport de zoom
		//if((zoomTemp.width > zoomTemp.height) && (zoomTemp.width/imgTemp.width >= rapport_zoom_vignette))
			
		placeholder.parentNode.href = zoompic;					// attribution du nouveau lien de zoom
		placeholder.parentNode.setAttribute('title', titlepic);	// attribution du nouveau title
		placeholder.setAttribute('alt', titlepic);				// attribution du nouveau alt
		placePic(placeholder);								// placement de l'image
	}// création d'une image temporaire
	imgTemp.src = whichpic.urlImg;
	
	// si l'image est chargée

}

function createTitleImg (elt) {	// réception de l'elt image
	// vérification de l'existance du strong
	var strongExist = elt.parentNode.getElementsByTagName('strong');
	if(strongExist.length > 0) {
		for(var iSE=0; iSE<strongExist.length; iSE++) {
			elt.parentNode.removeChild(strongExist[iSE]);
		}
	}
	if(elt.getAttribute('alt').length > 0){
		var strongplaceholder = document.createElement('strong');
		var titleplaceholder = document.createTextNode(elt.getAttribute('alt'));
		
		if(elt.parentNode.getElementsByTagName('dfn').length > 0){
			elt.parentNode.insertBefore(strongplaceholder, elt.parentNode.getElementsByTagName('dfn')[0]);
		} else {
			elt.parentNode.appendChild(strongplaceholder); 
		}		
		strongplaceholder.appendChild(titleplaceholder);
	}
}

function visuelProduit(elt, cImg) {
	var tabVPdt = elt.getElementsByTagName('a');
	var tabLVPdt = new Array();
	for(var iVP=0; iVP<tabVPdt.length; iVP++) {
		if(tabVPdt[iVP].className == 'btnShowImg') tabLVPdt.push(tabVPdt[iVP]);
	}
	// attribution des actions aux liens
	if(tabLVPdt.length > 0) tabLVPdt[0].className += " lienActif";
	for (var iLVP=0; iLVP<tabLVPdt.length; iLVP++) {
		tabLVPdt[iLVP].urlImg = tabLVPdt[iLVP].href;
		tabLVPdt[iLVP].href = 'javascript:void(0);';
		tabLVPdt[iLVP].cGlobal = elt;
		tabLVPdt[iLVP].cImg = cImg;
		tabLVPdt[iLVP].onclick = function() {
			showPic(this, this.cGlobal, this.cImg);
		}
	}
}

function addLoadListenerGalerieBook(func) {
	if (window.addEventListener) {
		window.addEventListener("load", func, false);
	} else if (document.addEventListener) {
		document.addEventListener("load", func, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", func);
	}
}
if (document.getElementById && document.createTextNode) {
	addLoadListenerGalerieBook(function() {
		// appel des fonctions visuel produit si nécessaire
		//alert(rapport_zoom_vignette);
		var tabVPDT = getElementsBySelector('div.visuelPdt')
		if(tabVPDT.length > 0) {
			for(iGB=0; iGB<tabVPDT.length; iGB++) {
				var cGlobal = tabVPDT[iGB];
				var cImg = document.getElementById('img' + tabVPDT[iGB].id.capitalize());
				placePic(cImg);
				visuelProduit(cGlobal, cImg);
			}
		}
	});
}
