document.documentElement.className += " hasJS hideBeforeResize";
if (document.documentElement.getAttribute('lang')){
	document.documentElement.className += " "+document.documentElement.getAttribute('lang');
}

if(/MSIE 6/.test(navigator.userAgent)) {
	document.execCommand("BackgroundImageCache",false,true);
	document.documentElement.className += "  isIE isIE6";
}
else if(/MSIE 7/.test(navigator.userAgent)) document.documentElement.className += " isIE isIE7";
else if(/MSIE 8/.test(navigator.userAgent)) document.documentElement.className += " isIE isIE8";
else if(/Firefox\/2/.test(navigator.userAgent)) document.documentElement.className += " isFF2";
else if(/Firefox\/3/.test(navigator.userAgent)) document.documentElement.className += " isFF3";
else if(/AppleWebKit/.test(navigator.userAgent)) document.documentElement.className += " isWK";
if(/Chrome/.test(navigator.userAgent)) document.documentElement.className += " isGC";



var CONF = {
	heightStyle: /MSIE [56]/.test(navigator.userAgent) ? "height" : "minHeight",
	isQuirks : Browser.Engine.trident && document.compatMode ? true : false
}

/**
	@function
	@description Notifier est une classe autoinstancie. Elle permet de dispatcher les events front et back.
	De plus, elle gere une "console" permettant d'afficher des messages personnalises cote front
	
	FRONT:
		2 methodes public :
			- register(): pour associer une function a un evenement
			- fireEvent(): pour executer les function associe a un evenement
	
	BACK:
		le language server ecrit dans un cookie, un hashage d'evenements et de parametres,
		Notifier "ecoute" le cookie, et fire les events
	
	CONSOLE:
		si l'event est lie a un message situe dans un xml, on affiche le message dans la console
**/

var Notifier = {
	
	// TODO prevoir param
	cookieName : "messageManager",
	
	// array to store the events
	aEvent: [],
	
	// boolean, could be pass to true in a fireEvent function to simulate the cancelBubble on virtual event
	doBreak: false,
	
	/**
		@function
		@param target {node|object}: l'element sur lequel sera 'apply' l'appel a la fonction
		@param eventType {String}: le nom de l'event
		@param fn {function}: une fonction apply sur le target a execute a l'evenement
	
	**/
	register: function (target, eventType, fn){
		if(!this.aEvent[eventType]){ this.aEvent[eventType] = [];}
		this.aEvent[eventType].push({target:target, fn:fn});
	},
	
	/**
		@ignore
		TODO : a faire pour de vrai
	**/
	unregister: function (target, eventType, fn){
		if(!fn){
			this.aEvent[eventType] = null;
		} else {
			this.aEvent[eventType].each(
				function (evt, i){
					// console.log(this.aEvent[eventType][i]);
					// console.log(evt);
				}
			)
		}
	},
	
	/**
		@function 
		@param eventType {string} l'evenement a execute
	**/
	fireEvent: function (eventType){
		args = this.getArgs(arguments);
		//console.info("---  ",eventType, '==> ', args,'  ---');
		if(this.aEvent[eventType]){
			this.aEvent[eventType].each(
				function (event){
					// a called function could break the "bubbling" of the event
					if(Notifier.doBreak) {
						Notifier.doBreak = false;
						return;
					}
					event.fn.apply(event.target, args);
				}
			);
		}
	},
	
	/**
		@private recupere les arguments a passer 
	**/
	getArgs: function (aArgs){
		var $aArgs = [];
		for(var i=1, l=aArgs.length;i<l;i++){
			$aArgs.push(aArgs[i]);
		}
		return $aArgs;
	}
}



