	$(document).ready(function() {
		$(".photoTools").each(function(i) {
			//$(this).hide();
			$(this).find(".sendLinkToFriend")
				.attr("href", $(this).find(".sendLinkToFriend").attr("href") + "&iframe=true&width=500&height=" + (($(this).find(".sendLinkToFriend").attr("rel") == 'audio') ? 200 : 460) + "&lars:nolayout=1" + (layoutId != "" ? "&layoutId="+layoutId : ""))
				.attr("rel", "sendBox")
				.mouseover(function() {$(this).attr("title", sendToFriendTitle)})
				.click(function() {$(this).attr("title", "")});
		});
		
		/*
		$(".galerie-prehled .photo").each(function(i) {
			$(this)
				.mouseover(function(){
					$(this).find(".photoTools").show();
				})
				.mouseout(function(){
					$(this).find(".photoTools").hide();
				});
		});
		*/
		
		//generate fake links for lightbox
		for (var i = currentPageFirstItem - 1; i >= 0; i--) {
			$("#photoMainOverview").prepend("<a href=\""+ prepareHref(i) +"\" rel=\"lightbox[gal]\" title=\"" + photosDescr[i] + "\" style=\"display: none;\"><img alt= \"" + photosAlt[i] + "\" /></a>");			
		}  
		for (var i = (currentPageLastItem + 1); i < photos.length; i++) {
			$("#photoMainOverview").append("<a href=\""+ prepareHref(i) +"\" rel=\"lightbox[gal]\" title=\"" + photosDescr[i] + "\" style=\"display: none;\"><img alt= \"" + photosAlt[i] + "\" /></a>");
		}
		//generate visible links for lightbox
		$("a.photoLink").each(function(i) {
			pos = i + currentPageFirstItem;
			$(this).attr("href", prepareHref(pos)).attr("rel", "lightbox[gal]");			
		});
				
		//call lightbox		
		$("a[rel^='lightbox']").prettyPhoto({padding: 40, theme: 'light_square', callback: lightboxEndCallback, language_: 'cs'});	
		$("a[rel^='sendBox']").prettyPhoto({padding: 40, theme: 'light_square', language_: 'cs'});	
	});
	
	function prepareHref(pos) {
		var galHref = "/script/galleryFile?guid=" + guid + "&photoId=" + photos[pos] + "&size=big";
		if (fileType[pos] == "video" || fileType[pos] == "audio") {
			var videoFile = "%2Fscript%2FgalleryFile%3Fguid%3D" + guid + "%26photoId%3D" + photos[pos] + "%26size%3Doriginal";
			var videoImage = "%2Fscript%2FgalleryFile%3Fguid=" + guid + "%26photoId=" + photos[pos] + "%26size=big";
			if (fileType[pos] == "video") {
				galHref = "/admin/flash/player.swf?width=" + videoWidth + "&height=" + videoHeight + "&flashvars=width=" + videoWidth + "&height=" + videoHeight + "&file=" + videoFile + "&image=" + videoImage + "&type=video";
			} else {
				galHref = "/admin/flash/player.swf?width=" + audioWidth + "&height=" + audioHeight + "&flashvars=width=" + audioWidth + "&height=" + audioHeight + "&file=" + videoFile + "&image=" + videoImage + "&type=video";
			}	
		}
		return galHref;
	}
		
	function lightboxEndCallback() {
		var returi = ret.lastImage+"";
		returi = returi.replace(new RegExp("%3D", "gi"), "=").replace(new RegExp("%2F", "gi"), "/").replace(new RegExp("%3F", "gi"), "?").replace(new RegExp("%26", "gi"), "&");
		var photoId = getURLParam('photoId', returi);
		var photoPos = parseInt(photos.find(photoId));
		var photoPage = Math.ceil((photoPos + 1) / pageSize);
		if (currentPage != photoPage) {
			document.location.href = uri + "?&pageIndex=" + (photoPage - 1);
		}		
	}
	
	function getURLParam(name, uri) {
		if (typeof uri == "undefined") uri = window.location.href;
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp(regexS);
	  var results = regex.exec(uri);
	  if(results == null)
	    return "";
	  else
	    return results[1];
	}
	
	Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
	}
	
	function galleryValidateSend(formdata) {
		
		if (formdata.from.value == "") {
			alert(gallery_mandatory_mailfrom);
			formdata.from.focus();
			return false;
		}
		
		if (!valid_email(formdata.from.value)) {
			alert(gallery_errormessage_mailfrom);
			formdata.from.focus();
			return false;
		}
		
		if (formdata.to.value == "") {
			alert(gallery_mandatory_mailto);
			formdata.to.focus();
			return false;
		}
		
		if (!valid_email(formdata.to.value)) {
			alert(gallery_errormessage_mailto);
			formdata.to.focus();
			return false;
		}
		
		if (formdata.comment.value == "") {
			alert(gallery_mandatory_comment);
			formdata.comment.focus();
			return false;
		}

		return true;
	}
	
	function resizeIframeWindow() {
		var originalWinHeight = parseInt($(".pp_content", parent.document.body).css("height"));
		var originaliFrameHeight = parseInt($("iframe", parent.document.body).attr("height"));
		if (originalWinHeight > 0 && originaliFrameHeight > 0) {
			var newWinHeight = originalWinHeight - 120;
			var newiFrameHeight = originaliFrameHeight - 120;
		}
		$("iframe", parent.document.body).attr("height", newiFrameHeight);
		$(".pp_content", parent.document.body).attr("style", "height:" + newWinHeight + "px;");
	}
	
	function valid_email(email){
    var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/
    if(RegExp.test(email)){
        return true;
    }else{
        return false;
    }
	} 