﻿/* Hlídá max. délku pole textarei. */
function keepTextareaLength(inputid, maxLength) {

	var box = document.getElementById(inputid);
	var length = box.value.length;
	if (length > maxLength)
		box.value = box.value.substring(0, maxLength);
}

function openPagePreviewWindow(url) {

	return window.open(url, 'preview', 'left=100,top=100,width=1024,height=768,toolbar=no,menubar=yes,location=yes,resizable=yes,scrollbars=yes');
}

function browser_isIE() {
	return navigator.appName.indexOf("Microsoft") != -1;
}

function browser_isMozilla() {
	return navigator.userAgent.indexOf("Mozilla") != -1;
}

function browser_isOpera() {
	return window.opera != null;
}

/*	GALERIE  */

function getWindowProportions() {

	var w = 800;
	var h = 600;
	
	if (browser_isIE()) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	
	var size = new Array();
	size[0] = w;
	size[1] = h;
	return size;
}

var gallery_prefix = "gallery_";
var gallery_startOpacity = 80;
var gallery_handlerUri = "/App_Publisher/SystemPages/GalleryHandler.ashx";
var gallery_request = null;

/* Spuštění galerie */
function gallery_Initialize(index, totalPhotos, thisPhoto, container, mode) {

	window.onresize				= gallery_RefreshSize;
	window.onscroll				= gallery_RefreshSize;

	gallery_GetPreviewImage().onload = gallery_ShowPreviewImage;

	if (index != null) {
		gallery_SetFieldValue("photoIndex", index);
	}

	if (totalPhotos != null)
		gallery_SetFieldValue("totalPhotos", totalPhotos);

	gallery_SetFieldValue("thisPhoto", thisPhoto);
	
	if (container && container != "")
		gallery_SetFieldValue("container", container);
		
	gallery_SetFieldValue("mode", mode);
	gallery_SetContentOpacity(gallery_startOpacity);
	gallery_SetTableVisibility(true);

	gallery_LoadPhoto(true);
}

/* Funkce na zobrazení fotky po načtení obrázku */
function gallery_ShowPreviewImage(display) {

	if (display != null && display == false) {
		gallery_SetContentOpacity(gallery_startOpacity);
		gallery_GetPreviewImage().style.display = "none";
	}
	else {
		//	schovat načítání.
		gallery_ManageLoading(false);
		
		gallery_SetContentOpacity(100);
		gallery_GetPreviewImage().style.display = "block";
	}
}

/* Pomocná funkce pro nastavení galerijních hodnot */
function gallery_SetFieldValue(name, value) {

	document.getElementById(gallery_prefix + name).value = value;
}

/* Pomocná funkce pro získání galerijní hodnoty */
function gallery_GetFieldValue(name) {

	return document.getElementById(gallery_prefix + name).value;
}

/* Vrátí objekt IMG s obrázkem */
function gallery_GetPreviewImage() {
	
	return document.getElementById(gallery_prefix + "preview");
}

/* Načtení aktuální fotky */
function gallery_LoadPhoto(afterInit) {

	if (afterInit) {

		gallery_AdjustSize(200, 200);
	}

	gallery_ShowPreviewImage(false);

	gallery_LoadXML();
}

/* Funkce pro aktulizaci velikostí při změně velikosti prohlížeče */
function gallery_RefreshSize() {

	var preview				= document.getElementById(gallery_prefix + "preview");
	var width				= Math.round(preview.attributes.getNamedItem("origWidth").nodeValue);
	var height				= Math.round(preview.attributes.getNamedItem("origHeight").nodeValue);

	gallery_AdjustSize(width, height);
}

/* Vytvoření asynchronního volání */
function gallery_LoadXML() {
	
	//	vytvoření url.
	var url = gallery_handlerUri
					+ "?LanguageID=" + gallery_GetLanguageID()
					+ "&Photo=" + gallery_GetFieldValue("thisPhoto")
					+ "&Mode=" + gallery_GetFieldValue("mode");
					
	if (gallery_GetFieldValue("photoIndex") != "")
		url			+= "&PhotoIndex=" + gallery_GetFieldValue("photoIndex")	;
	
	if (gallery_GetFieldValue("container") != "all")
		url			+= "&Container=" + gallery_GetFieldValue("container");
	
	if (gallery_GetFieldValue("totalPhotos") != "")
		url			+= "&TotalPhotos=" + gallery_GetFieldValue("totalPhotos");
	
    gallery_ManageLoading(true);
	
	if (window.XMLHttpRequest) {
	
		gallery_request		= new XMLHttpRequest();
		gallery_request.onreadystatechange = gallery_HandleXML;
		gallery_request.open("GET", url, true);
		gallery_request.send(null);

	} else if (window.ActiveXObject) {
	
		gallery_request		= new ActiveXObject("Microsoft.XMLHTTP");
		
		if (gallery_request) {
			gallery_request.onreadystatechange = gallery_HandleXML;
			gallery_request.open("GET", url, true);
			gallery_request.send();
		}
	}
}