function cufonize(){
	// IE tricks
	// return;
	Cufon.now();
	/*
	
	//commented because of nav generation
	
	if(!/IE|FF2/.test(document.documentElement.className)){
		var navA = $$("#mainNav a").filter(function (a) {return !a.getParent('ul').hasClass("subNav")});
		Cufon.replace(navA, {fontFamily:"medium", hover:true, hoverables:{a:true, ul:true, li:false}});
	}
	*/
	
	
	
	var b = document.documentElement;
	var aForbidden = ["jp", "ja", "kn", "ko", "zh", "zt"];
	
	if(b.dir != "rtl" && aForbidden.indexOf(b.lang) == -1 ){
		Cufon.replace('.cufon-light', {fontFamily:"light"});//avenir 55 roman
		Cufon.replace('.cufon-medium', {fontFamily:"medium"});//avenir 66 medium
		Cufon.replace('.cufon-heavy', {fontFamily:"heavy"});//avenir 85 heavy
	}
	
	if( aForbidden.indexOf(b.lang) != -1 ){
		$$(".cufon-heavy, .cufon-medium, .cufon-light").each(function(el){
			el.setStyle("visibility", "visible");
		});
	}
	
	
	
}


var DOMBrowser = function(){
	/** @function DOMBrowser 
		@description objet de mapping du DOM
		@return object 
			
			DOM_Map = {
				ids : [...], // tableau d'elm possedant un id,
				div : {
					block : [...], // tableau contenant les div.block
					blockShadow : [...], // tableau contenant les div.blockShadow
					...
				},
				
				span : {
					bold : [...], // tableau contenant les span.bold
					blockShadow : [...], // tableau contenant les div.blockShadow
					...
				},
				...
			}
	*/
	DOM_Map = {
		ids: []
	};
	
	//
	var all = document.all || document.body.getElementsByTagName("*");
	var i=0, l=all.length;
	var el, tag, klass;
	
	// cree et/ou renvoie l'objet DOM_Map.div
	var pushTag = function (tag){
		if(!DOM_Map[tag]) DOM_Map[tag] = {};
		return DOM_Map[tag];
	}
	
	var pushElm = function (oTag, elm){
		var klasses = elm.className.split(' ');
		var j=0, k=klasses.length, klass;
		for(;j<k;j++){
			klass = klasses[j];
			if(!oTag[klass]) oTag[klass] = [];
			oTag[klass].push(elm);
		}
	}
	
	var pushId = function (elm){
		var id = "#" + elm.id;
		DOM_Map.ids[id] = elm;	
	}
	
	//trace.time('DOM_Map')
	for(;i<l;i++){
		el = all[i];
		if (el.id || el.getAttribute("id")) pushId(el);
		tag = pushTag(el.tagName.toLowerCase());
		pushElm(tag, el);
	}
	//trace.timeEnd('DOM_Map')
	return DOM_Map;
};

/**
	ANONYMOUS FUNCTION
*/
var getVStyle = function(elm) {
		elm = $(elm);
		var vStyle = 
			elm.getStyle("padding-bottom").toInt() +
			elm.getStyle("padding-top").toInt() +
			elm.getStyle("border-top-width").toInt() +
			elm.getStyle("border-bottom-width").toInt();
		return vStyle;
//		return CONF.isQuirks ? 0 : vStyle;
	}
var getHStyle = function(elm) {
		elm = $(elm);
		var hStyle = 
			elm.getStyle("padding-left").toInt() +
			elm.getStyle("padding-right").toInt() +
			elm.getStyle("border-left-width").toInt() +
			elm.getStyle("border-right-width").toInt();
		return hStyle;
		//return CONF.isQuirks ? 0 : hStyle;
	}
var getLabel = function (input){ 
	var labs = input.form.getElementsByTagName('label');
	for(var i = 0 ; i < labs.length; i++){
		var theFor = labs[i].getAttribute('for') || labs[i].getAttribute('htmlFor');
		if(theFor == input.id) {
			return labs[i];
		}
	}
}

/***
* 	@function switchDirection 
	@description permet de passer d'un mode de lecture gauche/droite a droite/gauche
**/
function switchDirection(){
	var b = document.documentElement;
	if(b.dir == "rtl"){
		b.className = b.className.replace(/rtl/, "ltr");
		b.dir = "ltr"
	}
	else {
		b.className = b.className.replace(/ltr/, "rtl");
		b.dir = "rtl"
	}
}

