/********************/
/*		UTILS		*/
/********************/
String.prototype.capitalizer = 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		*/
/****************************************/
/*
 * ## CES FONCTIONS NE SONT PLUS UTILISEES
 * 
function placePic(elt) {
	//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
	}
	imgTemp.src = whichpic.urlImg;
}

function createTitleImg (elt) {
	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) {
	
	$$('#'+elt.id+' a.blocImg').each( function(a, index) {
		a.addClassName('produit'+(index+1));
		a.innerHTML += '<strong>'+a.down('img').getAttribute('alt')+'</strong>';
	});
	$$('#'+elt.id+' a.blocImg')[0].addClassName('lienActif');
	
	$$('#'+elt.id+' a.btnShowImg').each( function(a, index) {
		a.addClassName('produit'+(index+1));
		a.href = "javascript:void(0);"
		a.onclick = function() {
			chiffre = parseInt( this.innerHTML );
			
			var compteurNumero = 0;
			var compteurMedias = 0;
			
			$(this.parentNode.id).childElements().each( function (c, index) {

				if( c.className.search('btnShowImg')!=-1 ) {
					compteurNumero++;
					c.className = c.className.replace( 'lienActif', '');
					if( compteurNumero==chiffre )
						c.className += " lienActif";
				}
				else if( c.className.search('blocImg')!=-1) {
					compteurMedias++;
					if( compteurMedias==chiffre ) {
						c.firstChild.id = 'visuelPdtImage';
						c.style.display = 'block';
					}
					else {
						c.style.display = 'none';
						c.firstChild.id = 'visuelPdtImage'+compteurMedias;
					}
				}
			} );
		}
	});
	$$('#'+elt.id+' a.btnShowImg')[0].addClassName('lienActif');
}


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() {
		if($('visuelPdt')) visuelProduit($('visuelPdt'));
	});
}

