﻿/// ********************************
/// VALIDATIONS
/// ********************************
function ValidationStringMatchesRegExp(value, rx)
{
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}

function ValidationValidateRequiredInput(inp, msg, msgArray)
{
	if (jQuery.trim(inp.val()).length > 0) return true;
	else
	{
		if (msgArray) msgArray.push(msg);
		return false;
	}
}

function ValidationValidateEmailInput(inp, msg, msgArray)
{
	var emailValue = inp.val();
	
	if (jQuery.trim(emailValue).length > 0)
	{
		if (ValidationStringMatchesRegExp(emailValue, EmailRegExpPattern)) return true;
		else
		{
			if (msgArray) msgArray.push(msg);
			return false;
		}
	}
	else
	{
		inp.val("");
		return true;
	}
}

function ValidationCreateMessage(headerText, messageArray)
{
	var message = "";
	if (headerText != null && headerText.length > 0) message += headerText + "\n";
	
	var messageIterator = 0;
	for (messageIterator = 0; messageIterator < messageArray.length; messageIterator++)
	{
		message += "\n▪ " + messageArray[messageIterator];
	}
	
	return message;
}

function OnComboBoxClientValidate(validator, args)
{
	var cmb = eval(validator.controltovalidate);
	args.IsValid = cmb.GetText() != cmb.Items[0].Text;
}

function OnLawAgreementClientValidate(validator, args)
{
	args.IsValid = $(".law-agreement").attr("checked");
}

/// ********************************
/// TOP STORY
/// ********************************
var TopStorySet = new Array();
var TopStorySetAnimationTimeout = 5000;
var TopStorySetTimer = null;
var TopStoryItemIndex = 0;
var TopStorySelectedItemOpacity = "1";
var TopStoryDefaultItemOpacity = "0.4";

// Spusti animaci
function TopStorySetAnimationInit(initialItemIndex)
{
	TopStoryItemIndex = initialItemIndex;
	
	$(document).ready
	(
		function()
		{
			SetTopStorySetAnimationTiming();
	
			PreloadTopStorySetImages();
			
			InitTopStoryHoverEffects();
			
			UpdateTopStoryItems();
		}
	);
}

function InitTopStoryHoverEffects()
{
	$(".topstory")
		.mouseover(DisableTopStorySetAnimationTiming)
		.mouseout(SetTopStorySetAnimationTiming);
	
	var i = 0, topstoryItemsElem = $(".topstory-items");
	
	for (i = 0; i < TopStorySet.length; i++)
	{
		$("div[class*='topstory-item-"+String(i)+"']", topstoryItemsElem)
			.attr("topstory-item-index", String(i))
			.mouseover
			(
				function()
				{
					var index = $(this).attr("topstory-item-index");
					
					if (TopStoryItemIndex != index)
					{
						TopStoryItemIndex = index;
						
						DisableTopStorySetAnimationTiming();
						
						SetTopStorySetAnimationTiming();
						
						SelectTopStoryItem(index);
					}
				}
			);
	}
}

function PreloadTopStorySetImages()
{
	var i = 0, item = null, dullImage1 = null, dullImage2 = null, dullImage3 = null;
	for (i = 0; i < TopStorySet.length; i++)
	{
		item = TopStorySet[i];
		
		dullImage1 = new Image();
		dullImage1.src = item.TopStorySetImageUrl;
		
		dullImage2 = new Image();
		dullImage2.src = item.ListImageUrl;
	}
}

// Nastavi casovani animace
function SetTopStorySetAnimationTiming()
{
	TopStorySetTimer = setTimeout("TopStorySetAnimationRoutine()", TopStorySetAnimationTimeout);
}

function DisableTopStorySetAnimationTiming()
{
	if (TopStorySetTimer != null)
	{
		clearTimeout(TopStorySetTimer);
		TopStorySetTimer = null;
	}
}

// Animacni rutina
function TopStorySetAnimationRoutine()
{
	TopStoryItemIndex++;
	if (TopStoryItemIndex >= TopStorySet.length)
	{
		TopStoryItemIndex = 0;
	}
	
	SelectTopStoryItem(TopStoryItemIndex);
	
	SetTopStorySetAnimationTiming();
}

