
/** section Impl, related to: SL.ElementUpdater
 * SL.updateElement(event, target[, options = {}]) -> SL.ElementViewSwapper
 *     - evt (event | Event): The DOM or Mootools Event fired by any element (preferable `<a>`)
 *     - target (String | Element): a string of the id for an Element or an Element reference to update
 *     - options (Object): _(Optional)_ All options [[SL.ElementUpdater]] accepts
 * 
 * This is a convenient function for direct use as a event handler.
 * It will stop the propergation of the incomming event, fetch the URL for the request and update the target for you.
 *
 * ##### Options
 * You may pass the whole list supported by [[SL.ElementUpdater]], plus:
 * - url (String): If the Element, which fires the event, is not a link with an valid href, you may want to put the url in here
 *   If there no url in the href, nor in the options, the next parent w/o an empty href will be used (try to avoid this)
 * - updateMethod (String): `update`|`append`: `update` (replaces the current target content), `append` (append to the current target content)
 * 
 * ##### Examples
 *     <div id="wrapper123">
 *       <a href="foo/123" onclick="SL.updateElement(event, 'content123', {'spinnerTarget': 'wrapper123'});">Fetch some content!</a>
 *       <div id="content123">
 *         ...
 *       </div>
 *     </div>
 * 
 * _Note_ that the first argument to the function has to be `event` to get the click-event passed!
**/
SL.updateElement = function (evt, target, options) {
	evt = new Event(evt).stop();
	options = options || {};
	options.method = options.method || "get";
	var url = SL._fetchEventUrl(evt, options.url || '');
	(new SL.ElementUpdater(url, target, options)).send();
	
	SL.helperFunctions.linkSwitch(options);
};

/** section: Impl, related to: SL.ElementViewSwapper
 * SL.loadView(event, target[, options = {}]) -> undefined
 *     - event (Event): A elements event ('onclick' in most cases)
 *     - target (Element | String): The Element, or String of the ID of an Element for wich another view shall be loaded
 *     - options (Object): _(Optional)_ set of options, to pass to the underlying [[SL.ElementViewSwapper]]
 * 
**/
SL.loadView = function (evt, target, options) {
	
	evt = new Event(evt).stop();
	options = options || {};
	var url = SL._fetchEventUrl(evt, options.url || '');
	(SL.ElementViewSwapper.getInstance(target, url, options)).load();
};


/** section: Impl, related to: SL.ElementViewSwapper
 * SL.dismissView(event, target, options)
 *     - event (Event): A elements event ('onclick' in most cases)
 *     - target (Element | String): The Element, or String of the ID of an Element for which the other view shall be dismissed
 *     - options (Object): _(Optional)_ none yet
 * 
**/
SL.dismissView = function (evt, target, options) {
	evt = new Event(evt).stop();
	options = options || {};
	var swapper = SL.ElementViewSwapper.getInstance(target);
	if (swapper) {
		swapper.dismiss(options.force || false);
	}
};

/** section: Impl
 * SL.submitView(event, target, options)
 *     - event (Event): A form's submit event
 *     - target (Element | String): The Element, or String of the ID of an Element to which this Form-submit is related to
 *     - options (Object): _(Optional)_ set of options, to pass to the underlying [Form.Request](http://mootools.net/docs/more/Forms/Form.Request)
**/ 
SL.submitView = function (evt, target, options) {
	evt = new Event(evt).stop();
	options = options || {};
	
	var frm = evt.target;
	
	var request = new Form.Request(frm, target, options);
	request.addEvents({
		'onSuccess': function() {
			SL.helperFunctions.linkSwitch(options);
			SL.dismissView.pass([evt, target]);
		},
		'onRequest': function () {
			frm.removeClass('error');
		},
		'onFailure': function (xhr) {
			frm.addClass('error');
		},
		'onException': function () {
			frm.addClass('error');
		}
	});
	request.send();
};


/** section: Impl
 * SL.helperFunctions(event, target, options)
 *     - options (Object): this object has attributes like 'show' and 'hide' with id of elements. This function switch the visibility of two links
**/ 
SL.helperFunctions = SL.helperFunctions || {};
SL.helperFunctions.linkSwitch = function(options) {
	if($defined(options.linkSwitch)) {
		if($defined(options.linkSwitch.hide)) {
			$(options.linkSwitch.hide).addClass('hide');
		}
		if($defined(options.linkSwitch.show)) {
			$(options.linkSwitch.show).removeClass('hide');
		}
	}
}


/** section: Impl
 * SL.filterSearch(event, target, options)
 *     - event (Event): A link's click event
 *     - target (Element | String): The Element, or String of the ID of an Element to which this Form-submit is related to
 *     - filter (Object): the (filter-)text which will be inserted in the target element
 *     - searchSection (String): the search section value for the hidden field in search form
**/ 
SL.filterSearch = function (evt, target, filter, searchSection) {
	evt = new Event(evt).stop();

	target = document.id(target);
	if (!target || !filter || filter == '') return;

	target.set('html', filter).getParent().removeClass('hide');
	
	document.id('foSearchContent').addClass('hide');
	document.id('searchSection').value = searchSection;
}

