﻿/// ********************************
/// LOGIN FORM
/// ********************************
function LoginFormInit()
{
	$(document).ready
	(
		function()
		{
			$(".login-form-name").keypress(LoginFormOnKeyPress);
			$(".login-form-password").keypress(LoginFormOnKeyPress);
		}
	);
}


function LoginFormOnKeyPress(e)
{
	if (e.keyCode == KeyCode_Enter)
	{
		e.preventDefault();
		$(".login-form-submit").click();
	}
}

/// ********************************
/// WORLDWIDE SELECT
/// ********************************
var WorldwideSelect = null;

function WorldwideSelectionInit()
{
	$(document).ready
	(
		function()
		{
			WorldwideSelect = $(".worldwide-selection").change(WorldwideSelectionChange);
		}
	);
}

function WorldwideSelectionChange(e)
{
	var wselectionValue = WorldwideSelect.val();
	if (wselectionValue.length > 0) location.href = wselectionValue;
}

/// ********************************
/// VIRTUAL SIGHTSEEING
/// ********************************
var VirtualSightseeingContainer = null;
var VirtualSightseeingTotalAnimations = 0, VirtualSightseeingSelectedMoveIndex = 0, VirtualSightseeingSelectedItemIndex = 0,
	VirtualSightseeingPageSize = 3, VirtualSightseeingAnimationItemWidth = 144, VirtualSightseeingItems = null,
	VirtualSightseeingItemSet = new Array();
var VirtualSightseeingMoveForwardButton = null, VirtualSightseeingMoveBackwardButton = null,
	VirtualSightseeingList = null;

function VirtualSighseeingInit(index, pageSize, count)
{
	VirtualSightseeingSelectedItemIndex = index;
	VirtualSightseeingSelectedMoveIndex = index;
	VirtualSightseeingPageSize = pageSize;
	VirtualSightseeingTotalAnimations = count;
	
	$(document).ready
	(
		function()
		{
			VirtualSightseeingContainer = $("#sightseeing-container");
			
			VirtualSightseeingMoveForwardButton = $(".sightseeing-arrow-right")
				.click
				(
					function(e)
					{
						e.preventDefault();
						VirtualSighseeingMoveForward();
					}
				);
				
			VirtualSightseeingMoveBackwardButton = $(".sightseeing-arrow-left")
				.click
				(
					function(e)
					{
						e.preventDefault();
						VirtualSighseeingMoveBackward();
					}
				);
			
			VirtualSightseeingList = $(".sightseeing-list-inner")
				.width(VirtualSightseeingTotalAnimations * VirtualSightseeingAnimationItemWidth);
			
			VirtualSightseeingItems = $(".item", VirtualSightseeingList)
				.each
				(
					function(index)
					{
						$(this).click
						(
							function(e)
							{
								e.preventDefault();
								VirtualSighseeingSelectItem(index);
							}
						);
					}
				);
			
			VirtualSighseeingUpdateItemsVisibility();
			
			VirtualSighseeingUpdateMoveButtonsVisibility();
		}
	);
}

function VirtualSighseeingMoveForward()
{
	VirtualSighseeingMove(VirtualSightseeingSelectedMoveIndex + 1);
}

function VirtualSighseeingMoveBackward()
{
	VirtualSighseeingMove(VirtualSightseeingSelectedMoveIndex - 1);
}

function VirtualSighseeingMove(aindex)
{
	if (aindex < 0 || aindex > VirtualSighseeingGetLastMoveIndex()
		|| VirtualSightseeingSelectedMoveIndex == aindex) return;
	
	var leftPosition = VirtualSightseeingList.position().left,
		moveOffset = (aindex > VirtualSightseeingSelectedMoveIndex ? -VirtualSightseeingAnimationItemWidth : VirtualSightseeingAnimationItemWidth);
	
	//VirtualSightseeingList.animate({ left: String(leftPosition + moveOffset) + "px"}, "normal");
	VirtualSightseeingList.css("left", String(leftPosition + moveOffset) + "px");
	
	VirtualSightseeingSelectedMoveIndex = aindex;
	
	VirtualSighseeingUpdateMoveButtonsVisibility();
}

