function get_url_photo(id_reference, id_photo, iX, iY){
	var resultat = $.ajax({
		  type: "POST",
		  url: '../ajax/ajax_get_url_photo.php',
		  data: "id_reference="+id_reference+"&id_photo="+id_photo,
		  dataType: "html",
		  async:false
		}).responseText;
		if(resultat)return 'http://www.miliboo.com/photo-mobilier-design-'+resultat+'_'+iX+'_'+iY+'_0.jpg';
		return 'http://www.miliboo.com/photo-mobilier-design-'+id_reference+'-'+id_photo+'_'+iX+'_'+iY+'_0.jpg';
}

function apercu_photos(){
	scrollTo(0, 0);
	document.getElementById('zoom_photo').style.display = 'block';
}



//**********PARTIE SUR LE ZOOM DES PHOTOS**********
var iCoefZoom, iRaportPhotoZoom, iMargeGauche, iMargeHaut;
var iRapiditeZoom = 1.2;

function initialisation_zoom(){
	iCoefZoom = 100;
	iMargeGauche = 0;
	iMargeHaut = 0;
	
	//on passe le body en overflow: hidden pour ne pas afficher les barres de défilement
	document.body.style.overflow = "hidden";
	
	//on affiche la photo avec l'url de la photo choisie dans l'aperçu
	chargement_photo('img_zoom_large', 'chargement_zoom_large');
	document.getElementById('img_zoom_large').src = document.getElementById('img_zoom').src;
	document.getElementById('zoom_photo_large').style.display = 'block';
	document.getElementById('img_zoom_large').style.width = 'auto';
	document.getElementById('img_zoom_large').style.height = 'auto';
	
	//on calcule la différence de proportionalité entre l'écran du client et la photo
	iRaportPhotoZoom = document.getElementById('img_zoom_large').offsetHeight / document.getElementById('img_zoom_large').offsetWidth;
	iRaportEcran = iEcranY / iEcranX;
	iDifferenceRaport = iRaportPhotoZoom / iRaportEcran;
	
	//on regarde si on doitbaisser le pourcentage de la photo sur X ou Y
	if (iDifferenceRaport < 1){
		iDifferenceRaportX = 1;
		iDifferenceRaportY = iDifferenceRaport;
	}
	else{
		iDifferenceRaportX = 1 / iDifferenceRaport;
		iDifferenceRaportY = 1;
	}
	
	//les dimenssions de la photo sont maintenant exprimées en pourcentage par rapport au div parrent qui est aggrandi/réduit pour le zoom, ce pourcentage pour la photo tient compte de la différence de proportionalité entre l'écran et la photo pour le pas avoir une photo déformée
	document.getElementById('img_zoom_large').style.width = (100 * iDifferenceRaportX)+'%';
	document.getElementById('img_zoom_large').style.height = (100 * iDifferenceRaportY)+'%';
}

function fin_zoom(){
	iCoefZoom = 0;
	document.getElementById('zoom_photo_large').style.display = 'none';
	
	//on repasse le body en overflow: visible pour afficher les barres de défilement
	document.body.style.overflow = "visible";
}

