/***********************************************************************************************************
 * LIBRARY : system.js (Main include - Set system parameters and functions                                 *
/***********************************************************************************************************
  VERSION               : 5.6
  CREATION DATE         : 13/09/2010
  CREATION AUTHOR       : SBR
  MODIFICATION DATE     : 10/06/2011
  MODIFICATION AUTHOR   : SBR
/***********************************************************************************************************/

// ------------------------------------------------------------------------------------------
// - INITIALIZE                                                                             -
// ------------------------------------------------------------------------------------------
	// - Browser informations -
	var version	= null,
		browser = {
			isGecko		:false,
			isTrident	: false,
			isWebkit	: false,
			isPresto	: false,
			isKhtml		: false,
			name		: "",
			version		: 0
		}

	with (navigator) if (browser.isPresto = !!window.opera) {
		browser.name = "opera"
		version = /opera\/([\d]+[.]*[\d]*)/gi.exec(userAgent)
	} else if (browser.isKhtml = !!window.chrome) {
		browser.name = "google chrome"		
		version = /chrome\/([\d]+[.]*[\d]*)/gi.exec(userAgent)
	} else if (browser.isWebkit = !!window.WebKitPoint) {
		browser.name = "safari"
		version = /version\/([\d]+[.]*[\d]*)/gi.exec(userAgent)
	} else if (browser.isTrident = !!document.all) {
		browser.name = /avant browser/gi.test(userAgent)
			? "avant browser"
			: "internet explorer"
		version = /msie(\s*)([\d]+[.]*[\d]*)/gi.exec(userAgent)
	} else if (browser.isGecko = !!window.netscape) {
		var temp = /(navigator|k-meleon|seamonkey|songbird)/gi.exec(userAgent)
		if (RegExp.lastParen == "") {
			browser.name = "firefox"
			version = /firefox\/([\d]+[.]*[\d]*)/gi.exec(userAgent)
		} else {
			browser.name = RegExp.lastParen.toLowerCase()
			version = /(navigator|k-meleon|seamonkey|songbird)\/([\d]+[.]*[\d]*)/gi.exec(userAgent)
	}}
	browser.version = RegExp.lastParen

	// - Add HTML5 tags for Internet Explorer < 9 -
	if (browser.isTrident && browser.version < 9) {
		var tags = [
			"abbr", 	"article",	"aside", 		"audio", 
			"bb", 		"canvas", 	"datagrid", 	"datalist", 
			"details",	"dialog", 	"eventsource",	"figure",
			"footer", 	"header", 	"hgroup", 		"mark", 
			"menu", 	"meter", 	"nav", 			"output", 
			"progress", "section", 	"time", 		"video"
		]
		for (var element in tags) document.createElement(tags[element])
	}
	// - data (base64) for transparent 1 pixel png
	var png = {
		transparent : "data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUCNdjYGBgYAAAAAUAAV7zKjoAAAAASUVORK5CYII="
	}

	include("system.css")

