/**
* @fileoverview This file contains the static object  AdminTab.uploadMap
* @author Sara Lin
* @version 0.1
*/

/**
* UploadMap handles the new map image preview and uploading functionalities in the AdminTab's map page.  This object is currently initialized by the User._mapPage function.
* @namespace
*/
AdminTab.uploadMap = {
	/** @type boolean */
	hasTempMap: false,
	/** @type DOM element  */
	uploadForm: null,
	/** @type DOM element */
	fileInput: null,
	/** @type DOM element */
	cancelBtn: null,
	/** @type DOM element */
	originalImage: null,
	/** @type DOM element */
	newImage: null,
	/** @type DOM element */
	previewSelect: null,
	
	/**
	* save DOM elements for future use; attaches event observers to upload new floor plan images and add observer to the floor manager to check for floor switches.
	* @constructor
	*/
	initialize: function() {
		this.uploadForm = $('editMapForm');
		this.fileInput = $('uploadImg');
		this.cancelBtn = $(this.uploadForm).getInputs('button', 'cancelImage')[0];
		this.previewSelect = $('uploadImgView');
		this.switchView = this._switchView.bindAsEventListener(this);
		
		$('uploadImgFloor').value = FloorManager.curFloor;
		Event.observe('uploadImg', 'change', this.uploadPreview);
		Event.observe(this.cancelBtn, 'click', this.resetUpload);
		var submitBtn = $(this.uploadForm).getInputs('button', 'submitImage')[0];
		Event.observe(submitBtn, 'click', this.submitMap);
		FloorManager.floorSubject.attachObserver('uploadMap', AdminTab.uploadMap.promptReset);
		
		this.uploadForm.getInputs('radio','view').each(function(input) {
				Event.observe(input, 'click', AdminTab.uploadMap.switchView);
		});
	},

	/**
	* this is invoked when administrator tries to switch floor.  The function prompts user only when a new floorplan image has been saved by not submitted.  
	* The administrator can either cancel current changes and switch floor or cancel floor switch and continue with their editing. 
	* If the admin decides to swtich floor, new floor level is saved in the upload map form.
	* @param {number} floor floor level of the new floor 
	*/
	promptReset: function(floor) {
		if (AdminTab.uploadMap.hasTempMap) {
			var confirmSwitch = confirm("If you do not submit the changes to the current floor, they will be lost.  Do you wish to continue?");
			if (!confirmSwitch) 
				return false;
			else  {
				AdminTab.uploadMap.resetUpload(null);
				AdminTab.uploadMap.originalImage = null;
				AdminTab.uploadMap.newImage = null;
			}
		}
		
		if ($('uploadImgFloor') && floor)
			$('uploadImgFloor').value = floor;
		return true;
	},
	
	/**
	* invoked by onChange event of the floor map image browse field.  FOr now, the image file must have .gif extension.
	* If the image is valid, then it will be saved as a temporary floormap image for preview.
	* @event
	*/
	uploadPreview: function(event) {
		if ($('uploadImg').value.lastIndexOf(".gif")==-1) {
			alert("Error: only .gif file allowed!");
			return;
		}
		//check file size
		AdminTab.uploadMap.originalImage = $$('#floor_'+FloorManager.curFloor+' .original')[0];
		$('editMapForm').submit();
	},
	
	/**
	* triggered after upload floor image for preview.  This function constructs a new html img tag to load the new floormap image file for preview.
	* the switch view option is shown for the admin to switch between new and original floormap images.
	* @param {number} floor the current floor level
	*/
	previewMap: function(floor) {
		AdminTab.uploadMap.newImage = $$('#floor_'+floor+' .new')[0];
		if (AdminTab.uploadMap.newImage) {
			alert("Error: a map image is already loaded for preview!");
			return;
		}
		
		this.newImage = AdminTab.uploadMap.originalImage.cloneNode(false);
		this.newImage.className = 'new admin';
		var path = this.newImage.src.split('/');
		path.splice(path.size()-1,0,'tmp');
		this.newImage.src = path.join('/');

		path = this.newImage.src.split('?');
		this.newImage.src = path[0]+'?'+Math.random();
		
		$('floor_'+floor).appendChild(this.newImage);
		$(AdminTab.uploadMap.originalImage).hide();
	
		AdminTab.uploadMap.setDefaultView();
		$(AdminTab.uploadMap.previewSelect).show();
		AdminTab.uploadMap.hasTempMap = true;
	},
	
	/**
	* setDefaultView iterates through the two radio options in swtich view and resets the checked option.
	*/
	setDefaultView: function() {
		AdminTab.uploadMap.uploadForm.getInputs('radio','view').each(function(el) {
			if (el.defaultChecked) {
				el.checked=true;
			}
		});
	},
	
	/**
	* invoked by the administrator clicking on one of the radio buttons.  calls switchViewHelper to toggle the floorplan images.
	* @event
	*/
	_switchView: function(event) {
		var value = Event.element(event).value;
		this._switchViewHelper(value);
	},
	
	/**
	* toggles between the new and original floorplan images
	* @param {String} value indicates the floorplan to show, it can either be 'original' or 'new'
	*/
	_switchViewHelper: function(value) {
		var floor = FloorManager.curFloor;
		
		if (value=='original') {
			$(this.newImage).hide();
			$(this.originalImage).show();
		}
		else {
			$(this.newImage).show();
			$(this.originalImage).hide();
		}
	},
	
	/**
	* reset the map upload form; hide the swtich view field and review the new image tag.
	* @even 
	*/
	resetUpload: function(event) {
		if (!AdminTab.uploadMap.originalImage)
			AdminTab.uploadMap.originalImage = $$('#floor_'+FloorManager.curFloor+' .original')[0];
		$(AdminTab.uploadMap.originalImage).show();
		
		//delete new image tag
		var newImage = $$('#floor_'+FloorManager.curFloor+' .new')[0];
		if (newImage)
			Element.remove($(newImage));
			
		//clear upload input field
		$(AdminTab.uploadMap.fileInput).value = '';

		//hide previewSelect radion buttons
		$(AdminTab.uploadMap.previewSelect).hide();
		
		//delete new image tag and image cache on server
		new Ajax.Request(base_url+'admin/deleteTmpMapImg/'+FloorManager.curFloor);
		AdminTab.uploadMap.hasTempMap = false;
	}, 
	
	/**
	* if a temporary floorplan image exists, then the administrator can choose to submit it as the new floorplan image.  The floorplan on the map will be reloaded to reflect the change.
	* @event
	*/
	submitMap: function(event) {
		if (!AdminTab.uploadMap.hasTempMap) {
			alert("you have not upload any new image");
			return;
		}
		
		var url = encodeURL(base_url+'admin/commitMapImg',FloorManager.curFloor);
		new Ajax.Request(url, {
			onSuccess: function(response) {
				AdminTab.uploadMap.originalImage.src = AdminTab.uploadMap.originalImage.src+"?"+Math.random();
				AdminTab.uploadMap.resetUpload(null);
			}
		});
	}
}