function handle(delta) {
	//si on est pas sur le zoom, la molette sert normalement au défilement de la page
	if (document.getElementById('zoom_photo').style.display != 'block'){
		return true;
	}
	
	//si le zoom n'a pas encore été initialisé (première action sur la molette)
	if (!iCoefZoom){
		//si on est en zoom arrière, on ne veut pas afficher le zoom
		if (delta < 0){
			return false;
		}
		//si on initialise le zoom
		else{
			initialisation_zoom();
			return maj_position_photo_large();
		}
	}
	
	iPourcentageSourisEcranX = (1 - ((iEcranX - iSourisX) / iEcranX));
	iPourcentageSourisEcranY = (1 - ((iEcranY - iSourisY) / iEcranY));
	
    if (delta < 0){
		iCoefZoom /= iRapiditeZoom;
		
		iMargeGauche -= 100 * iPourcentageSourisEcranX * (1 - 1 / iRapiditeZoom) / iCoefZoom;
		iMargeHaut -= 100 * iPourcentageSourisEcranY * (1 - 1 / iRapiditeZoom) / iCoefZoom;
    }
    else{
		iMargeGauche += 100 * iPourcentageSourisEcranX * (1 - 1 / iRapiditeZoom) / iCoefZoom;
		iMargeHaut += 100 * iPourcentageSourisEcranY * (1 - 1 / iRapiditeZoom) / iCoefZoom;
		
		iCoefZoom *= iRapiditeZoom;
    }
    
    //si l'utilisateur veut reculer le zoom plus que le minimum, on considère qu'il veut arrêter le zoom
    if (iCoefZoom < 100){
    	return fin_zoom();
    }
    
    maj_position_photo_large();
	
    return false;
}

function maj_position_photo_large(){
	//la photo ne doit pas pouvoir sortir des bornes
    if (iMargeGauche < 0){
    	iMargeGauche = 0;
    }
    if (iMargeHaut < 0){
    	iMargeHaut = 0;
    }
    if (iMargeGauche > 1 - 100 / iCoefZoom){
    	iMargeGauche = 1 - 100 / iCoefZoom;
    }
    if (iMargeHaut > 1 - 100 / iCoefZoom){
    	iMargeHaut = 1 - 100 / iCoefZoom;
    }
    
	//on met à jour la taille de la photo
	document.getElementById('zoom_photo_large').style.width = iCoefZoom+'%';
	document.getElementById('zoom_photo_large').style.height = iCoefZoom+'%';
    
	//on met à jour la position de la photo (qui sera décalée en haut à gauche pour sortir de l'écran et n'afficher que la partie voulue)
	document.getElementById('zoom_photo_large').style.left = '-'+(iCoefZoom * iMargeGauche)+'%';
	document.getElementById('zoom_photo_large').style.top = '-'+(iCoefZoom * iMargeHaut)+'%';
}



//**********Gestion du drag & drop**********
var iDragPositionInitialeX, iDragPositionInitialeY, iCptMouvement;

function start_drag(iCpt){
	//si ce n'est pas un clic gauche, on est pas concerné
	if (iBoutonSouris > 1){
		return false;
	}
	
	if (!iCpt){
		iCpt = 0;
		iDragPositionInitialeX = iSourisX;
		iDragPositionInitialeY = iSourisY;
		iCptMouvement = 0;
	}
	else{
		//si le bouton de la souris n'est plus enfoncé
		if (!iBoutonSouris){
			//si le client a laché le bouton de la souris avant 300ms et qu'il n'a pas bougé la souris de plus de 25px, on considère que c'est un clic de souris et non pas un drag&drop
			if (iCpt <= 30 && iCptMouvement < 25){
				return handle(1);
			}
			//sinon on stope le drag&drop
			else{
				//on remet le cursur avec la loupe
				document.getElementById('img_zoom_large').style.cursor = "url('images/curseur_zoom.cur'), auto";
				return true;
			}
		}
		
		//si on est suppérieur à 30 (ou que le client a bougé la souris d'au moins 25px), on considère que c'est un drag&drop et on change le curseur de la souris
		if (iCpt > 30 || iCptMouvement >= 25){
			document.getElementById('img_zoom_large').style.cursor = 'move';
		}
		
		//si on a un déplacement de la souris
		if (iSourisX != iDragPositionInitialeX){
			//on modifie la marge (on ajoute le pourcentage de déplacement de la souris par rapport à la largeur totale de l'écran, avec le coef du zoom)
			iMargeGauche -= ((iSourisX - iDragPositionInitialeX) / iEcranX) * 100 / iCoefZoom;
			
			//on ajoute le déplacement de la souris au compteur
			iCptMouvement += Math.abs(iSourisX - iDragPositionInitialeX);
			
			//on enregistre l'ancienne position
			iDragPositionInitialeX = iSourisX;
		}
		if (iSourisY != iDragPositionInitialeY){
			iMargeHaut -= ((iSourisY - iDragPositionInitialeY) / iEcranY) * 100 / iCoefZoom;
			iCptMouvement += Math.abs(iSourisY - iDragPositionInitialeY);
			iDragPositionInitialeY = iSourisY;
		}
		
		//on met à jour la position de la photo
		maj_position_photo_large();
	}
	
	//on incrémente le compteur à chaque passage abouti dans la fonction
	iCpt += 1;
	//et on relance la fonction dans 10ms
	setTimeout(function(){start_drag(iCpt);}, 10);
}



