/**
 * @classDescription Gallery class, manages user gallery Thumbnails must have class ".galleryimg" and id img# where # is the number of image, image attribute must contain big image src, title attribute set to image title and date attribute set to date and time of picture 
 * @author Givan
 * @author Iurie Safonov <iurie dot safonov at google dot com>
 * @version $id
 * @constructor 
 */
var Gallery = 
{
	/**
	 * @nrImages holds the number of images, used internally
	 */
	nrImages : 1,
	/**
	 * @selectedImage holds the current image number
	 */
	selectedImage: 1,
	type: 'user',
	galleryIds: new Array(),
	/**
	 * Initializes gallery, this counts the number of images and calls pagination
	 */
	init: function()
	{
		if ( ( this.nrImages = $('.galleryimg').length ) > 0 ) 
		{
			var i = 0;
			$('.galleryimg > a > img').each(function(i){
				Gallery.galleryIds[i] = this.getAttribute('_id');
				i++;
			});

			this.pagination();
			switch (Gallery.type)
			{
			case 'user':
				Comments.template = imageCommentsTemplate;
				break;
			case 'fanclub':
				Comments.template = imageCommentsFanclubTemplate;
				Comments.type = 'fanclubImageComment';
				break;
			case 'category':
				Comments.template = newsCommentsTemplate;
				Comments.type = 'categoryImageComment';
				break;
			default:
				Comments.template = newsCommentsTemplate;
				Comments.type = 'starComments';
			}
			Comments.init();
		}
	},
	/**
	 * Generates pagination for gallery
	 */
	pagination : function()
	{
		//if only one page then hide pagination 
		if ( this.nrImages <= 1)
		{
			$("#fanclub_pagination").html("");
			return true;
		}
		
		//doing it the old way (innerHTML) because there are to many nodes to create and it's slower
		
		//prev link
		var pagination = '<a href="#" onclick="Gallery.prev();return false;">anterioara</a>';
		
		//calculate from what number to start and to what number to stop to always have 10 links on the page (if there are at least 10 images) 
		if (this.nrImages <= 10)
		{
			var from = 1;
			var to = Math.min( 10, this.nrImages );
		} else
		if ( this.selectedImage + 5 > this.nrImages )
		{
			var from = this.nrImages - 9;
			var to = this.nrImages;
		} else
		if ( this.selectedImage > 5 )
		{
			var from = this.selectedImage - 4;
			var to = Math.min( this.selectedImage + 5, this.nrImages );
		}

		//link to first page
		pagination += '<a href="#" class="pages" onclick="Gallery.page(1);return false;">1</a>..';
		
		//generate links
		for ( var i = from; i <= to; i++ )
		{
			//page links
			if ( i == this.selectedImage )
			{
				pagination += '<a href="#" id="page'+ i +'" class="sel" onclick="Gallery.page('+ i +');return false;">'+ i +'</a>';	
			} else
			{
				pagination += '<a href="#" id="page'+ i +'" onclick="Gallery.page('+ i +');return false;">'+ i +'</a>';
			}
		}

		pagination += '..<a href="#" onclick="Gallery.page(' + this.nrImages + ');return false;">' + this.nrImages + '</a>';

		//next link
		pagination += '<a href="#" onclick="Gallery.next();return false;">urmatoarea</a>';
		
		//insert pagination html
		$(".imagelinks").html( pagination );
	},
	/**
	 * Go to the specified image (page) number 
	 * @param {Object} pageNr
	 */
	page : function( pageNr )
	{
		this.selectedImage = pageNr;
		
		var $image = $('#img'+ Gallery.selectedImage );
		
		//preload next image
		if ( pageNr < this.nrImages )
		{
			var image = new Image;
			image.src = $('#img'+ ( Gallery.selectedImage + 1 )  ).attr('title');
		}

		//fadeout image
		$('#galleryimage').fadeOut( 500, function()
		{ 
			//change image and fade in
			$('#galleryTitle').html($image.attr('title'));
			$('#galleryDate').html('adaugat la ' + $image.attr('date'));
			var tags = $image.attr("tags").split(",");
			var tagsString = '';
			for (var i = 0;i < tags.length;i++)
			{
				tagsString += "<a href='/galerie/" + tags[i] + ".html'>" + tags[i] + "</a>";
				if (i < tags.length - 1)
				{
					tagsString += ", ";
				}
			}
			$('.tags').html('Etichete: ' + tagsString);

			$('#galleryimage').attr( 'src', $image.attr('image') ).fadeIn( 500,
			function()
			{	
				//add border around selected thumbnail
				$('.galleryimg').removeClass('galleryimg_selected');
				$('#g'+ Gallery.selectedImage).addClass('galleryimg_selected');


				//console.log($('#g' + Gallery.selectedImage));
				//scroll image gallery
				$('#galleryscroll').scrollTo( $('#g' + Gallery.selectedImage), 500, { easing:'elasout', onAfter:function() 
				{
					//regenerate pagination 
					Gallery.pagination();
					
					//change banners
					rotateBanners();
					
					//load comments for image
					Comments.id = $image.attr('_id');
					Comments.get(1);
				}
				});
			}
			);
		});
	},
		/**
	 * Go to the specified video 
	 * @param {Object} videoId
	 */
	item : function(itemId)
	{
		for (var i = 0, j = Gallery.galleryIds.length; i < j; i++) 
		{
			if (Gallery.galleryIds[i] == itemId)
			{
				this.page(i+1);
				break;
			}
		}
	},
	/**
	 * Select next image, this is circular if the are no images left the first image is selected
	 */
	next : function()
	{
		if ( ++this.selectedImage > this.nrImages )
		{
			this.selectedImage = 1;
		}
		this.page( this.selectedImage );
	},
	/**
	 * Select previous image, this is circular if the are no images left the last image is selected
	 */
	prev : function()
	{
		if ( --this.selectedImage < 1 )
		{
			this.selectedImage = this.nrImages;
		}
		this.page( this.selectedImage );
	}
}
