var includesPath = "photo";

function getWindowHeight() {
    if (document.body)
    {
	return (document.body.offsetHeight);
    }
    else
    {
	return (window.innerHeight);
    }
}

function getWindowWidth() {
    if (document.body)
    {
	return (document.body.offsetWidth);
    }
    else
    {
	return (window.innerWidth);
    }
}

function displayLoad(id) {
    var container = document.getElementById(id);
    var divLoad = document.createElement("div");
    divLoad.setAttribute("style", "position : relative; float : left; min-width : 100%; min-height : 100%; background-color : grey; background-image : url(includes/data/img/ajax-loader.gif); background-repeat : no-repeat; background-position : center; opacity : 0.4;");
    container.appendChild(divLoad);
}

function getStyleCss(rule) {
    var style = document.styleSheets;
    var rules;
    for(var i = 0; i < style.length; i++) {
	if (style[i].cssRules)
	    rules = style[i].cssRules;
	else
	    rules = style[i].rules;
	for (var j = 0; j < rules.length; j++) {
	    if (rules[j].selectorText.toLowerCase() == rule)
		return rules[j];
	}
    }
    return false;
}

var diapoStart = false;

function diaporama(start, id) {
    var div = document.getElementById(id);
    if(start) {
	timer = setInterval("execDiaporama('"+id+"')", 5000);
	div.style.backgroundImage = "url("+includesPath+"/includes/data/img/stop-img.png)";
	diapoStart = true;
    }
    else {
	if(timer != null) {
	    diapoStart = false;
	    clearInterval(timer);
	    div.style.backgroundImage = "url("+includesPath+"/includes/data/img/play-img.png)";
	    timer = null;
	}
    }
}

function execDiaporama (id) {
    if(document.getElementById('nextImg').onclick != null)
	document.getElementById('nextImg').onclick();
    else
	diaporama(false, id)
}

function displayBigSizePhoto(id, album, index, path, width, height, duration, hasPrev, hasNext) {
    var container = document.getElementById(id);
    var divRefPos = container.parentNode;


    var rule = null;
    while (divRefPos.nodeName != "BODY") {

	if(((rule = getStyleCss("#"+divRefPos.id)) && rule.style.position == "absolute") ||
	   ((rule = getStyleCss("."+divRefPos.class))  && rule.style.position == "absolute") ||
	   ((rule = getStyleCss(divRefPos.nodeName)) && rule.style.position == "absolute")) {
	    break;
	}
	divRefPos = divRefPos.parentNode;
    }

    var wWidth = divRefPos.offsetWidth;
    var wHeight = divRefPos.offsetHeight;

    var divControl = document.createElement('div');
    divControl.id = "control";
    divControl.onclick = function () {diaporama(!diapoStart, divControl.id) };
    divControl.style.backgroundImage = "url("+includesPath+"/includes/data/img/play-img.png)";
    divControl.style.display = "none";
    
    var img = document.createElement('img');
    img.id = "imgOriginal";
    img.src = path;
    img.alt = path;
    img.style.display = "none";
    
    var divPrev = document.createElement('div');
    divPrev.id = "prevImg";
    divPrev.style.display = (hasPrev)?"":"none";
    divPrev.onclick = function (event) {
	event.stopPropagation();
	img.style.opacity = 0;
	getAjaxFile(includesPath+"/includes/php/interfaces/functions.php?method=getPhotoPath&serializedalbum="+album+'&index='+(--index), 
		    null, 
		    "GET", 
		    null, 
		    callback);
    };

    var divNext = document.createElement('div');
    divNext.id = "nextImg";
    divNext.style.display = (hasNext)?"":"none";
    divNext.onclick = function (event) {
	event.stopPropagation();
	img.style.opacity = 0;
	getAjaxFile(includesPath+'/includes/php/interfaces/functions.php?method=getPhotoPath&serializedalbum='+album+'&index='+(++index), 
		    null, 
		    "GET", 
		    null, 
		    callback); 
    };

    var callback = function(node) {
	var root = node.documentElement;
	var path = root.getElementsByTagName("path")[0].childNodes[0].nodeValue;
	var next = root.getElementsByTagName("hasnext")[0].childNodes[0].nodeValue;
	var prev = root.getElementsByTagName("hasprev")[0].childNodes[0].nodeValue;

	img.src = path;
	img.onload = function () { fade (img.id, 1); };
	getAjaxFile(includesPath+"/includes/php/interfaces/functions.php?method=getExifDataDiv&serializedalbum="+album+'&index='+index, "exifDiv", "GET", null);

	// Controle de l'affichage de la navigation droite
	divNext.style.display = (next=="true")?"":"none";
	
	// Controle de l'affichage de la navigation gauche
	divPrev.style.display = (prev=="true")?"":"none";
    };

    var divNavigation = document.createElement('div');
    divNavigation.id = "navigationBar";
    divNavigation.style.width = width+"px";
    divNavigation.appendChild(divPrev);
    divNavigation.appendChild(divNext);

    var exif = document.createElement('div');
    exif.setAttribute('id', 'exifDiv');
    exif.style.width = (width-10)+"px";
    exif.style.top = (height-90)+"px";

    var divClose = document.createElement('div');
    divClose.id = "closeImg";
    divClose.onclick = function (event) { event.stopPropagation(); div.style.display="none"; };
    divClose.appendChild(document.createTextNode(" "));
    

    if (document.getElementById('photoDiv')) {
	container.removeChild(document.getElementById('photoDiv'));
    }

    var div = document.createElement('div');
    div.setAttribute('id', 'photoDiv');
    div.style.height =  (height / 2) + "px";
    div.style.width =  (width / 2) + "px";
    div.style.top = ((wHeight - parseInt(div.style.height)) / 2) + "px";
    div.style.left = ((wWidth - parseInt(div.style.width)) / 2) + "px";

    divControl.appendChild(exif);    
    divControl.appendChild(divClose);
    divControl.appendChild(divNavigation);
    
    div.appendChild(img);
    div.appendChild(divControl);

    container.appendChild(div);

    getAjaxFile(includesPath+"/includes/php/interfaces/functions.php?method=getExifDataDiv&serializedalbum="+album+"&index="+index, "exifDiv", "GET", null);

    animateBigSizePhoto(div.id, wWidth, wHeight, width, height, duration, false);
}

