var lockGallery		= false;
var pMenu;
var addCommentCanAdd = false;
var containerPageBar = document.getElementById("pageBar");
var thumbnailContainer = document.getElementById("thumbnailContainer");
var pictures;
var showedPictures = new Array();
var maxCommentsPerPage = 10;
var currentCommentPage = 0;
var CommentPages;
var mepp = 6;
var mepr = 6;
var pageRange = 10;
var pictureContainer = document.getElementById("pictureContainer");
var pictureTable = document.getElementById("pictureTable");
var pictureTableTD = document.getElementById("pictureTableTD");
var pictureTable2 = document.getElementById("pictureTable2");
var pictureTableTD2 = document.getElementById("pictureTableTD2");
var friendBox = document.getElementById("friendBox");
var pictureTagContainer = document.getElementById("pictureTagContainer");
var ajaxLoaderImage = document.getElementById("ajaxLoaderImage");
var picture = document.getElementById("picImg");
var picture2 = document.getElementById("picImg2");
var commentBox = document.getElementById("comments");
var commentPager = document.getElementById("commentPager");
var setAsPreviewPic = document.getElementById("setAsPreviewPic");
var deleteGalleryPic = document.getElementById("deleteGalleryPic");
var favGallery = document.getElementById("favGallery");
var addPictures = document.getElementById("addPictures");
var deleteGallery = document.getElementById("deleteGallery");
var changeGallery = document.getElementById("changeGallery");
var taggedPicNames = document.getElementById("taggedPicNames");

var onlyAllPics = document.getElementById("onlyAllPics");
var onlyTaggedPics = document.getElementById("onlyTaggedPics");
var onlyCommentPics = document.getElementById("onlyCommentPics");

