/**
* @fileoverview This is a floor object file.
* @author Sara Lin
* @version 0.1
*/

/**
* Floor object contains its own floor level and an array of items that belong to this floor.
 * @class 
 */
var Floor = Class.create();

Floor.prototype = {
	/** 
	* @constructor
	* @param {number} floor the floor level
	* @param {function} optionalCallback a function that will be invoked after floorplan image and floor items have been loaded.
	*/
	initialize: function(floor,optionalCallback) {
		this.floor = floor;
		this.items = [];
		this.uploadFloor(optionalCallback);
	},

	/** upload image map and all item information contained in it; create an array of item objects.
	* @param {function} optionalCallback a function that will be invoked after floorplan image and floor items have been loaded.
	*/
	uploadFloor: function(optionalCallback) {
		var floorObj = this;
		var thisFloor = this.floor;
		
		//update image map html to map div
		new Ajax.Request(base_url+'map/getMapCoords/'+this.floor, {
			onSuccess: function(response) {
				// remove the currently displayed floor if it has already been loaded
				var floorDiv = $('floor_'+thisFloor);
				if (floorDiv) {
					floorDiv.remove();
				}
				
				new Ajax.Request(base_url+'map/getHoverDesc/'+thisFloor, {
					onSuccess: function(response) {
						var items = [];
						var json = response.responseText.evalJSON();
						json.each(function(i) {
							items[i.itemID] = new Item(i.itemID, i.name, i.description, i.hotspot, i.customhp, i.label);
						});
						floorObj.items = items;
						floorObj.items.each(function(item) {
							if (item!=undefined) {
								item.addEvents();
							}
						});
						
						if (optionalCallback)
							optionalCallback();
					}
				});
				
				new Insertion.Bottom('floors', response.responseText);
				$('floor_'+thisFloor).show();
			}
		});	
	},
	
	/** unsets regular map item click observers and sets observer for editing, for each item */
	setCoordEditObservers: function() {
		this.items.each(function(item) {
			if (item!=undefined) {
				item.delEvents();
				item.addCoordEditEvent();
			}
		});
	},
	
	/** sets observer for editing for each item, but does not try to unset regular click observers */
	setJustCoordEditObservers: function() {
		this.items.each(function(item) {
			if (item!=undefined) {
				item.addCoordEditEvent();
			}
		});
	},
	
	/** unsets special observers for editing items, and resets regular map item click observers */
	unsetCoordEditObservers: function() {
		this.items.each(function(item) {
			if (item!=undefined) {
				item.delCoordEditEvent();
				item.addEvents();
			}
		});
	},
	
	/** unsets map item editing observers, but does not reset regular map item click observers */
	unsetJustCoordEditObservers: function() {
		this.items.each(function(item) {
			if (item!=undefined) {
				item.delCoordEditEvent();
			}
		});
	},

	/** hide the current floor */
	hide: function() {
		var element = $('floor_'+this.floor);
		Element.hide(element);
	},
	
	/** show the floor */
	show: function() {
		var element = $('floor_'+this.floor);
		Element.show(element);
	},
	
	/** returns whether the floor is visible */
	visible: function() {
		return $('floor_'+this.floor).visible();
	}
}