// Nastavi vybranou polozku jako aktivni
function SelectTopStoryItem(index)
{
	// nastaveni top story
	var selectedItem = TopStorySet[index];
	var selectedItemTitleDecoded = $("<div />").html(selectedItem.Title).text();
	
	var topstoryElem = $(".topstory");
	
	topstoryElem.hide();
	
	var topstoryImageElem = $(".topstory-image", topstoryElem);
	var topstoryTitleElem = $(".topstory-title", topstoryElem);
	var topstoryDescriptionElem = $(".topstory-description", topstoryElem);
	
	topstoryImageElem.attr("href", selectedItem.Url).attr("alt", selectedItemTitleDecoded)
		.attr("title", selectedItemTitleDecoded);
	
	$("img", topstoryImageElem).attr("src", selectedItem.TopStoryImageUrl);
	
	topstoryTitleElem.attr("href", selectedItem.Url).attr("alt", selectedItemTitleDecoded)
		.attr("title", selectedItemTitleDecoded).html(selectedItem.Title);
	
	topstoryDescriptionElem.html(selectedItem.Description);
	
	// nastaveni seznamu vedle topstory
	UpdateTopStoryItems();
	
	topstoryElem.fadeIn("normal");
}

function UpdateTopStoryItems()
{
	var iterator = 0;
	for (iterator = 0; iterator < TopStorySet.length; iterator++)
	{
		UpdateTopStoryItem(iterator, iterator == TopStoryItemIndex);
	}
}

function UpdateTopStoryItem(index, active)
{
	var relatedItem = TopStorySet[index];
	var relatedItemTitleDecoded = $("<div />").html(relatedItem.Title).text();
	
	var itemElem = $("div[topstory-item-index='"+String(index)+"']");
	var itemImageElem = $(".topstory-item-image", itemElem);
	var itemTitleElem = $(".topstory-item-title", itemElem);
	
	if (active)
	{
		itemElem.addClass("active");
		itemImageElem.css("opacity", TopStorySelectedItemOpacity);
	}
	else
	{
		itemElem.removeClass("active");
		itemImageElem.css("opacity", TopStoryDefaultItemOpacity);
	}
	
	itemImageElem.attr("href", relatedItem.Url).attr("alt", relatedItemTitleDecoded).attr("title", relatedItemTitleDecoded);
	itemTitleElem.attr("alt", relatedItemTitleDecoded).attr("title", relatedItemTitleDecoded).html(relatedItem.Title);
}

function OnNewsFolderChanged_ComboBox(sender, args)
{
	var selectedIndex = sender.Index;
	var selectedValue = sender.Value;
	
	$(".articles div[class*='folder-']").hide();
	$(".articles .folder-" + String(selectedIndex)).fadeIn("normal");
	
	var date = new Date();
	date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
	$.cookie(PreferredNewsFolderCookieName, selectedValue, { path: '/', expires: date });
}


/// ********************************
/// 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.4";
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.floor(GalleryTotalPhotos / GalleryPageSize) + (GalleryTotalPhotos % GalleryPageSize > 0 ? 1 : 0);
	
	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();
}


/// ********************************
/// GALLERY - ITEM SELECTION
/// ********************************
var GalleryItemSelectionTotalItems = 0;
var GalleryItemSelectionSyncRoot = null;
var GalleryItemSelectionContainer = null;

function GalleryItemSelectionInit()
{
	$(document).ready
	(
		function()
		{
			GalleryItemSelectionContainer = $(".gallery-item-container").css("min-height", "475px");
			
			var gitems = $(".gallery-item");
			GalleryItemSelectionTotalItems = gitems.length;
			
			if (GalleryItemSelectionTotalItems > 1)
			{
				gitems.each
				(
					function (gfindex)
					{
						$(".gallery-item-anchor", $(this)).click
						(
							function(e)
							{
								e.preventDefault();
								GalleryItemSelectionClick($(this), gfindex);
							}
						);
					}
				);
			}
		}
	);
}

function GalleryItemSelectionClick(anchor, gfindex)
{
	$(".gallery-item:lt("+ String(gfindex) +")").animate({ opacity : "0.01" }, "fast",
		function()
		{
			GalleryItemSelectionClickCallback(gfindex);
		});
	
	$(".gallery-item:gt("+ String(gfindex) +")").animate({ opacity : "0.01" }, "fast",
		function()
		{
			GalleryItemSelectionClickCallback(gfindex);
		});
}

