/**
 * function read_albumlist()
 *
 * read list of albums and display them as drop-down menu
 *
 * no params needed ;-)
 */
function read_albumlist() {

	/**
	 * create new instance of XML parse, get response and parse
	 */
	var request = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n";
	request += "<base_element>\n";
	request += "	<album universum=\"0\" />\n";
	request += "</base_element>\n";

    var xml = new JKL.ParseXML(ifc2_url, "input=" + request);
    var album = xml.parse();
	var album_element = album.base_element.album.albumElement;
	var album_selector = document.getElementById("album");

	/**
	 * go throught album elements and add options
	 */
	if (album_element.length == null) {
		var tmp = new Array();
		tmp[0] = album_element;
		album_element = tmp;
	}
	 
	for (var i in album_element) {

		var option = document.createElement("option");
		option.text = album_element[i].title;
		option.value = album_element[i].album_ID;

		try {
	    	album_selector.add(option, null);
	  	} catch(ex) {
			album_selector.add(option);
		}
	}
}
addEvent(window, "load", read_albumlist);


/**
 * function read_album()
 *
 * get list of photos in selected album, parse and call
 * function to display page
 *
 * @param string "url" (image list URL)
 */
function read_album(album_ID) {

	/**
	 * URL must not be empty
	 */
	if (album_ID != "") {
		switch (album_ID) {
			case "#login" :
				needLoginError();
				break;
				
			default :
				/**
				 * create new instance of XML parse, get response and parse
				 */
				var request = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n";
				request += "<base_element>\n";
				request += "	<photoElement action=\"return\">\n";
				request += "		<albumElement action=\"return\" album_ID=\"" + album_ID + "\" />\n";
				request += "	</photoElement>\n";
				request += "</base_element>\n";

			    var xml = new JKL.ParseXML(ifc2_url, "input=" + request);
			    var album = xml.parse();
				
				if (album.base_element != null) {
					tn = album.base_element.photoElement;
					tn_id = album.base_element.photoElement;
					
					if (tn.length == null) {
						var ta = new Array(), tb = new Array();
						ta[0] = tn;
						tb[0] = tn;
						tn = ta;
						tn_id = tb;
					}

					/**
					 * we need photo ID to be key in other array
					 */
					var tmp = [];
					for (var i in tn_id) tmp[tn[i].uni_ident] = tn_id[i];
					tn_id = tmp;
					
				} else {
					tn = [];
					tn_id = [];
				}

				/**
				 * display first album page
				 */
				album_page(1);
				break;
		}
	}
}


/**
 * function album_page()
 *
 * display image in album content dependign on selected page number,
 * display / hide links to previous / next page
 *
 * @param int "page_number" (page to display)
 */
function album_page(page_number) {

	/**
	 * set start and end index and previous and next page
	 */
	var index_end = page_number * album_page_images - 1;
	var index_start = index_end - album_page_images + 1;
	var page_prev = page_number - 1;
	var page_next = page_number + 1;
	var images_total = tn.length - 1;
	var pages_total = Math.ceil(tn.length / album_page_images);
	var is_content = document.getElementById("is_content");

	/**
	 * assign links to previous adn next page, if theese pages exists
	 */
	document.getElementById("is_prev").innerHTML = (page_prev >= 1 && page_prev <= pages_total) ? "<a href=\"\" title=\"předchozí\" onclick=\"album_page(" + page_prev + "); return false;\"></a>" : "";
	document.getElementById("is_next").innerHTML = (page_next >= 1 && page_next <= pages_total) ? "<a href=\"\" title=\"další\" onclick=\"album_page(" + page_next + "); return false;\"></a>" : "";

	/**
	 * if start index is higher than 0 and end index is lower or equal to ammount
	 * of element in thumbnail array, we can draw page...	 
	 */
	if (index_start > -1 && index_end <= (tn.length + album_page_images)) {

		/**
		 * clear thumbnails content, but do not remove childNodes
		 */
		if (is_content.hasChildNodes()) {
			while (is_content.childNodes.length >= 1) is_content.removeChild(is_content.firstChild);
		}

		/**
		 * display images
		 */
		var node_loop = 1;
		var album_key = 0;
		
		for (var i = index_start; i <= index_end; i++) {
			if (tn[i]) {

				/**
				 * we need image stored ina array
				 */
				album_images[album_key] = tn[i].full_path;
				album_images_medium[album_key] = tn[i].medium_path;
				album_images_resX[album_key] = tn[i].flag_resX;
				album_images_resY[album_key] = tn[i].flag_resY;
				album_images_ratio[album_key] = tn[i].flag_sizeRatio;

				/**
				 * create image element, append and add events
				 */
				var tn_img = document.createElement("div");
				tn_img.id = "image_" + album_key;
				tn_img.style.background = "url('/isrc/" + tn[i].tiny_path + "') 0 0 no-repeat";
				is_content.appendChild(tn_img);
				addEvent(tn_img, "mouseover", image_over);
				addEvent(tn_img, "mouseout", image_out);
				addEvent(tn_img, "mousedown", drag_image);

				/**
				 * unset image, increase node loop and overall album key
				 */
				tn_img = null;
				node_loop++;
				album_key++;
			} else {
				break;
			}
		}

		/**
		 * display text ammount of images currently shown, depending on their ammount
		 */
/*
		if (index_start == (i - 1)) {
			document.getElementById("is_info").innerHTML = "&nbsp;&middot;&nbsp;(obrázek " + (index_start + 1) + " z celkem " + (images_total + 1) + ")";
		} else {
			i--;
			document.getElementById("is_info").innerHTML = "&nbsp;&middot;&nbsp;(obrázky " + (index_start + 1) + " až " + (i + 1) + " z celkem " + (images_total + 1) + ")";
		}
*/
	}
}