


var GalleriesManager = {
		
	IMAGE_PREVIEW_SELECTOR : ".galleries_layout .navigator .image_preview",
	
	TITLE_SELECTOR : ".gallery_info .title",
	
	RELEASE_DAY_SELECTOR : ".gallery_info .release_day",
	
	RELEASE_HOUR_SELECTOR : ".gallery_info .release_hour",
	
	PHOTO_PANEL_SELECTOR : "#photo_panel",
	
	imagePreview : null,
	
	title : null,
		
	galleries : null,
	
	otherGalleries : null,
	
	day : null,
	
	hour : null,
	
	currentGallery : null,
	
	photoPanel : null,
	
	photoIndex : 0,
		
		
	nextPicturePreview : function()
	{
		this.nextPicture();
		GalleriesManager.showPicturePreview();
	},
	
	prevPicturePreview : function()
	{
		this.prevPicture();
		GalleriesManager.showPicturePreview();
	},
	
	showPicturePreview : function()
	{
		this.imagePreview.empty();
		var imgPath = this.currentGallery.photos[this.currentGallery.currentPictureIndex].src;
		this.imagePreview.append(this.currentGallery.photos[this.currentGallery.currentPictureIndex].imgPreview);
		Site.incrementViews(imgPath,function( data )
		{
			var viewed = $(".foto_navigator .viewed");
			viewed.empty();
			viewed.append(data.count);
		});
		
		var pictureGrade = $(".foto_navigator .grade_holder");
		pictureGrade.attr("resource", imgPath );
		Site.getCurrentGrade( imgPath, function( data ){
			Site.setGradeAtSite( pictureGrade, data.grade );
		});
	},
	
	
	
	nextPicture : function()
	{
		this.currentGallery.currentPictureIndex = this.currentGallery.currentPictureIndex + 1;
		if( this.currentGallery.currentPictureIndex + 1 > this.currentGallery.photos.length )
		{
			this.currentGallery.currentPictureIndex = 0;
		}	
	},
	
	prevPicture : function()
	{
		this.currentGallery.currentPictureIndex = this.currentGallery.currentPictureIndex - 1;
		if( this.currentGallery.currentPictureIndex < 0 )
		{
			this.currentGallery.currentPictureIndex = this.currentGallery.photos.length-1;
		}
	},
	
	setOverlayVisible : function( visible )
	{
		var overlay = $("#page_overlay");
		if( visible)
		{
			if( overlay.length == 0 )
			{
				$("body").append("<div id='page_overlay' ></div>");
			}
		}
		else
		{
			if( overlay.length == 1)
			{
				overlay.remove();
				
			}
		}
	},
	
	
	previewGallery : function( galleryIndex )
	{
		
		this.__previewGallery(this.galleries[galleryIndex]);
		
	},
	
	__previewGallery : function( gallery )
	{
		this.currentGallery = gallery;
		if( !this.currentGallery.currentPictureIndex )
		{
			this.currentGallery.currentPictureIndex = 0;
		}
		this.imagePreview.empty();
		this.title.empty();
		this.day.empty();
		this.hour.empty();
		this.title.append(this.currentGallery.title);
		this.day.append(this.currentGallery.day);
		this.hour.append(this.currentGallery.hour);
		var galleryViewed = new Array();
		
		var userGrade = $(".gallery_info .grade_holder");
		userGrade.attr("resource",this.currentGallery.folder);
		
	
		
		galleryViewed[0] = $(".gallery_info .viewed");
		
		Site.incrementViews(this.currentGallery.folder,function( data )
		{
			galleryViewed[1] = $(".galleries_layout .viewed[resource="+data.resource+"]");
			for(i=0; i < galleryViewed.length; i++)
			{
				galleryViewed[i].empty();
				galleryViewed[i].append(data.count);
			}
		});
		this.showPicturePreview();
		
	},
	
	init : function( galleries, index )
	{
		if( !index )
		{
			index = 0;
		}
		this.galleries = galleries;
		this.imagePreview = $(this.IMAGE_PREVIEW_SELECTOR);
		this.title = $(this.TITLE_SELECTOR);
		this.day = $(this.RELEASE_DAY_SELECTOR);
		this.hour = $(this.RELEASE_HOUR_SELECTOR);
		this.photoPanel = $(this.PHOTO_PANEL_SELECTOR);
		this.previewGallery(index);
	},
	
	setActiveGallery : function( galleryIndex )
	{
		this.currentGallery = this.galleries[galleryIndex];
		if( !this.currentGallery.currentPictureIndex )
		{
			this.currentGallery.currentPictureIndex = 0;
		}
	},
	
	simpleInit : function( galleries )
	{
		this.galleries = galleries;
		this.setActiveGallery(0);
		
	},
	
	setOtherGalleries : function( otherGalleries )
	{
		this.otherGalleries = otherGalleries;
	},
	
	previewOtherGallerie : function( index )
	{
		this.__previewGallery(this.otherGalleries[index]);
	},
	
	seeFullSize : function()
	{
		this.setOverlayVisible(true);
		this.photoPanel.removeClass('invisible');
		this.setFullSizePicture();
	},
	
	text: {
		// For translating
		image:		'Image',
		of:			'of',
		close:		'Close X',
		closeInfo:	'You can also click anywhere outside the image to close.',
		download:	'Download.',
		help: {
			close:		'Click to close',
			interact:	'Hover to interact'
		},
		about: {
			text: 	'jQuery Lightbox Plugin (balupton edition)',
			title:	'Licenced under the GNU Affero General Public License.',
			link:	'http://jquery.com/plugins/project/jquerylightbox_bal'
		}
	},
	
	__lightbox : function( singlePhoto )
	{

		var photos = new Array();
		var photo = new Object();
		
		if( singlePhoto )
		{
			photo =  singlePhoto;
			photos[0] = singlePhoto;
		}
		else
		{
			photos = this.currentGallery.photos;
			if( !this.currentGallery.photos[ this.photoIndex ] )
			{
				this.photoIndex = 0;
			}
			photo = this.currentGallery.photos[ this.photoIndex ];
		}
		
		
		$.Lightbox = $.Lightbox || new $.LightboxClass();

		$.Lightbox.download_link = false;
		$.Lightbox.init( photo, photos);
	
	
		var text = {
			// For translating
			image:		'Obraz',
			of:			'z',
			close:		'Zamknij',
			closeInfo:	'',
			download:	'Pobierz',
			help: {
				close:		'Kliknij aby zamknąć',
				interact:	''
			},
			about: {
				text: 	'',
				title:	'Licenced under the GNU Affero General Public License.',
				link:	'http://jquery.com/plugins/project/jquerylightbox_bal'
			}
		};
		/*
		if( !$.browser.msie &&  $.browser.version.indexOf('6') != 0 )
		{
			$.Lightbox.construct({text : text});
		}*/
		$.Lightbox.start();
	

		
		/* var photoHolder = $(".photo_holder",this.photoPanel);
		photoHolder.empty();
		photoHolder.append("<img src='"+this.currentGallery.photos[this.currentGallery.currentPictureIndex].src+"' />");
		
		
		
		var userGrade = $(".photo_panel .grade_holder");
		userGrade.attr("resource",this.currentGallery.photos[this.currentGallery.currentPictureIndex].src);
		*/
	},
	
	showSinglePhoto : function( photo )
	{
		this.__lightbox( photo );
	},
	
	lightbox : function( photoIndex )
	{
		if( photoIndex )
		{
			this.photoIndex = photoIndex;
		}
		this.__lightbox();
	}
	
	
	
}