function VirtualSighseeingSelectItem(index)
{
	VirtualSightseeingSelectedItemIndex = index;
	
	VirtualSightseeingContainer.empty();
	
	var swfWriter = new SWFObject(VirtualSightseeingItemSet[index].Url, "sightseeing", "500", "300", "0", "#000");
	swfWriter.addParam("wmode", "transparent");
	swfWriter.write(VirtualSightseeingContainer.attr("id"));
	
	VirtualSighseeingUpdateItemsVisibility();
}

function VirtualSighseeingUpdateItemsVisibility()
{
	VirtualSightseeingItems.each
	(
		function(index)
		{
			if (index != VirtualSightseeingSelectedItemIndex) $(this).animate({ opacity: "0.4" });
			else $(this).animate({ opacity: "1" });
		}
	);
}

function  VirtualSighseeingUpdateMoveButtonsVisibility()
{
	if (VirtualSightseeingSelectedMoveIndex == 0) VirtualSightseeingMoveBackwardButton.animate({ opacity: "0.2" });
	else VirtualSightseeingMoveBackwardButton.animate({ opacity: "1" });
	
	if (VirtualSightseeingSelectedMoveIndex < VirtualSighseeingGetLastMoveIndex()) VirtualSightseeingMoveForwardButton.animate({ opacity: "1" });
	else VirtualSightseeingMoveForwardButton.animate({ opacity: "0.2" });
}

function VirtualSighseeingGetLastMoveIndex()
{
	if (VirtualSightseeingTotalAnimations <= VirtualSightseeingPageSize) return 0;
	else
	{
		return VirtualSightseeingTotalAnimations - VirtualSightseeingPageSize;
	}
}

/// ********************************
/// GALLERY
/// ********************************
var GalleryPhotoSet = new Array();
var GallerySelectedPageIndex = 0;
var GallerySelectedPhotoIndex = 0;
var GalleryPageSize = 5;
var GalleryTotalPages = 0;
var GalleryTotalPhotos = 0;
var GalleryPreloadedImages = new Array();
var GalleryPageHeight = 442;
var GallerySelectedPhotoOpacity = "1";
var GalleryDefaultPhotoOpacity = "0.3";
var GalleryLoadingImageSmallUrl = "/Images/gallery_loading_small.gif";

var GalleryElement_CurrentPosition = null, GalleryElement_PreviousAnchor = null, GalleryElement_NextAnchor = null,
	GalleryElement_MainImageContainer = null, GalleryElement_MainImage = null, GalleryElement_MainImageLoading = null,
	GalleryElement_MoveForwardAnchor = null, GalleryElement_MoveBackwardAnchor = null;

function GalleryInit(pageIndex, pageSize, photoIndex)
{
	GallerySelectedPageIndex	= pageIndex;
	GalleryPageSize				= pageSize;
	GallerySelectedPhotoIndex	= photoIndex;
	
	GalleryTotalPhotos			= GalleryPhotoSet.length;
	GalleryTotalPages			= Math.round(GalleryTotalPhotos / GalleryPageSize);
	
	GalleryInitPhotos();
	
	GalleryPreloadPhotos(GallerySelectedPhotoIndex);
	
	GalleryInitElements();
	
	GalleryInitMainImage();
	
	GalleryInitMoveButtons();
	
	GalleryInitPrevNextButtons();
	
	GalleryInitKeyEvents();
}

function GalleryInitElements()
{
	GalleryElement_CurrentPosition = $(".gallery-current-position");
	GalleryElement_PreviousAnchor = $(".gallery-previous-anchor");
	GalleryElement_NextAnchor = $(".gallery-next-anchor");
	GalleryElement_MainImageContainer = $(".gallery-main-image-container");
	GalleryElement_MainImage = $(".gallery-main-image");
	GalleryElement_MainImageLoading = $(".gallery-loading");
	GalleryElement_MoveForwardAnchor = $(".gallery-move-forward");
	GalleryElement_MoveBackwardAnchor = $(".gallery-move-backward");
}

function GalleryInitMainImage()
{
	GalleryElement_MainImage.load
	(
		function()
		{
			GalleryElement_MainImageLoading.stop().fadeOut("fast",
				function()
				{
					GalleryElement_MainImageContainer.fadeIn("fast");
				});
		}
	)
	.click
	(
		GalleryMoveToNextPhoto
	);
}

