PhotosProfile = {
	lists : null,
	limit : 20,
	GalleryType: null,
	GalleryOwner: null,
	GalleryContentID: null,
	previewPhotoNo: null,
	previewPhotoFile: null,
	autoPreviewPhotoNoInterval: 0
};

PhotosProfile.EventHandlers = {
	onPhotoClick: function(currentNo, filename, callback) {
		$('.photo-item').removeClass('selected'); // Un-select the prev thumb
		$(this).parent().addClass('selected'); // Select the new thumb
		
		var url = Config.URL.PHOTO +'/'+ PhotosProfile.GalleryType +'/400/'+ filename;
		
		$("#photos-preview-content").html('<div style="background:url(' + url + ') no-repeat center center;" title="'+ Dictionary.PHOTOS.FullSizeOnClick +'" />');
		if(PhotosProfile.previewPhotoFile == null) {
			$("#photos-preview").slideDown("slow", function() {
			});
		} else {
		}
		PhotosProfile.previewPhotoNo = currentNo;
		PhotosProfile.previewPhotoFile = filename;
		
		if(currentNo==1) { //disable prev
			if(PhotosProfile.lists.currentPage==1) {
				$('#photos-preview-prev-link').fadeOut();
			}
		} else {
			$('#photos-preview-prev-link').fadeIn();
		}
		if(currentNo==PhotosProfile.lists.data.list.length) {//disable next
			if((PhotosProfile.lists.currentPage-1)*PhotosProfile.limit + currentNo == PhotosProfile.lists.data.totalCount) {
				$('#photos-preview-next-link').fadeOut();
			}
		} else {
			$('#photos-preview-next-link').fadeIn();
		}
		
		if($.isFunction(callback)) {
			callback();
		}
	},
	onSlideUpClick: function() {
		$("#photos-preview").slideUp("slow", function() {
			$('.photo-item').removeClass('selected'); // Un-select the prev thumb
			PhotosProfile.previewPhotoNo = null;
			PhotosProfile.previewPhotoFile = null;
			$("#photos-preview-content").html('');
		});
	},
	changePreview : function(to) {
		var currentNo = PhotosProfile.previewPhotoNo;
		var nextNo = parseInt(currentNo) + parseInt(to);  
		
		if(nextNo>PhotosProfile.limit) {
			if(PhotosProfile.lists.currentPage*PhotosProfile.limit < PhotosProfile.lists.data.totalCount) {
				PhotosProfile.previewPhotoNo = 2;
				PhotosProfile.autoPreviewPhotoNoInterval = -1;
				$("#photos-list-container-pagination .next").click();
			}
		} else if(nextNo<1) {
			if(PhotosProfile.lists.currentPage>1) {
				PhotosProfile.previewPhotoNo = PhotosProfile.limit-1;
				PhotosProfile.autoPreviewPhotoNoInterval = 1;
				$("#photos-list-container-pagination .prev").click();
			}
		} else {
			if (to > 0) { 
				mLeft = -800;
			} else { 
				mLeft = 800;
			}
			var nextPhoto = $(".photo-item-inner[dataNo='"+ nextNo +"']")
			if(nextPhoto.length>0) {
				$('#photos-preview-content').animate({ marginLeft:mLeft+"px" }, 1000, 'swing', function() {
					PhotosProfile.EventHandlers.onPhotoClick(nextNo, nextPhoto.attr('photoFilename'), function() {
						$('#photos-preview-content').attr("style","margin-left:"+(-1*mLeft)+"px");
						$('#photos-preview-content').animate({ marginLeft:0 }, 1000, 'swing');
					});
				});
			}
		}
	},
	onPreviewedPhotoClick : function() {
		if(PhotosProfile.previewPhotoFile == null) return;
		
		var url = Config.URL.PHOTO +'/'+ PhotosProfile.GalleryType +'/full/'+ PhotosProfile.previewPhotoFile;
		
		$.showWindowSK('window-photo', {
			ajax	:	'/pages/snippets/photo.php?photoUrl=' + url
		});
		
		return false;
	},
	onPhotoRemove : function() {
		var element = this;
		var parameters = {
			id			: 1,
			api			: 'Photo',
			command		: 'remove',
			params		: {
				type			: PhotosProfile.GalleryType,
				recordId		: PhotosProfile.GalleryContentID, 
				photoId			: $(this).attr('photo-id')
			}
		};
		$.postSK({
			data : {data:JSONstring.make(parameters)},
			onsuccess : function (data) {
				if (data.error == null && data['1'].error == null) {
					PhotosProfile.lists.reload();
				}
			}
		});
		
		return false;
	},
	onSetProfilePhoto : function() {
		
		$(this).parent().parent().attr('style','border-color:red');
		
		var parameters = {
			id			: 1,
			api			: 'Photo',
			command		: 'setProfile',
			params		: {
				type			: PhotosProfile.GalleryType,
				recordId		: PhotosProfile.GalleryContentID, 
				photoId			: $(this).attr('photo-id')
			}
		};
		var thisItem = $(this);
		$.postSK({
			data : {data:JSONstring.make(parameters)},
			onsuccess : function (data) {
				if (data.error == null && data['1'].error == null) {
					$('.profile-image img').parent().fadeOut(function() {
						$('.profile-image img').parent().html('<img width="132" height="132" src="'+ Config.URL.PHOTO +'/'+ PhotosProfile.GalleryType +'/132/'+data['1'].file+'"/>').fadeIn();
					});
					if(PhotosProfile.GalleryType=='user') {
						$('#cp-avatar').fadeOut(function() {
							$('#cp-avatar').html('<img width="40" height="40" src="'+ Config.URL.PHOTO +'/user/40/'+data['1'].file+'"/>').fadeIn();
						});
					}
				}
				thisItem.parent().parent().attr('style','border-color:default;');
			}
		});
		
		return false;
	},
	onUploadPhoto : function()
	{
		Marvin.Utilities.PhotoUpload( {'mode': PhotosProfile.GalleryType, 'setProfile': true, 'recordId': PhotosProfile.GalleryContentID } );
		return false;		
	}
}

