
var GalleryAdmin = {

	Version: '0.9.0',
	Album: 0,
	Images: new Array(),
	Remaining: new Array(),
	Categories: new Array(),
	ImageBlock: '',
	ScriptURL: '', 
	PageID: 0, 
	Running: false,
	StopRequest: false,
	Languages: new Array(),
	album: {
		uid: 0,
		cover: 0,
		title: '',
		description: '',
		images: new Array()
	},
	controls: {
		albumCreate: new Array(),
		albumCreateButton: new Array(),
		albumBlock: ''
	},
	
	initDefault: function(ids) {
		this.Categories = ids.split(',');
		for (var i = 0; i < this.Categories.length; i++) {
			var id = this.Categories[i];
			
				// get create new album controls for each category
			this.controls.albumCreate[id] = $('jm-gallery-category-create-album-'+id).innerHTML;
			this.controls.albumCreateButton[id] = $('jm-gallery-category-create-album-button-'+id).innerHTML;
			$('jm-gallery-category-create-album-'+id).innerHTML = '';
			
				// get albumBlock template
			this.controls.albumBlock = $('jm-gallery-album-block').innerHTML;
			//$('jm-gallery-album-block').remove();
			
				// create Sortable for each category
			Sortable.create('jm-gallery-category-album-list_'+id, {
				tag: 'div',
				only: 'jm-gallery-admin-album-container',
				overlap: 'vertical',
				constraint: false,
				onUpdate: function(element) {
					var temp = element.id.split('_');
					var category = temp[1];
					new Ajax.Request(GalleryAdmin.ScriptURL, {
						method:'get',
    					parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=reOrderAlbums&category='+category+'&'+Sortable.serialize(element.id)
					});
				}
    		});
		}
	},
	
	initEditAlbum: function(id) {
		this.ImageBlock = $('jm-gallery-image-blocks').innerHTML;
		$('jm-gallery-image-blocks').innerHTML = '';
		$('jm-gallery-label-image-blocks-control').className = 'changealbum-container-hidden';
		$('jm-gallery-refresh-stop-button').disabled = true;
		this.Album = id;
		this.album.uid = id;
		this.album.title = $('jm-gallery-label-album-name').innerHTML;
		this.album.description = $('jm-gallery-label-album-description').innerHTML;
		this.album.cover = $('jm-gallery-form-element-album-cover').value;
	},
	
	setScriptURL: function(url) {
		this.ScriptURL = url;
	},
	
	setPageID: function(id) {
		this.PageID = id;
	},
	
	processImageForEditAlbum: function() {
		if (this.StopRequest) {
			this.Running = false;
			this.StopRequest = false;
			$('jm-gallery-refresh-stop-button').disabled = true;
			$('jm-gallery-refresh-button').disabled = false;
			$('jm-gallery-label-image-blocks-control').className = 'changealbum-container';
			return;
		}
		var image = this.Remaining.shift();
		new Ajax.Request(GalleryAdmin.ScriptURL, {
    		method:'get',
    		parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=getImage&image='+image+'&album='+GalleryAdmin.Album,
    		onSuccess: function(transport){
      						var response = transport.responseText;
      						var o = response.evalJSON(true);
      						var t = GalleryAdmin.ImageBlock.replace(/###IMAGE_THUMBNAIL###/g, o['thumbnail']);
      						t = t.replace(/###IMAGE_UID###/g, o['uid']);
      						t = t.replace(/###IMAGE_SORTING###/g, o['sorting']);
      						t = t.replace(/###IMAGE_CAPTION###/g, o['caption']);
      						$('jm-gallery-image-blocks').innerHTML += t;
      						GalleryAdmin.album.images[o['uid']] = o;
      						
      						if (GalleryAdmin.Remaining.length > 0) {
      							var count = GalleryAdmin.Images.length - GalleryAdmin.Remaining.length;
      							$('jm-gallery-label-refresh-info').innerHTML = 'Processed: '+count+'/'+GalleryAdmin.Images.length;
      							GalleryAdmin.processImageForEditAlbum();
      						} else {
      							var count = GalleryAdmin.Images.length - GalleryAdmin.Remaining.length;
      							$('jm-gallery-label-refresh-info').innerHTML = 'Processed: '+count+'/'+GalleryAdmin.Images.length;
      							$('jm-gallery-label-image-blocks-control').className = 'changealbum-container';
      							$('jm-gallery-refresh-button').disabled = false;
      							$('jm-gallery-refresh-stop-button').disabled = true;
      							
      							// finally create Sortable
      							Sortable.create("jm-gallery-image-blocks", {
									tag:'div',
									overlap:'horizontal',
									constraint:false,
									onUpdate: function() {
										new Ajax.Request(GalleryAdmin.ScriptURL, {
											method:'get',
    										parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=reOrderImages&album='+GalleryAdmin.Album+'&'+Sortable.serialize("jm-gallery-image-blocks")
										});
									}
    							});
      						}
    					}
  		});
	},
	
	getImageListAndProcess: function() {
		this.Running = true;
		$('jm-gallery-refresh-stop-button').disabled = false;
		$('jm-gallery-refresh-button').disabled = true;
		$('jm-gallery-refresh-button').blur();
		$('jm-gallery-image-blocks').innerHTML = '';
		new Ajax.Request(GalleryAdmin.ScriptURL, {
    		method:'get',
    		parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=getImageList&album='+GalleryAdmin.Album,
    		onSuccess: function(transport){
      						var response = transport.responseText;
      						if (response == 'Access Denied!') {
      							alert('Access Denied! - Please contact your website administrator if this is not meant to happen!');
      							this.Running = false;
								$('jm-gallery-refresh-stop-button').disabled = true;
								$('jm-gallery-refresh-button').disabled = false;
      						} else {
	      						GalleryAdmin.Images = response.evalJSON(true);
    	  						GalleryAdmin.Remaining = response.evalJSON(true);
      							if (GalleryAdmin.Remaining.length > 0) {
      								$('jm-gallery-label-refresh-info').innerHTML = 'Processed: 0/'+GalleryAdmin.Images.length;
									GalleryAdmin.processImageForEditAlbum();
								}
      						}
    					}
  		});
	},
	
	getImageListAndProcessAtOnce: function() {
		$('jm-gallery-refresh-button').disabled = true;
		$('jm-gallery-refresh-complete-button').disabled = true;
		$('jm-gallery-refresh-complete-button').blur();
		$('jm-gallery-image-blocks').innerHTML = '';
		new Ajax.Request(GalleryAdmin.ScriptURL, {
    		method:'get',
    		parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=getCompleteImageList&album='+GalleryAdmin.Album,
    		onSuccess: function(transport){
      						var response = transport.responseText;
      						var o = response.evalJSON(true);
      						var result = '';
      						
      						if (response == 'Access Denied!') {
      							alert('Access Denied! - Please contact your website administrator if this is not meant to happen!');
      							this.Running = false;
								$('jm-gallery-refresh-stop-button').disabled = true;
								$('jm-gallery-refresh-button').disabled = false;
      						} else {
      						
	      						for (var i = 0; i < o.length; i++) {
	      							var t = GalleryAdmin.ImageBlock.replace(/###IMAGE_THUMBNAIL###/g, o[i]['thumbnail']);
	      							t = t.replace(/###IMAGE_UID###/g, o[i]['uid']);
	      							t = t.replace(/###IMAGE_SORTING###/g, o[i]['sorting']);
	    	  						t = t.replace(/###IMAGE_CAPTION###/g, o[i]['caption']);
	    	  						result += t;
		      						GalleryAdmin.album.images[o[i]['uid']] = o[i];
	      						}
	      						
	      						$('jm-gallery-image-blocks').innerHTML = result;
	      						$('jm-gallery-label-image-blocks-control').className = 'changealbum-container';
								$('jm-gallery-refresh-button').disabled = false;
								$('jm-gallery-refresh-complete-button').disabled = false;
								
								// finally create Sortable
								Sortable.create("jm-gallery-image-blocks", {
									tag:'div',
									overlap:'horizontal',
									constraint:false,
									onUpdate: function() {
										new Ajax.Request(GalleryAdmin.ScriptURL, {
											method:'get',
	    									parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=reOrderImages&album='+GalleryAdmin.Album+'&'+Sortable.serialize("jm-gallery-image-blocks")
										});
									}
	    						});
      						}
    					}
  		});
	},
	
	stopImageListAndProcess: function() {
		if (this.Running) {
			this.StopRequest = true;
		}
	},
	
	processImageBlockButton: function() {
		var action = $F('jm-gallery-form-element-image-edit-part-action');
		var elements = Form.getInputs('jm-gallery-form-image-edit', 'checkbox');
		var images = new Array();
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].checked) {
				images.push(elements[i].value);
			}
		}
		switch(action) {
			case 'deleteImage':
					new Ajax.Request(GalleryAdmin.ScriptURL, {
    					method:'get',
    					parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=deleteImages&album='+GalleryAdmin.Album+'&images='+Object.toJSON(images)
  					});
					for (var i = 0; i < images.length; i++) {
						try {
							$('jm-gallery-form-element-image_'+images[i]).remove();
							$('jm-gallery-form-element-cover_'+images[i]).remove();
						} catch (e) {
						}
					}
					break;
		}
		return false;
	},
	
	updateImageCaption: function(id) {
		var element = $('jm-gallery-form-element-image-caption_'+id);
		if (element.value == GalleryAdmin.album.images[id]['caption']) {
		} else {
			// start updating the caption through AJAX
			new Ajax.Request(GalleryAdmin.ScriptURL, {
    			method:'get',
    			parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=updateImageCaption&image='+id+'&caption='+element.value+'&album='+GalleryAdmin.Album
  			});
		}
	},
	
	updateAlbumTitle: function(id) {
		var element = $('jm-gallery-form-element-album-title_'+id);
		if (element.value == GalleryAdmin.album.title) {
		} else {
			// start updating the title through AJAX
			if (id == 0) { $('jm-gallery-label-album-name').innerHTML = element.value; }
			this.album.title = element.value;
			new Ajax.Request(GalleryAdmin.ScriptURL, {
    			method:'get',
    			parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=updateAlbumTitle&title='+element.value+'&album='+GalleryAdmin.Album+'&language='+id
  			});
		}
	},
	
	updateAlbumDescription: function(id) {
		var element = $('jm-gallery-form-element-album-description_'+id);
		if (element.value == GalleryAdmin.album.description) {
		} else {
			// start updating the description through AJAX
			if (id == 0) { $('jm-gallery-label-album-description').innerHTML = element.value; }
			this.album.description = element.value;
			new Ajax.Request(GalleryAdmin.ScriptURL, {
   				method:'get',
   				parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=updateAlbumDescription&description='+element.value+'&album='+GalleryAdmin.Album+'&language='+id
 				});
		}
	},
	
	changeAlbumCover: function() {
		var element = $('jm-gallery-form-element-album-cover');
		if (element.value == GalleryAdmin.album.description) {
		} else {
			// start updating the caption through AJAX
			this.album.cover = element.value;
			$('jm-gallery-form-element-album-cover').disabled = true;
			new Ajax.Request(GalleryAdmin.ScriptURL, {
    			method:'get',
    			parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=setAlbumCover&cover='+element.value+'&album='+GalleryAdmin.Album,
    			onSuccess: function(transport){
      					var o = transport.responseText.evalJSON(true);
      					$('jm-gallery-label-album-cover').innerHTML = o;
      					$('jm-gallery-form-element-album-cover').disabled = false;
    			}
  			});
		}
	},
	
	deleteAlbum: function (category, album) {
		new Ajax.Request(GalleryAdmin.ScriptURL, {
    		method:'get',
    		parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=deleteAlbum&category='+category+'&album='+album,
    		onSuccess: function(transport){
      				$('jm-gallery-album-entry-'+category+'_'+album).remove();
    		}
  		});
	},
	
	createAlbum: function (category) {
		var name = $('jm-gallery-category-create-album-'+category+'-name').value;
			// do something only if the name has been given
		if (name != '') {
			new Ajax.Request(GalleryAdmin.ScriptURL, {
    			method:'get',
    			parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=createAlbum&category='+category+'&name='+name,
    			onSuccess: function(transport){
      					$('jm-gallery-category-create-album-'+category).className = 'create-new-album-control-hidden';
						$('jm-gallery-category-create-album-'+category).innerHTML = '';
						$('jm-gallery-category-create-album-button-'+category).innerHTML = GalleryAdmin.controls.albumCreateButton[category];
						
							// get result and compose new record...
						var o = transport.responseText.evalJSON(true);
						var html = GalleryAdmin.controls.albumBlock;
						html = html.replace(/###ALBUM_EDIT_LINK###/g, o['editLink']);
						html = html.replace(/###ALBUM_DESCRIPTION###/g, '');
						html = html.replace(/###ALBUM_PICTURECOUNT###/g, 0);
						html = html.replace(/###ALBUM_CRDATE###/g, o['crDate']);
						html = html.replace(/###ALBUM_UID###/g, o['uid']);
						html = html.replace(/###ALBUM_DELETE_ICON###/g, o['deleteIcon']);
						html = html.replace(/###CATEGORY_UID###/g, o['category']);
						
							// add it to the end of the album list
						$('jm-gallery-category-album-list_'+o['category']).innerHTML += html;
						
							// recreate Sortable
						Sortable.create('jm-gallery-category-album-list_'+o['category'], {
							tag: 'div',
							only: 'jm-gallery-admin-album-container',
							overlap: 'horizontal',
							constraint: false,
							onUpdate: function(element) {
								var temp = element.id.split('_');
								var category = temp[1];
								new Ajax.Request(GalleryAdmin.ScriptURL, {
									method:'get',
    								parameters: 'eID=tx_jmgallery_pi1&displayMode=admin&pid='+GalleryAdmin.PageID+'&function=reOrderAlbums&category='+category+'&'+Sortable.serialize(element.id)
								});
							}
    					});
    			}
  			});
		} else { // if no name has been given, just hide the control again
			$('jm-gallery-category-create-album-'+category).className = 'create-new-album-control-hidden';
			$('jm-gallery-category-create-album-'+category).innerHTML = '';
			$('jm-gallery-category-create-album-button-'+category).innerHTML = GalleryAdmin.controls.albumCreateButton[category];	
		}
	},
	
	showControl: function (category) {
		$('jm-gallery-category-create-album-'+category).innerHTML = GalleryAdmin.controls.albumCreate[category];
		$('jm-gallery-category-create-album-'+category).className = 'create-new-album-control';
		$('jm-gallery-category-create-album-button-'+category).innerHTML = '';
		$('jm-gallery-category-create-album-'+category+'-name').focus();
		$('jm-gallery-category-create-album-'+category+'-name').select();
	}
	
}