function GalleryInitPhotos()
{
	$(".gallery-photo").each
	(
		function(index)
		{
			$(this).attr("index", index);
			
			var photoObject = GalleryPhotoSet[index];
			var photoObjectListImg = $("img", this);
			var photoObjectListImgLoading = $("span", this);
			
			if (index == GallerySelectedPhotoIndex) photoObjectListImg.css("opacity", GallerySelectedPhotoOpacity);
			else photoObjectListImg.css("opacity", GalleryDefaultPhotoOpacity);
			
			photoObjectListImg.src = photoObject.ListImageUrl;
			photoObjectListImg.css("display", "none");
			photoObjectListImgLoading.css("display", "block");
			
			$(this).click
			(
				function(e)
				{
					e.preventDefault();
					GalleryPhotoClick($(this));
				}
			);
		}
	);
}

function GalleryInitMoveButtons()
{
	GalleryElement_MoveForwardAnchor.click
	(
		function(e)
		{
			e.preventDefault();
			GalleryMoveForward();
		}
	);
	
	GalleryElement_MoveBackwardAnchor.click
	(
		function(e)
		{
			e.preventDefault();
			GalleryMoveBackward();
		}
	);
}

function GalleryInitPrevNextButtons()
{
	GalleryElement_PreviousAnchor.click
	(
		function(e)
		{
			e.preventDefault();
			GalleryMoveToPreviousPhoto();
		}
	);
	
	GalleryElement_NextAnchor.click
	(
		function(e)
		{
			e.preventDefault();
			GalleryMoveToNextPhoto();
		}
	);
}

function GalleryInitKeyEvents()
{
	if (jQuery.browser.msie)
	{
		$(document).keyup(GalleryKeyEventRaised);
	}
	else $(window).keydown(GalleryKeyEventRaised);
}

function GalleryKeyEventRaised(e)
{
	if (e.keyCode == KeyCode_ArrowLeft || e.keyCode == KeyCode_ArrowUp)
	{
		e.preventDefault();
		GalleryMoveToPreviousPhoto();
	}
	else if (e.keyCode == KeyCode_ArrowRight || e.keyCode == KeyCode_ArrowDown)
	{
		e.preventDefault();
		GalleryMoveToNextPhoto();
	}
	else if (e.keyCode == KeyCode_PageUp)
	{
		e.preventDefault();
		GalleryMoveForward();
	}
	else if (e.keyCode == KeyCode_PageDown)
	{
		e.preventDefault();
		GalleryMoveBackward();
	}
	else if (e.keyCode == KeyCode_Home)
	{
		e.preventDefault();
		GalleryMoveToFirstPhoto();
	}
	else if (e.keyCode == KeyCode_End)
	{
		e.preventDefault();
		GalleryMoveToLastPhoto();
	}
}

/// ******************************************************************
/// Posune vypis fotografii o jednu stranu vzad.
/// ******************************************************************
function GalleryMoveForward()
{
	var initialPhoto = GalleryGetPageBorders(GallerySelectedPageIndex - 1).UpperBorder;
			
	GallerySelectPage(GallerySelectedPageIndex - 1, initialPhoto,
		function()
		{
			GallerySelectPhoto(initialPhoto);
		});
}

/// ******************************************************************
/// Posune vypis fotografii o jednu stranu vzad.
/// ******************************************************************
function GalleryMoveBackward()
{
	var initialPhoto = GalleryGetPageBorders(GallerySelectedPageIndex + 1).LowerBorder;
			
	GallerySelectPage(GallerySelectedPageIndex + 1, initialPhoto,
		function()
		{
			GallerySelectPhoto(initialPhoto);
		});
}

/// ******************************************************************
/// Vybere uplne prvni fotografii jako aktualni.
/// ******************************************************************
function GalleryMoveToFirstPhoto()
{
	GallerySelectPhoto(0);
}

/// ******************************************************************
/// Vybere uplne posledni fotografii jako aktualni.
/// ******************************************************************
function GalleryMoveToLastPhoto()
{
	GallerySelectPhoto(GalleryGetLastPhotoIndex());
}

