/* JavaScript part of Max-Hebergs (www.max-hebergs.net). **
** Created by Loic Percetti, copyright Max-Hebergs, all rights reserved. **
** Reproduction without written autorization is prohibited. */

var inputs;
var imgFalse = 'media/images/checkbox_off.jpg';
var imgTrue = 'media/images/checkbox_on.jpg';

// Cette fonction se lancera quand la page sera chargée, mettez le reste du code ici
function init() {
	replaceChecks();
	infobulles('info', 200);
}

/* REPLACE-CHECKBOX */

function replaceChecks() {
	// Listage des inputs.
	inputs = document.getElementsByTagName('input');

	for(var i=0; i < inputs.length; i++) {
		// Vérificiation si checkbox
		if(inputs[i].getAttribute('type') == 'checkbox') {
            
			// Création de l'image.
			var img = document.createElement('img');

			// Vérifier et importer les onchange actuels
			if(inputs[i].onchange)
				elems = '; '+inputs[i].getAttribute("onchange");
			else
				elems = '';
            
			// Vérifier si le checkbox est actif
			if(inputs[i].checked)
				img.src = imgTrue;
			else
				img.src = imgFalse;

			// Mise en place d'un ID à l'input
			img.id = 'checkImage'+i;

			// Mise en place de l'image
			img.onclick = new Function ('checkChange('+i+')'+elems);

			// Placage de l'image à l'avant du checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
            
			// Masquer l'input
			inputs[i].style.display = 'none';
		}
	}
}

// Modifie le statut de l'input et son image.
function checkChange(i) {
	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalse;
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;
	}
}


/* INFO-BULLE */

function infobulles(infobulleClass, infobulleWidth, tags) {
	var theinfobulleWidth = (infobulleWidth+"px") || ("200px");
	
	switch (typeof(tags)) {
		case "string":
			tags = tags.replace(/\s+/, '').split(',');
			break;
		case "object": // array
			if ( tags.length > 0 )
				break;
		default:
			tags = new Array('a', 'span');
	}
	
	var links,i,j,tl,l,h,spans;

	if(!document.getElementById || !document.getElementsByTagName) return;

	h = document.createElement("div");
	h.id = "btc";
	h.setAttribute("id","btc");
	h.style.position = "absolute";
	document.getElementsByTagName("body")[0].appendChild(h);

	for(i = 0, tl = tags.length; i < tl; i++) {
		var elements = document.getElementsByTagName(tags[i]);
		
		for (j = 0, l = elements.length; j < l; j++) {
			if(elements[j].className.indexOf(infobulleClass) != -1)
				Prepare(elements[j], theinfobulleWidth);
		}
	}
}

function Prepare(el, width){
	el.infobulleWidth = width;
	el.getinfobulle = getinfobulle;
	el.onmouseover = showinfobulle;
	el.onmouseout = hideinfobulle;
	el.onmousemove = Locate;
	if (el.getAttribute('title')) 
		el.removeAttribute('title');
}

var _infobulleTop, _infobulleBottom;

function getinfobulle() {
	if(this.infobulle)
		return this.infobulle;

	var infobulle, infobulleTitle, infobulleTopLeft, infobulleTopRight, infobulleContent, infobulleBottomLeft, infobulleBottomRight;

	var el = this;
	/* On affiche pas les alt des éléments enfants de type image */
	if (el.hasChildNodes()) {
		var childs = el.childNodes;
		for (var i=0, len=childs.length; i<len; i++) {
			if (childs[i].nodeName == 'IMG') {
				if(childs[i].getAttribute("alt"))
					childs[i].removeAttribute("alt");
			}
		}
	}

	/* Création de l'élément principal de l'infoBulle */
	infobulle = document.createElement("div");
	infobulle.className = "infobulle";

	/* Partie supérieure */
	if (!_infobulleTop) {
		_infobulleTop = document.createElement("div");
		_infobulleTop.className = "infobulleTop";

			/* Arrondi supérieur à gauche */
			infobulleTopLeft = document.createElement("div");
			infobulleTopLeft.className = "tootltipTopLeft";
			_infobulleTop.appendChild(infobulleTopLeft);

			/* Arrondi supérieur à droite */
			infobulleTopRight = document.createElement("div");
			infobulleTopRight.className = "tootltipTopRight";
			_infobulleTop.appendChild(infobulleTopRight);
	}

	/* Contenu texte du infobulle */
	infobulleContent = document.createElement("div");
	infobulleContent.className = "infobulleContent";
	
	if (typeof(el.nextSibling) != 'undefined' && el.nextSibling.className == "infobulleContenu") {
		infobulleContenu = el.nextSibling;

		/* Ne pas afficher de tip vide */
		if (!/\S/.test(infobulleContenu.innerHTML))
			return;

		infobulleContenu.style.position = "static";
		infobulleContent.appendChild(infobulleContenu);
	} else {
		infobulleTitle = el.getAttribute("title");
		if(infobulleTitle == null || infobulleTitle.length == 0) { return; } else { el.removeAttribute("title"); }
		infobulleContent.appendChild(document.createTextNode(infobulleTitle));
	}

	/* Partie inférieure */
	if ( !_infobulleBottom ) {
		_infobulleBottom = document.createElement("div");
		_infobulleBottom.className = "infobulleBottom";

		/* Arrondi inférieur à gauche */
		infobulleBottomLeft = document.createElement("div");
		infobulleBottomLeft.className = "infobulleBottomLeft";
		_infobulleBottom.appendChild(infobulleBottomLeft);

		/* Arrondi inférieur à droite */
		infobulleBottomRight = document.createElement("div");
		infobulleBottomRight.className = "infobulleBottomRight";
		_infobulleBottom.appendChild(infobulleBottomRight);
	} 


	/* Construction du infobulle */
	infobulle.appendChild(_infobulleTop.cloneNode(true));
	infobulle.appendChild(infobulleContent);
	infobulle.appendChild(_infobulleBottom.cloneNode(true));
	infobulle.style.width = el.infobulleWidth;

	this.infobulle = infobulle;

	return this.infobulle;
}

