/**
 * @classDescription Video Gallery class, manages user video gallery ".galleryvideo" 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 VideoGallery = 
{
	/**
	 * @videoNo holds the number of videos, used internally
	 */
	videoNo: 1,
	/**
	 * @selectedVideo holds the current video number
	 */
	selectedVideo: 1,
	
	playerCode: '',
	videoIds: new Array(),
	
	playerCodeArray: new Array('', 
				'<object width="450" height="344">' + 
				'<param name="movie" value="http://www.youtube.com/v/{videoCode}"></param>' + 
				'<param name="allowFullScreen" value="true"></param>' +
				'<embed src="http://www.youtube.com/v/{videoCode}&autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" width="450" height="344"></embed>' +
				'</object>'
				),
				
					
	type: 'user',
	/**
	 * Initializes gallery, this counts the number of images and calls pagination
	 */
	init: function()
	{
		if ( (this.videoNo = $('.galleryvideo').length) > 0 ) 
		{
			var i = 0;
			$('.galleryvideo > a > img').each(function(i){
				VideoGallery.videoIds[i] = this.getAttribute('_id');
				i++;
			});

			this.pagination();
			switch (VideoGallery.type)
			{
			case 'user':
				Comments.template = videoCommentsTemplate;
				Comments.type = 'userVideoComments';
				break;
			case 'fanclub':
				Comments.template = imageCommentsTemplate;
				Comments.type = 'fanclubVideoComment';
				break;
			case 'category':
				Comments.template = newsCommentsTemplate;
				Comments.type = 'categoryVideoComment';
				break;
			case 'starVideoComments':
				Comments.template = newsCommentsTemplate;
				Comments.type = 'starVideoComments';
				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.videoNo <= 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="javascript:void(0)" onclick="VideoGallery.prev();return false;"><img src="/images/back.gif" border="0" alt=""></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.videoNo <= 10)
		{
			var from = 1;
			var to = Math.min( 10, this.videoNo );
		} else
		if ( this.selectedVideo + 5 > this.videoNo )
		{
			var from = this.videoNo - 9;
			var to = this.videoNo;
		} else
		if ( this.selectedVideo > 5 )
		{
			var from = this.selectedVideo - 4;
			var to = Math.min( this.selectedVideo + 5, this.videoNo );
		}

		//link to first page
		pagination += '<a href="javascript:void(0)" class="pages" onclick="VideoGallery.page(1);return false;">1</a>..';
		
		//generate links
		for ( var i = from; i <= to; i++ )
		{
			//page links
			if ( i == this.selectedVideo )
			{
				pagination += '<a href="javascript:void(0)" id="page'+ i +'" class="pages page_selected" onclick="VideoGallery.page('+ i +');return false;">'+ i +'</a>';	
			} else
			{
				pagination += '<a href="javascript:void(0)" id="page'+ i +'" class="pages" onclick="VideoGallery.page('+ i +');return false;">'+ i +'</a>';
			}
		}
		
		//link to last page
		pagination += '..<a href="javascript:void(0)" class="pages" onclick="VideoGallery.page(' + this.videoNo + ');return false;">' + this.videoNo + '</a>';

		//next link
		pagination += '<a href="javascript:void(0)" onclick="VideoGallery.next();return false;"><img src="/images/next.gif" alt="" border="0"></a>';
		
		//insert pagination html
		$("#videolinks").html( pagination );
	},
	/**
	 * Go to the specified image (page) number 
	 * @param {Object} pageNr
	 */
	page : function( pageNr )
	{
		this.selectedVideo = pageNr;
		
		var $image = $('#img'+ VideoGallery.selectedVideo );
		
		//preload next image
		if ( pageNr < this.videoNo )
		{
			var image = new Image;
			image.src = $('#img'+ ( VideoGallery.selectedVideo + 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'));
			$('#galleryTags').html('Taguri: ' + $image.attr('tags'));
			
			VideoGallery.setPlayerCode($image.attr('siteId'), $image.attr('videoCode'));
			
			$('#galleryimage').html(VideoGallery.playerCode).fadeIn( 500,
			function()
			{	
				//add border around selected thumbnail
				$('.galleryvideo').removeClass('galleryvideo_selected');
				$('#g'+ VideoGallery.selectedVideo).addClass('galleryvideo_selected');


				//scroll image gallery
				$('#galleryscroll').scrollTo( '#g' + VideoGallery.selectedVideo, 500, { easing:'elasout', onAfter:function() 
				{
					//regenerate pagination 
					VideoGallery.pagination();
					
					//load comments
				//	Comments.get( VideoGallery.selectedVideo );
	
					//change banners
					rotateBanners();
					
					//load comments for image
					Comments.id = $image.attr('_id');
					Comments.get(1);
				}
				});
			}
			);
		});
	},
	/**
	 * Go to the specified video 
	 * @param {Object} videoId
	 */
	video: function(videoId)
	{
		for (var i = 0, j = VideoGallery.videoIds.length; i < j; i++) 
		{
			if (VideoGallery.videoIds[i] == videoId)
			{
				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.selectedVideo > this.videoNo )
		{
			this.selectedVideo = 1;
		}
		this.page( this.selectedVideo );
	},
	/**
	 * Select previous image, this is circular if the are no images left the last image is selected
	 */
	prev : function()
	{
		if ( --this.selectedVideo < 1 )
		{
			this.selectedVideo = this.videoNo;
		}
		this.page( this.selectedVideo );
	},
	
	setPlayerCode : function(siteId, videoCode)
	{
	    this.playerCode = this.playerCodeArray[siteId].replace('{videoCode}', videoCode);
	    this.playerCode = this.playerCode.replace('{videoCode}', videoCode);
	}
}