function GalleryItemSelectionClickCallback(gfindex)
{
	if (GalleryItemSelectionSyncRoot) return;
	else GalleryItemSelectionSyncRoot = true;
	
	var csize = { Width : 0, Height : 0 }, isize = { Width : 0, Height : 0 }, iposition = { Left : 0, Top : 0 },
		animationMove = { Left : 0, Top : 0 }, redirectUrl = "";
	
	GalleryItemSelectionContainer
		.css("position", "relative")
		.each
		(
			function()
			{
				csize.Width = $(this).width();
				csize.Height = $(this).height();
			}
		);
	
	$(".gallery-item:eq("+ String(gfindex) +")").each
	(
		function()
		{
			isize.Width = $(this).width();
			isize.Height = $(this).height();
			
			iposition.Left = $(this).position().left;
			iposition.Top = $(this).position().top;
			
			animationMove.Left = ((csize.Width - isize.Width) / 2) - iposition.Left;
			animationMove.Top = ((csize.Height - isize.Height) / 2) - iposition.Top;
			
			if (Math.abs(animationMove.Left) < 20) animationMove.Left = 0;
			if (Math.abs(animationMove.Top) < 20) animationMove.Top = 0;
		}
	)
	.css("position", "relative").css("left", "0px").css("top", "0px")
	.animate({ left : String(animationMove.Left) + "px", top : String(animationMove.Top) + "px" }, "fast",
		function()
		{
			$(".gallery-item-title", $(this)).animate({ color : "#FFEB5C" }, 800,
				function()
				{
					$(this).animate({ color : "#000" }, 600,
						function()
						{
							redirectUrl = $(this).attr("href");
					
							$(".gallery-item:eq("+ String(gfindex) +")").animate({ opacity : "0.01" }, "fast",
								function()
								{
									location.href = redirectUrl;
								});	
						});
				});
		});
}

function OnGallerySelectionChanged_ComboBox(sender, args)
{
	location.href = sender.Value;
}

/// ********************************
/// ROUNDUP TABSTRIP
/// ********************************
var RoundupTabstripTabSet = new Array();
var RoundupTabstripTabSelectedSet = new Array();
var RoundupTabstripInfoSet = new Array();
var RoundupTabstripContentContainer = null;
var RoundupTabstripSelectedIndex = 0;

function RoundupTabstripInit(initialIndex)
{
	RoundupTabstripSelectedIndex = initialIndex;
	
	$(document).ready
	(
		function()
		{
			RoundupTabstripContentContainer = $(".roundup-container")
				.css("position", "relative")
				.css("overflow", "hidden")
				.css("width", "100%")
				.css("height", "150px");
			
			$(".roundup-tab").each
			(
				function(index)
				{
					RoundupTabstripTabSet.push($(this));
					
					$("a", this).click
					(
						function(e)
						{
							e.preventDefault();
							RoundupTabstripTabClick(index);
						}
					);
				}
			);
			
			$(".roundup-tab-selected").each
			(
				function()
				{
					RoundupTabstripTabSelectedSet.push($(this));
				}
			);
			
			$(".roundup").each
			(
				function()
				{
					RoundupTabstripInfoSet.push($(this));
					
					$(this).css("position", "absolute").css("width", "100%");
				}
			);
		}
	);
}

function RoundupTabstripTabClick(index)
{
	if (index < 0 || index >= RoundupTabstripTabSet.length) return;
	if (index == RoundupTabstripSelectedIndex) return;
	
	var iterator = 0, tab = null;
	for (iterator = 0; iterator < RoundupTabstripTabSet.length; iterator++)
	{
		tab = RoundupTabstripTabSet[iterator];
		if (iterator == index) tab.hide();
		else tab.show();
	}
	
	for (iterator = 0; iterator < RoundupTabstripTabSelectedSet.length; iterator++)
	{
		tab = RoundupTabstripTabSelectedSet[iterator];
		if (iterator == index) tab.show();
		else tab.hide();
	}
	
	// animace prechodu
	var rightToLeft = index > RoundupTabstripSelectedIndex;
	
	RoundupTabstripInfoSet[RoundupTabstripSelectedIndex].css("left", "0px")
		.animate({ left: (rightToLeft ? "-" : "") + String(RoundupTabstripContentContainer.width()) + "px" }, "fast",
			function()
			{
				$(this).hide();
			});
	
	RoundupTabstripInfoSet[index]
		.css("left", (rightToLeft ? "" : "-") + String(RoundupTabstripContentContainer.width()) + "px")
		.fadeIn(0).animate({ left: "0px" }, "fast");
	
	RoundupTabstripSelectedIndex = index;
}

/// ********************************
/// VIDEO PERMA-LINK COPY
/// ********************************
var VideoPermaLinkCopySuccessMessage = null;
var VideoPermaLinkCopyFailureMessage = null;
var VideoPermaLinkCopyClipBoardReady = false;