function animateBigSizePhoto (id, wWidth, wHeight, width, height, duration, up) {
    var div = document.getElementById(id);
    
    if (parseInt(div.style.height) < height) {
	div.style.height = (parseInt(div.style.height) + (height / (duration / 2))) + "px";
	div.style.top = ((((wHeight - parseInt(div.style.height)) / 2) < 10)?10:((wHeight - parseInt(div.style.height)) / 2)) + "px";
    } else {
	if (isNaN(parseInt(div.style.height))) {
	    div.style.height = (height / 2)+"px";
	    div.style.top = ((((wHeight - parseInt(div.style.height)) / 2) < 10)?10:((wHeight - parseInt(div.style.height)) / 2)) + "px";
	}
	else {
	    up = !up;
	}
    }
    
    if (up) {
	if (parseInt(div.style.width) < width) {
	    div.style.width = (parseInt(div.style.width) + (width / (duration / 2))) + "px";
	    div.style.left = ((wWidth - parseInt(div.style.width)) / 2) + "px";
	} else {
	    if (isNaN(parseInt(div.style.width))) {
		div.style.width = (width / 2) + "px";
		div.style.left = ((wWidth - parseInt(div.style.width)) / 2) + "px";
	    }
	}
    }
    
    if (parseInt(div.style.height) >= height && parseInt(div.style.width) >= width) {
	div.childNodes[0].style.display = "";
	div.childNodes[1].style.display = "";
	div.style.height = height+"px";
	div.style.width = width+"px";
	return;
    }
    else {
	setTimeout("animateBigSizePhoto('"+id+"', "+wWidth+", "+wHeight+","+width+","+height+","+duration+", "+up+")", 1);
    }
}

var currentTranslation

function animatePage(node) {
    currentTranslation = new translatePage(node);
    currentTranslation.startTranslate();
}

function translatePage(node) {
    this.idNextPage = "nextPage";
    this.idCurrentPage = "currentPage";
    this.idContainerPage = "pageContainer";

    this.newContent = node.childNodes[0];

    this.nextPage = document.getElementById(this.idNextPage);
    this.currentPage = document.getElementById(this.idCurrentPage);
    this.containerPage = document.getElementById(this.idContainerPage);

    this.startTranslate = function () {
	this.nextPage.appendChild(this.newContent);
	this.currentPage.style.marginLeft = "0px";
	this.timer = setInterval("currentTranslation.execTranslate()", 1);
    }

    this.execTranslate = function () {
	if (parseInt(this.currentPage.offsetWidth) <= parseInt(this.currentPage.style.marginLeft))
	    this.endTranslate();	    
	else {
	    this.currentPage.style.marginLeft = (parseInt(this.currentPage.style.marginLeft)-10) + "px";
	}
    }

    this.endTranslate = function () {
	if (this.timer != null)
	    clearInterval(this.timer);
	this.timer = null;

	this.containerPage.removeChild(this.currentPage);
	this.nextPage.id = this.idCurrentPage;
	this.currentPage = this.nextPage;
	this.nextPage = document.createElement("div");
	this.nextPage.id = this.idNextPage;
	this.containerPage.appendChild(this.nextPage);
    }
}

function ajuste(id) {

    var div = document.getElementById(id);
    var nodes = div.childNodes;
    var max = 0;

    for (var i=0; i < nodes.length; i++) {
	if (nodes[i].nodeType == 1) {
	    if (nodes[i].offsetHeight > max) {
		max = nodes[i].offsetHeight;
	    }
	}
    }

    for (var j=0; j < nodes.length; j++) {
	if (nodes[j].nodeType == 1) {
	    nodes[j].style.height = max + "px";
	}
    }
}

var fadingArray = new Array;

function fade (id, o) {
    if (fadingArray[id]) {
	fadingArray[id].stopFade();
	fadingArray[id] == null;
    }
    fadingArray[id] = new Fadable(id, o);
    fadingArray[id].startFade();
}

function execFadeInterval (id) {
    fadingArray[id].timer = setInterval("fadingArray['"+id+"'].execFade()", 1);
}

function Fadable (id, o) {
    this.id = id;
    this.elt = document.getElementById(id);
    this.o = o;
    this.timer = null;
    this.execFade = function (){
	if (this.o == parseFloat(this.elt.style.opacity)) {
	    this.elt.style.opacity = parseFloat(this.o);
	    clearInterval(this.timer);
	    this.timer = null;
	}
	else if (this.o > parseFloat(this.elt.style.opacity)) {
	    this.elt.style.opacity = parseFloat(this.elt.style.opacity) + 0.02;
	}
	else if (this.o < parseFloat(this.elt.style.opacity)) {
	    this.elt.style.opacity = parseFloat(this.elt.style.opacity) - 0.02;
	}
    };
    this.startFade = function () {
	if (this.timer != null)
	    clearInterval(timer);
	execFadeInterval(this.id);
    }
    this.stopFade = function () {
	if (this.timer != null)
	    clearInterval(this.timer);
	this.timer = null;
    }
}