/* Zobrazení či schování animace při načítání */
function gallery_ManageLoading(starting) {

	document.getElementById(gallery_prefix + "contentTable").style.display = starting ? "none" : "block";
	document.getElementById(gallery_prefix + "content").className = starting ? "shadow" : "content";
	document.getElementById(gallery_prefix + "contentLoading").style.display = starting ? "block" : "none";
}

/* Zpracování výsledku asynchronního volání */
function gallery_HandleXML() {

	if (gallery_request.readyState == 4) {

		if (gallery_request.status == 200) {
			
			var doc = gallery_request.responseXML;
			var slideShow = doc.getElementsByTagName("slideshow")[0];
			
			var photoIndex = Math.round(slideShow.attributes.getNamedItem("photoIndex").nodeValue);
			var totalPhotos = Math.round(slideShow.attributes.getNamedItem("totalPhotos").nodeValue);
			
			var preceding = gallery_HandleXMLSibling(doc.getElementsByTagName("precedingPhoto")[0]);
			var following = gallery_HandleXMLSibling(doc.getElementsByTagName("followingPhoto")[0]);
			
			var photo = doc.getElementsByTagName("photo")[0];
			var photoID = photo.attributes.getNamedItem("id").nodeValue;
			
			var photoName, photoDescription, photoCreationTime, photoImagePath, photoWidth, photoHeight, photoLink, child, childTagName;
			for(i=0; i<photo.childNodes.length; i++) {
			
				child				= photo.childNodes[i];
				childTagName		= child.tagName;

				if (childTagName == "name") {
					photoName		= child.childNodes[0].nodeValue;
				}
				else if (childTagName == "description") {
					photoDescription = child.childNodes[0].nodeValue;
				}
				else if (childTagName == "imagePath") {
					photoImagePath	= child.childNodes[0].nodeValue;
				}
				else if (childTagName == "width") {
					photoWidth		= Math.round(child.childNodes[0].nodeValue);
				}
				else if (childTagName == "height") {
					photoHeight		= Math.round(child.childNodes[0].nodeValue);
				}
				else if (childTagName == "link") {
					photoLink		= child.childNodes[0].nodeValue;
				}
				else if (childTagName == "creationTime") {
					photoCreationTime = child.childNodes[0].nodeValue;
				}
			}
			
			//	nastavení hodnot.
			gallery_SetFieldValue("precedingPhoto", preceding);
			gallery_SetFieldValue("thisPhoto", photoID);
			gallery_SetFieldValue("followingPhoto", following);
			
			//	zobrazení.
			gallery_DisplayPhoto(photoName, photoDescription, photoCreationTime, photoImagePath, photoWidth, photoHeight, photoLink,
				preceding != null, following != null, photoIndex, totalPhotos);
		 }
		 else
			window.alert("There was a problem retrieving the XML data.");
	}
}

/* Zpracuje sousedský element fotky */
function gallery_HandleXMLSibling(elem) {

	if (elem.attributes.getNamedItem("isnull") != null)
		return null;
	else
		return elem.attributes.getNamedItem("id").nodeValue;
}

