/* inputValue : function
	Permet de mettre le texte par defaut d'un champ de type texte.
*/ 
function inputValue(elm, state) {
	elm.oldValue=elm.value;
	elm.onfocus=function() {
		if (!this.isChecking && this.value==this.oldValue) this.value='';			
	};
	elm.onblur=function() {
		if(this.value=='') this.value=this.oldValue;
	};
	if (!elm.isChecking) elm.onfocus();
};

function forceCheck(elm, linkedInput) {
	linkedInput = typeof linkedInput=="string" ? document.getElementById(linkedInput) : linkedInput;
	linkedInput.checked = elm.checked;
};

/**************
/* generates corners and others elements if needed 
**************/
function generateElements(parent, stringClasses) {
    var i, x;
	parent = (typeof parent == "string") ? document.getElementById(parent) : parent;
	var content = parent || document.body;
	var div = content.getElementsByTagName("div");
	var span = content.getElementsByTagName("span");

	
	//recupere un node avec la class blockInsideParDefaut
	function getIsd(node, className) { return getNode(node, {className: (className || "blockInside")})};
	
	// fonction de creation d'un coin (b avec className) 
	function nc(clN) {var b = document.createElement("span");b.className=clN;return b;};
	
	//ajoute un element ou une liste d'elements (c) sur l'element x
	function add(x, c) {
		var i=0; if (!x) return; 
		if (c.length) for (i=0; i<c.length; i++) x.appendChild(c[i].cloneNode(true));
		else x.appendChild(c.cloneNode(true));
	};
	
	//-- creation des elements qui seronts clones --
	var corners = [nc("tl"), nc("tr"), nc("bl"), nc("br")]; //corners 
	var shadow = nc("specialShadow"); add(shadow, [nc("lt"), nc("rt"), nc("trame")]); // specialShadow
	var overtl = nc("overtl"); // overtl : coin arrodis supplementaire pour les blocks avec des bordures speciales
	
	
	// -- creation des coins ou autres elements -- 
	// parcours des divs pour leur rajouter les corners
	for (i=div.length-1; i>=0; i--) {
		x=div[i];
		if (!x.alreadyProcessed) {
			if (x.className.match(/\bblock\b/)) //block par defaut		
				add(getIsd(x), corners);
			if (x.className.match(/\bcaption\b/)) // tableau avec coin arrondi
				add(x, corners);
			if (x.className.match(/\bspecialShadow\b/)) // block avec ombre dessous
				add(x, shadow);
			if (x.className.match(/\bblockToggle|blockShadowSimple\b/)) { //blocks avec coins absolus sur tous les cotes.
				add(getIsd(x), overtl);
			};
			x.alreadyProcessed = true;	
			initOtherBlocks(x); // fonction d'initialisation d'autres blocks
		};
	};
	for (i=span.length-1; i>=0; i--) {
		x=span[i];
		if (!x.alreadyProcessed) {
			if (x.className.match(/\bcaption\b/)) // tableau avec coin arrondi
			{				
				add(x, corners);
			}
			x.alreadyProcessed = true;	
		};
	};
	fixColumns();
};
/* initOtherBlocks() : fonction rajoute d'autres fonctionnalites sur differents blocks  (toggle, onglets),
   Cette fonction est forcement lancee depuis generateElements, et cela evite de faire une deuxieme passe sur les divs de la page
 */
function initOtherBlocks(x) {
	// block a onglets
	if (x.className.match(/\bblockTabs(Sub)?\b/)) //block d'onglets en general (gere tous types d'onglets).
		tabs.init(x);
	if (x.className.indexOf("blockToggle")!=-1) //block ouvert/ferme
		toggleBlock.init(x);
	if (x.className.indexOf("scrollH")!=-1)  //block de scroll horizontal
		scroll.init(x);
	if (x.className.match(/\bline\b/)) //si ligne de block, on la stocke dans un tableau
		linesOfBlocks.push(x);
};


/**************
* sizeBlocks : alignement des blocks en hauteurs
**************/
var linesOfBlocks=[];
function sizeBlocks(parentBlock) {
	function size(block, size){
		if (block){
			var body = block.className.match(/\bblock\b/) ? getNode(block, {nodeName:"div", className:"body"}) : block; //si on a une line ou bien un block
			if (body) body.style[heightPropertyToUse] = body.offsetHeight + size - getVStyles(body) + "px";
		};
	};
	linesOfBlocks.eachInv(function () { //les lignes fournies sont
		var line = this;
		var units = getChildNodes(line, {className:"unit"});
		units.each(function() {
			var unit = this;
			var blocks = getChildNodes(unit, {className:"(block|line)"}, {className:"noresize"});
			var sizeToApply = line.offsetHeight-unit.offsetHeight;
			var sizePerBlock = parseInt(sizeToApply/blocks.length);
			blocks.each(function() {
				size(this, sizePerBlock);
			});
			//sur une division on tombe parfois sur un calcul pas precis, on resize le dernier element d'un unit, afin que le calcul soit correct
			if (blocks.length>1) size(blocks.last(), line.offsetHeight-unit.offsetHeight);
		});
	});
};

