/*

Copyright (c) 2005, Daniel Juliano
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:
	
    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.
    * The names of contributors to this source code may not be used to endorse 
      or promote products derived from this software without specific prior 
      written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

var jpRootElement = null;
var jpAlbums = null;
var jpCategories = null;

function initializePhotos(url, id) {
	jpRootElement = document.getElementById(id);
	highlightInit(Array("photo_div", "photo_div_highlight"), 0);
	xmlRequest(url, null, jaxPhotoXMLHandler);
}
function jaxPhotoXMLHandler(xml) {
	
	// Capture filter and album trees.
	filter = xml.getElementsByTagName("list_filter")[0];
	albums = xml.getElementsByTagName("list_albums")[0];
	
	// Check for empty trees.
	if (albums == null || albums.childNodes.length == 0) {
		var html = "";
		html += "Please visit the admin section <a href=\"admin.php\">here</a> \n";
		html += "or view the installation setup file <a href=\"README.html\">here</a> \n";
		document.getElementById("photo_filter").innerHTML = html;
		return;
	}
	
	// Move xml to an array.
	var records = albums.getElementsByTagName("record");
	jpAlbums = Array();
	for (var i = 0; i < records.length; i++) {
		jpAlbums[i] = Array();
		
		jpAlbums[i][0] = jaxPhotoConditionalRead(records[i], "title");
		jpAlbums[i][1] = jaxPhotoConditionalRead(records[i], "description");
		jpAlbums[i][2] = jaxPhotoConditionalRead(records[i], "path_album");
		jpAlbums[i][3] = jaxPhotoConditionalRead(records[i], "path_thumb");
		jpAlbums[i][4] = jaxPhotoConditionalRead(records[i], "categories");
		jpAlbums[i][5] = jaxPhotoConditionalRead(records[i], "modified_1");
		jpAlbums[i][6] = jaxPhotoConditionalRead(records[i], "modified_2");
		jpAlbums[i][7] = jaxPhotoConditionalRead(records[i], "photocount");
	}
	
	if (filter == null || filter.childNodes.length == 0) {
		jpCategories = Array();
		jpCategories[0] = "All";
		
	} else {
		var records = filter.getElementsByTagName("record");
		jpCategories = Array();
		jpCategories[0] = "All";
		for (var i = 0; i < records.length; i++) {
			jpCategories[i + 1] = records[i].firstChild.nodeValue;
		}
	}
	
	jaxPhotoFilterCategories();
	jaxPhotoShowAlbums(null);
}
function jaxPhotoShowAlbums(elementId) {
	
	var html = "\n";
	var html_photo = "";
	
	var valid = false;
	var category = "";
	var categories = "";
	var filter = "";
	if (elementId != null) {
		filter = jpCategories[parseInt(elementId.substring("filter_".length))];
	}
	
	for (var i = 0; i < jpAlbums.length; i++) {
		
		// Filtering may exclude the current album if it does not contain the same category.
		valid = false;
		if (elementId == null || elementId == "filter_0") {
			valid = true;
		} else {
			categories = jpAlbums[i][4].split(",");
			for (var j = 0; j < categories.length; j++) {
				category = categories[j].replace(/^\s*|\s*$/g,"");	// trim spaces.
				if (category != "" && category == filter) {
					valid = true;
					break;
				}
			}
		}
		
		if (valid) {
			html_photo = "\n";
			
			html_photo += "<div class=\"photo_div\" onClick=\"location.href='" + jpAlbums[i][2] + "'\" onMouseOver=\"highlightRow(this, 1);\" onMouseOut=\"highlightRow(this, 0);\">\n";
			html_photo += "\t<div class=\"photo_thumbnail\"><img src=\""+ jpAlbums[i][3] + "\" class=\"photo_thumbnail\" /></div>\n";
			html_photo += "\t<div class=\"photo_meta\">last updated " + jpAlbums[i][5] + "<br />" + jaxPhotoCleanCategories(jpAlbums[i][4]) + "<br />" + jaxPhotoCleanCount(jpAlbums[i][7]) + "</div>\n";
			html_photo += "\t<div class=\"photo_title\">" + jpAlbums[i][0] + "</div>\n";
			html_photo += "\t<div class=\"photo_description\">" + jaxPhotoCleanDescription(jpAlbums[i][1]) + "</div>\n";
			html_photo += "</div>\n";
			
			html += html_photo;
		}
	}
	jpRootElement.innerHTML = html + "\t\t";
}
function jaxPhotoCleanDescription(description) {
	var cutoff = 180;
	if (description.length > cutoff) {
		for (var i = cutoff; i > 0; i--) {
			if (description.charAt(i) == " ") {
				return description.substring(0, i) + " ...";
			}
		}
	}
	return description;
}
function jaxPhotoCleanCategories(categories) {
	if (categories == "") return "no categories listed";
	return "categories: " + categories;
}
function jaxPhotoCleanCount(count) {
	if (count == "") return "photo count unknown";
	return count + " photos";
}
function jaxPhotoConditionalRead(record, key) {
	var nodes = record.getElementsByTagName(key);
	if (nodes.length > 0) {
		if (nodes[0].firstChild) {
			return nodes[0].firstChild.nodeValue;
		}
	}
	return "";
}

function jaxPhotoFilterCategories() {
	
	var html = "\n";
	html += "\t\t\t<div class=\"filter_category\">Filter:</div>\n";
	for (var i = 0; i < jpCategories.length; i++) {
		html += "\t\t\t<div class=\"filter_category\">" + jpCategories[i] + "</div>\n";
	}
	document.getElementById("photo_filter").innerHTML = html + "\t\t";
	
	var children = document.getElementById("photo_filter").childNodes;
	var width = 80;
	var offset_x = 5;
	var offset_y = 5;
	count = 0;
	for (var i = 0; i < children.length; i++) {
		if (children[i].nodeName == "DIV") {
			if (count == 0) {
				children[i].style.borderStyle = "none";
				children[i].style.cursor = "default";
			} else {
				children[i].id = "filter_" + (count - 1);
				children[i].onclick = function(event) { jaxPhotoShowAlbums(this.id) };
				children[i].onmouseover = function(event) { highlight(this, true) };
				children[i].onmouseout = function(event) { highlight(this, false) };
			}
			children[i].style.left = (offset_x + (count++ * (width + offset_x)))  + "px";
			children[i].style.top = offset_y + "px";
		}
	}
}
function highlight(element, over) {
	if (over) {
		element.className = "filter_category_highlight";
	} else {
		element.className = "filter_category";
	}
}