// ------------------------------------------------------------------------------------------
// - PROTOTYPES                                                                             -
// ------------------------------------------------------------------------------------------
	// - NUMBER : decToHex -
	Number.prototype.decToHex   = function() {return this.toString(16)}
	
	// - STRING : utf8Encode -
	String.prototype.utf8Encode	= function() {return unescape(encodeURIComponent(this))} // BAD beurk

	// - STRING : utf8Decode -
	String.prototype.utf8Decode	= function() {return decodeURIComponent(escape(this))}	// BAD beurk

	// - STRING : jsonDecode -
	String.prototype.jsonDecode	= function() {return eval("(" + this + ")")}

	// - STRING : hexToDec -
	String.prototype.hexToDec	= function() {return parseInt(this,16)}

	// - STRING : chr64 (Base 64 allow characters) -
	String.prototype.chr64		= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

	// - STRING : base64Encode -
	String.prototype.base64Encode = function() {
		var ret = ""
		with (this) for (var i = 0;i < length;i += 3) {
			var chr = [
				charCodeAt(i + 0),
				charCodeAt(i + 1),
				charCodeAt(i + 2)
			]
			ret += 	chr64[chr[0] >> 2] + chr64[(chr[0] & 0x03) << 4 | chr[1] >> 4] + 
					chr64[chr[1] | chr[2] ? (chr[1] & 0x0f) << 2 | chr[2] >> 6 : 64] + 
					chr64[chr[2] ? chr[2] & 0x3f : 64]
		}
		return ret
	}
		
	// - STRING : base64Decode -
	String.prototype.base64Decode = function() {
		var ret = ""	
		with (this) for (var i = 0;i < length;i += 4) {
			var chr = [
				chr64.indexOf(charAt(i + 0)),
				chr64.indexOf(charAt(i + 1)),
				chr64.indexOf(charAt(i + 2)),
				chr64.indexOf(charAt(i + 3))
			]
			ret += 	String.fromCharCode(chr[0] << 2 & 0xff | chr[1] >> 4 & 0x3) + 
					(chr[2] < 64 ? String.fromCharCode(chr[1] << 4 & 0xff | chr[2] >> 2 & 0x0f) : "") + 
					(chr[3] < 64 ? String.fromCharCode(chr[2] << 6 & 0xff | chr[3]) : "")
		}
		return ret
	}

	// - ARRAY : serialize - "/!\ only numeric index"
	Array.prototype.serialize = function() {
		var out = "a:" + this.length + ":{";
			n = 0;
		for(var i in this) {
			var val = this[i]
			switch (typeof(val)) {
				case "function" : 
					continue
				case "number" :
					if (/\./.test(val))
						out += "i:" + n + ";" + "d:" + val + ";"
					else
						out += "i:" + n + ";" + "i:" + val + ";"
				break
				default :
					out += "i:" + n + ";" + "s:" + val.toString().length + ":\"" + val + "\";"
				break
			}
			n++
		}
		return out + "}";
	}

	// - DATE : set full date -
	Date.prototype.setFullDate = function(D,M,Y) {
		with (this)
			setDate(D),
			setMonth(M - 1),
			setYear(Y)
	}
	
	// - DATE : age -
	Date.prototype.age = function(date) {
		var year = this.getFullYear() - date.getFullYear()
		return 	this.getMonth() < date.getMonth() 
				|| (this.getMonth() == date.getMonth() 
				&& this.getDate() < date.getDate())
					? --year
					: year
	}

	// - OBJECT : caller name -
	Object.prototype.callerName = function() {
		return /^function\s+([^(]+)/.exec(this.callee.caller.toString())[1]
	}

	// - OBJECT : current function name -
	Object.prototype.functionName = function() {
		return /^function\s+([^(]+)/.exec(this.callee.toString())[1]
	}

	// - XMLHTTPREQUEST : sendAsBinary -
	if (browser.isWebkit || browser.isKhtml) try {
		XMLHttpRequest.prototype.sendAsBinary = function(string) {
			var builder	= new BlobBuilder(),
				binary 	= new ArrayBuffer(1),
				uint8 	= new Uint8Array(binary, 0)
			for (var i in string) if (string.hasOwnProperty(i)) {
					uint8[0] = string[i].charCodeAt(0) & 0xff
					builder.append(binary)
			}
			this.send(builder.getBlob())
	}} catch(error) {}

// ------------------------------------------------------------------------------------------
// - EVENTS                                                                                 -
// ------------------------------------------------------------------------------------------
	var mouse = client = null
	var key = {
		TAB		: 9,
		RETURN	: 13,
		ESCAPE	: 27
	}

	// - Patch for Internet Explorer -
	if (browser.isTrident && browser.version < 9) {
		window.addEventListener	  = function(e,f) {this.attachEvent("on" + e, f)}
		document.addEventListener = function(e,f) {this.attachEvent("on" + e, f)}
	}

	with (window) onload = onresize = function() {client = getClientSize()}
	
	document.onmousemove = function(event) {mouse = getEventResult(event)}

	// - Strop propagation of event
	function stopEvent(event) {
		with (event || window.event) if (browser.isTrident)
			cancelBubble = true
		else
			stopPropagation()
		return false
	}

	// - Get result of event
	function getEventResult(event) {
		with (event || window.event) switch (type) {
			case "mousemove" : return {
				x : clientX + document.documentElement.scrollLeft , 
				y : clientY + document.documentElement.scrollTop 
			}
			case "keypress"	 : 
			case "keydown"	 :
			case "keyup"	 : 
			case "mousedown" : 
			case "mouseup"	 : return browser.isTrident 
				? {CODE : keyCode, 	CHAR : String.fromCharCode(keyCode)} 
				: {CODE : which,	CHAR : String.fromCharCode(which)}
	}}
// ------------------------------------------------------------------------------------------
// - SHORTCUTS                                                                              -
// ------------------------------------------------------------------------------------------
	// - Element -
	var $ = function(element) {
		if (element.tagName == undefined)
		if (document.getElementById(element))				element = document.getElementById(element)
		else if (document.getElementsByName(element)[0])	element = document.getElementsByName(element)[0]
		else return null

		// - getStyle -
		element.getStyle = function(pname) {
			return (browser.isTrident)
				? this.currentStyle[pname]
				: window.getComputedStyle(this,null)[pname]
		}

		// - Horizontal center -
		element.xCenter = function() {
			var client = getClientSize()
			
			with (this) {
				style.position = "fixed"
				try {
					style.left = document.documentElement.scrollLeft + (client.width - offsetWidth >> 1) + "px"
				} catch (error) {client.width - offsetWidth >> 1 + "px"}
		}}

		// - Vertical center -
		element.yCenter = function(ep) {
			var client = getClientSize()
			with (this) {
				style.position = "fixed"
				try {
					style.top = document.documentElement.scrollTop + (client.height - offsetHeight >> 1) + "px"
				} catch (error) {client.height - offsetHeight >> 1 + "px"}
		}}

		// - rounder + shadow (CSS3) -	
		element.roundShadow = function() {
			try {
				with (this) {
					if (/radius/.test(getAttribute("data-style")))
						setStyle("borderRadius",STYLES[tagName.toUpperCase()].RADIUS)
					if (/shadow/.test(getAttribute("data-style")))
						setStyle("boxShadow",STYLES[tagName.toUpperCase()].SHADOW)
			}} catch(error) {}
		}

		// - setStyle -
		element.setStyle = function(pname,vname) {
			with (this) try {
				switch(pname) {
					case "opacity" :
						if (browser.isTrident) 
							style.filter = "alpha(opacity:" + vname + ")"
						else
							style.opacity = vname / 100
					break			
					case "width" :
					case "height"  :
						with (browser) if (!isTrident || version >= 8 || vname != "auto") return style[pname] = vname
						style[pname] = '300px'
						style[pname] = eval("scroll" + pname.charAt(0).toUpperCase() + pname.substr(1)) + "px"
					break
					default:
						style[pname] = 
						style["Moz"		+ (pname = pname.charAt(0).toUpperCase() + pname.substr(1))] =
						style["Webkit"  + pname] = 
						style["Khtml"   + pname] = vname
					break
			}} catch(error) {
		}}

		// - addEventListener -
		if (browser.isTrident) {
			element.addEventListener	= function(e,f) {this.attachEvent("on" + e, f)}
			element.removeEventListener	= function(e,f) {this.detachEvent("on" + e, f)}
		}
		return element
	}
// ------------------------------------------------------------------------------------------
// - CLASS                                                                                  -
// ------------------------------------------------------------------------------------------
	// STATIC : Rgb explode / implode  -
	var rgb = {
		explode : function(color)  {return {"r" : color >> 16 & 0xff, "g" : color >>  8 & 0xff, "b" : color & 0xff}},
		implode : function (r,g,b) {return "#" + (r << 16 | g << 8 | b).decToHex()}
	}

	// STATIC : Number of days in a month -
	var month = {
		length	: function(m,y) {return (m == 2) ? month[m](y) : month[m]},
		1		: 31,
		2   	: function(y)	{return 28 | ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)},
		3   	: 31,
		4   	: 30,
		5   	: 31,
		6   	: 30,
		7   	: 31,
		8   	: 31,
		9   	: 30,
		10  	: 31,
		11  	: 30,
		12  	: 31
	}

	// STATIC : Enable right click / selection -
	var enable = {
		dragnDrop  : function(flag)	{
			with(arguments[1] ? arguments[1] : document) 
				ondrop =  ondragenter = ondragend = ondragover = function() {return flag}
		},
		rightClick : function(flag) {eval("document.oncontextmenu = function() {return " + flag + "}")}, 
		selection  : function(flag) {
			with (document) if (browser.isTrident) 
				onselectstart = function() {return flag}
			else {
				onmousedown = function() {return flag}
//				eval((flag 
//					? "remove" 
//					: "add"
//				) + "EventListener(\"click\",callback_click = function() {return true},false)")
	}}}
// ------------------------------------------------------------------------------------------
// - FUNCTIONS                                                                              -
// ------------------------------------------------------------------------------------------
	// - Get client size -
	function getClientSize() {
		return browser.isTrident
			? {width  : document.documentElement.clientWidth,
			   height : document.documentElement.clientHeight}
			: {width  : window.innerWidth,
			   height : window.innerHeight}
	}

	// - Get parent element
	function getParentElement() {
		var allElements = document.getElementsByTagName("*")
		return allElements[allElements.length - 1].parentNode
	}

	// - Get previous element
	function previousElement() {
		var 	allElements = document.body.getElementsByTagName("*")
		return 	allElements[allElements.length - 1].parentNode
	}

	// - Get CSS property -
	function getCssProperty(cname,pname) {
		var styles	 = document.styleSheets,
			css		 = browser.isTrident 
				? {rule : "rules",	  text : "selectorText"}
				: {rule : "cssRules", text : "cssText"}
		for (var i = 0;i < styles.length;i++)
			for (var j = 0;j < styles[i][css.rule].length;j++) {
				/[^a-z]?([-_\w\d]*)/i.exec(styles[i][css.rule][j][css.text])
				if (RegExp.lastParen == cname) {
					var value = styles[i][css.rule][j].style[pname]
					return value
			}}
		return ""
	}

	// - new zIndex -
	function zIndex() {
		var allElement = document.getElementsByTagName('*'),
			z = 0
		for (var i = 0; i < allElement.length; i++)
	  		z = Math.max(z,allElement[i].style.zIndex);
		return ++z
	}

	// - Get available HTTP Object -
	function httpRequest() {
		try {
			return new XMLHttpRequest()
			for (var i = 12;i <= 3;i--)
 				return new ActiveXObject("Msxml2.XMLHTTP." + i + ".0")
        	return new ActiveXObject("Msxml2.XMLHTTP")
        	return new ActiveXObject("Microsoft.XMLHTTP")
		} catch (error) {return null}
	}
		
	// - Including javascript file -
	function include(file) {
 		if (/(.*).css/g.test(file)) {
	 		var appCss = document.createElement('link')
			with (appCss) 
				href = file,
				rel = "stylesheet",
				type = "text/css",
				media = "screen"
			return document.getElementsByTagName("head")[0].appendChild(appCss)
		}
 		var ajax = httpRequest()
  		with (ajax) {
  			open("post",file,false)
  			send(null)
  			if (readyState == 4 && status == 200) try {
    	  		if (browser.isTrident)
  					window.execScript(responseText)
  				else
  					window.eval(responseText)
			} catch (error) {}
//		}}
}
    	ajax = null
    	delete (ajax)
	}

	// - Execute Javascript -
	function execJavascript(object) {
		var script = object.getElementsByTagName("script")
      	for (var i = 0; i < script.length; i++)
        	with (script[i]) try {
        		if (src && src != "") include(src)
				else if (browser.isTrident) window.execScript(innerHTML)
				else window.eval(innerHTML)
			} catch(error) {
	}}