/** 
	@function getQueryString renvoie un objet representant la querySring ou la valeur d'une cle de la QS
	@param [key] {String} le nom de la variable GET recherchee
	@use 
		page.html?ln=fr&name=toto
			getQueryString(); //  {ln: "fr",name: "toto"}
			getQueryString("ln"); // "fr";
*/
function getQueryString(key){
	var $tmp = document.location.search.replace(/\?/, "").split("&");
	var qs = {}, sp;
	$tmp.each(function (kv){
		sp = kv.split("=");
		qs[sp[0]] = sp[1];
	})
	return key ? qs[key] : qs;
}


/**
	FOGMANGER: doc to be done ... 
*/
var fogManager = new Class({
	
	Implements: [Options, Events],

	options: {
		onStartOpen: $empty(),
		onCompleteOpen: $empty(),
		onStartClose: $empty(),
		onCompleteClose: $empty(),
		duration: 200,
		styles: {
			backgroundColor: "",
			opacity: "0.7"
		},
		locked: false,
		noAnimation: false,
		setTo: null,
		loading: false,
		keepClose: false,
		name : ""
	},
	
	
    initialize : function (element, options){
		this.setOptions(options)
		
		// ferme par defaut
		this.isOpen = false;
		this.ctnElement = element;
		// creation de la structure
		this.elm = new Element("div", {'class': 'fog hidden'});
		$(document.body).adopt(this.elm);
		this.elm.setStyles(this.options.styles);
		// au resize, on repositionne
		window.addEvent("resize", function (){
			if(!this.fogs) this.setToElm(this.lastElm);
		}.bind(this))
		
		this.addEvent('onCompleteClose', 
			function (){
				if(!this.elm) return;
				this.isOpen = false;
				this.elm.addClass("hidden");
				this.elm.dispose();
				this.elm = null;
				Object.reset(this);
			}
		)
		
		if(!this.options.locked) this.elm.addEvent('click', function (){this.close();}.bind(this));
		if(this.options.loading) this.elm.addClass("loading");
		if(!this.options.keepClose) this.open();
	},
    
    toggle: function(){
		this.isOpen == true ? this.close() : this.open(oOpts);
    },
    
    open: function (){
		
		this.setToElm();
		
		if(this.options.noAnimation) {
			this.elm.removeClass('hidden');
			this.fireEvent("onCompleteOpen", this.elm, $(this.options.setTo));
			return;
		}
		
		if(this.ctnElement){
			elm = $(this.ctnElement);
			
			elm.getElements(".layerClose").addEvent("click", function (e){e.stop();this.close();}.bind(this))
			
			// ajout de l'anim sur le ctn
			this.currentContent = elm;
			var myFx2 = new Fx.Tween(elm, {
				onStart: function (){
					this.element.removeClass("hidden");
					this.element.setStyle("marginLeft", -this.element.offsetWidth/2)
					this.element.setStyle("marginTop", -this.element.offsetHeight/2)
				},
				duration: this.options.duration
			});
			myFx2.set("opacity", "0");
			myFx2.start("opacity", "1");
		}
		else {
			this.currentContent = null;
		}
		
		var _self = this;
		// ouverture du fog
		var myFx = new Fx.Tween(this.elm, {
			onStart: function (){
				this.fireEvent("onStartOpen", this.elm, $(this.options.setTo));
				this.elm.removeClass("hidden");
				
			}.bind(this),
			onComplete: function (){
				this.fireEvent("onCompleteOpen", this.elm, $(this.options.setTo));
				this.isOpen = true;
			}.bind(this),
			duration: this.options.duration
		});
		myFx.set("opacity", "0");
		myFx.start("opacity", this.options.styles.opacity);
		
	},
	
	close: function (){
		if(this.options.noAnimation) {
			this.fireEvent("onCompleteClose", this.elm, $(this.options.setTo));
			return;
		}
		// fermeture du fog
		var myFx = new Fx.Tween(this.elm, {
			onStart: function (){
				this.fireEvent("onStartClose", this.elm, $(this.options.setTo));
			},
			onComplete: function (){
				this.fireEvent("onCompleteClose", this.elm, $(this.options.setTo));
			}.bind(this),
			duration: this.options.duration + 10
		});
		myFx.set("opacity", this.options.styles.opacity);
		myFx.start("opacity", "0");
		
		if(this.ctnElement){
			var myFx2 = new Fx.Tween($(this.ctnElement), {
				onStart: function (){}.bind(this),
				onComplete: function (){
				}.bind(this),
				duration: this.options.duration
			});
			myFx2.set("opacity", "1");
			myFx2.start("opacity", "0");
		}
	},
	
	setToElm: function (){
		
		if(this.options.setTo && $(this.options.setTo)){
			var coo = $(this.options.setTo).getCoordinates();
			this.elm.setStyles({
				"width": coo.width,
				"height": coo.height,
				"top": coo.top,
				"left": coo.left})
		}
		else {
			var coo = $(document.body).getCoordinates();
			//console.log(this.options.styles.width)
			if(!this.options.styles.width || !this.options.styles.width === 0) this.elm.setStyle('width', document.body.scrollWidth)
			if(!this.options.styles.height ||!this.options.styles.height === 0) this.elm.setStyle('height', document.body.scrollHeight)
			if(!this.options.styles.top ||!this.options.styles.top === 0) this.elm.setStyle('top', 0)
			if(!this.options.styles.left ||!this.options.styles.left === 0) this.elm.setStyle('left', 0)
		}
	}
});