/// ******************************************************************
/// Udalost kliknuti na malou fotografii.
/// ******************************************************************
function GalleryPhotoClick(item)
{
	var index = parseInt(item.attr("index"));
	
	// vyber nove
	GallerySelectPhoto(index);
}

/// ******************************************************************
/// Udalost kliknuti na tlacitko s predchozi fotografii.
/// ******************************************************************
function GalleryMoveToPreviousPhoto()
{
	GallerySelectPhoto(GallerySelectedPhotoIndex - 1);
}

/// ******************************************************************
/// Udalost kliknuti na tlacitko s dalsi fotografii.
/// ******************************************************************
function GalleryMoveToNextPhoto()
{
	GallerySelectPhoto(GallerySelectedPhotoIndex + 1);
}

/// ******************************************************************
/// Vyber nove fotografie.
/// ******************************************************************
function GallerySelectPhoto(photoIndex)
{
	if (photoIndex == GallerySelectedPhotoIndex) return;
	else if (photoIndex < 0 || photoIndex >= GalleryTotalPhotos) return;
	
	if (GalleryGetPageIndexByPhotoIndex(photoIndex) != GallerySelectedPageIndex)
	{
		GallerySelectPage(GalleryGetPageIndexByPhotoIndex(photoIndex), photoIndex,
			function()
			{
				GallerySelectPhotoInner(photoIndex);
			});
	}
	else GallerySelectPhotoInner(photoIndex);
}

/// ******************************************************************
/// Vyber nove fotografie (vlastni rutina).
/// ******************************************************************
function GallerySelectPhotoInner(photoIndex)
{
	// deaktivace predchozi zvolene fotografie
	$(".gallery-photo:eq("+ String(GallerySelectedPhotoIndex) +") img").each
	(
		function()
		{
			var photoObject = GalleryPhotoSet[GallerySelectedPhotoIndex];
			$(this).animate({ opacity : GalleryDefaultPhotoOpacity }, "fast");
		}
	);
	
	// aktivace aktualne zvolene fotografie
	$(".gallery-photo:eq("+ String(photoIndex) +") img").each
	(
		function()
		{
			var photoObject = GalleryPhotoSet[photoIndex];
			
			// zmena viditelnosti
			$(this).animate({ opacity : GallerySelectedPhotoOpacity }, "normal");
			
			// vyber nove hlavni fotografie
			if (GalleryPhotoImageIsPreloaded(photoObject.DetailImageUrl))
			{
				GalleryElement_MainImage.attr("src", photoObject.DetailImageUrl);
			}
			else
			{
				GalleryElement_MainImageContainer.fadeOut(200,
					function()
					{
						GalleryElement_MainImage.attr("src", photoObject.DetailImageUrl);
						GalleryElement_MainImageLoading.fadeIn(0);
					}
				);
			}
		}
	);
	
	// aktualizace odkazu na dalsi a predchozi fotku
	GalleryUpdatePrevNextButtons(photoIndex);
	
	// aktualizace informaci o pozici aktualni fotky
	GalleryUpdatePhotoPosition(photoIndex);
	
	GallerySelectedPhotoIndex = photoIndex;
}

/// ******************************************************************
/// Aktualizuje zobrazeni odkazu pro zobrazeni predchozi/dalsi fotky.
/// ******************************************************************
function GalleryUpdatePrevNextButtons(pindex)
{
	if (pindex > 0)
	{
		GalleryElement_PreviousAnchor.each
		(
			function()
			{
				// $(this).attr("href", GalleryPhotoSet[pindex - 1].Url);
				if ($(this).is("visible") == false)
				{
					$(this).fadeIn("fast");
				}
			}
		);
	}
	else GalleryElement_PreviousAnchor.fadeOut("fast");
	
	if (pindex < (GalleryTotalPhotos - 1))
	{
		GalleryElement_NextAnchor.each
		(
			function()
			{
				// $(this).attr("href", GalleryPhotoSet[pindex + 1].Url);
				if ($(this).is("visible") == false)
				{
					$(this).fadeIn("fast");
				}
			}
		);
	}
	else GalleryElement_NextAnchor.fadeOut("fast");
}

