// JavaScript Document

/**
 * Functions used by photo gallery plugin
**/
var PhotoGallery = {
	delete_url: null,
	delete_completion: null,
	AddIndexHandlers: function() {
		if ($$('.delete_album')) {
			$$('.delete_album').each(function(el) {
				el.observe("click", function(event) {
		    		Event.stop(event);
					if(window.confirm("Are you sure you want to delete this album? All photos in the album will be removed. This action cannot be undone.")) {
						var url = el.href;
						PhotoGallery.Delete(url,false);
					}
				});
			});
		}
		if ($$('.delete_photo')) {
			$$('.delete_photo').each(function(el) {
				el.observe("click", function(event) {
		    		Event.stop(event);
					if(window.confirm("Are you sure you want to delete this photo? This action cannot be undone.")) {
						var url = el.href;
						PhotoGallery.Delete(url,false);
					}
				});
			});
		}
		if ($$('.edit_album')) {
			$$('.edit_album').each(function(el) {
				el.observe("click",function(event) {
					Event.stop(event);
					var my_id = el.id;
					if (my_id.match(/edit\_/)) {
						var edit_id = my_id.substring(5);		// value after 'edit_'
					}
					else {
						var edit_id = '';
					}
					PhotoGallery.EditAlbumInLightview(edit_id);
				});
			});
		}
		if ($$('.edit_photo')) {
			$$('.edit_photo').each(function(el) {
				el.observe("click",function(event) {
					Event.stop(event);
					var my_id = el.id;
					if (my_id.match(/edit\_/)) {
						var edit_id = my_id.substring(5);		// value after 'edit_'
						var album_id = '';
					}
					else if (my_id.match(/add\_to\_/)) {
						var edit_id = '';
						var album_id = my_id.substring(7);		// value after 'add_to_'
					}
					PhotoGallery.EditPhotoInLightview(edit_id,album_id);
				});
			});
		}
		if ($$('.edit_album_photos')) {
			$$('.edit_album_photos').each(function(el) {
				el.observe("click",function(event) {
					Event.stop(event);
					var url = el.id.substring(1);		// value after the preceding slash
					$Nav.page_switch(url);
				});
			});
		}
		if ($('gallery_home')) {
			$('gallery_home').observe("click",function(event) {
				Event.stop(event);
				$Nav.page_switch('photos');
			});
		}
		if ($$('.batch_add')) {
			$$('.batch_add').each(function(el) {
				el.observe("click",function(event) {
					Event.stop(event);
					var album_id = el.id.substring(6);		// Value after "album_"
					PhotoGallery.EditInLightview(album_id+'/batch_add','Batch Photo Upload');
				});
			});
		}
		// Make albums sortable:
		if ($('album_sort_form')) {
			Biscuit.Crumbs.Sortable.create('albums','div','photos','album_list',{
				handles: $$('.album_title'),
				action: 'resort_albums',
				hoverclass: 'draggable',
				array_name: 'album_sort',
				constraint: false,
				overlap: 'horizontal'
			});
		}
		// Make photos sortable:
		if ($('photo_sort_form')) {
			Biscuit.Crumbs.Sortable.create('photos','div','photos','photo_list',{
				handles: $$('.photo_title'),
				action: 'resort_photos',
				hoverclass: 'draggable',
				array_name: 'photo_sort',
				constraint: false,
				overlap: 'horizontal',
				onChange: this.RenumberPhotos
			});
		}
	},
	AddEditHandlers: function() {
		// add form validation on submit
		if ($('photo_editor')) {
			$('photo_editor').observe("submit", function(event){
				Event.stop(event);
				new Biscuit.Ajax.FormValidator('photo_editor',{
					throbber_id: 'submit_throbber',
					use_ping_timer: true
				});
			});
		}
		if ($('album_editor')) {
			$('album_editor').observe("submit", function(event){
				Event.stop(event);
				new Biscuit.Ajax.FormValidator('album_editor',{
					throbber_id: 'submit_throbber',
					ajax_submit: true,
					update_div: 'page_content',
					complete_callback: function() {
						Lightview.hide();
					}
				});
			});
		}
		if ($('batch_uploader')) {
			$('batch_uploader').observe("submit", function(event) {
				Event.stop(event);
				new Biscuit.Ajax.FormValidator('batch_uploader',{
					request_type: 'validate_batch',
					throbber_id: 'submit_throbber',
					use_ping_timer: true
				});
			})
		}
		// add confirm to delete album
		if ($('delete_album')) {
			$('delete_album').observe("click", function(event){
	    		Event.stop(event);
				if(window.confirm("Are you sure you want to delete this album? All photos in the album will be removed. This action cannot be undone.")) {
					var url = $('delete_album').href;
					PhotoGallery.Delete(url,true);
				}
			});
		}
		// add confirm to delete photo
		if ($('delete_photo')) {
			$('delete_photo').observe("click", function(event){
	    		Event.stop(event);
				if(window.confirm("Are you sure you want to delete this photo? This action cannot be undone.")) {
					var url = $('delete_photo').href;
					PhotoGallery.Delete(url,true);
				}
			});
		}
		$('close_editor').observe("click",function(event){
			Event.stop(event);
			Lightview.hide();
		});
	},
	EditAlbumInLightview: function(id) {
		if (id == '') {
			var action = 'new_album';
			var title = 'New Photo Album';
		}
		else {
			var action = 'edit_album/'+id;
			var title = 'Edit Photo Album';
		}
		this.EditInLightview(action,title);
	},
	EditPhotoInLightview: function(id,album_id) {
		if (id == '') {
			var action = album_id+'/new_photo';
			var title = 'New Photo';
		}
		else {
			var action = 'edit_photo/'+id;
			var title = 'Edit Photo';
		}
		this.EditInLightview(action,title);
	},
	EditInLightview: function(action,title) {
		Lightview.show({
			href: "/photos/"+action,
			rel: 'ajax',
			title: title,
			options: {
				topclose: false,
				scrolling: true,
				autosize: true,
				ajax: {
					method: 'get',
					evalScripts: true,
					requestHeaders: Biscuit.Ajax.RequestHeaders('update')
				}
			}
		});
	},
	RenumberPhotos: function() {		// Renumber the items in the chart and update their classnames after resorting
		var element_ids = new Array();
		var i = 1;
		$$('#photos div').each(function(el) {
			var my_id = el.id.substr(6);		// Everything after "photo_"
			var my_num = i;
			var curr_title = $('title_'+my_id).innerHTML;
			if (curr_title.match(/Photo ([0-9]+)/)) {
				$('title_'+my_id).update("Photo "+my_num);
			}
			i += 1;
		});
	},
	Delete: function(url,from_lightview) {
		this.delete_url = url;
		if (from_lightview === true) {
			$('SubmitButton').value = 'Hang on...';
			$('SubmitButton').disabled = true;
			this.delete_completion = function() {
				Lightview.hide();
			}
			this.DoDelete();
		}
		else {
			Biscuit.Crumbs.ShowThrobber();
			this.delete_completion = function() {
				$Nav.slide_in();
			}
			$Nav.slide_out(function() {
				PhotoGallery.DoDelete();
			})
		}
	},
	DoDelete: function() {
		new Ajax.Updater('page_content',this.delete_url,{
			method: 'get',
			evalScripts: true,
			requestHeaders: Biscuit.Ajax.RequestHeaders('update'),
			onComplete: this.delete_completion
		});
	}
}
