/**
 * function get_templates()
 *
 * get templates available for currently selected page
 *
 * param int index "current page index"
 */
function get_templates(index) {
	var tl = document.getElementById("ts_content");
	var select_template_ID = 0;
	
	/**
	 * create new instance of XML parse, get response and parse
	 */
    var request = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n";
	request += "<request_data>\n";
	request += "	<command>get_page_univ</command>\n";
	request += "	<index>" + index + "</index>\n";
	request += "</request_data>\n";

	var xml = new JKL.ParseXML(ifc_url, "input=" + request);
	var data = xml.parse();
	err(data.response_data, "get_templates()", request);
	var template = data.response_data.page;

	/**
	 * clear template list
	 */
	if (tl.hasChildNodes()) {
		while (tl.childNodes.length >= 1) tl.removeChild(tl.firstChild);
	}

	/**
	 * if area is not array, we need to transform it into array
	 */
	if (!template[0]) {
		tmp = [];
		tmp[0] = template;
		template = tmp;
	}

	/**
	 * because we want to have template thumbnails transparent, we must detect
	 * MSIE6 and lower to apply transparency properly
	 */
	is_ie6 = navigator.userAgent.toLowerCase().indexOf("msie 6") != -1;

	/**
	 * display templates. we must use temporary variable because of MSIE
	 */
	if (template[0]) {
		for (var i in template) {

			/**
			 * template wrap
			 */
			var t_wrap = document.createElement("div");
			t_wrap.id = "template_" + template[i].pt_page_ID;
			addEvent(t_wrap, "mouseover", tpl_over);
			addEvent(t_wrap, "mouseout", tpl_out);
			addEvent(t_wrap, "click", change_template);

			/**
			 * thumbnail (if any)
			 */
			if (template[i].thumbnail) {
				var t_thumbnail = document.createElement("img");
				t_thumbnail.src = tt_url + template[i].thumbnail;
				
				t_thumbnail.alt = "náhled šablony";
				t_wrap.appendChild(t_thumbnail);
				t_thumbnail = null;
			}

			/**
			 * description (if any)
			 */
			if (template[i].title) {
				var t_comment = document.createElement("p");
				t_comment.innerHTML = base64_decode(template[i].title);
				t_wrap.appendChild(t_comment);
			}

			/**
			 * append template into list
			 */
			tl.appendChild(t_wrap);

			/**
			 * update index, which will be used for page selection
			 */
			if (select_template_ID == 0) select_template_ID = template[i].pt_page_ID;
		}

		/**
		 * select first template and draw page
		 */
		select_template(select_template_ID);
		draw_page();
	}
}


/**
 * function select_template()
 *
 * select template specified by ID of parent element, where event was performed.
 * unmark all templates in template list but this one and call function to draw
 * page into template content
 *
 * @param int|event "evt" (event which called this function or integrer
 *                         indentyfying directly selected template)
 */
function select_template(pt_page_ID) {

	/**
	 * clear array with assigned images
	 */
	assigned_images = [];

	/**
	 * if template object is in template wrapper, remove it
	 */
	if (document.getElementById("template")) document.getElementById("template_wrap").removeChild(document.getElementById("template"));

	/**
	 * unmark all templates in list except the one currently selected
	 */
	var de = document.getElementById("ts_content").childNodes;
	for (var i = 0; i < de.length; i++) de[i].className = de[i].getAttribute("id") == ("template_" + pt_page_ID) ? "template_selected" : "";
}


/**
 * function select_template()
 *
 * select template specified by ID of parent element, where event was performed.
 * unmark all templates in template list but this one and call function to draw
 * page into template content
 *
 * @param int|event "evt" (event which called this function or integrer
 *                         indentyfying directly selected template)
 */