/// ******************************************************************
/// Aktualizuje zobrazeni poradi vybrane fotky.
/// ******************************************************************
function GalleryUpdatePhotoPosition(pindex)
{
	GalleryElement_CurrentPosition.text(String(pindex + 1));
}

/// ***************************************
/// Vybere novou stranu.
/// ***************************************
function GallerySelectPage(pageIndex, initialPhotoIndex, callback)
{
	if (GalleryPageIndexIsValid(pageIndex) == false) return;
	else if (pageIndex == GallerySelectedPageIndex) return;
	
	// aktualizace cache fotografii
	GalleryPreloadPhotos(initialPhotoIndex ? initialPhotoIndex : pageIndex * GalleryPageSize);
	
	// 1) schovani fotografii aktualni strany
	// 2) zobrazeni fotografii nove strany
	var movingForward = pageIndex < GallerySelectedPageIndex;
	
	$(".gallery-page:animated").stop();
	
	$(".gallery-page-" + String(GallerySelectedPageIndex)).fadeOut("fast",
		function()
		{
			if (movingForward == false)
			{
				$(".gallery-page-" + String(pageIndex))
					.css("top", String(GalleryPageHeight + 10) + "px")
					.show(0)
					.animate({ top : "-10px" }, "normal",
						function()
						{
							$(this).animate({ top : "5px" }, "fast",
								function()
								{
									$(this).animate({ top : "0px" }, "normal",
										function()
										{
											if (callback) callback();
										});
								});
						});
			}
			else
			{
				$(".gallery-page-" + String(pageIndex))
					.css("top", String(- GalleryPageHeight - 10) + "px")
					.show(0)
					.animate({ top : "10px" }, "normal",
						function()
						{
							$(this).animate({ top : "-5px" }, "fast",
								function()
								{
									$(this).animate({ top : "0px" }, "normal",
										function()
										{
											if (callback) callback();
										});
								});
						});
			}
		});
	
	// aktivace/deaktivace tlacitek pro presun mezi stranami
	GalleryUpdateMoveButtons(pageIndex);
	
	// ulozeni aktualni hodnoty
	GallerySelectedPageIndex = pageIndex;
}

/// ****************************************************************
/// Aktivace/deaktivace tlacitek pro posun mezi stranami.
/// ****************************************************************
function GalleryUpdateMoveButtons(pageIndex)
{
	if (pageIndex > 0)
	{
		if (GalleryElement_MoveForwardAnchor.is("visible") == false)
		{
			GalleryElement_MoveForwardAnchor.fadeIn("fast");
		}
	}
	else GalleryElement_MoveForwardAnchor.fadeOut("fast");
	
	if (pageIndex < GalleryGetLastPageIndex())
	{
		if (GalleryElement_MoveBackwardAnchor.is("visible") == false)
		{
			GalleryElement_MoveBackwardAnchor.fadeIn("fast");
		}
	}
	else GalleryElement_MoveBackwardAnchor.fadeOut("fast");
}

/// ****************************************************************
/// Spusti prednatecni fotografii.
/// ****************************************************************
function GalleryPreloadPhotos(initialPhotoIndex)
{
	// nacteni vybrane fotografie
	GalleryPreloadSinglePhoto(GalleryPhotoSet[initialPhotoIndex], true);
	
	// nacteni nadchazejici fotografie
	var followingPhotoIndex = null;
	if (initialPhotoIndex < GalleryGetLastPhotoIndex())
	{
		followingPhotoIndex = initialPhotoIndex + 1;
		GalleryPreloadSinglePhoto(GalleryPhotoSet[followingPhotoIndex], true);
	}
	
	// nacteni predchozi fotografie
	var precedingPhotoIndex = null;
	if (initialPhotoIndex > 0)
	{
		precedingPhotoIndex = initialPhotoIndex - 1;
		GalleryPreloadSinglePhoto(GalleryPhotoSet[precedingPhotoIndex], true);
	}
	
	// nacteni ostatnich
	var loadingIterator = 0;
	for (loadingIterator = 0; loadingIterator <= GalleryGetLastPhotoIndex(); loadingIterator++)
	{
		if (precedingPhotoIndex && loadingIterator == precedingPhotoIndex) continue;
		else if (followingPhotoIndex && loadingIterator == followingPhotoIndex) continue;
		else GalleryPreloadSinglePhoto(GalleryPhotoSet[loadingIterator], false);
	}
}

