// Images par defaut
var erreur='http://comparer.lesservicesdemacarte.fr/styles/imgv6/noimage60.gif';
var preErreur=new Image();
preErreur.src=erreur;

var loader='http://comparer.lesservicesdemacarte.fr/styles/imgv6/loader.gif';
var preLoader=new Image();
preLoader.src=loader;

var maxWidthLigne=75;
var maxHeightLigne=75;

var maxWidthZoom=300;
var maxHeightZoom=300;

// loadPage: fonctions appelées
function loadPage(){
	loadImage();
}

// Gestion des images
function loadImage(){
	var nbI=urlsPreChargees.length;
	for(i=1;i<nbimg;i++){
		var idImg='imLoad'+i;
		if(nbI==0){
			urlsPreChargees[i]=new Image();
			urlsPreChargees[i].id=i;
			urlsPreChargees[i].nb=0;
			urlsPreChargees[i].src=urls[i];
		}
		if(urls[i]!=''){
			viewImage(urlsPreChargees[i]);
		}
		else{
			stopImage(urlsPreChargees[i]);
		}
	}
}

function stopImage(img){
	var idImg='imLoad'+img.id;
	if(document.getElementById('divproduit_'+img.id)){
		document.getElementById(idImg).src=erreur;
		document.getElementById(idImg).className='loaderError';
	}
}

function viewImage(img){
	var idImg='imLoad'+img.id;
	if(img.complete||img.nb>15){
		if(img.naturalWidth==0||img.naturalHeight==0||img.nb>15){
			stopImage(img);
		}
		else{
			var bulle='';
			var widthReel=img.width;
			var heightReel=img.height;

			var nt=dimImage(img.width,img.height,maxWidthLigne,maxHeightLigne);
			var pad=String(Math.round((maxWidthLigne-nt[1])/2))+'px';
			var vtip='topLeft';
			var vtarget='topLeft';

			var vx=0;
			var vy=0;
			
			document.getElementById(idImg).className='zoomOut';
			document.getElementById(idImg).src=img.src;
			document.getElementById(idImg).width=nt[0];
			document.getElementById(idImg).height=nt[1];
			document.getElementById(idImg).style.paddingTop=pad;

			if(heightReel>maxWidthLigne||widthReel>maxHeightLigne){
				if(heightReel>maxWidthZoom||widthReel>maxHeightZoom){
					nt=dimImage(widthReel,heightReel,maxWidthZoom,maxHeightZoom);
					widthReel=nt[0];
					heightReel=nt[1];
				}
				var padZoom=String(Math.round((maxHeightZoom-heightReel)/2))+'px';
				bulle='<img src="'+document.getElementById(idImg).src+'" style="padding-top:'+padZoom+'" width="'+widthReel+'" height="'+heightReel+'"/>';

				var supportClone = $('donate').cloneNode(true);
				supportClone.className='cadreZoom';
				supportClone.innerHTML='';
				supportClone.insert({ top: bulle });
				new Tip($(idImg), supportClone, {
						hideOn:{element:'tip',event:'mouseout'},
						duration: 0.2,
						hideAfter:0.5,
						effect:'appear',
						hook:{tip:vtip,target:vtarget},
						offset:{x:vx,y:vy},
						className:'bulleServiceLiensOn'}
					);
			}
		}
	}
	else{
		img.nb=img.nb+1;
		setTimeout(function(){goviewImage(img)},1000);
	}
}

function goviewImage(img){
	viewImage(img);
}

function dimImage(iWidth,iHeight,maxW,maxH){
	// On recupere les tailles reelles
	var h=dH=iHeight;
	var w=dW=iWidth;
	// Si la largeur ou la hauteur depasse la taille maximale
	if((h>=maxH)||(w>=maxH)){
		// Si la largeur et la hauteur depasse la taille maximale
		if((h>=maxH)&&(w>=maxH)){
			// On cherche la plus grande valeur
			if(h>w){
				dH=maxH;
				// On recalcule la taille proportionnellement
				dW=parseInt((w*dH)/h,10);
			}
			else{
				dW=maxH;
				// On recalcule la taille proportionnellement
				dH=parseInt((h*dW)/w,10);
			}
		}
		else if((h>maxH)&&(w<maxH)){
			// Si la hauteur depasse la taille maximale
			dH=maxH;
			//On recalcule la taille proportionnellement
			dW=parseInt((w*dH)/h,10);
		}
		else if((h<maxH)&&(w>maxH)){
			// Si la largeur depasse la taille maximale
			dW=maxH;
			// On recalcule la taille proportionnellement
			dH=parseInt((h*dW)/w,10);
		}
	}
	return Array(dW,dH);
}

function zelflink(uld, target) {
	var l='';
	var rg=new RegExp("\\|","g");
	var rgd=new RegExp("@","g");
	l=uld.replace(rg,"/").replace(rgd,".");
	if(l.charAt(0)=='#')
		l=l.substring(1,l.length);
	if(target=='_blank'){
		window.open(l);
	}
	else{
		window.location.href=l;
	}
}

function transit(id){
	new Ajax.Request('http://comparer.lesservicesdemacarte.fr/transit_ws.php?id='+id,{method: 'get'});
} 
