var x_fin, y_fin, ratio_w, ratio_h, ratio_x, ratio_y, w_win, h_win = 0;
var preloader;
var vitesse = -30;
var active = '';



// Detection et transformation des liens image
function detect() {
    var txt = document.body.innerHTML;
    txt = txt.replace(/<a href="([^\"]+)"([^\>]?)><img/g, '<a onclick="javascript:ouvre_img()" href="$1ghghgh"$2>');
}


// Voile noir
function voile_noir(voile) {
    document.getElementById("total").style.opacity = (voile / 10);
    document.getElementById("total").style.filter = "alpha(opacity=" + (voile*10) + ")";
}
    
// Fonction INIT
function ouvre_img(vignette, img, w_img, h_img) {

    if (active != "") {
    document.getElementById(active).style.opacity = "1";
    document.getElementById(active).style.filter = "alpha(opacity=100)";
    }
    
    vitesse = -30;
    var elm = document.getElementById("imgpp");
    elm.src = document.getElementById(vignette).src;
    elm.style.opacity = "0.3";
    
    preloader = new Image(100,25); 
    preloader.src = img;
    
    var fenetre = getPageSize();
    w_win = fenetre[0];
    h_win = fenetre[3];
    
    
    elm.style.width = jsmove(vignette, "w") + "px";
    elm.style.height = jsmove(vignette, "h") + "px";
    elm.style.left = jsmove(vignette, "left") + "px";
    elm.style.top = jsmove(vignette, "top") + "px";
    elm.style.display = "block";
    
    // calcul des elm de fin
    x_fin = (w_win - w_img) / 2;
    y_fin = (h_win - h_img) / 2 + fenetre[5];
    if (x_fin < 0) x_fin = 0;
    if (y_fin < 0) y_fin = 0;
    ratio_w = (w_img - jsmove(vignette, "w")) / 10;
    ratio_h = (h_img - jsmove(vignette, "h")) / 10;
    ratio_x = (x_fin - jsmove(vignette, "left")) / 10;
    ratio_y = (y_fin - jsmove(vignette, "top")) / 10;
    
    
    if (elm.complete == true) {
        agrandi(img, w_img, h_img);
        // Vignette Disparait
        document.getElementById(vignette).style.opacity = "0.1";
        document.getElementById(vignette).style.filter = "alpha(opacity=10)";
        active = vignette;
    } else {
        setTimeout("ouvre_img('"+vignette+"', '"+img+"', '"+w_img+"', '"+h_img+"')", 10);
    }
}

// Agrandir
function agrandi(img, w_img, h_img) {
    vitesse = vitesse + 5;
    var elm = document.getElementById("imgpp");
    elm.style.left = (parseInt(elm.style.left) + ratio_x + vitesse) + "px";
    elm.style.top = (parseInt(elm.style.top) + ratio_y + vitesse) + "px";
    elm.style.width = (parseInt(elm.style.width) + ratio_w + vitesse) + "px";
    elm.style.height = (parseInt(elm.style.height) + ratio_h + vitesse) + "px";
    elm.style.opacity = parseFloat(elm.style.opacity)+0.05;
    
    if (parseInt(elm.style.width) <= w_img) {
        setTimeout("agrandi('"+img+"', '"+w_img+"', '"+h_img+"')", 70 - (vitesse*1.5 + 45) );
    } else {
        verif_complete();
        elm.className = "popup_in";
        elm.style.width = w_img + "px";
        elm.style.height = h_img + "px";
        elm.style.left = (parseInt(elm.style.left) - ratio_x) + "px";
        elm.style.top = (parseInt(elm.style.top) - ratio_y) + "px";
    }
}
function verif_complete() {
    if (preloader.complete == true) {
    var elm = document.getElementById("imgpp");
    elm.src = preloader.src;
    elm.style.opacity = "1";
    setTimeout("voile_noir(9)", 0);
    setTimeout("voile_noir(8.5)", 30);
    setTimeout("voile_noir(8)", 60);
    setTimeout("voile_noir(7)", 90);
    setTimeout("voile_noir(6)", 120);
    setTimeout("voile_noir(5)", 150);
    setTimeout("voile_noir(3)", 180);
    setTimeout("voile_noir(2)", 210);
    } else {
        setTimeout("verif_complete()", 50);
    }
}



// ferme
function ferme_img() {
    document.getElementById("total").style.opacity = "1";
        document.getElementById("total").style.filter = "alpha(opacity=100)";
    var elm = document.getElementById("imgpp");
        elm.className = "popup";
    elm.style.width = elm.style.height = "0";
    elm.style.display = "none";
    elm.style.src = " ";
    document.getElementById(active).style.opacity = "1";
    document.getElementById(active).style.filter = "alpha(opacity=100)";
}






















// Cacul de la taille fenetre

function getPageSize(){
	
	var xScroll, yScroll, y_ratio;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
		y_ratio = window.pageYOffset;
	} else if (document.body.scrollTop){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
		y_ratio = document.body.scrollTop;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.documentElement.offsetWidth;
		yScroll = document.documentElement.offsetHeight;
		y_ratio = document.documentElement.scrollTop;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,xScroll,y_ratio) 
	return arrayPageSize;
}
