/**
 * @projectDescription
 * <p><code>Edge.Tools</code> is mainly a namespace that contains some helper functions and classes to improve productivity</p>
 * 
 * <p>
 * <b>Compatibility:</b> IE7/PC, IE6/PC, FireFox 2.0,  <br>
 * <b>Not tested:</b> IE5.5/PC and below, FireFox 1.5, IE5.2/Mac, Opera 8.0, Safari, Camino <br>
 * <b>Breaks</b> on 
 * </p>
 * 
 * @version built on Prototype 1.5
 * @author Maxime Cowez
 * @copyright <a href="http://www.edge.be">Edge.be</a>
 */

<!-- 
	//old general.js
	function PassLanguage(Parameter){
		document.Language.language_code.value = Parameter;
		document.Language.submit();
	}
//-->

/** @namespace Edge */
if (!Edge) var Edge = {};

/** @namespace Edge.Tools */
Edge.Tools = {
	
	/**
	 * @namespace Ajax
	 * all data formatting helper functions
	 */
	Ajax : {
		
		get: function(resp, url) {
			return /.cfc$/i.test(url) ? Edge.Tools.Ajax.cfcResponse(resp) : Edge.Tools.Ajax.servletResponse(resp);
		},
		
		servletResponse: function(resp) {
			return resp.responseText.evalJSON();
		},
		
		cfcResponse: function(resp) {
			if (window.ActiveXObject) {
				var XMLDOM = new ActiveXObject("Microsoft.XMLDOM");
				XMLDOM.async = "false";
				XMLDOM.loadXML(resp.responseText);
				resp = XMLDOM;
				if (XMLDOM.parseError.errorCode != 0) 
					Edge.Tools.Debug.error("Error from the Microsoft XMLDOM XML parser: " + XMLDOM.parseError.reason + "; " + XMLDOM.parseError.srcText);
			} 
			else if (document.implementation && document.implementation.createDocument) resp = new DOMParser().parseFromString(resp.responseText, "text/xml");
			else Edge.Tools.Debug.error("Could not generate an XML DOM object");
			
			var respString = "";
			$A(resp.getElementsByTagName("string").item(0).childNodes).collect(function(node) {
				respString += node.nodeValue;
			});
			return respString.evalJSON();
		},
		
		request: function (url, method, parameters, callback) {
			parameters.method = method;
			new Ajax.Request(url, {
				parameters: parameters, 
				onComplete: callback ? function(resp) {callback(Edge.Tools.Ajax.get(resp, url));} : null, 
				onFailure: Edge.Tools.Debug.ajaxError
			});
		}
		
	},
	
	/**
	 * @namespace Form
	 * form helper functions
	 */
	Form : {
		
		/**
		 * @method getListValue	gets the selected values(s) of radio and check buttons
		 * @param {Array} list	an array of input elements
		 * @return {String,Array}
		 */
		getListValue: function(list) {
			var values = $$('input:checked[type="' + list[0].type + '"][name="' + list[0].name + '"]').pluck('value');
			return values.size() ? values.reduce() : null;
		}
		
	},
	
	/** 
	 * @namespace Timer 	
	 * timer helper: use for action delaying or animation <br>
	 * <b>Example usage:</b> <br>
	 * <code>
	 * var timer = toolkit.Timer.create(seconds * 1000, 25); <br>
	 * timer.execute = function() { <br>
	 * 	$('some_div').setStyle({width: timer.progress*100 + '%'}); <em>//scale animation</em> <br>
	 * }; <br>
	 * timer.callBack = function() { this.someAction(); }.bindAsEventListener(this); <br>
	 * </code>
	 * As with event observation, if you want to use the <code>this</code> scope, you have to bind it to the function
	 */
	Timer : {
		
		/**
		 * @method create	creates a Timer object at the <code>window</code> level
		 * @param {Number} duration		duration of the timer in milliseconds
		 * @param {Number} fps			animation speed in frames per second
		 */
		create: function(duration, fps) {
			window.timerObj = new Object();
			window.timerObj.duration = window.timerObj.countdown = window.timerObj.progress = duration;
			window.timerObj.interval = 1000/fps;
			window.timerObj.timer = setInterval('Edge.Tools.Timer.update()', window.timerObj.interval);
			return window.timerObj;
		},
		
		/**
		 * @method update	
		 * updates the Timer object periodically until duration has expired <br>
		 * on update: calls <code>execute()</code>, a function you have to define yourself <br>
		 * on expiration: calls <code>callBack()</code>, a function you have to define yourself
		 */
		update: function() {
			window.timerObj.countdown -= window.timerObj.interval;
			window.timerObj.progress = window.timerObj.countdown/window.timerObj.duration;
			window.timerObj.execute();
			if (window.timerObj.countdown <= 0) {
				window.timerObj.callBack();
				clearInterval(window.timerObj.timer);
			}
		}
		
	},
	
	/**
	 * @namespace Format
	 * all data formatting helper functions
	 */
	Format : {
		
		/**
		 * @method todayString
		 * Returns the actual date as string [yyyymmdd]
		 * @return {String}
		 */
		todayString: function() {
			var today = new Date();
			return String(today.getFullYear()) + Edge.Tools.Format.addLeadingZeros(today.getMonth()+1) + Edge.Tools.Format.addLeadingZeros(today.getDate());
		},
		
		/**
		 * adds a 0 in front of a number smaller than 10 and returns it as a String
		 * @param {Number} nr
		 * @return {String}
		 */
		addLeadingZeros: function(nr) {
			return String(nr).length < 2 ? "0" + nr : nr;
		}
		
	},
	
	/**
	 * @namespace Gfx
	 * all graphics helper functions
	 */
	Gfx : {
		
		/**
		 * @method makeTransparent
		 * makes a given element transparent through CSS
		 * @param {Element,String} elem		the element to apply the transparency to
		 * @param {Number} value			the opacity value to set
		 */
		makeTransparent: function(elem, value) {
			$(elem).setStyle({
				'filter':		'alpha(opacity='+(value*100)+')',
				'-moz-opacity': value,
				'opacity': 		value
			});
		}
		
	},

	/**
	 * @namespace Error
	 * debugging helpers
	 */
	Debug : {
		
		ajaxError: function(msg) {
			console.error("Ajax error " + msg.status + "(" + msg.statusText + "): " + msg.responseText);
		}
		
	},
	
	Browser: {
		
		/**
		 * @method versionRange
		 * Returns whether the users browser version is within a given range
		 * @param {Number} low		the low version limit
		 * @param {Number} high		the high version limit
		 * @return {Boolean}
		 */
		IErange: function(low, high){
			if (!Prototype.Browser.IE) 
				return false;
			
			var regex = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
			return regex != null && Number(regex[1]) >= low && Number(regex[1]) <= high;
		}
		
	}
	
}

/** Crossbrowser debug */
if (!console) {

	var console = window.log && nitobi.Debug ? {
		
		trace: function(msg) {
			nitobi.Debug.log(msg); log.info(msg);
		},
		
		log: function(msg) {
			nitobi.Debug.log(msg); log.info(msg);
		},
		
		warn: function(msg) {
			nitobi.Debug.log(msg); log.warn(msg);
		},
		
		error: function(msg) {
			nitobi.Debug.log(msg); log.error(msg);
		},
		
		profile: function(msg) {
			nitobi.Debug.log(msg); log.profile(msg);
		}
		
	} : {
		trace: 	function() {},
		log: 	function() {},
		warn: 	function() {},
		error: 	function(msg) {
			throw new Error(msg);
		}
	}
	
}

/** Prototype extensions */
$F = $F.wrap(function(proceed, list) {
	try {
		return proceed(list);
	} 
	catch (e) {
		return Edge.Tools.Form.getListValue(list);
	}
});

var $E = console.error; 

Prototype.Browser.IErange = Edge.Tools.Browser.IErange;