//**********Gestion des flèche pour le diaporama**********
function gestion_fleches_diaporama(){
	//on récupère les bords gauche et droit de l'image
	iBordGauche = get_position(document.getElementById('zoom_fleche_gauche'), 'x');
	iBordDroit = get_position(document.getElementById('zoom_fleche_droite'), 'x') + document.getElementById('zoom_fleche_droite').offsetWidth;
	
	iCoef = (iSourisX - iBordGauche) / (iBordDroit - iBordGauche);
	iPourcentageSouris = iCoef;
	
	//si la souris est dans la partie gauche
	if (iPourcentageSouris < 0.5){
		iCoefDroite = 0;
		iCoefGauche = (0.38 - iPourcentageSouris) / 0.38 * 2;
	}
	else{
		iCoefGauche = 0;
		iCoefDroite = (0.38 - (1 - iPourcentageSouris)) / 0.38 * 2;
	}
	
	if (iCoefGauche > 1){
		iCoefGauche = 1;
	}
	if (iCoefDroite > 1){
		iCoefDroite = 1;
	}
	
	document.getElementById('zoom_fleche_gauche').style.opacity = iCoefGauche;
	document.getElementById('zoom_fleche_gauche').style.filter = 'alpha(opacity='+(iCoefGauche * 100)+')';
	document.getElementById('zoom_fleche_droite').style.opacity = iCoefDroite;
	document.getElementById('zoom_fleche_droite').style.filter = 'alpha(opacity='+(iCoefDroite * 100)+')';
	
	setTimeout(function(){gestion_fleches_diaporama();}, 10);
}

var iPhotoCourante = 0;

function diaporama_photos(iReference, iSigne){
	iPhotoCourante += iSigne;
	
	if (iPhotoCourante >= aPhotos.length){
		iPhotoCourante = 0;
	}
	if (iPhotoCourante < 0){
		iPhotoCourante = aPhotos.length - 1;
	}
	
	changer_photo(iReference);
}

function changer_photo(iReference, iPhoto){
	if (iPhoto != undefined){
		iPhotoCourante = iPhoto;
	}
	else { iPhoto=0;}

	chargement_photo('img_photo_produit', 'chargement_photo_produit');
	document.getElementById('img_photo_produit').src = get_url_photo(iReference, aPhotos[iPhotoCourante], 660, 427);
	document.getElementById('img_photo_produit').src += '?'+Math.floor(Math.random()*11);
	
	chargement_photo('img_zoom', 'chargement_zoom');
	document.getElementById('img_zoom').src = get_url_photo(iReference, aPhotos[iPhotoCourante], 0, 0);
	document.getElementById('img_zoom').src += '?'+Math.floor(Math.random()*11);
}



//**********Image d'attente pendant le chargement d'une photo**********
function chargement_photo(sIdPhoto, sIdChargement){
	document.getElementById(sIdChargement).style.display = 'block';
	document.getElementById(sIdPhoto).style.height = '0px';
	document.getElementById(sIdPhoto).onload = function(){document.getElementById(sIdChargement).style.display = 'none';document.getElementById(sIdPhoto).style.height = 'auto';};
}
