 /**
* @author: jonathan jolivalt - www.netline.lu
*
* fait un zoom progressif sur un objet
* id: id de l'objet
* tmin: taille de départ
* tmax: taille finale
* sens: true=zoom, false=dezoom
* hoz: alignement horizontal
* vert: alignement vertical
*/
arr_zoom = new Array();//tableau des zooms
arr_sens = new Array();//tableau des sens de zoom
function zoom(id, tmin, tmax, sens, hoz, vert){
	variation = parseInt((tmax - tmin) / qualite_zoom);
	tab = les_menus;
	//si le sens du zoom change on stop le timeout
	if(arr_sens[id] != sens){
		clearTimeout(arr_zoom[id]);
		arr_sens[id] = sens;
	}
	
	obj = document.getElementById(id);

	//initialisation des variables
	if(!obj.style.height) obj.style.height = tmin+'px';
	if(!obj.style.width) obj.style.width = tmin+'px';
	if(!obj.style.top) obj.style.top = '0px';
	if(!obj.style.left) obj.style.left = '0px';
	if(!obj.style.zIndex) obj.style.zIndex = 1;
	
	//récupération de la taille
	if(sens) taille = entier(obj.style.height)+variation;
	else taille = entier(obj.style.height)-variation;
	
	//on vérifie si la taille/position doit être modifiée
	if(sens && taille > tmax || !sens && taille < tmin){
		clearTimeout(arr_zoom[id]);
		return false;
	}
	
	//on défini l'objet qui passe devant les autres
	if(sens) obj.style.zIndex = 2;
	else obj.style.zIndex = 1;
	
	//ou pousse les cases environnantes si nécessaire
	if(!fixe_menu) pousser(tab, id, variation/2, sens);
	
	//affectation de la nouvelle taille et position
	if(sens){
		//alignement horizontal
		switch(hoz){
			case "center":
				pos = entier(obj.style.left)-(variation/2);
				break;
			case "right":
				pos = entier(obj.style.left)-variation;
				break;
			default:
				pos = entier(obj.style.left);
		}
		
		//alignement vertical
		switch(vert){
			case "center":
				pos2 = entier(obj.style.top)-(variation/2);
				break;
			case "bottom":
				pos2 = entier(obj.style.top)-variation;
				break;
			default:
				pos2 = entier(obj.style.top);
		}
				
	}
	else{
		//alignement horizontal
		switch(hoz){
			case "center":
				pos = entier(obj.style.left)+(variation/2);
				break;
			case "right":
				pos = entier(obj.style.left)+variation;
				break;
			default:
				pos = entier(obj.style.left);
		}
		
		//alignement vertical
		switch(vert){
			case "center":
				pos2 = entier(obj.style.top)+(variation/2);
				break;
			case "bottom":
				pos2 = entier(obj.style.top)+variation;
				break;
			default:
				pos2 = entier(obj.style.top);
		}
	}
	
	//application des nouvelles tailles/positions
	obj.style.height = taille+'px';
	obj.style.width = taille+'px';
	obj.style.left = pos+'px';
	obj.style.top = pos2+'px';
	
	arr_zoom[id] = setTimeout('zoom("'+id+'",'+tmin+','+tmax+','+sens+',"'+hoz+'","'+vert+'")', 30);
}

/**
* @author: jonathan jolivalt - www.netline.lu
*
* déplace les cases environnantes à une case
* tab: le tableau qui contient les cases du menu
* id: l'id de la case qui zoom
* variation: la taille du déplacement
* sens: le sens du déplacement ("right" "bottom")
*/
function pousser(tab, id, variation, sens){
	trouve = false;
	for(i=0;i<tab.length;i++){
		if(tab[i].id != id && !trouve){
			if(sens){
				if(direction_menu != "bottom") tab[i].style.left = entier(tab[i].style.left)-variation+'px';
				else tab[i].style.top = entier(tab[i].style.top)-variation+'px';
			}
			else{
				if(direction_menu != "bottom") tab[i].style.left = entier(tab[i].style.left)+variation+'px';
				else tab[i].style.top = entier(tab[i].style.top)+variation+'px';
			}
		}
		else if(tab[i].id != id && trouve){
			if(sens){
				if(direction_menu != "bottom") tab[i].style.left = entier(tab[i].style.left)+variation+'px';
				else tab[i].style.top = entier(tab[i].style.top)+variation+'px';
			}
			else{
				if(direction_menu != "bottom") tab[i].style.left = entier(tab[i].style.left)-variation+'px';
				else tab[i].style.top = entier(tab[i].style.top)-variation+'px';
			}
		}
		else if(tab[i].id == id) trouve = true;
	}
}