/**************
* sizeContent : alignement des contenus
**************/
var contentsArray=[];
function contentAdd(ContainerAttr, childNodeAttr, numberPerLines) {
	contentsArray.push({container:ContainerAttr, child:childNodeAttr, number:numberPerLines});
};
function contentSize() {
	contentsArray.each(function() {
		var attr = this;
		var containers = getNodes(document, attr.container);
		containers.each(function() {
			var childs = getNodes(this, attr.child);
			var lineBreak = attr.number;
			var maxH = 0, count=1, lineArray=[];
			function sizeElements() {
				lineArray.each(function() {
					this.style[heightPropertyToUse] = maxH - getVStyles(this) + "px";
				});
				lineArray=[];
				count=0;
				maxH=0;
			};
			childs.each(function() {
				if (count==1){
					this.parentNode.style.clear="left";
				};
				if (this.offsetHeight>maxH) maxH = this.offsetHeight;
				lineArray.push(this);
				if (count>=lineBreak) {
					sizeElements();
				};
				count++;
			});
			sizeElements();
		});
	});
};

/***************
* fixColumns()
****************/
function fixColumns() {
	function fix() {
		var colonnes = ['main','rightColumn'],
			colonnesInside = ["", ""];
		var hMax=0, i, b, minMax=0, sum=0; hToU = heightPropertyToUse;
		
		function each(f) { //fonction d'iteration
			for (i=0; i<colonnes.length; i++){
				b = $(colonnes[i]);
				f();
			};
		};
		
		each(function() {
			sum=0;
			if (b) {
				getChildNodes(b, {}).each(function() { sum+=this.offsetHeight });
				if (sum>minMax) minMax=sum;
			};
		});
		// on remet la hauteur par defaut a toutes les colonnes (hauteur minimum)
		each(function() {
			var bToSize = $(colonnesInside[i]) || b;
			if (bToSize) bToSize.style[hToU] = minMax + "px";
		});
		// on recupere la hauteur la plus grande
		each(function() {
			if (b){
				if (b.offsetHeight + intStyle(b, "margin-top") >= hMax) {
					hMax = b.offsetHeight - intStyle(b, "margin-top");
				};
			};
		});
		// on applique la nouvelle hauteur sur les colonnes
		each(function() {
			var bIsd = $(colonnesInside[i]);
			if (b && bIsd) {
				var diff = (hMax - b.offsetHeight) + bIsd.clientHeight - getVStyles(bIsd);
				bIsd.style[hToU] = diff + 'px';
			} else if (b) {
				getVStyles(b);
				b.style[hToU] = hMax  - getVStyles(b) - intStyle(b, "margin-top") + 'px';
			};
			
		});
	};
	setTimeout(fix,1);
};

function initDate(){
	var a = new Date();
	var days = a.getDate();
	var month = a.getMonth();
	var year = a.getFullYear();
	
	
	if(days.toString().length ==1) days = '0'+days;
	if(month.toString().length ==1) month = '0'+month;
	
	if ($('theDate')){
		$('theDate').value = days+"/"+month+"/"+year;
	};
};

function todaysDate(){
	if ($('theDate')){
		var a = new Date();
		var days = a.getDate();
		var month = (a.getMonth()).toInt()+1;
		var year = a.getFullYear();
		
		if(days.toString().length ==1) days = '0'+days;
		if(month.toString().length ==1) month = '0'+month;
		
		
		$('theDate').value = days+"/"+month+"/"+year;
	};
};

/***********
* Init
************/
//initialisation de certains elements (contenus à aligner);
// ex : contentAdd({nodeName:"ul", className:"list2cols"}, {nodeName:"div", className:"listCtn"}, 2);

//fonction lancee pendant le chargement de la page
window.addEvent('domready',function(){
	generateElements();
	todaysDate();
});
//fonction lancee une fois toute la page chargee
window.addEvent('load',function(){
	contentSize();
	//sizeBlocks();
	fixCorners();
	fixHeights();
	fixColumns(); //oblige pour laisser au navigateur le temps de se rafraichir
});

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}