function change_template(evt) {
	evt = evt || window.event;
	var element = evt.srcElement ? evt.srcElement.parentNode : evt.target.parentNode;
	var pt_page_ID = element.id.split("_")[1];
	
	/**
	 * sometimes user can click outside template selector, even when
	 * mose is over. we must ignore theese clicks to avoid errors	 
	 */
	if (pt_page_ID != "content") {

		/**
		 * if there are some assigned images, we must store them before leaving page
		 */
		if (assigned_images.length > 0) {

			/**
			 * now the funny stuff. when we are changing page, we must save all that was
			 * done on this page... so we will begin creating request
			 */
			var request = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n";
			request += "<request_data>\n";
			request += "	<command>set</command>\n";
			request += "	<index>" + current_page_index + "</index>\n";

			/**
			 * go through assigned images
			 */
			for (var i in assigned_images) {
				if (assigned_images[i]) {

					/**
					 * expand request
					 */
					request += "	<area>\n";
					request += "		<pt_area_ID>" + i + "</pt_area_ID>\n";
					request += "		<src>" + assigned_images[i]["src"] + "</src>\n";
					request += "		<pt_image_ID>" + assigned_images[i]["pt_image_ID"] + "</pt_image_ID>\n";
					request += "		<rotation>" + assigned_images[i]["rotation"] + "</rotation>\n";
					request += "		<crop>\n";
					request += "			<left>" + assigned_images[i]["left"] + "</left>\n";
					request += "			<top>" + assigned_images[i]["top"] + "</top>\n";
					request += "			<width>" + assigned_images[i]["rel_width"] + "</width>\n";
					request += "			<height>" + assigned_images[i]["rel_height"] + "</height>\n";
					request += "		</crop>\n"

					/**
					 * count final cropping values and expand request
					 */
					var final_width = 1 / assigned_images[i]["rel_width"];
					var final_height = 1 / assigned_images[i]["rel_height"];
					var final_left = -1 * (1 / assigned_images[i]["rel_width"] * assigned_images[i]["left"]);
					var final_top = -1 * (1 / assigned_images[i]["rel_height"] * assigned_images[i]["top"]);

					request += "		<crop_final>\n";
					request += "			<left>" + final_left + "</left>\n";
					request += "			<top>" + final_top + "</top>\n";
					request += "			<width>" + final_width + "</width>\n";
					request += "			<height>" + final_height + "</height>\n";
					request += "		</crop_final>\n"
					request += "	</area>\n";
				}
			}

			/**
			 * end request and send it
			 */
			request += "</request_data>";

			var xml = new JKL.ParseXML(ifc_url, "input=" + request);
			var data = xml.parse();
			err(data.response_data, "select_page()", request);

			/**
			 * reset field with assigned images
			 */
			assigned_images = [];
			drop_areas = [];
		}

		/**
		 * clear array with assigned images
		 */
		assigned_images = [];

		/**
		 * if template object is in template wrapper, remove it
		 */
		if (document.getElementById("template")) document.getElementById("template_wrap").removeChild(document.getElementById("template"));

		/**
		 * unmark all templates in list except the one currently selected
		 */
		var de = document.getElementById("ts_content").childNodes;
		for (var i = 0; i < de.length; i++) de[i].className = de[i].getAttribute("id") == ("template_" + pt_page_ID) ? "template_selected" : "";

		/**
		 * set this template into session as template for current page index
		 */
	    var request = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n";
		request += "<request_data>\n";
		request += "	<command>alter_page</command>\n";
		request += "	<index>" + current_page_index + "</index>\n";
		request += "	<pt_page_ID>" + pt_page_ID + "</pt_page_ID>\n";
		request += "</request_data>\n";

		var xml = new JKL.ParseXML(ifc_url, "input=" + request);
		var data = xml.parse();
		err(data.response_data, "select_template()", request);

		/**
		 * set current page ID
		 */
		current_pt_page_ID = pt_page_ID;

		/**
		 * draw page
		 */
		draw_page();
	}
}

function add_page(num) {
	var request = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n";
	request += "<request_data>\n";
	request += "	<command>add_page</command>\n";
	if (num != null) request += "	<num>" + num + "</num>\n";
	request += "</request_data>\n";
	
	var xml = new JKL.ParseXML(ifc_url, "input=" + request);
	var data = xml.parse();
	err(data.response_data, "add_page()", request);
	
	if (data.response_data.add_fail == 1) alert("Další stranu již nelze přidat, limit stránek byl překročen!");
	else {
		available_pages = parseInt(data.response_data.pages_count);
		pagelist_create();
		select_page(current_page_index);
	}
}

function remove_page() {
	var request = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n";
	request += "<request_data>\n";
	request += "	<command>remove_page</command>\n";
	request += "	<index>" + current_page_index + "</index>\n";
	request += "</request_data>\n";
	
	var xml = new JKL.ParseXML(ifc_url, "input=" + request);
	var data = xml.parse();
	err(data.response_data, "remove_page()", request);
	
	if (data.response_data.remove_fail == 1) alert("Tuto stranu nelze odstranit!");
	else {
		available_pages = parseInt(data.response_data.pages_count);
		pagelist_create();
		select_page(current_page_index);
	}
}