/**
* trouve un entier contenu dans une chaine
* chaine: la chaine
* return: l'entier trouvée (positif ou négatif)
*/
function entier(chaine){
	regex = /-?\d+/;
	return parseInt(regex.exec(chaine));
}

/**
* @author: jonathan jolivalt - www.netline.lu
*
* class MenuMac (non ce n'est pas un menu Mac Donald mais Macintosh)
* id: id du menu
* nom: id des cases du menu (un numéro sera automatiquement ajouté à la fin)
* direction: direction du menu ("right" "bottom")
* tmin: taille de départ d'une case
* tmax: taille finale d'une case
* ecart: ecart entre les cases du menu
* hoz: alignement horizontal lors du zoom ("center" "left" "right")
* vert: alignement vertical lors du zoom ("center" "top" "bottom")
* fixe: déplacement des icones environnantes si false
* zoom: qualité de transition du zoom (+ c'est élevé + le nombre d'images pour la transition sera élevé)
* latence: latence de transition (+ c'est élevé + ca sera lent)
*/
var les_menus = new Array();//tableau qui contient les cases du menu
var direction_menu;//la direction du menu ("right" -> vers le droite, "bottom" -> vers le bas)
var fixe_menu;//true -> le zoom ne déplace pas les case environnates, false il les déplace
var qualite_zoom;
var mm_latence;
var op = 80; //opacité de départ
function MenuMac(id, nom, direction, tmin, tmax, ecart, hoz, vert, fixe, zoom, latence){
	//variables internes au menu
	this.id = id;
	this.nom = nom;
	this.tmin = tmin;
	this.tmax = tmax;
	this.ecart = ecart;
	this.hoz = hoz;
	this.vert = vert; 
	
	//variables utilisées en dehors du menu
	direction_menu = direction;
	fixe_menu = fixe;
	qualite_zoom = zoom;
	mm_latence = latence;
	
	this.obj_menu = document.getElementById(this.id);//objet menu
	
	this.init = init_menu_mac;
}
/**
* initialise le menu
*/
function init_menu_mac(){
	num_menu = 0;
	mm_nb = this.obj_menu.childNodes.length;

	for(i=0;i<mm_nb;i++){
		if(this.obj_menu.childNodes[i].nodeName == 'A'){
			obj_lien = this.obj_menu.childNodes[i];
			mm_tot = obj_lien.childNodes.length;
			for(j=0;j<mm_tot;j++){
				if(obj_lien.childNodes[j].nodeName == "IMG"){
					obj_img = obj_lien.childNodes[j];
					
					//création des id des images
					obj_img.id = this.nom+num_menu;
					
					//application de l'opacité
					//change_opacity(obj_img.id, op);
					
					//création des attributs css pour les images du menu
					obj_img.className = "mac";
					obj_img.style.width = this.tmin+"px";
					obj_img.style.height = this.tmin+"px";
					switch(direction_menu){
						case "bottom":
							obj_img.style.top = ( (num_menu * this.tmin) + (num_menu * this.ecart) )+"px";
							break;
						default:
							obj_img.style.left = ( (num_menu * this.tmin) + (num_menu * this.ecart) )+"px";
					}
		
					//évènements à appliquer aux images
					var tmin = this.tmin;
					var tmax = this.tmax;
					var hoz = this.hoz;
					var vert = this.vert;
					obj_img.onmouseover = function(){
						zoom(this.id, tmin, tmax, true, hoz, vert);
						//opacity(this.id, op, 100, qualite_zoom, mm_latence);
					}
					obj_img.onmouseout = function(){
						setTimeout('zoom("'+this.id+'", '+tmin+', '+tmax+', false, "'+hoz+'", "'+vert+'")', mm_latence*qualite_zoom);
						//setTimeout('opacity("'+this.id+'", 100, '+op+', '+qualite_zoom+', '+mm_latence+')', mm_latence*qualite_zoom);
					}
					
					les_menus[num_menu] = obj_img;//on place l'objet image dans le tableau des menus
					num_menu++;
				}
			}
		}
	}
	switch(direction_menu){
		case "bottom":
			this.obj_menu.style.width = this.tmin + "px";
			this.obj_menu.style.height = (num_menu * this.tmin) + ((num_menu-1) * this.ecart) + "px";
			break;
		default:
			this.obj_menu.style.width = (num_menu * this.tmin) + ((num_menu-1) * this.ecart) + "px";
			this.obj_menu.style.height = this.tmin + "px";
	}
	this.obj_menu.style.display = "block"; //on l'affiche seuleument à la fin pour éviter les bugs pendant l'initialisation
}

function create_menu(){
	m = new MenuMac('topnav', 'zoom', 'right', 65, 100, 5, 'center', 'center', false, 4, 30);
	m.init();
}