var currentPage = 0;
var currentPicture = 0;
var pages = 0;
var comments;
var xMousePos = 0;
var yMousePos = 0;
var choosePoint = false;
var dojoCombine;
var stopTimeout = false;
var toPreload = new Array();
var alreadyPreloaded = new Array();
var reverseIndexing = new Object();
var pointType = "";
var absoluteMouseY;
var absoluteMouseX;
var tagCount = 0;
var tagImgTip = new Object();
var picIndexFilter = new Object();
var picFilter;
var previewPicIndex;
var GalleryType;
var taggedUserId = 0;
dojo.require("dojox.fx");
dojo.style("friendBox","opacity","0");
friendBox.style.display = "none";
var InfoGalleryID = 0;
var pictureIDToPicIndex = new Array();
var isAlreadyPreloading = new Array();
var isTagged = false;
var Gallery = {

		init: function ()
		{
			showAjaxLoader();
			dojo.xhrPost( {
				url: "/"+ galleryConf['language'] +"/ajax/gallery/getPictures",
				handleAs:"json",
				content: 
				{ 
					mediaID: galleryConf['mediaID'],
					mediaTYP: galleryConf['mediaTYP'],
					commentLimit: maxCommentsPerPage
				},
				load: function (result){
					if(result.data.length > 0)
					{
						document.getElementById("galleryContainer").style.display = "block";
						Gallery.ParseThumbnails(result,picFilter);
					}
					else
					{
						hideAjaxLoader();
						document.getElementById("galleryContainerMessage").innerHTML = rb_locale['JSGALLERY_NO_IMAGES_FOUND'];
						document.getElementById("galleryContainerMessage").style.display = "block";

						if(result.info.galleryPermission.canEdit == true)
						{
							InfoGalleryID = result.info.galleryID;
							addPictures.style.display = "block";
							deleteGallery.style.display = "block";
							changeGallery.style.display = "block";
						}
					}
				}
			});
		},
		
		ParseThumbnails: function( items , filterType)
		{

			if(Gallery.isLocked())
			{
				return;
			}
			showAjaxLoader();
			if(filterType == undefined)
			{
				filterType = "all";
			}
			
			picFilter = filterType;
			showedPictures = new Array();
			reverseIndexing['comments'] = new Object();
			reverseIndexing['tagged'] = new Object();
			reverseIndexing['taggedUser'] = new Object();
			reverseIndexing['all'] = new Object();
			if(items != "")
			{
				pictures = items.data;
			}
		
			var filterShowI = 0;
			var filterI = 0
			for(; filterI < pictures.length; filterI++)
			{
				pictureIDToPicIndex[pictures[filterI].pictureID] = filterI;
				if(filterType == "comments")
				{
					if(pictures[filterI].numComments > 0)
					{
						
						showedPictures[filterShowI] = pictures[filterI];
						reverseIndexing['comments'][filterShowI] = filterI;
						filterShowI++;
					}
				}
				else if(filterType == "tagged")
				{
					if(pictures[filterI].numTagged > 0)
					{
						
						showedPictures[filterShowI] = pictures[filterI];
						reverseIndexing['tagged'][filterShowI] = filterI;
						filterShowI++;
					}
				}
				else if(filterType == "taggedUser")
				{
					
					if(pictures[filterI].numTagged > 0)
					{

						var numTagUserI = 0;
						
						for(numTagUserI = 0; numTagUserI < pictures[filterI].taggedPeople.length; numTagUserI++)
						{

							if(pictures[filterI].taggedPeople[numTagUserI].taggedID == taggedUserId)
							{
								showedPictures[filterShowI] = pictures[filterI];
								reverseIndexing['taggedUser'][filterShowI] = filterI;
								filterShowI++;
								break;
							}
						}
						
					}
				}
				else
				{
					showedPictures[filterShowI] = pictures[filterI];
					reverseIndexing['all'][filterShowI] = filterI;
					filterShowI++;
				}
			}
			if(items != "")
			{
				previewPicIndex = items.info.previewPicIndex;
			}
			else
			{
				previewPicIndex = 0;
			}
			if(items.info != undefined)
			{
				if(items.info.type == undefined)
				{
					items['info']['type'] = "tagged";
				}
				GalleryType = items.info.type;
			}
			else
			{
				GalleryType = "tagged";
			}
			if(items != "" && items.info.type == 'global' && items.info.topGallery != "")
			{
				if(items.info.topGallery == "0")
				{
					document.getElementById('favGalleryLink').innerHTML = rb_locale['JSGALLERY_FAV_LINK_ADD'];
				}
				else if(items.info.topGallery == "1")
				{
					document.getElementById('favGalleryLink').innerHTML = rb_locale['JSGALLERY_FAV_LINK_DELETE'];
				}
			}
			
			if(showedPictures.length > 0)
			{
				pages = Math.ceil(showedPictures.length/mepp);
				var setPicIndex
				if(items != "")
				{
					
					alreadyPreloaded[items.info.previewPicIndex] = true;
					currentPage = Math.floor(items.info.previewPicIndex/mepp);
					setPicIndex = items.info.previewPicIndex;
					
				}
				else
				{
					currentPage = 1;
					if(picIndexFilter[picFilter] == undefined)
					{
						setPicIndex = 0;
					}
					else
					{
						setPicIndex = picIndexFilter[picFilter];
					}
				}
				
				if(currentPage >= 0 && currentPage < pages)
				{
					
					Gallery.setPage(currentPage);
				}
				document.getElementById("gallerygonext").style.display = "block";
				document.getElementById("gallerygoprev").style.display = "block";
				Gallery.setPicture(setPicIndex);
			}
			else
			{
				alert('keine bilder da');
				Gallery.ParseThumbnails('');
			}
			hideAjaxLoader();
			
			
		},

		showOnlyTagged: function(userid)
		{
			taggedUserId = userid;
			var picidsToPreload = "";
			var selectPicI = 0;
			for(selectPicI = 0; selectPicI < pictures.length; selectPicI++)
			{
				if(alreadyPreloaded[selectPicI])
				{
					continue;
				}
				
				if(pictures[selectPicI].numTagged > 0)
				{
					picidsToPreload = picidsToPreload+pictures[selectPicI].pictureID+",";
				}
			}
			if(picidsToPreload != "")
			{
				Gallery.preloadingTaggedPictures(picidsToPreload);
			}
			
		},

		preloadingTaggedPictures: function(picids)
		{
			dojo.xhrPost( {
				url: "/"+ galleryConf['language'] +"/ajax/gallery/preloadTagged",
				handleAs:"json",
				content:
				{
					pictureIDs: picids
				},
				load: function (result)
				{
					var preloadTagI = 0;
					
					for(preloadTagI = 0;preloadTagI < result.length; preloadTagI++)
					{
						pictures[pictureIDToPicIndex[result[preloadTagI].pictureID]].taggedPeople = result[preloadTagI].taggedPeople;
					}
					Gallery.ParseThumbnails('','taggedUser');
				}
			});


		},

		setPage: function( pageid )
		{
			
			if(Gallery.isLocked())
			{
				return;
			}
			dojo.fadeOut({
					node: "thumbnailContainer", 
					duration:500, 
					onEnd: function (){ Gallery.setPage_process(pageid);}
			}).play();
			
		},
		
		setPage_process: function(pageid)
		{
			if(Gallery.isLocked())
			{
				return;
			}
			var fadeIn = dojo.fadeIn({node: "thumbnailContainer", duration:1000});
			var rowTR = document.createElement("tr");
			var forI = pageid*mepp;
			var c = 1;
			var addLastDiv = true;
			thumbnailContainer.innerHTML = "";
			var tableThumbnails = document.createElement("table");
			tableThumbnails.setAttribute("cellspacing","0");
			tableThumbnails.setAttribute("cellpadding","5");
			for(i = forI; i < (forI+mepp); i++)
			{
				
				if(showedPictures[i] == null || showedPictures[i] == undefined)
				{
					break;
				}
				addLastDiv = true;
				var thumbTD = document.createElement("td");
				thumbTD.style.margin = "3px";
				thumbTD.style.width = Math.ceil(610/mepr)+"px";
				thumbTD.style.height = "67px";
				thumbTD.style.align = "center";
				
				var imgThumbLink = document.createElement("a");
				imgThumbLink.setAttribute("href","javascript:Gallery.setPicture('"+i+"')");
				
				var imgThumb = document.createElement("img");
				imgThumb.setAttribute("src",showedPictures[i].picSrc+".2.jpg");
				imgThumb.setAttribute("onmouseover", 	"Gallery.timeoutMouseOver("+i+");");
				imgThumb.setAttribute("onmouseout", 	"Gallery.stopMouseOver("+i+");");
				
				
				imgThumbLink.appendChild(imgThumb);
				thumbTD.appendChild(imgThumbLink);

				
				rowTR.appendChild(thumbTD);
				addLastDiv = true;
				
				if(c%mepr == 0)
				{
					tableThumbnails.appendChild(rowTR);
					rowTR = document.createElement("tr");
					addLastDiv = false;
				}
				c++;
			}
			
			if(addLastDiv)
			{
				var dummyTD = document.createElement("td");
				for(i = 0;i<c%mepr;i++)
				{
					rowTR.appendChild(dummyTD);
				}
				tableThumbnails.appendChild(rowTR);
			}
			
			thumbnailContainer.innerHTML = "<table cellspacing=\"0\" cellpadding=\"5\">"+tableThumbnails.innerHTML+"</table>";
			
			
			currentPage = pageid;
			containerPageBar.innerHTML = JSNavbar( currentPage, pages, pageRange, "javascript:Gallery.setPage(%count%)");
			dojo.fadeIn({
					node: "thumbnailContainer", 
					duration:500
			}).play();
		},

		downloadPic:function(msg)
		{
			if(!isTagged)
			{
				alert(msg);
			}
			else
			{
				window.open("/DE/ajax/dlPic?u="+showedPictures[currentPicture].pictureID,"", "width=0,height=0");
			}
		},
		
		setPicture: function( pictureIndex, mustPreloaded )
		{
			
			if(Gallery.isLocked())
			{
				return;
			}
			
			
			
			var elpictureTableTD = dojo.coords(picture2);
			var elpictureTable = dojo.coords(picture2);
			var diff = parseInt(elpictureTableTD.w)-50;
			
			if(diff < 0) {
				ajaxLoaderImage.style.left = "0px";
			}
			else{
				ajaxLoaderImage.style.left = parseInt(diff/2)+"px";
			}
			
			diff = parseInt(elpictureTable.h)-50;
			if(diff < 0) {
				ajaxLoaderImage.style.top = "0px";
			}
			else{
				ajaxLoaderImage.style.top = parseInt(diff/2)+"px";
			}
			
			if(alreadyPreloaded[reverseIndexing[picFilter][pictureIndex]] == undefined || alreadyPreloaded[reverseIndexing[picFilter][pictureIndex]] == false)
			{
				ajaxLoaderImage.style.display = "block";
				dojo.style("picImg","opacity","0");
				dojo.style("picImg2","opacity","0");
				Gallery.hideTaggedPeople();
				Gallery.preloadImage(pictureIndex,"loadPicture");
				return;
				
			}			
			
			
			currentPicture = pictureIndex;
			
			pictureTagContainer.innerHTML = "";
			if(toPreload[reverseIndexing[picFilter][pictureIndex]] != "undefined")
			{
				Gallery.stopMouseOver(pictureIndex);
			}
			
			
			var tmpPage = Math.floor(pictureIndex/mepp);
			if(tmpPage != currentPage)
			{
				Gallery.setPage(tmpPage);
			}
			
			
			var preloadIndex = (parseInt(pictureIndex)+1);
			var currentPic = showedPictures[pictureIndex];
			
			setAsPreviewPic.style.display = "none";
			deleteGalleryPic.style.display = "none";
			
			if(GalleryType == "global")
			{
				favGallery.style.display = "none";
				addPictures.style.display = "none";
				deleteGallery.style.display = "none";
				changeGallery.style.display = "none";
				document.getElementById("gotogalleryactionlink").style.display = "none";
				document.getElementById("gotogalleryactionlinkbr").style.display = "none";
			}
			else if(GalleryType == "tagged")
			{
				document.getElementById("reportGallery").style.display = "none";
				onlyTaggedPics.style.display = "none";
				changeGallery.style.display = "none";
			}

			if(currentPic.permissions.canEdit == true)
			{	
				setAsPreviewPic.style.display = "block";
				deleteGalleryPic.style.display = "block";
				if(GalleryType == "global" || GalleryType == "private")
				{
					addPictures.style.display = "block";
					deleteGallery.style.display = "block";
					changeGallery.style.display = "block";
				}
			}
			
			if(GalleryType == "global" && currentPic.permissions.isSuperAdmin == true)
			{
				favGallery.style.display = "block";
			}
			
			var currentPicSize = currentPic.picSize;
			
			
			picture.src = picture2.src;
			picture.style.width = picture2.style.width;
			picture.style.height = picture2.style.height;
			
			pictureTableTD.style.width = pictureTableTD2.style.width;
			pictureTable.style.height = pictureTable2.style.height;
			pictureTable.style.top = pictureTable2.style.top;
			pictureTable.style.left = pictureTable2.style.left;
			
			
			
			document.getElementById("picImg").style.display = "block";
			dojo.style("picImg","opacity","1");
			dojo.style("picImg2","opacity","0");
			
			dojo.style("pictureTagContainer","opacity","0");
			
			picture2.src = currentPic.picSrc+".3.jpg";
			picture2.style.width = currentPicSize.width+"px";
			picture2.style.height = currentPicSize.height+"px";
			
			
			
			var elPos = dojo.coords(pictureContainer);
			var ContainerHalfWidth = Math.ceil(elPos.w/2);

			elPos = dojo.coords(picture2);
			var pic2SizeWidth = Math.ceil(elPos.w/2);
			var pic2SizeHeight = Math.ceil(elPos.h/2);
			var sizeTo;
			if(elPos.h < 430)
			{
				sizeTo = 430;
				pictureContainer.style.height = "430px";
				pictureTable2.style.top = (215-pic2SizeHeight)+"px";
			}
			else
			{
				sizeTo = currentPicSize.height;
				pictureContainer.style.height = currentPicSize.height+"px";
				pictureTable2.style.top = "0px";
			}		
			pictureTable2.style.left = "-"+pic2SizeWidth+"px";
			tagImgTip = new Object();
			var tagDiv = new Array(currentPic.taggedPeople.length);
			tagCount = 0;
			var myTest = "";
			taggedPicNames.innerHTML = "";
			isTagged = false;
			for(tagCount = 0; tagCount < currentPic.taggedPeople.length; tagCount++)
			{
				
				var currentTag = currentPic.taggedPeople[tagCount];
				tagImgTip[tagCount] = currentTag.showTip;
				var xPos =  (currentTag.coordX-15);
				var yPos =  (currentTag.coordY-15);
				Gallery.createTaggedPoint(tagCount,xPos,yPos);
				var deleteTagImg = "";
				if(currentTag.taggedID == galleryConf['sessionID'] && !isTagged)
				{
					isTagged = true;
				}
				if(currentTag.taggedID == galleryConf['sessionID'] || currentTag.taggerID == galleryConf['sessionID'] || galleryConf['isAdmin'] == "1")
				{
					deleteTagImg = "<a href=\"javascript:Gallery.deleteTag('"+currentTag.taggedID+"','"+tagCount+"');\"><img src=\""+galleryConf['templatePath']+"/images/button/delete12.png\"></a>";
				}
				else
				{
					deleteTagImg = "";
				}
				var tagNameLink = "<span id=\"taglinkspan_"+tagCount+"\"><a href='/"+galleryConf['language']+"/user/view/"+currentTag.taggedID+"' id=\"taglinkname_"+tagCount+"\" onmouseover=\"Gallery.showTag('"+tagCount+"');\" onmouseout=\"Gallery.hideTag('"+tagCount+"');\">"+currentTag.taggedFirstname+" "+currentTag.taggedName+"</a>"+deleteTagImg+"</span>&nbsp;";
				
				taggedPicNames.innerHTML = taggedPicNames.innerHTML+tagNameLink;
			}

			if(currentPic.numTagged <= 0)
			{
				taggedPicNames.innerHTML = "<center><b>"+rb_locale['JSGALLERY_NO_USER_TAGGED']+"</b></center>";
			}
			
			comments = currentPic.numComments;
			CommentPages = Math.ceil(comments/maxCommentsPerPage);

			picIndexFilter[picFilter] = pictureIndex;
			
			commentBox.innerHTML = "";
			if(galleryConf['commentOrderType'] == 'asc')
			{
				currentCommentPage = CommentPages-1;
			}
			else
			{
				currentCommentPage = 0;
			}
			if(currentPic.numComments > 0)
			{
				var numCommentsI = currentPic.commentsDesign.length;
				var commentI = 0;
				for(;commentI < numCommentsI; commentI++)
				{
					commentBox.innerHTML = commentBox.innerHTML+currentPic.commentsDesign[commentI];
				}
			}
			
			commentPager.innerHTML = JSNavbar( currentCommentPage, CommentPages, pageRange, "javascript:Gallery.setCommentPage(%count%)");
			
			
			var x1 = 	dojo.fadeOut({node: "picImg", duration:1000, onEnd: function(){ document.getElementById("picImg").style.display = "none";}});
			var x2 = 	dojo.fadeIn({node: "picImg2", duration:1000});
			var x3 = 	dojo.fadeIn({node: "pictureTagContainer", duration:1000});
			dojo.fx.combine([x2,x1,x3]).play();
			
			if(showedPictures[preloadIndex] != null && showedPictures[preloadIndex] != undefined)
			{
				Gallery.preloadImage(preloadIndex);
			}
			if(showedPictures[preloadIndex-2] != null && showedPictures[preloadIndex-2] != undefined)
			{
				Gallery.preloadImage(preloadIndex-2);
			}
		},

		sendReport: function()
		{
			sendReport('galleryPicture',showedPictures[currentPicture].pictureID);
		},

		setCommentPage: function(page)
		{
			dojo.fadeOut({ node: "comments", duration: 500, onEnd: function()
			{
				currentCommentPage = page;
				dojo.xhrPost(
				{
					url: "/"+ galleryConf['language'] +"/ajax/getComments",
					handleAs:"json",
					content: 
					{ 
						type: "picture",
						pictureID: showedPictures[currentPicture].pictureID,
						limit: maxCommentsPerPage,
						offset: (maxCommentsPerPage*page)
					},
					load: function (result)
					{ 
						commentBox.innerHTML = "";
						
						for(commentI = 0;commentI < result.commentsDesign.length; commentI++)
						{
							commentBox.innerHTML = commentBox.innerHTML+result.commentsDesign[commentI];
						}
						
						dojo.fadeIn({ node: "comments", duration: 500,onEnd: function()
						{
							commentPager.innerHTML = JSNavbar( currentCommentPage, CommentPages, pageRange, "javascript:Gallery.setCommentPage(%count%)");
						}}).play();
					}
				});
			}}).play();
			
		},
		
		GoPrev: function()
		{
			if(Gallery.isLocked())
			{
				return;
			}
			var gotoPic  = currentPicture - 1;
			if(gotoPic < 0)
			{
				gotoPic = showedPictures.length-1;
			}
			Gallery.setPicture(gotoPic);
		},
		
		GoNext: function()
		{
			if(Gallery.isLocked())
			{
				return;
			}
			var gotoPic  = (parseInt(currentPicture)+1);
			if(gotoPic > showedPictures.length-1)
			{
				gotoPic = 0;
			}
			Gallery.setPicture(gotoPic);
		},

		GoPrevPage: function()
		{
			if(Gallery.isLocked())
			{
				return;
			}
			var gotoPage  = currentPage - 1;
			if(gotoPage < 0)
			{
				gotoPage = pages-1;
			}
			Gallery.setPage(gotoPage);
		},

		GoNextPage: function()
		{
			if(Gallery.isLocked())
			{
				return;
			}
			var gotoPage  = currentPage + 1;
			if(gotoPage > pages-1)
			{
				gotoPage = 0;
			}
			Gallery.setPage(gotoPage);
		},

		GoPic: function( picId )
		{
			if(Gallery.isLocked())
			{
				return;
			}
		},

		preloadImage: function(picIndex,loading)
		{
			if(Gallery.isLocked() || alreadyPreloaded[reverseIndexing[picFilter][picIndex]])
			{
				return;
			}
			
			dojo.xhrPost( {
				url: "/"+ galleryConf['language'] +"/ajax/gallery/preloadImage",
				handleAs:"json",
				content: 
				{ 
					pictureID: showedPictures[picIndex].pictureID,
					commentLimit: maxCommentsPerPage
				},
				load: function (result)
				{
					showedPictures[picIndex] = result.data;
					pictures[reverseIndexing[picFilter][picIndex]] = result.data;
					alreadyPreloaded[reverseIndexing[picFilter][picIndex]] = true; 
					if(loading == "loadPicture")
					{
						ajaxLoaderImage.style.display = "none";
						Gallery.setPicture(picIndex,true);
					}
				}
			});
			
			var preloadImg = new Image();
			preloadImg.src = showedPictures[picIndex].picSrc+".3.jpg";
			
			
			
			
		},

		timeoutMouseOver: function(id)
		{
			if(alreadyPreloaded[reverseIndexing[picFilter][id]] == undefined)
			{
				toPreload[reverseIndexing[picFilter][id]] = window.setTimeout("Gallery.preloadImage("+id+")", 1000);
			}
		},
		
		stopMouseOver: function(id)
		{
			window.clearTimeout( toPreload[reverseIndexing[picFilter][id]] );
		},
		
		setTag: function(type)
		{
			dojo.style("picImg2","opacity","0.5");
			pointType = type;
			Gallery.lockGallery(true);
			choosePoint = true;
			picture2.style.cursor =  "url('"+galleryConf['templatePath']+"/images/gallery/"+imageTaggerNorm+"'),url('"+galleryConf['templatePath']+"/images/gallery/marker.cur'), crosshair";
		},
		
		stopTagging: function()
		{
			choosePoint = false;
			Gallery.lockGallery(false);
			picture2.style.cursor =  "default";
			dojo.style("picImg2","opacity","1");
		},

		
		deleteTag: function(taggedID,tagCount)
		{
			glooobleConfirm(rb_locale["JSGALLERY_TAG_DELETE_QUESTION"],rb_locale["MAIN_NO"],"",rb_locale["MAIN_YES"],"Gallery.deleteTagNow('"+taggedID+"','"+tagCount+"');");
		},
		
		deleteTagNow: function(taggedID,tagCount)
		{
			dojo.xhrPost({
				url: "/"+ galleryConf['language'] +"/ajax/tagManager",
				handleAs: "json",
				content:
				{
					mediaType:	'picture',
					action:		'deleteTag',
					taggedID:	taggedID,
					pictureID:	showedPictures[currentPicture].pictureID
				},
				load: function(result)
				{
					if(result.error != "")
					{
						alert(result.error);
					}
					else
					{
						alert(result.message);
						pictureTagContainer.removeChild(document.getElementById("tagimg_"+tagCount));
						taggedPicNames.removeChild(document.getElementById("taglinkspan_"+tagCount));
					}
				}
			});
			
		},
		
		savePoints: function(x,y,tagID,friend)
		{
			if(friend)
			{
				if(!confirm(rb_locale['JSGALLERY_GALLERY_REALLY_FRIEND_ON_THIS_PICTURE']))
				{
					return;
				}
			}
			dojo.xhrPost( {
				url: "/"+ galleryConf['language'] +"/ajax/tagManager",
				handleAs:"json",
				content: 
				{
					mediaType:	'picture',
					action:		'setTag',
					coordX:		x,
					coordY:		y,
					taggedID:	tagID,
					pictureID:	showedPictures[currentPicture].pictureID,
					galleryID:	showedPictures[currentPicture].galleryID
				},
				load: function (result)
				{ 
					Gallery.stopTagging();
					Gallery.closeFriendSearch();
					alert(result.message);
					if(result.tagged == "1")
					{
						tagCount++;
						tagImgTip[tagCount] = result.tip;
						var xPos =  (x-15);
						var yPos =  (y-15);
						Gallery.createTaggedPoint(tagCount,xPos,yPos);
						alreadyPreloaded[reverseIndexing[picFilter][currentPicture]] = false;
						Gallery.setPicture(currentPicture);
					}
				},
				error: function (data) {
					Gallery.stopTagging();
					Gallery.closeFriendSearch();
				}
			});
		},
		setPoint: function(x,y)
		{
			if(pointType == "own")
			{
				if(!confirm(rb_locale['JSGALLERY_GALLERY_REALLY_ON_THIS_PICTURE']))
				{
					Gallery.stopTagging();
					return;
				}
				Gallery.savePoints(x,y,galleryConf['sessionID']);
			}
			else
			{
				Gallery.showFriendbox(x,y);
			}
			
			
	
		},
		
		showFriendbox: function(x,y)
		{
			var friendSuggest = document.getElementById("friendSuggest");
			var friendbox = document.getElementById("friendBox");
			var friendBoxEntries = document.getElementById("friendBoxEntries");
			choosePoint = false;
			
			picture2.style.cursor =  "default";
			dojo.style("picImg2","opacity","1");

			var picCoords = dojo.coords("picImg2",true);

			friendbox.style.position = "absolute";
			friendbox.style.left = (x+picCoords.x-15)+"px";
			friendbox.style.top = (y+picCoords.y-15)+"px";
			friendBox.style.display = "block";
			dojo.fadeIn({node: 'friendBox', duration:1000}).play();
			friendBoxEntries.innerHTML = "";
			dojo.connect(friendSuggest,"onkeyup",function()
					{			
						dojo.xhrPost(
						{
							url: "/"+ galleryConf['language'] +"/ajax/getFriends",
							handleAs:"json",
							content: 
							{ 
								returnAs: 'searchTaggedFriends',
								search: friendSuggest.value+'*'
							},
							load: function (result)
							{ 
								friendBoxEntries.innerHTML = "";
								friendbox.style.height = "100px";
								for(i = 0; i < result.friends.length; i++)
								{
									var friendEntry = document.createElement("div");								
									var friendLinkName = document.createElement("a");
									friendLinkName.setAttribute("href", "javascript:Gallery.savePoints('"+x+"','"+y+"','"+result.friendIds[i]+"',true);");
									friendLinkName.innerHTML = result.friends[i];
									friendEntry.appendChild(friendLinkName);
									var elPos = dojo.coords(friendbox); 
									friendbox.style.height = (parseInt(elPos.h)+62)+"px";
									friendBoxEntries.appendChild(friendEntry);
									

								}
							}							
						});
						return true;
						
					});
			
		},
		
		createTaggedPoint: function(tagC,x,y)
		{
			x = x+"px";
			y = y+"px";
			var newTag = "<img id=\"tagimg_"+tagC+"\" class=\""+imageTagger+"\" style=\"display:none;left:"+x+";top:"+y+";position:absolute;\" src='"+galleryConf['templatePath']+"/images/button/1x1spacer.gif' onmouseover=\"createMouseOver('"+tagC+"');\" onmouseout=\"createMouseOut(event,'"+tagC+"');\"/>";
			pictureTagContainer.innerHTML = pictureTagContainer.innerHTML+newTag;
		},
		
		closeFriendSearch: function()
		{
			dojo.fadeOut({node: 'friendBox', duration:1000,onEnd: function()
				{
					friendBox.style.display = "none";
					Gallery.stopTagging();
				}
			}).play();
		},
		switchToGallery: function()
		{
			if(Gallery.isLocked())
			{
				return;
			}
			location.href = "/<?=LANG?>/picture/viewGallery/"+showedPictures[currentPicture].galleryID;
		},
		
		lockGallery: function(val)
		{
			lockGallery = val;
		},
		
		isLocked: function()
		{
			return lockGallery;
		},
		
		setPreviewPic: function(msg)
		{
			
		},
		
		showTaggedPeople: function(evt)
		{
			for(tagI = 0; tagI < tagCount;tagI++)
			{
				var obj = document.getElementById("tagimg_"+tagI);
				if(obj != undefined && obj != null)
				{
					obj.style.display = "block";
				}
			}
		},
		
		hideTaggedPeople: function(evt)
		{
			if(evt != undefined)
			{
			    var elPos = dojo.coords(picture2);
				var coordsX = evt.clientX-elPos.x;
				var coordsY = evt.clientY-elPos.y;
		
				if(coordsX < 0 || coordsY < 0 || coordsX > elPos.w || coordsY > elPos.h)
				{
					for(tagI = 0; tagI < tagCount;tagI++)
					{
						var obj = document.getElementById("tagimg_"+tagI);
						if(obj != undefined && obj != null)
						{
							obj.style.display = "none";
						}
					}
				}
			}
			else
			{
				for(tagI = 0; tagI < tagCount;tagI++)
				{
					var objtagimg = document.getElementById("tagimg_"+tagI);
					if(objtagimg != undefined && objtagimg != null)
					{
						objtagimg.style.display = "none";
					}
				}
			}
		},
		
		hideTag: function(tagCount)
		{
			var obj = document.getElementById("tagimg_"+tagCount);
			if(obj != undefined && obj != null)
			{
				obj.style.display = "none";
			}
		},
		
		showTag: function(tagCount)
		{
			var obj = document.getElementById("tagimg_"+tagCount);
			if(obj != undefined && obj != null)
			{
				obj.style.display = "block";
			}
		},
		
		deletePictureNow: function()
		{
			dojo.xhrPost({
				url: "/"+ galleryConf['language'] +"/ajax/gallery/deletePicture",
				handleAs: "json",
				content:
				{
					pictureID: showedPictures[currentPicture].pictureID
				},
				load: function(result)
				{
					if(result.error != "")
					{
						alert(result.error);
					}
					else
					{
						alert(result.message);
						var tmpPage = currentPage;
						Gallery.init();
						Gallery.setPage(tmpPage);
					}
				}
			});
		},
		
		deletePicture: function()
		{
			glooobleConfirm(rb_locale["JSGALLERY_REALLY_DELETE"],rb_locale["MAIN_NO"],"",rb_locale["MAIN_YES"],"Gallery.deletePictureNow();");
			
		},
		
		deleteGallery: function()
		{
			var galID =0;
			if(showedPictures[currentPicture] == undefined && InfoGalleryID != undefined)
			{
				galID = InfoGalleryID;
			}
			else if(showedPictures[currentPicture].galleryID != undefined)
			{
				galID = showedPictures[currentPicture].galleryID;
			}

			dojo.xhrPost({
				url: "/"+ galleryConf['language'] +"/ajax/gallery/deleteGallery",
				handleAs: "json",
				content:
				{
					galleryID: galID
				},
				load: function(result)
				{
					if(result.error != "")
					{
						alert(result.error);
					}
					else
					{
						alert(result.message);
					}
				}
			});
		},
		
		setAsPreviewPic: function()
		{
			dojo.xhrPost({
				url: "/"+ galleryConf['language'] +"/ajax/gallery/setPreviewPicture",
				handleAs: "json",
				content:
				{
					pictureID: showedPictures[currentPicture].pictureID,
					
					galleryID: showedPictures[currentPicture].galleryID,
					oldPreviewPictureID: pictures[previewPicIndex].pictureID
				},
				load: function(result)
				{
					if(result.error != "")
					{
						alert(result.error);
					}
					else
					{
						alert(result.message);
					}
				}
			});
		},
		
		setTopGallery: function()
		{
			dojo.xhrPost({
				url: "/"+ galleryConf['language'] +"/ajax/gallery/setTopGallery",
				handleAs: "json",
				content:
				{
					galleryID: showedPictures[currentPicture].galleryID
				},
				load: function(result)
				{
					alert(result.message);
					if(result.topGallery == "0")
					{
						document.getElementById('favGalleryLink').innerHTML = rb_locale['JSGALLERY_FAV_LINK_ADD'];
					}
					else if(result.topGallery == "1")
					{
						document.getElementById('favGalleryLink').innerHTML = rb_locale['JSGALLERY_FAV_LINK_DELETE'];
					}
				}
			});
		}
		
}

