function initHSLider() {
	hslider = new HSlider('categoriesAJAX', {itemWidth : 209});
}

function addCategory(inCol,id,name,subfolders) {
	new Insertion.Bottom("col"+inCol, '<li'+((subfolders) ? ' class="f"' : '')+'><a href="javascript:clickOnCategory(\''+id+'\',\''+inCol+'\');">'+name+'</a></li>');
	$("col"+inCol).style.opacity = "0";
	new Effect.Opacity("col"+inCol, {duration:0.2,from:0.0,to:1.0,afterFinish:function() {$("col"+inCol).style.position="relative";}});
	
}

function clickOnCategory(id,col,openCat) {
	if (openCat != false) {
		clearColumnsAfter(col);
		openCategory(id);
	}
	setTitle(id);
	getPhotos(id);
	hidePhoto();
}

function addPhoto(id,name,url,large) {
	new Insertion.Bottom("photosAJAX", '<li><a href="javascript:showPhoto(\''+id+'\');"><img src="'+url+'" /></a></li>');
}

function addColumn(id) {
	if ($("col"+id) != null) {
		$("col"+id).innerHTML = "";
	}
	else
		new Insertion.Bottom("categoriesAJAX",'<li id="colli'+id+'"><ul id="col'+id+'" class="column"></ul></li>');
}

function initCategories() {
	new Ajax.Request(root+"xml/categories.php", {onSuccess:function(obj) {initCategoriesNext(obj);}});
}

function initCategoriesNext(obj) {
	doc = getXML(obj);
	doc = doc.getElementsByTagName("xmldocument")[0];
	
	openCategory(-1);
}

function getFolders(inCol) {
	if (inCol == -1)
		xml = doc;
	else {
		txml = getFolderById(inCol);
		childs = childElements(txml);
		for (i=0;i<childs.length;i++) {
			if (childs[i].nodeName == "folders") {
				xml = childs[i];
				i = childs.length;							
			}
		}
	}

	folders = new Array();
	childs = childElements(xml);
	for(i=0;i<childs.length;i++) {
		folder = childs[i];
		folderChilds = childElements(folder);
		fol = new Array();
		fol.push(folderChilds[0].firstChild.nodeValue); //id
		fol.push(folderChilds[1].firstChild.nodeValue); //name
		fol.push(""); //desc
		fol.push(folderChilds[3].firstChild.nodeValue); //photos
		fol.push(childElements(folderChilds[4]).length); //nb sub folders
		folders.push(fol);
	}
	return folders;
}

function getFolderById(id) {
	folders = doc.getElementsByTagName("folder");
	for (i=0;i<folders.length;i++) {
		if (folders[i].childNodes[1].firstChild.nodeValue == id)
			return folders[i];
	}
	return 0;
}

function getPhotoById(id) {
	photos = photosXML.getElementsByTagName("photo");
	for (i=0;i<photos.length;i++) {
		if (photos[i].childNodes[1].firstChild.nodeValue == id)
			return photos[i];
	}
	return 0;
}

function clearColumnsAfter(id) {
	var elements = findElements("categoriesAJAX","li");
	var beginClear = false;
	for(i=0;i<elements.length;i++) {
		e = elements[i];
		if (beginClear) {
			Element.remove(e.id);
		}
		else if (e.id == "colli"+id) {
			beginClear = true;
		}
	}
}

function setTitle(id) {
	var folder = getFolderById(id);
	var infos = childElements(folder);
	$("photosTitle").innerHTML = "Photos de \""+infos[1].firstChild.nodeValue+"\"";	
}

function openCategory(inCol) {
	var folders = getFolders(inCol);
	if (folders.length > 0) {
		addColumn(inCol);
		for (i=0;i<folders.length;i++) {
			var f = folders[i];
			var t = f[1]+" ("+f[3]+")";
			if (f[4] > 0) { 
				var subfolders = true;
			}
			else
				var subfolders = false;
				
			addCategory(inCol,f[0],t,subfolders);
			
			if (inCol == -1 && i == 0) {
				clickOnCategory(f[0],inCol,false);
			}
		}
	}
	checkSlide();
}

function checkSlide() {
	width = Element.getDimensions("categories").width - 20;
	var elements = findElements("categoriesAJAX","li");
	widthAjax = elements.length * 209;
	if (widthAjax < width) {
		hslider.scrollTo(0);
	}
	else {
		dif = widthAjax - width;
		nb = Math.ceil(dif / 209);
		hslider.scrollTo(nb);
	}
}

function getPhotos(id) {
	new Ajax.Request(root+"xml/photos.php?id="+id, {onSuccess:function(obj) {getPhotosNext(obj);}});
}

function getPhotosNext(obj) {
	$("photosAJAX").innerHTML = "";
	photosXML = photos = getXML(obj);
	photos = photos.getElementsByTagName("xmldocument")[0];
	childs = childElements(photos);
	for(i=0;i<childs.length;i++) {
		child = childs[i];
		infos = childElements(child);
		id = infos[0].firstChild.nodeValue;
		name = infos[1].firstChild.nodeValue;
		tn = root + infos[3].firstChild.nodeValue;
		large = root + infos[4].firstChild.nodeValue;
		addPhoto(id,name,tn,large);
	}
}

function showPhoto(id) {
	folder = getPhotoById(id);
	infos = childElements(folder);
	$("largeTitle").innerHTML = infos[1].firstChild.nodeValue;
	$("largeContent").innerHTML = "";
	if (infos[2].firstChild != null && infos[2].firstChild.nodeValue != "" && infos[2].firstChild.nodeValue != undefined)
		$("largeContent").innerHTML = '<p class="description">'+infos[2].firstChild.nodeValue+'</p>';
	$("largeContent").innerHTML += '<img src="'+root+infos[4].firstChild.nodeValue+'" />';
}

function hidePhoto() {
	$("largeTitle").innerHTML = "";
	$("largeContent").innerHTML = "";
}

function childElements(parentNode) {
     var l= new Array();
     for (var i= 0; i<parentNode.childNodes.length; i++)
       if (parentNode.childNodes[i].nodeType==1)
         l[l.length]= parentNode.childNodes[i];
     return l;
}

Event.observe(window, 'resize', checkSlide);
Event.observe(window, 'load', initHSLider);
Event.observe(window, 'load', initCategories);