// On DOM ready - Start
$(function() {
	PhotosProfile.GalleryType = GalleryType;
	PhotosProfile.GalleryOwner = GalleryOwner;
	PhotosProfile.GalleryContentID = GalleryContentID;
	
	PhotosProfile.lists = new marvinList({ 
		div_id:"photos-list-container", 
		api:"Photo.list",
		apiParams:{
			type: PhotosProfile.GalleryType,
			recordId: PhotosProfile.GalleryContentID, 
			limit: PhotosProfile.limit,
			order: 'new'
		},
		template: Templates.NewPhoto.ListItem,
		templateParameters: {'isOwner': PhotosProfile.GalleryOwner, 'type': PhotosProfile.GalleryType},
		show:true, 
		pagination:true,
		onComplete: function() {
			if(PhotosProfile.autoPreviewPhotoNoInterval!=0) {
				PhotosProfile.EventHandlers.changePreview(PhotosProfile.autoPreviewPhotoNoInterval);
				PhotosProfile.autoPreviewPhotoNoInterval = 0;
			}
		}
	});

	$('.photo-item-inner').livequery(function () {
		$(this).bind('click', function () { 
			PhotosProfile.EventHandlers.onPhotoClick($(this).attr('dataNo'),$(this).attr('photoFilename'));
		});
	});
	
	$('#photos-preview-slide-up').bind('click', function() { 
		PhotosProfile.EventHandlers.onSlideUpClick(); 
	});
	
	$('#photos-preview-prev-link').bind('click', function() { 
		PhotosProfile.EventHandlers.changePreview(-1); 
	});

	$('#photos-preview-next-link').bind('click', function() { 
		PhotosProfile.EventHandlers.changePreview(+1); 
	});
	
	$('#photos-preview-content').bind('click', PhotosProfile.EventHandlers.onPreviewedPhotoClick);
	
	$('.photo-item .icon-remove').livequery(function(){
		$(this).bind('click', PhotosProfile.EventHandlers.onPhotoRemove);
	});
	
	$('.photo-item .icon-tick').livequery(function(){
		$(this).bind('click', PhotosProfile.EventHandlers.onSetProfilePhoto);
	});
	
	$('#add-photo').bind( 'click', PhotosProfile.EventHandlers.onUploadPhoto );
	
	
});
// On DOM ready - End