function createMouseOver(tagC)
{
	var obj = document.getElementById("taglinkname_"+tagC);
	if(obj != undefined && obj != null)
	{
		obj.style.color = "#FF0000";
	}
	
	Tip(tagImgTip[tagC],FADEOUT, 0, OFFSETY, -25);
}

function createMouseOut(evt,tagC)
{
	var obj = document.getElementById("taglinkname_"+tagC);
	if(obj != undefined && obj != null)
	{
		obj.style.color = "#153F65";
	}
	Gallery.hideTaggedPeople(evt);
	UnTip();
}

dojo.connect(picture2,"onmouseover",Gallery.showTaggedPeople);
dojo.connect(picture2,"onmouseout",Gallery.hideTaggedPeople);
dojo.connect(picture2,"onclick",getXY);
dojo.addOnLoad(function()
{
	dojo.connect(document,"keydown",galleryKeyDown);
});

function galleryKeyDown (evt)
{
	
	var curKey = evt.keyCode;

	switch (curKey)
	{
		case 27:
			if(Gallery.isLocked())
			{
				Gallery.stopTagging();
			}
            break;
		case 37:
			Gallery.GoPrev();
			
            break;
		case 39:
			Gallery.GoNext();
            break;
	}
}



function getXY(evt)
{
	if(!choosePoint)
	{
		return;
	}

	xMousePos = evt.clientX;
    yMousePos = evt.clientY;
    
    absoluteMouseX = xMousePos;
    absoluteMouseY = yMousePos;
    
    var elPos = dojo.coords(picture2);
	var coordsX = xMousePos-elPos.x+15;
	var coordsY = yMousePos-elPos.y+15;
	if(coordsY > elPos.h)
	{
		coordsY = elPos.h;
	}
	if(coordsX > elPos.w)
	{
		coordsX = elPos.w;
	}
	Gallery.setPoint(coordsX,coordsY);
}