function showinfobulle(e) {
	var d = document.getElementById("btc");

	if (!d)
		return;

	var tip = this.getinfobulle();
	if (!tip)
		return;
	
	d.appendChild(tip);
	Locate(e);
}

function hideinfobulle(e) {
	var d = document.getElementById("btc");

	if (d && d.childNodes.length > 0)
		d.removeChild(d.firstChild);
}

function setOpacity(el) {
	el.style.filter = "alpha(opacity:95)";
	el.style.KHTMLOpacity = "0.95";
	el.style.MozOpacity = "0.95";
	el.style.opacity = "0.95";
}

function Locate(e) {
	var posx=0, posy=0;
	var btc = document.getElementById("btc");

	if (!btc)
		return;

	if (e == null) {
		e = window.event;		
	}
	if (e.pageX || e.pageY) {
    		posx = e.pageX;
		posy=e.pageY;
	} else if (e.clientX || e.clientY) {
		if(document.documentElement.scrollTop) {
			posx = e.clientX+document.documentElement.scrollLeft;
			posy = e.clientY+document.documentElement.scrollTop;
		} else {
			posx = e.clientX+document.body.scrollLeft;
			posy = e.clientY+document.body.scrollTop;
		}
	}
	btc.style.top = (posy+10) + "px";
	btc.style.left = (posx-20) + "px";
}

/* INDICATEUR DE MOT DE PASSE */

function pass_secure(password) {
	secu = 0;

	if(password.match(/[a-z]/))
		secu++;
	if(password.match(/\d+/))
		secu++;
	if(password.match(/(\d.*\D)|(\D.*\d)/))
		secu+=2;
	if(password.match(/(.*[0-9].*[0-9].*[0-9])/))
		secu++;
	if(password.match(/(.*[a-z].*[a-z].*[a-z])/))
		secu++;

	if(password.length <= 5)
		secu++;
	else
		if(password.length <= 6)
			secu+=2;
		else
			if(password.length <= 8)
				secu+=3;
			else
				secu+=4;

	secu = secu*10;

	if(password.length == 0)
		secu = 0;

	colorg = Math.round(secu*5.1);
	colorr = 500-colorg;

	if(colorr < 0)
		colorr = 0;
	if(colorr > 255)
		colorr = 255;
	if(colorg < 0)
		colorg = 0;
	if(colorg > 255)
		colorg = 255;

	document.getElementById('chkpass').style.backgroundColor = "rgb("+colorr+","+colorg+",16)";
	document.getElementById('chkpass').style.width = secu+"%";
}

/* KONAMI */

if(window.addEventListener) {
	var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
	window.addEventListener("keydown", function(e) {
		kkeys.push(e.keyCode);
		if(kkeys.toString().indexOf(konami) >= 0)
			document.write('<strong>D&eacute;couvrez Max-Hebergs ..</strong><br /><br /><object id="MediaPlayer" height="0" width="0" classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112" style="visibility:hidden;"><param name="filename" value="media/Max-Hebergs.wma"><param name="Showcontrols" value="True"><param name="autoStart" value="True"><embed type="application/x-mplayer2" src="media/Max-Hebergs.wma" name="MediaPlayer" height="0" width="0"></embed></object>');
	}, true);
}

window.onload = init;

