/**
 * fusionLib JavaScript utility library.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Copyright (c) 2007 - 2013 fusionLib. All rights reserved.
 */

(function() {

	var loadedJS = [];

	// Find the URL to the library folder
	var libPath = '';
	var script = document.getElementsByTagName('script').item(0);
	while(script && !libPath.length) {
		if(script.src && script.src.indexOf('/fusionLib.js') != -1)
			libPath = script.src.replace(/fusionLib.js$/, '');
		script = script.nextSibling;
	}

	fusionLib.fn.extend({
		/**
		 * Get a parameter from the address.
		 *
		 * @param string name The name of the parameter to get.
		 * @return string The parameter value.
		 */
		getURLParam: function(name) {
			var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
			var results = regex.exec(window.location.href);
			return results == null ? '' : unescape(results[1]).replace(/\+/g, ' ');
		},

		/**
		 * Fetch data from the server and wait for it to arrive before returning.
		 *
		 * @param string url The URL to send the request to.
		 * @param string|array Optional data to pass with the request.
		 * @return string The data read from the server.
		 */
		fetch: function(url, data) {
			var result = '';

			this.ajax(url, {
				type: 'get',
				success: function(d) { result = d; },
				data: data,
				async: false
			});

			return result;
		},

		/**
		 * Load the script into the browser.
		 *
		 * @param string js The script to load if it starts with fusionLib. the it is loaded from the
		 * fusionLib libraries.
		 */
		loadScript: function(js) {
			if(js.match(/^fusionLib\./))
				js = libPath + js;

			// Only load once
			if(!loadedJS[js]) {
				loadedJS[js] = 1;
				var d = fusionLib.fetch(js);
				var script = document.createElement('script');
				script.type = 'text/javascript';
				script.defer = true;
				script.text = d;
				var s = document.getElementsByTagName('script').item(0);
				s.parentNode.insertBefore(script, s);
			}
		},

		/**
		 * Set or get the value of elements. When getting only the 1st elements value is returned.
		 * For radio and checkbox elements the checked state is read/set rather than their value.
		 *
		 * @param string v The value to set if present.
		 */
		value: function(v) {
			if(arguments.length) {
				return this.each(function() {
					if(this.type == 'checkbox')
						this.checked = v;
					else if(this.type == 'radio')
						this.checked = this.value == v;
					else if(this.type == 'select-multiple' && v instanceof Array) {
						for(var i=0;i<this.options.length;i++)
							this.options[i].selected = v.indexOf(this.options[i].value) != -1;
					}
					else
						this.value = v;
				});
			}
			else {
				var val = false;

				if(this[0].type == 'checkbox')
					val = this[0].checked;
				else if(this[0].type == 'radio')
					this.each(function() { if(this.checked) { val = this.value; return false; } });
				else if(this[0].type == 'select-multiple') {
					val = [];
					for(var i=0;i<this[0].options.length;i++) {
						if(this[0].options[i].selected)
							val.push(this[0].options[i].value);
					}
				}
				else
					val = this[0].value;

				return val;
			}
		}
	});

	fusionLib.getURLParam = fusionLib.fn.getURLParam;
	fusionLib.fetch = fusionLib.fn.fetch;
	fusionLib.loadScript = fusionLib.fn.loadScript;

	// Load the JSON library if JSON not nativly supported
	if(!window.JSON)
		fusionLib.loadScript('fusionLib.json.js');
})();