/*
 * This control is used to help the user to make the next step with creating
 * geo-based resources.
 */
function SResourceNonGeoControl(resource, clazz) {
	this.clazz = clazz;
	this.resource = resource;
	
	if (!this.resource)
		throw 'No resource is given';
}
SResourceNonGeoControl.prototype = Object.extend(new GControl(), {
	requestNewUrl : '/resources;xhr_new',
	requestCreateUrl : '/resources;xhr_create',
	
	initialize : function(map) {
		this.map = map;
		rControl.reset(rControl.sNonGeoResource);
		
		if (this.clazz == 'struct') {
			var opts = {
				struct : this.resource,
				parent : null,
				map : this.map
			}
			delete this.clazz;
			this.resource = this.resource.isGeo() ? new SGeoResource(opts) : new SResource(opts);
		}
			
		this.container = this.prepareContent();
		
		this.resize();
		this.resource.isNew() ? this.requestNew() : this.requestEdit();
		
		return this.container;
	},
	
	/**
	 * Will be called if user hit the close button.
	 * If this resource is new, user will be asked if he
	 * really want's to cancel the creation of a new resource.
	 * 
	 * Otherwise user got displayed information about an existing
	 * resource, so this form can be closed without confirmation.
	 */
	close : function(askForConfirmation) {
		askForConfirmation = askForConfirmation == null ? true : false;
		if (this.resource.isNew() && (askForConfirmation ? confirm(t('tResourceNonGeoControlCloseConfirm')) : true)) {
			this.resource.destroy();
		}
		this.map.rControl.reset();
	},
	
	accept : function(formId) {
		var self = this;
		var result = Form.serialize(formId);
		if (this.resource.isAGeo()) {
			 result += '&' + 'resource[json]=' + this.resource.serialize().toJSON();	
		}
		
		new Ajax.Request(this.requestCreateUrl, {
			asynchronous : true,
			method : 'post',
			parameters : result,
			
			onCreate : function() {
				Element.show("indicator")
			},
			onComplete : function() {
				Element.hide("indicator");
			},
			onSuccess : function() {
				if (self.resource.isNew()) {
					self.resource.destroy();
				}
				self.map.rControl.clearResources();
				self.map.rControl.reset();
			}
		});
	},
	
	resize : function() {
		this.size = this.map.getSize();
		this.container.style.width = this.size.width + this.size.widthUnit;
		this.container.style.height = this.size.height + this.size.heightUnit;
	},
	
	getDefaultPosition : function() {
		return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(0, 0));
	},
	
	requestNew : function() {
		new Ajax.Updater(this.window,
			this.requestNewUrl, {
			method : 'post',
			evalScripts : true,
			parameters : 'resource[struct_symbol]=' + this.resource.struct.symbol,
			
			onCreate : function() {
				Element.show('indicator')
			},
			onComplete : function() {
				Element.hide('indicator')
			}
		});
	},
	
	requestEdit : function() {
		new Ajax.Updater(this.window,
			this.resource.requestDetailsUrl, {
			method : 'post',
			evalScripts : true,
			
			onCreate : function() {
				Element.show('indicator');
			},
			onComplete : function() {
				Element.hide('indicator');
			}
		});
	},
	
	destroy : function() {
		GEvent.removeListener(this.onResizeEvent);
		this.map.removeControl(this);
	},
	
	prepareContent : function() {
		var self = this;
		
		/* First create a container with the size of the map */
		var container = document.createElement("div");
		container.id = 'ResourceContainer';
		
		/* Make a alpha background for better look */
		this.bg = document.createElement("div");
		this.bg.id = 'ResourceContainerBG';
		
		/* 
		 * The window has a white background and contains the 
		 * generated resource form.
		 */
		this.window = document.createElement("div");
		this.window.id = 'ResourceContainerWindow';
		
		/* Add container */
		container.appendChild(this.bg);
		container.appendChild(this.window);
		this.map.getContainer().appendChild(container);
		/* 
		 * The user will be able to cancel this operation
		 * with this closebutton.
		 */
		var closeButton = document.createElement('a');
		closeButton.id = 'ResourceContainerCloseButton'
		closeButton.innerHTML = '<img src="/images/16x16/cross.png" />'
		container.appendChild(closeButton);
		
		/* Register click-event on close button. */
		closeButton.onclick = function() {
			self.close();
		}
		
		/* If user resizes the map this compotent will also resized. */
		this.onResizeEvent = GEvent.bind(this.map, 'resize', this, function() {
			this.resize();
		});
		
		/* Returns the complete container, witch was added to the map. */
		return container;
	},
	
	printable : function() {
		return false;
	},
	
	selectable : function() {
		return false;
	}
});