/* Zobrazení uvedené fotografie */
function gallery_DisplayPhoto(name, desc, creationTime, path, width, height, link, hasPreceding, hasFollowing, photoIndex, totalPhotos) {

	gallery_ShowPreviewImage(false);

	var preview				= document.getElementById(gallery_prefix + "preview");
	preview.src				= path;
	preview.setAttribute("width", width);
	preview.setAttribute("height", height);
	preview.setAttribute("origWidth", width);
	preview.setAttribute("origHeight", height);

	var descElem = document.getElementById(gallery_prefix + "description");
	if (desc) {
		descElem.innerHTML		= descElem.attributes.getNamedItem("template").nodeValue + desc;
	}
	else
		descElem.innerHTML		= "";
		
	//	datum vložení.
	document.getElementById(gallery_prefix + "creationTime").innerHTML = creationTime;
	
	/*var linkElem				= document.getElementById(gallery_prefix + "link");
	var linkElemBaseLink		= linkElem.attributes.getNamedItem("baseLink").nodeValue;
	linkElem.innerHTML			= linkElemBaseLink + link;
	linkElem.href				= linkElem.innerText;*/
	
	document.getElementById(gallery_prefix + "position").innerHTML = (photoIndex+1).toString() + " z " + totalPhotos.toString();
	
	//	tlačítka pro posun.
	var moveButtons				= document.getElementById(gallery_prefix + "moveButtons");
	var previousText			= moveButtons.attributes.getNamedItem("previousText").nodeValue;
	var nextText				= moveButtons.attributes.getNamedItem("nextText").nodeValue;
	//var separator				= moveButtons.attributes.getNamedItem("separator").nodeValue;
	
	while (moveButtons.childNodes.length > 0)
		moveButtons.removeChild(moveButtons.childNodes[0]);
	
	if (hasPreceding) {
		
		var prevButton			= document.createElement("a");
		prevButton.innerHTML	= previousText;
		prevButton.setAttribute("href", "javascript:void(0);");
		prevButton.onclick		= gallery_GoToPreviousPhoto;
		moveButtons.appendChild(prevButton);
	}
	else {
		
		var prevElem			= document.createElement("span");
		prevElem.innerHTML		= previousText;
		prevElem.className		= "disabled";
		moveButtons.appendChild(prevElem);
	}
	
	moveButtons.appendChild(document.createTextNode(" "));
	
	if (hasFollowing) {
		
		var nextButton			= document.createElement("a");
		nextButton.innerHTML	= nextText;
		nextButton.setAttribute("href", "javascript:void(0);");
		nextButton.onclick		= gallery_GoToNextPhoto;
		moveButtons.appendChild(nextButton);
	}
	else {
		
		var nextElem			= document.createElement("span");
		nextElem.innerHTML		= nextText;
		nextElem.className		= "disabled";
		moveButtons.appendChild(nextElem);
	}
	
	gallery_AdjustSize(width, height);

	//	měřiče.
	gallery_runMonitors();
}

/* Nastaví potřebné velikosti vzhledem k rozměrům nastaveného obrázku */
function gallery_AdjustSize(width, height) {

	var origWidth				= width;
	var origHeight				= height;

	var proportions				= getWindowProportions();
	var windowWidth				= proportions[0];
	var windowHeight			= proportions[1];

	var origRatio				= origWidth / origHeight;
	var sizeAdjusted			= false;

	var widthSubtract			= 30;
	var heightSubtract			= 110;

	var windowSizeRatio			= (windowWidth - widthSubtract) / (windowHeight - heightSubtract);
	var imageSizeRatio			= width / height;

	if (origWidth > (windowWidth - widthSubtract) || origHeight > (windowHeight - heightSubtract)) {

		if (imageSizeRatio > windowSizeRatio) {
			width				= windowWidth - widthSubtract;
			height				= width / origRatio;
		}
		else if (imageSizeRatio < windowSizeRatio) {
			height				= windowHeight - heightSubtract;
			width				= origRatio * height;
		}
		else {
			width				= windowWidth - widthSubtract;
			height				= windowHeight - heightSubtract;
		}
		
		sizeAdjusted			= true;
	}

	if (!sizeAdjusted) {
		width					= origWidth;
		height					= origHeight;
	}
	
	if (width < 250) {
	
		width					= 250;

		if (sizeAdjusted) {
		
			height				= width / origRatio;
			if (windowWidth < 250)
				window.resizeTo(290, windowHeight);
		}
	}

	var	galleryTable			= document.getElementById(gallery_prefix + "table");
	var containerWidth			= gallery_GetContainerWidth();
	var containerHeight			= Math.round(document.getElementById("containerHeight").value) + 100;

	var	leftRightSpace			= Math.round((windowWidth - width)/2);
	if (leftRightSpace < 0)
		leftRightSpace			= 10;
	
	var topSpace				= Math.round((windowHeight - height - 60)/2);
	if (topSpace < 0)
		topSpace				= 10;
	
	var galleryTableLeft		= (windowWidth - containerWidth)/2;
	if (galleryTableLeft < 0)
		galleryTableLeft		= 10;
		
	var contentWidth			= windowWidth - (leftRightSpace * 2);
	var contentHeight			= Math.round(document.getElementById(gallery_prefix + "content").clientHeight);
	
	if (contentHeight < 50)
		contentHeight			= windowHeight;

	topSpace					= topSpace + gallery_GetWindowScrollTop();

	document.getElementById(gallery_prefix + "content").style.height = height + "px";
	document.getElementById(gallery_prefix + "content").style.width = contentWidth + "px";
	document.getElementById(gallery_prefix + "left").style.width	= leftRightSpace+"px";
	document.getElementById(gallery_prefix + "right").style.width	= leftRightSpace+"px";
	document.getElementById(gallery_prefix + "top").style.height	= topSpace+"px";
	document.getElementById(gallery_prefix + "tdPreview").style.width = contentWidth + "px";
	document.getElementById(gallery_prefix + "top").style.width = contentWidth + "px";
	document.getElementById(gallery_prefix + "contentTable").style.width = contentWidth + "px";
	
	if (!browser_isMozilla())
		galleryTable.style.width = windowWidth + "px";
	else
		galleryTable.style.width = (windowWidth-7) + "px";
	
	galleryTable.style.left		= "-" + galleryTableLeft + "px";

	//	nastavení obrázku dle velikosti okna.
	document.getElementById(gallery_prefix + "preview").setAttribute("width", !sizeAdjusted ? origWidth : width);
	document.getElementById(gallery_prefix + "preview").setAttribute("height", !sizeAdjusted ? origHeight : height);		
	document.getElementById(gallery_prefix + "bottom").style.height	= (containerHeight-topSpace-height) + "px";
}

