var GUI_LANG 						= "de";
var GUI_LANGS						= ["de", "en", "fr"];
var DEFAULT_GUI_LANG			= "en";
var GUI_GALLERY_COUNT_COLS	= 5;
var GUI_SLIDESHOW_SECS			= 6;

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

var XML_DATA 						= null;
var XML_PRESENTATION			= null;	
var XML_EXHIBITION				= null;
var XML_SELECTION_ARRAY		= null;
var XML_SELECTION					= null;
var XML_SEL_ITEM_NODE			= null;
var XML_ARTWORK_ARRAY			= null;


/* ---------------------------------------------------------------------------------------------------------------------------------------- */
/* MAIN */

$(document).ready (function()
{
	$.get("frontend/jolegat_artwork.xml", function (_xml_data)
	{
		var gui_lang = $.query.get('lang');
		if (gui_lang != "") 
		{
			var lang_found = false;
			for (var i = 0; !lang_found && i < GUI_LANGS.length; i++)
			{
				if (GUI_LANGS[i] == gui_lang) lang_found = true;
			}
			if (lang_found) GUI_LANG = gui_lang;
		}

		XML_DATA = _xml_data;
		XML_PRESENTATION = joGetFirstElement (XML_DATA, "presentation");
		
		var exh_desc = joGetFirstElement (XML_PRESENTATION, "desc");
		if (exh_desc)
		{
			var exh_desc_text = GetLocaleTextObject (exh_desc)
			$("#menu_up").children().first().html ("&gt;" + GetRawText (joGetFirstElement (exh_desc_text, "title")));
		}

		document.onkeyup = OnKeyPress;


		ChoosePage($.query.get('exhibition'));
	})
})

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function ChoosePage (exhibition_id)
{
	XML_EXHIBITION = (exhibition_id && exhibition_id != "") ? joGetFirstElement (XML_PRESENTATION, "exhibition", [["id", exhibition_id]]) : null;
	XML_EXHIBITION ? ShowExhibition() : ShowStartPage();
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function GoUp()
{
	XML_SELECTION = null;
	SetSlideshowMode (false);
	
	if (XML_SELECTION)
	{
		ShowExhibition();
	}
	else
	{
		XML_EXHIBITION = null;
		ChoosePage();
	}
	
	
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function ShowStartPage()
{
	var menu_node					= $("#home_menu");
	var xml_exhibitions_obj 	= "";
	var xml_menu_obj;
	var menu_node, item_node, a_node, item_content_node, item_img_node;

	var item_template_node 	= $("#main_menu_item_template");

	//..................................................................................................................................prepare
	menu_node.children().remove();	
	xml_exhibitions_obj = joGetFirstElement (XML_PRESENTATION, "exhibitions");
	
	ShowTitle ($("#home_header"), XML_PRESENTATION);
		
	//................................................................................................................................fill menu
	for (var i = 0; i < xml_exhibitions_obj.childNodes.length; i++)
	{
		xml_menu_obj = xml_exhibitions_obj.childNodes[i];
		if (xml_menu_obj.nodeName == "exhibition" || xml_menu_obj.nodeName == "reference")
		{
			item_node 								= item_template_node.clone();
				item_content_node					= item_node.find (".content");
					item_content_node.html 	(BuildTitleHTMLContent (xml_menu_obj, 3));
				item_img_node						= item_node.find ("img");
					item_img_node.attr 			("src", xml_menu_obj.getAttribute ("thumbnail_url"));
				item_node.appendTo 				(menu_node);
				
				if (xml_menu_obj.nodeName == "exhibition")
				{
					item_node.attr 				("id", "id_" + xml_menu_obj.getAttribute ("id"));
					item_node.click				(function () { ChoosePage (this.id.substr(3)) });
				}
				else
				{
					var links_node = joGetFirstElement (xml_menu_obj, "content");
					var link = GetLocaleObject (links_node, "link");
				
					item_node.attr 				("url", link.getAttribute ("url"));
					item_node.click				(function () { OpenWindow (this.getAttribute ("url"), "scrumlies", 800, 600, 'true', 'true', 'true') });
				}
			menu_node.append						('<div class="nofloat"></div>');
		}
		
	}
	
	joShowExclusively ("page_container", "page_home", "TR");	
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function ShowTitle (parent_node, xml_object, title_level)
{
	parent_node.html (BuildTitleHTMLContent (xml_object, title_level));
	
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function BuildTitleHTMLContent (xml_object, title_level)
{
	var result = "";
	
	if (!title_level) title_level = 1;
	
	var exh_desc = joGetFirstElement (xml_object, "desc");
	if (exh_desc)
	{
		var exh_desc_text = GetLocaleTextObject (exh_desc)
		{

			var exh_title = GetHTMLText (joGetFirstElement (exh_desc_text, "title"));
			var exh_subtitle = GetHTMLText (joGetFirstElement (exh_desc_text, "subtitle"));
			
			if (exh_title != "") result += "<h" + title_level + ">" + exh_title + "</h" + title_level + ">";
			if (exh_subtitle != "") result += "<h" + (title_level + 1) + ">" + exh_subtitle + "</h" + (title_level + 1) + ">";
		}
	}
	return result;	
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function CollectPieces (exhibit_array)
{
	var result = [];
	var images_array = joGetElements (joGetFirstElement (XML_DATA, "collections"), "piece");
	var found;
	
	for (var i = 0; i < exhibit_array.length; i++)
	{
		found = false;
		for (var j = 0; !found && j < images_array.length; j++)
		{
			if (images_array[j].getAttribute ("id") == exhibit_array[i].getAttribute ("id"))
			{
				result.push (images_array[j]);
				found = true;
				break;
			}
		}
	}
	
	images_array = null;
	
	return result;
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function GetLocaleTextObject (parent_node)
{
	return GetLocaleObject (parent_node, "text");
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function GetLocaleObject (parent_node, object_name)
{
	var result = joGetFirstElement (parent_node, object_name, [["lang_id", GUI_LANG]]);
	if (!result) result = joGetFirstElement (parent_node, object_name, [["lang_id", DEFAULT_GUI_LANG]]);
	return result;
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function GetHTMLText (node, _result)
{
	return node ? GetDOMText (node, _result, true) : "";
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function GetRawText (node, _result)
{
	return node ? GetDOMText (node, _result, false) : "";
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function GetDOMText (node, _result, as_html)
{
	var result = "";
	
	if (_result) result = _result;
	
	if (node)
	{
		switch (node.nodeType)
		{
			/* node */
			case 1:
			{
				switch (node.nodeName)
				{
					case "symbol":
					{
						switch (node.getAttribute ("type"))
						{
							case "and": result += "&amp;"; break;
							case "copyright": result += "&copy;"; break;
							case "euro": result += "&#8364;"; break;
							case "greater than": result += "&gt;"; break;
							case "less than": result += "&lt;"; break;
							
						}
						break;
					}
					
					case "line":
					{
						for (var i = 0; i < node.childNodes.length; i++)  result += GetHTMLText (node.childNodes[i], _result, as_html);
						result += as_html ? "<br />" : " - ";	
						break;
					}
					
					case "italic":
					{
						if (as_html) result += "<i>";
						for (var i = 0; i < node.childNodes.length; i++)  result += GetHTMLText (node.childNodes[i], _result, as_html);
						if (as_html) result += "</i>";
						break;
					}
					
					case "paragraph":
					{
						if (as_html) result += "<p>";
						for (var i = 0; i < node.childNodes.length; i++)  result += GetHTMLText (node.childNodes[i], _result, as_html);
						result += as_html ? "</p>" : " - ";
						break;
					}
					
					case "strong":
					{
						if (as_html) result += "<strong>";
						for (var i = 0; i < node.childNodes.length; i++)  result += GetHTMLText (node.childNodes[i], _result, as_html);
						if (as_html) result += "</strong>";
						break;
					}
					
					default:
					{
						for (var i = 0; i < node.childNodes.length; i++)  result += GetHTMLText (node.childNodes[i], _result, as_html);
					}
				}
			
				break;
			}

			/* text node */
			case 3:
			{
				result += node.nodeValue;
			}
			
		}
	}
	
	return result;
}


/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function ShowExhibition()
{
	ShowTitle ($("#content_header"), XML_EXHIBITION);
	InitExhibitionMenu();
	ShowSelection (0);
	joShowExclusively ("page_container", "page_content", "TR");
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function InitExhibitionMenu()
{
	var menu_node					= $("#exh_menu");
	var item_template_node 	= $("#exh_menu_item_template");
	var xml_selections_obj 	= "";
	var xml_menu_obj, xml_title_obj;
	var menu_node, item_node;

	//..................................................................................................................................prepare
	XML_SEL_ITEM_NODE = null;
	menu_node.children().remove();	
	xml_selections_obj = joGetFirstElement (XML_EXHIBITION, "selections");
	XML_SELECTION_ARRAY = joGetElements (xml_selections_obj, "selection");
	
	//................................................................................................................................fill menu
	for (var i = 0; i < XML_SELECTION_ARRAY.length; i++)
	{
		xml_menu_obj = XML_SELECTION_ARRAY[i];
		if (xml_menu_obj.nodeName == "selection")
		{
			item_node 						= item_template_node.clone();
			xml_title_obj					= GetLocaleTextObject (xml_menu_obj);

			item_node.attr					("title", GetRawText (joGetFirstElement (xml_title_obj, "title")));
			item_node.attr 				("id", "sel_" + i);
			item_node.attr 				("sel_id", "id_" + xml_menu_obj.getAttribute ("id"));
			item_node.mouseover			(function () { SelMenuHover (this, true); });
			item_node.mouseout			(function () { SelMenuHover (this, false); });
			item_node.click				(function () { ShowSelection (parseInt (parseFloat (this.id.substr(4)))) });
			item_node.appendTo 			(menu_node);
		}
	}

}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function SelMenuHover (sel_menu_node, hover)
{
	if (sel_menu_node.className.indexOf("active") == -1)
	{
		sel_menu_node.className = hover ? "item hover" : "item";
		$("#exh_menu_item").html (hover ? "" + sel_menu_node.title : (XML_SEL_ITEM_NODE ? XML_SEL_ITEM_NODE.attr ("title") : ""));
	}
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

var curr_sel_no;

function ShowSelection (selection_no)
{
	try 
	{
		if (XML_SEL_ITEM_NODE)
		{
			XML_SEL_ITEM_NODE.removeClass ("active");
		}
	
		XML_SEL_ITEM_NODE = $("#sel_" + selection_no);
		XML_SEL_ITEM_NODE.removeClass ("hover");
	
		var selection_id = XML_SEL_ITEM_NODE.attr ("sel_id").substr (3);
			
		$("#exh_menu_item").html (XML_SEL_ITEM_NODE.attr ("title"));
		XML_SEL_ITEM_NODE.addClass ("active");
	
		XML_SELECTION = joGetFirstElement (XML_EXHIBITION, "selection", [["id", selection_id]]);
	
		joShowExclusively ("mainbar", "artwork_menu");	
			
		InitGallery (XML_SELECTION);
		
		curr_sel_no = selection_no;
	}
	catch(e){};
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function SwitchSelection (shift)
{
	curr_sel_no += shift;
	
	if (curr_sel_no < 0)
	{
		curr_sel_no = XML_SELECTION_ARRAY.length - 1;
	}
	
	var sel_node = $("#sel_" + curr_sel_no);
	if (!sel_node.length)
	{
		curr_sel_no = 0;
	}
	ShowSelection (curr_sel_no);
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function InitGallery (xml_parent_obj)
{
	var xml_exhibits					= joGetElements (xml_parent_obj, "exhibit");
	var item_template_node 		= $ ("#sel_gal_item_template");
	var mainbar_node					= $ ("#mainbar");
	var table_node 					= $ ("#artwork_gallery");
	var mainbar_node, tr_node, td_node, img_node;
	
	var xml_image;
	var xml_img_title;
	var img_title						= "";
	var img_subtitle					= "";

	var pics_per_line					= parseInt (parseFloat (xml_parent_obj.getAttribute ("gui_thumbnails_per_line")));
	var col_width						= Math.round (100 / pics_per_line);

	RefreshSelMenu (-1);
	table_node.children().remove();	
	XML_ARTWORK_ARRAY	 = CollectPieces (xml_exhibits);
	mainbar_node.removeClass();
	mainbar_node.addClass ("gallery_" + XML_EXHIBITION.getAttribute("id"));
	
	for (count = 0; count < XML_ARTWORK_ARRAY.length;)
	{
		tr_node = table_node.append ("<tr />").children().last();

		for (var i = 0; i < pics_per_line && count < XML_ARTWORK_ARRAY.length; i++, count++)
		{
			xml_image								= XML_ARTWORK_ARRAY[count];
			img_title								= "";
			xml_img_title		 					= GetLocaleTextObject (xml_image);
			if (xml_img_title)
			{
				img_title							= GetRawText (joGetFirstElement (xml_img_title, "title"));
				img_subtitle						= GetRawText (joGetFirstElement (xml_img_title, "subtitle"));
				if (img_subtitle != "") 		img_title += " - " + img_subtitle;
			}
			
			td_node 									= item_template_node.clone();
				td_node.attr 						("width", col_width + "%");
				td_node.appendTo					(tr_node);	
				img_node								= td_node.append ("<img />").children().first();
				img_node.attr 						("src", xml_image.parentNode.parentNode.getAttribute ("path_thumbnail") + xml_image.getAttribute ("file_name"));
				img_node.attr 						("alt", img_title);
				img_node.attr 						("title", img_title);
				img_node.attr 						("id", "id_" + xml_image.getAttribute ("id"));
				img_node.attr 						("no", "no_" + count);
				img_node.click	(function () 	{ ShowArtwork (this.getAttribute ("no").substr (3)) });
				
		}	
	}
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function ShowArtwork (artwork_no)
{
	var artwork_id 		= XML_ARTWORK_ARRAY[artwork_no].getAttribute ("id");
	var artwork_piece 	= $("#artwork_piece");
	
	RefreshSelMenu (artwork_id);

	artwork_piece.src = "images/jolegat_waiting.gif";
	$("#artwork_title").html ("");
	$("#artwork_subtitle").html ("");

	try
	{
		var xml_image 			= joGetFirstElement (XML_DATA, "piece", [["id", artwork_id]]);
		var xml_img_title 	= GetLocaleTextObject (xml_image);
		if (xml_img_title)
		{
			var img_title			= GetHTMLText (joGetFirstElement (xml_img_title, "title"));
			var img_subtitle		= GetHTMLText (joGetFirstElement (xml_img_title, "subtitle"));
			
			$("#artwork_title").html (img_title);
			$("#artwork_subtitle").html (img_subtitle);
		}
		
		var img_url = xml_image.parentNode.parentNode.getAttribute ("path_preview") + xml_image.getAttribute ("file_name");
		
		artwork_piece.attr ("src", img_url);
		artwork_piece.attr ("title", "[" + xml_image.getAttribute ("id") + "]");
		artwork_piece.attr ("no", artwork_no);
		
		joShowExclusively ("mainbar", "artwork");	

	}
	catch (e) {}
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function RefreshSelMenu (artwork_no)
{
	$("#piece_prev").css("visibility", artwork_no < 0 ? "hidden" : "visible" );
	$("#piece_next").css("visibility", artwork_no < 0 ? "hidden" : "visible" );

	if (artwork_no < 0)
	{
		SetSlideshowMode (false);
	}
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function SwitchArtwork (shift)
{
	var artwork_piece = $("#artwork_piece");
	var new_artwork_no = parseInt (parseFloat (artwork_piece.attr ("no"))) + shift;
	var count_pieces = XML_ARTWORK_ARRAY.length;
	
	while (new_artwork_no < 0) new_artwork_no+= count_pieces;
	while (new_artwork_no >= count_pieces) new_artwork_no-= count_pieces;

	ShowArtwork (new_artwork_no);	
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

var slideshow_on = false;
var slideshow_interval;

function SetSlideshowMode (truefalse)
{
	var button_node = $("#piece_slideshow");
	truefalse ? button_node.addClass ("active") : button_node.removeClass ("active");
	slideshow_on = truefalse;
	
	if (slideshow_on)
	{
		if ($("#artwork").css("display") == "none") ShowArtwork (0);
		slideshow_interval = window.setInterval("SwitchArtwork (1)", 1000 * GUI_SLIDESHOW_SECS);
	}
	else
	{
		window.clearInterval (slideshow_interval);
	}
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function ToggleSlideshow()
{
	slideshow_on = ! (slideshow_on);
	SetSlideshowMode (slideshow_on);
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function OnHoverPieceSlideshow (truefalse)
{
	var button_node = $("#piece_slideshow");
	
	if (!slideshow_on)
	{
		truefalse ? button_node.addClass ("hover") : button_node.removeClass ("hover");
	}
	else
	{
		button_node.removeClass ("hover");
	}
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function OnKeyPress (event)
{
	if (XML_EXHIBITION)
	{
		var show_foto = $("#artwork").css("display") == "none" ? false : true;
		var show_content = $("#page_content").css("display") == "none" ? false : true;
		var show_artwork;
		
		if (!event) event = window.event;
		switch (event.keyCode)
		{
			case 37: // left
				if (show_foto) SwitchArtwork (-1); 
				else if (show_content) ShowArtwork (0);
				break;
			case 39: // right
				if (show_foto) SwitchArtwork (1); 	
				else ShowArtwork (0);	
				break;
			case 38: // up
				show_artwork = ($("#artwork").css("display") != "none");
				SwitchSelection (-1);
				if (show_artwork) ShowArtwork (0);
				break;
			case 40: // down
				show_artwork = ($("#artwork").css("display") != "none");
				SwitchSelection (1);
				if (show_artwork) ShowArtwork (0);
				break;
			case 32: // blank
				if (show_foto || show_content)
				{
					ToggleSlideshow();
				}
				break;
			case 33: // page up
			{
				GoUp();
			}
			case 34: // page up
			{
				break;
			}
			case 13: // enter
				($("#artwork").css("display") == "none") ? ShowArtwork (0) : joShowExclusively ('mainbar', 'artwork_menu');
				break;
		}
		//	alert (event.keyCode);
	}
	
	return false;
	
}