/// ****************************************************************
/// Spusti prednatecni jedne fotografie.
/// ****************************************************************
function GalleryPreloadSinglePhoto(photoObject, fullPreload)
{
	// list
	GalleryPreloadSinglePhotoImage(photoObject.Index, photoObject.ListImageUrl, GalleryPhotoListImageLoadedCallback);
	
	if (fullPreload) GalleryPreloadSinglePhotoImage(photoObject.Index, photoObject.DetailImageUrl);
}

function GalleryPhotoListImageLoadedCallback(callbackPhotoIndex)
{
	$(".gallery-photo:eq("+ String(callbackPhotoIndex) +")").each
	(
		function()
		{
			$("span", $(this)).css("display", "none");
			$("img", $(this)).css("display", "inline");
		}
	);
}

function GalleryPreloadSinglePhotoImage(pindex, imageUrl, callback)
{
	var imageObject = new Image();
	
	// zachyceni udalosti nacteni obrazku jeste pred samotnym
	// nastavenim, aby se predeslo nezachyceni rychle nactenych obrazku
	
	imageObject.onload =
		function(e)
		{
			if (GalleryPhotoImageIsPreloaded(imageUrl) == false)
			{
				GalleryPreloadedImages.push(imageUrl);
			}
			
			if (callback) callback(pindex, imageUrl);
		};
	
	imageObject.src = imageUrl;
}

function GalleryPhotoImageIsPreloaded(imageUrl)
{
	var iterator = 0;
	
	for (iterator = 0; iterator < GalleryPreloadedImages.length; iterator++)
	{
		if (GalleryPreloadedImages[iterator] == imageUrl) return true;
	}
	
	return false;
}

/// ***************************************
/// Overi, zda uvedene dva indexy fotografii patri na stejnou stranu.
/// ***************************************
function GalleryPhotosMatchSamePage(firstPhotoIndex, secondPhotoIndex)
{
	return GalleryGetPageIndexByPhotoIndex(firstPhotoIndex) == GalleryGetPageIndexByPhotoIndex(secondPhotoIndex);
}

/// ***************************************
/// Vrati index strany, na ktere se nachazi dana fotografie.
/// ***************************************
function GalleryGetPageIndexByPhotoIndex(photoIndex)
{
	return Math.floor(photoIndex / GalleryPageSize);
}

function GalleryGetPhotosByPage(pageIndex)
{
	if (GalleryPageIndexIsValid(pageIndex))
	{
		var photosByPage = new Array();
		var i = 0;
		
		for (i = GalleryGetPageBorders(pageIndex).LowerBorder; i <= GalleryGetPageBorders(pageIndex); i++)
		{
			photosByPage.push(GalleryPhotoSet[i]);
		}
		
		return photosByPage;
	}
	else return null;
}

/// ***************************************
/// Vrati index posledni fotografie.
/// ***************************************
function GalleryGetLastPhotoIndex()
{
	return GalleryTotalPhotos - 1;
}

/// ***************************************
/// Vrati index posledni strany.
/// ***************************************
function GalleryGetLastPageIndex()
{
	return GalleryTotalPages - 1;
}

/// ***************************************
/// Vrati hranice vybrane stranky.
/// ***************************************
function GalleryGetPageBorders(pageIndex)
{
	if (GalleryPageIndexIsValid(pageIndex))
	{
		var lowerBorder = pageIndex * GalleryPageSize;
		var upperBorder = (pageIndex * GalleryPageSize) + GalleryPageSize - 1;
		
		if (upperBorder > GalleryGetLastPhotoIndex())
		{
			upperBorder = GalleryGetLastPhotoIndex();
		}
		
		var borders = 
		{
			LowerBorder : lowerBorder,
			UpperBorder : upperBorder
		};
		
		return borders;
	}
	else return null;
}

/// ***************************************
/// Overi, zda je uvedeny index stranky platny.
/// ***************************************
function GalleryPageIndexIsValid(pageIndex)
{
	return pageIndex >= 0 && pageIndex <= GalleryGetLastPageIndex();
}