/* Vrátí vzdálenost stavu stránky od horní linky */
function gallery_GetWindowScrollTop() {

	var fromTop					= document.body.scrollTop;
	if (fromTop == null || fromTop == 0) {
		if (window.pageYOffset)
			fromTop				= window.pageYOffset;
		else if (document.body.parentElement && document.body.parentElement.scrollTop)
			fromTop				= document.body.parentElement.scrollTop;
	}
	
	return fromTop;
}

/* Přechod na předchozí fotku */
function gallery_GoToPreviousPhoto() {

	//	posun indexu.
	var currentIndex			= Math.round(gallery_GetFieldValue("photoIndex"));
	currentIndex--;
	gallery_SetFieldValue("photoIndex", currentIndex);
	
	//	nastavení předchozí fotky jako aktuální.
	gallery_SetFieldValue("thisPhoto", gallery_GetFieldValue("precedingPhoto"));
	
	//	načtení.
	gallery_LoadPhoto(false);
}

/* Přechod na další fotku */
function gallery_GoToNextPhoto() {

	//	posun indexu.
	var currentIndex			= Math.round(gallery_GetFieldValue("photoIndex"));
	currentIndex++;
	gallery_SetFieldValue("photoIndex", currentIndex);
	
	//	nastavení další fotky jako aktuální.
	gallery_SetFieldValue("thisPhoto", gallery_GetFieldValue("followingPhoto"));
	
	//	načtení.
	gallery_LoadPhoto(false);
}

/* Vrácení aktuálního jazyka na webu. */
function gallery_GetLanguageID() {
	
	return document.getElementById(gallery_prefix + "languageID").value;
}

/* Vrátí šířku střední části stránky */
function gallery_GetContainerWidth() {

	return Math.round(document.getElementById(gallery_prefix + "table").attributes.getNamedItem("containerWidth").nodeValue);
}

/*	Zavření galerie	*/
function gallery_Finish() {
	
	// var action = "gallery_Finishing(100);";
	// setTimeout(action, 50);
	gallery_Finishing(0);
}

/* Zmenšení opacity tabulky */
function gallery_Finishing(leftOpacity) {

	if (leftOpacity <= 20) {
	
		gallery_SetTableVisibility(false);
		//window.onscroll		= null;
		//window.onresize		= null;
		return;
	}
	
	//	snížení viditelnosti.
	leftOpacity				= leftOpacity - 20;
	
	gallery_SetContentOpacity(leftOpacity);

	var action = "gallery_Finishing("+ leftOpacity +");";
	setTimeout(action, 40);
}

/* Nastaví opacitu hlavní buňce s obsahem */
function gallery_SetContentOpacity(opacity) {

	var style				= document.getElementById(gallery_prefix + "content").style;

	if (browser_isIE()) {
		style.filter = "alpha(opacity="+ opacity +")";
	}
	else {
		style.opacity = (opacity/100);
	}
}

/* Nastavení viditelnosti tabulky s galerií */
function gallery_SetTableVisibility(display) {

	if (display) {
		document.getElementById(gallery_prefix + "table").style.display = "block";
		document.getElementById("header_2_dropLocation").style.display = "none";
	}
	else {
		document.getElementById(gallery_prefix + "table").style.display = "none";
		document.getElementById("header_2_dropLocation").style.display = "inline";
	}
}