/**
 * Copyright 2008 C.line
 * c.line@ymail.com
 **/

var cur_img = "";
var cur_alb = "";

/* Current displayed album (level 0)*/
function setCurrentAlbum(albumName){
	cur_alb = albumName;
}

/* load image */
function preloadingMini(directory, file, indexDiv)
{
    url = directory+"/"+file;
    var xhr_obj = null;

    if(window.XMLHttpRequest) // Firefox
        xhr_obj = new XMLHttpRequest();
    else if(window.ActiveXObject) // Internet Explorer
        xhr_obj = new ActiveXObject("Microsoft.XMLHTTP");
    else { // XMLHttpRequest non support par le navigateur
        alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
        return;
    }

    xhr_obj.onreadystatechange=function(){

        if(xhr_obj.readyState == 4){
            var content = xhr_obj.responseText;
            var im = new Image(70,70);

            im.src = content;

            document.getElementById("pic_"+indexDiv).innerHTML = "<img src=\""+directory+"/"+file+"\" alt=\"#\" onmouseover=\"do_displayPhoto('"+directory+"', '"+file+"'); cur_img='"+file.replace("gif", "png")+"';\" >";

        }
    };

    xhr_obj.open("GET", url , true);
    xhr_obj.send(null);
}

/* load image */
function preloadingLarge(directory, file)
{

    url = directory+"/"+file;
    var xhr_obj = null;

    if(window.XMLHttpRequest) // Firefox
        xhr_obj = new XMLHttpRequest();
    else if(window.ActiveXObject) // Internet Explorer
        xhr_obj = new ActiveXObject("Microsoft.XMLHTTP");
    else { // XMLHttpRequest non support par le navigateur
        alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
        return;
    }

    xhr_obj.onreadystatechange=function(){
        if(xhr_obj.readyState == 4){
            //alert(xhr_obj.responseText);

            var content = xhr_obj.responseText;
			if(cur_img == file){
            	var im = new Image(); //taille

            	im.src = content;
			
	            document.getElementById("piclarge").style.visibility = 'hidden';
	            document.getElementById("piclarge").innerHTML = "<img  src=\""+directory+"/"+file+"\" alt=\"#\" onload=\"document.getElementById('chrgmt').style.display='none'; document.getElementById('piclarge').style.visibility = 'visible'; document.getElementById('copyright').style.display='block';\" >";
        	}
        }
    };

    xhr_obj.open("GET", url , true);
    xhr_obj.send(null);
}

/* Manage sub-menu and Display mini photos of an album */
function do_displayAlbum(selObj, cur){
	
	var rep = selObj.split("|");
	
	dir = rep[0];
	
    var xhr_object = null;

    if(window.XMLHttpRequest) // Firefox
       xhr_object = new XMLHttpRequest();
    else if(window.ActiveXObject) // Internet Explorer
       xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    else { // XMLHttpRequest non support par le navigateur
       alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
       return;
    }

    xhr_object.open("GET", "files/functions.php5?displaySubMenuAlbum="+rep[1], true);

    xhr_object.onreadystatechange = function() {
        if(xhr_object.readyState == 4){
			
	        document.getElementById("album").style.display = "block";
	        document.getElementById("content").style.marginLeft = "47%";
	        document.getElementById("piclarge").innerHTML = "";
		        
	        if(xhr_object.responseText != "" && xhr_object.responseText.split("|").length > 0){

		        var listSubAlbum = document.getElementById("subalbum_"+rep[1]);
		        listSubAlbum.style.display = "block";
		        
		        var subAlbums = xhr_object.responseText.split("|");
				do_displayAlbumPhotos(subAlbums[0]);
				
	        }
	        else{
		    	do_displayAlbumPhotos(rep[0]);
			}

            if(cur_alb == "") cur_alb = cur;
		        
	        if(cur_alb != cur){
		        if(document.getElementById("subalbum_"+cur_alb))
		        	document.getElementById("subalbum_"+cur_alb).style.display = "none";
		        cur_alb = cur;
	        }
        }
    }

    xhr_object.send(null);
}

/** Display photos of an album **/
function do_displayAlbumPhotos(dir){
    var area = document.getElementById("album");
    area.innerHTML = "<img src=\"img/ajax-loader.gif\" atl=\"loading\">";

    var xhr_object = null;

    if(window.XMLHttpRequest) // Firefox
       xhr_object = new XMLHttpRequest();
    else if(window.ActiveXObject) // Internet Explorer
       xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    else { // XMLHttpRequest non support par le navigateur
       alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
       return;
    }

    xhr_object.open("GET", "files/functions.php5?displayAlbum="+dir, true);

    xhr_object.onreadystatechange = function() {
        if(xhr_object.readyState == 4){

	        if(xhr_object.responseText != ""){
	            var photos = xhr_object.responseText.split("|");
	            area.innerHTML = "";
	            for(var i=0; i<photos.length; i++){
	                // entourer dune div relative
	                // div chrgmt numerote
	                area.innerHTML += "<div class=\"pic\" id=\"pic_"+i+"\" ><img src=\"img/ajax-loader.gif\" atl=\"...\"></div>";
	
	                preloadingMini(photos[i].split(",")[0], photos[i].split(",")[1],i);
	            }
        	}
        	else
        		area.innerHTML = "";
        }
    }

    xhr_object.send(null);	
}


/* display a large picture when rollover a mini */
function  do_displayPhoto(rep, mini){


    mini = mini.replace("gif", "png");
 	var bigPhoto = preloadingLarge(rep+"/_realSize", mini);

  	document.getElementById("piclarge").style.visibility="hidden";
 	document.getElementById("chrgmt").style.display="block";

}

function  do_displayNothing(){
    var area = document.getElementById("piclarge");
    area.innerHTML = " ";
}




  

