jQuery(document).ready(function() 
{	
	if ($(".album").length)
	{
		var url = document.location.href;
		url = url.split('?');

		$('.GalleryAlbumImageLink').hover(
			function()
			{
					$(this).find('div.editor_top:hidden').show();
					$(this).find('div.editor_bottom:hidden').show();
			},
			function()
			{
				$(this).find('div.editor_top:visible').hide();
				$(this).find('div.editor_bottom:visible').hide();
			}
		);
		
		if ($(".album").attr('name'))
		{
			$('.btnImageSetThumb').click(
				function()
				{
					var id = $(this).parent().attr('id');
					var album = $(this).parent().attr('album');
					
					ImageSetThumb(id, album);				
				});
			$('.btnImageEdit').click(
				function()
				{
					var id = $(this).parent().attr('id');
					var album = $(this).parent().attr('album');
									
					$.ajax({
						url: url[0] + '?service=service_gallery&saction=edit_image&id=' + id, 
						success: function(data) 
						{		
							dialogSetContentAndShow(data, "GalleryImageEditModal", $("#gallery_edit_image_caption").attr('name'));
							
							$(".EditImageConfirmButton").click(function()
							{	
								Name = $("#edit_image_name").attr("value");
								Tags = $("#edit_image_tags").attr("value");
								Desc = $("#edit_image_description").attr("value");
								
								$.getJSON(url[0] + '?service=service_gallery&saction=edit_image&id=' + id + '&name='+Name+'&tags='+Tags+'&desc='+Desc,	
						      //запрос завершен, получаем данные
						        function (data) 
								{				
									alert(data.error);
									dialogClose('GalleryImageEditModal');
									location.reload();
								});
								

							});
						}});
					});
			$('.btnImageDelete').click(
				function()
				{
		    		if (confirmDelete())
		    		{
		    			var id = $(this).parent().attr('id');
		    			
						$.getJSON(url[0] + '?service=service_gallery&saction=delete_image&id=' + id,	
					      //запрос завершен, получаем данные
					        function (data) 
							{				
								alert(data.error);
								location.reload();
							});
		    			
		    		}
				});
			
			$.preloadImages($(".album").attr('spinner'));
			
			$(".GalleryAlbumImage").click(function()
			{
//				ShowBigPicture($(this).attr('f_id'));
				ShowBigPicture($(this).attr('f_id'));
			});
			function ImageSetThumb(id, album_id)
			{
				$.getJSON(url[0] + '?service=service_gallery&saction=set_thumb&id=' + id + '&album_id='+album_id,	
			  //запрос завершен, получаем данные
			    function (data) 
				{				
					alert(data.error);
				})};
			
			function EditImageConfirmButton() 
			{
				alert('!');
			}
			
			function ShowBigPicture(id)
			{
				var Txt;
	
				$.getJSON(url[0] + '?service=service_gallery&saction=get_info&image_id='+id,
			    //запрос завершен, получаем данные
			    function (data) 
			    {		
					Txt = '<div><div class="GalleryImageView"><div class="previous_image">';
					if (data.prev > 0)
						Txt = Txt + '<a class="prev_page" href="#" id="' + data.prev + '"></a>';
						
					Txt = Txt + '</div><a href="' + data.src +'" target="blank"><img class="GalleryAlbumImageBig" alt="' + data.name + '" src="' + data.src_2 + '"/></a>';

					Txt = Txt + '<div class="next_image">';
					if (data.next > 0)
						Txt = Txt + '<a class="next_page" href="#" id="' + data.next + '"></a>';
						
					Txt = Txt + '</div></div></div><div style="clear: both;"></div>';
					Txt = Txt + '<div class="GalleryImageShow"><div class="key">' + $('.album').attr('name') + ':&nbsp; </div> <div class="value"> ' + data.name + '</div></div>';
					Txt = Txt + '<div class="GalleryImageShow"><div class="key">' + $('.album').attr('tags') + ':&nbsp; </div> <div class="value"> ' + data.tags + '</div></div>';
					Txt = Txt + '<div class="GalleryImageShow"><div class="key">' + $('.album').attr('desc') + ':&nbsp; </div> <div class="value"> ' + data.desc + '</div></div>';
					Txt = Txt + '<div class="GalleryImageViewTop">&nbsp;</div>';

					dialogSetContentAndShow(Txt, "ShowBigPictureModal", $(".album").attr('caption'));
					
					$(".next_page").click(function()
					{
						ShowBigPicture($(this).attr('id'));
						return false;
					});
					
					$(".prev_page").click(function()
					{
						ShowBigPicture($(this).attr('id'));
						return false;
					});				
			    });
			
			}
		}
	}
});