/* extension tool tip pour gestion corners */

var CustomTip = new Class({
	Extends : Tips,
	getTip : function(){
		return new Element('div', {
			'class': this.options.className,
			styles: {
				visibility: 'hidden',
				display: 'none',
				position: 'absolute',
				top: 0,
				left: 0
			}
		}).adopt(
			new Element('span', {'class': 'tipArrow'}),
			new Element('div', {'class': 'tip-top'}).adopt(
				new Element('div').set('html','&nbsp;')
			),
			this.container,
			new Element('div', {'class': 'tip-bottom'}).adopt(
				new Element('div').set('html','&nbsp;')
			)
		).inject(document.body);
	},
	position: function(event){
		var size = window.getSize(), scroll = window.getScroll(),
			tip = {x: this.tip.offsetWidth, y: this.tip.offsetHeight},
			props = {x: 'left', y: 'top'},
			obj = {};
		
		for (var z in props){
			obj[props[z]] = event.page[z] + this.options.offset[z];
			if ((obj[props[z]] + tip[z] - scroll[z]) > size[z]) obj[props[z]] = event.page[z] - this.options.offset[z] - tip[z];
		}
				
		if (this.options.heightDiff){
			obj.top -= this.tip.offsetHeight;
		}
		
		this.tip.setStyles(obj);
	}
})

/* toggle générique */
var Toggler = new Class({
	initialize: function(elm, handlers, classToToggle){
		var _this = this;
		this.elm = elm;
		this.handlers = handlers;
		this.classToToggle = classToToggle;
		this.handlers.each(function(handler){
			handler.addEvent('click', function(e){
				new Event(e).stop();
				_this.elm.toggleClass(_this.classToToggle);
				Notifier.fireEvent("onLayoutChanged");
			})
		});
		
	}	
});

