// Extension to Prototype.Browser
Prototype.Browser.getDimensions=function() {
	if (document.documentElement && document.documentElement.scrollTop) {
		t=document.documentElement.scrollTop;
		l=document.documentElement.scrollLeft;
		w=document.documentElement.scrollWidth;
		h=document.documentElement.scrollHeight;

	} else if (document.body) {
		t=document.body.scrollTop;
		l=document.body.scrollLeft;
		w=document.body.scrollWidth;
		h=document.body.scrollHeight;
	}

	iw=self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0;
	ih=self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;

	return {
		offsetX: l,
		offsetY: t,
		width: w,
		height: h,
		clientWidth: iw,
		clientHeight: ih
	};
}

Prototype.Browser.version=parseFloat(navigator.appVersion);

// DomReady event
Prototype.Browser.loaded=false;
Prototype.Browser.onLoadCallBack=new Array();

Prototype.Browser.onLoad=function (fnCallback) {
	if (Prototype.Browser.loaded) {
		fnCallback();

	} else {
		Prototype.Browser.onLoadCallBack.push(fnCallback);
	}
}

Prototype.Browser.domReady=function () {
	if (Prototype.Browser.loaded) {
		return;
	}

	Prototype.Browser.loaded=true;

	for (iIndex=0; iIndex<Prototype.Browser.onLoadCallBack.length; iIndex++) {
		Prototype.Browser.onLoadCallBack[iIndex]();
	}

	Prototype.Browser.onLoadCallBack.clear();
}

Event.observe(window, 'load', Prototype.Browser.domReady);
Event.observe(document, 'DOMContentLoaded', Prototype.Browser.domReady);
document.observe('dom:loaded', Prototype.Browser.domReady);

// Add OverlappingFix class (base from Shim class of Rico) what allow element to be over form input
var OverlappingFix=Class.create();

if (Prototype.Browser.IE) {
	OverlappingFix.addMethods({
		oIFrame: null,

		initialize: function() {
			this.oIFrame=new Element('iframe', {src: "javascript:false;", frameborder: 0, scrolling: 'no'});
			this.oIFrame.setStyle({
				position: 'absolute',
				filter: 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)'
			});
			this.oIFrame.hide();
			var oBody=document.getElementsByTagName("body")[0];
			oBody.appendChild(this.oIFrame);
		},

		hide: function() {
			this.oIFrame.hide();
		},

		show: function(oTargetElement) {
			// Laisse ce code ici au cas ou le fix dans prototype est supprime
			try {
				this.oIFrame.clonePosition(oTargetElement);
				this.oIFrame.setStyle({
					zIndex: this.getTargetIndex(oTargetElement)-1,
					display: "block"
				});
			} catch(e) {
			}
		},

		update: function(oTargetElement) {
			oTargetElement=$(oTargetElement);
			var oPosition=oTargetElement.cumulativeOffset();

			this.oIFrame.setStyle({
				width: oTargetElement.offsetWidth,
				height: oTargetElement.offsetHeight,
				top: oPosition.top,
				left: oPosition.left,
				zIndex: this.getTargetIndex(oTargetElement)-1
			});
		},

		remove: function() {
			this.oIFrame.remove();
		},

		getTargetIndex: function (oTargetElement) {
			if (oTargetElement.getStyle('z-index')) {
				return oTargetElement.getStyle('z-index');

			} else if (oTargetElement.parentNode) {
				return this.getTargetIndex(oTargetElement.parentNode);
			}

			return 0;
		}
	});

} else {
	OverlappingFix.addMethods({
		initialize: function() {},
		hide: function() {},
		show: function() {},
		remove: function() {},
		update: function() {}
	});
}

Prototype.Browser.activateFlashOverlappingFix=function() {
	if (Prototype.Browser.Gecko && navigator.userAgent.indexOf('Linux')>-1) {
		OverlappingFix.addMethods({
			oIFrame: null,

			initialize: function() {
				this.oIFrame=new Element('iframe', {src: "javascript:false;", frameborder: 0, scrolling: 'no', allowTransparency: "true"});
				this.oIFrame.setStyle({
					position: 'absolute',
					opacity: 0
				});
				this.oIFrame.hide();
				var oBody=document.getElementsByTagName("body")[0];
				oBody.appendChild(this.oIFrame);
			},

			hide: function() {
				this.oIFrame.hide();
			},

			show: function(oTargetElement) {
				this.oIFrame.clonePosition(oTargetElement);
				this.oIFrame.setStyle({
					zIndex: this.getTargetIndex(oTargetElement)-1,
					display: "block"
				});
			},

			update: function(oTargetElement) {
				oTargetElement=$(oTargetElement);
				var oPosition=oTargetElement.cumulativeOffset();

				this.oIFrame.setStyle({
					width: oTargetElement.offsetWidth,
					height: oTargetElement.offsetHeight,
					top: oPosition.top,
					left: oPosition.left,
					zIndex: this.getTargetIndex(oTargetElement)-1
				});
			},

			remove: function() {
				this.oIFrame.remove();
			},

			getTargetIndex: function (oTargetElement) {
				if (oTargetElement.getStyle('z-index')) {
					return oTargetElement.getStyle('z-index');

				} else if (oTargetElement.parentNode) {
					return this.getTargetIndex(oTargetElement.parentNode);
				}

				return 0;
			}
		});
	}
}

/**
	Load a JS dynamically but this require that object does not have "var" before Class definition (find a solution to fix it automatically)
*/
Prototype.require=function(scriptName) {
	// check if script has not been already include
	var aScripts=$$('SCRIPT[src*="' + scriptName + '"]');

	if (!aScripts.length) {
		// search path of js
		aScripts=$$('SCRIPT[src*=jojoPrototype.js]');
		scriptPath='';

		if (aScripts.length) {
			iOffset=aScripts[0].src.indexOf('jojoPrototype.js');
			scriptPath=aScripts[0].src.substring(0, iOffset);
		}

//		document.write('<script type="text/javascript" src="' + scriptPath + scriptName + '"><\/script>');
		oScript=new Element('script', {type: 'text/javascript"', src: scriptPath + scriptName});
		aRoot=$$('HEAD,BODY,HTML');

		if (aRoot.length) {
			aRoot[0].appendChild(oScript);
			new Ajax.Request(scriptPath + scriptName, {
				evalScripts: false,
				asynchronous: false,
				onSuccess: function(transport) {
					oScript.innerText=transport.responseText;
				}
			});
		}
	}
}