function VideoPermaLinkCopyInit(successMessage, failureMessage)
{
	VideoPermaLinkCopySuccessMessage = successMessage;
	VideoPermaLinkCopyFailureMessage = failureMessage;
	
	$(document).ready
	(
		function()
		{
			$(".video-perma-link-copy").click(VideoPermaLinkCopyDisplayFailureAlert);
		}
	);
	
	$.clipboardReady
	(
		function()
		{
			VideoPermaLinkCopyClipBoardReady = true;
			
			$(".video-perma-link-copy")
				.unbind("click", VideoPermaLinkCopyDisplayFailureAlert)
				.click
				(
					function(e)
					{
						e.preventDefault();
						VideoPermaLinkCopyProcess();
					}
				);
		}, { debug: false }
	);
}

function VideoPermaLinkCopyDisplayFailureAlert(e)
{
	e.preventDefault();
	if (VideoPermaLinkCopyClipBoardReady == false)
	{
		window.alert(VideoPermaLinkCopyFailureMessage);
	}
}

function VideoPermaLinkCopyProcess()
{
	$.clipboard($(".video-perma-link").val());
	window.alert(VideoPermaLinkCopySuccessMessage);
}

/// ********************************
/// COMMENT CREATION
/// ********************************
var CommentCreationFullName = null, CommentCreationEmail = null, CommentCreationText = null, CommentCreationSubmit = null;
var CommentCreationRules = null;
var CommentCreationRulesButton = null;
var CommentCreationValidationHeaderText = null, CommentCreationAuthorFullNameRequiredMessage = null,
	CommentCreationEmailIncorrectMessage = null, CommentCreationTextRequiredMessage = null;

function CommentCreationInit(htext, authorReqMessage, emailIncMessage, textReqMessage)
{
	$(document).ready
	(
		function()
		{
			CommentCreationValidationHeaderText = htext;
			CommentCreationAuthorFullNameRequiredMessage = authorReqMessage;
			CommentCreationEmailIncorrectMessage = emailIncMessage;
			CommentCreationTextRequiredMessage = textReqMessage;
			
			CommentCreationFullName = $(".comment-creation-full-name");
			CommentCreationEmail = $(".comment-creation-email");
			CommentCreationText = $(".comment-creation-text");
			CommentCreationSubmit = $(".comment-creation-submit");
			
			CommentCreationRules = $(".comment-creation-rules");
			CommentCreationRulesButton = $(".comment-creation-rules-button");
			
			CommentCreationSubmit.click
			(
				function(e)
				{
					return CommentCreationOnSubmit(e);
				}
			);
			
			CommentCreationRulesButton.click
			(
				function(e)
				{
					e.preventDefault();
					CommentCreationSetRulesVisibility();
				}
			);
		}
	);
}

function CommentCreationOnSubmit(e)
{
	if (CommentCreationValidate())
	{
		return true;
	}
	else
	{
		e.preventDefault();
		return false;
	}
}

function CommentCreationValidate()
{
	var creationMessageArray = new Array();
	
	ValidationValidateRequiredInput(CommentCreationFullName, CommentCreationAuthorFullNameRequiredMessage, creationMessageArray);
	ValidationValidateEmailInput(CommentCreationEmail, CommentCreationEmailIncorrectMessage, creationMessageArray);
	ValidationValidateRequiredInput(CommentCreationText, CommentCreationTextRequiredMessage, creationMessageArray);
	
	if (creationMessageArray.length == 0) return true;
	else
	{
		window.alert(ValidationCreateMessage(CommentCreationValidationHeaderText, creationMessageArray));
		return false;
	}
}

function CommentCreationSetRulesVisibility()
{
	if (CommentCreationRules.is(":visible")) CommentCreationRules.fadeOut("fast");
	else CommentCreationRules.fadeIn("normal");
}


/// ********************************
/// INQUIRY VOTE
/// ********************************
var InquiryVoteEnabled = true, InquiryVoteDisabledMessage = "";

function InquiryVoteInit(msg)
{
	$(document).ready
	(
		function()
		{
			InquiryVoteDisabledMessage = msg;
			
			$(".inquiry-vote").click(InquiryVoteOnClick);
		}
	);
}

function InquiryVoteOnClick(e)
{
	if (InquiryVoteEnabled == false)
	{
		e.preventDefault();
		window.alert(InquiryVoteDisabledMessage);
	}
}