WebSiteResizer = new Class({
	initialize : function(){
		/* ajout evenement resize */
		Notifier.register(this, "onLayoutChanged", this.setColumns);
		//enregistrement des elements DOM à resizer
		this.leftCol = $('leftCol');
		this.main = $('main');
		this.centerCol = $('mainInside');
		this.rightCol = $('rightColMDR');
		this.content = $('content');
		this.leftColgv = this.leftCol ? getVStyle(this.leftCol) : 0;
		this.centerColgv = this.centerCol ? getVStyle(this.centerCol) : 0;
		//trick pour certains cas particuliers
		if (this.centerCol){
			this.lastBlock = this.centerCol.getElement('.blockToResize');
		}

		/* lancement init */
		this.setColumns();
	},
	setColumns : function(){
		var leftPartHeight = null;
		if (this.centerCol){
			this.centerCol.style[CONF.heightStyle] = 0;
		}
		if (this.leftCol){
			this.leftCol.style[CONF.heightStyle] = 0;
		}
		if (this.leftCol && this.centerCol && this.leftCol.offsetHeight != this.centerCol.offsetHeight){			
			if (parseInt(this.leftCol.offsetHeight) < this.centerCol.offsetHeight){
				this.leftCol.style[CONF.heightStyle] = this.centerCol.offsetHeight - this.leftColgv + 'px'
			}
			if (parseInt(this.leftCol.offsetHeight) > this.centerCol.offsetHeight){
				this.centerCol.style[CONF.heightStyle] = this.leftCol.offsetHeight - this.centerColgv + 'px'
			}
		}
		if (!this.main) return;
		leftPartHeight = parseInt(this.main.offsetHeight);

		if (this.rightCol && leftPartHeight < this.rightCol.offsetHeight){
			if (this.leftCol){
				this.leftCol.style[CONF.heightStyle] = this.rightCol.offsetHeight - Math.abs(this.rightCol.getTop() - this.leftCol.getTop()) - this.leftColgv  + parseInt(100) + 'px';
			}
			
			if(this.centerCol){
				this.centerCol.style[CONF.heightStyle] = this.rightCol.offsetHeight - Math.abs(this.rightCol.getTop() - this.centerCol.getTop()) - this.centerColgv /*+ parseInt(3)*/ + 'px';
				
			}
		}
			
		if (this.lastBlock){
			var body = this.lastBlock.getElement('.body');
			//body.style[CONF.heightStyle] = 'auto';
			
			if(this.centerCol.offsetHeight > this.rightCol.offsetHeight){
				if(this.centerCol){
					body.style[CONF.heightStyle] = this.rightCol.offsetHeight - Math.abs(this.centerCol.getTop() - this.lastBlock.getTop()) - getVStyle(body) - getVStyle(body.getParent()) + 'px';
				}
			}
			else
			{		
				body.style[CONF.heightStyle] = this.centerCol.offsetHeight - Math.abs(this.centerCol.getTop() - this.lastBlock.getTop()) - getVStyle(body) - getVStyle(body.getParent()) - getVStyle(this.lastBlock) + 'px';
			}
		}
	}
});







/* trace; little console for debugging (like console.log(for firefox)); */
function trace() {
	if (trace.debug == true) {
		if (window.console && !trace.forall && !document.all) {
			console.log.apply(console, arguments);
		}
		else {
			var debugConsole = document.getElementById('debugConsole');
			if (!debugConsole) {
				debugConsole = document.createElement('div');
				debugConsole.id = 'debugConsole';
				with (debugConsole.style) {
					top = 0;right = 0;position = 'absolute';
					width = '200px';height = '200px';
					overflow = 'auto';
					background = '#fff'; border = '1px solid #000';
					zIndex = 9999999;
				}
				document.body.appendChild(debugConsole);
				setInterval(function(){
					debugConsole.style.top = document.documentElement.scrollTop + 'px';
				}, 50);
			}
			var str = Array.prototype.slice.call(arguments).join(' ').replace(/</g, '&lt;').replace(/>/g, '&gt;');
			debugConsole.innerHTML += str + '<hr/>';
			debugConsole.scrollTop = debugConsole.scrollHeight;
		}
	}
}
trace.activate = function(all) {
	if (all)
		trace.forall = true;
	trace.debug = true;
}

//trace.activate();

