/* -------- extrait licence --------
	MaBulle.com (c) 2005-2008
	 Version: primipile 3.0
--------------------------------- */
/* -- submits recherche + newsletter -- */
function clearData(Qid,Qsbm,Qval) {
	var Qbox = document.getElementById(Qid);
	if (Qbox.value == Qval) {
		Qbox.value = '';
		enableQbut(Qsbm);
	}
}
function checkData(Qid,Qsbm,Qval) {
	var Qbox = document.getElementById(Qid);
	var Qbut = document.getElementById(Qsbm);
	var estVide = Qbox.value == null || Qbox.value == '' || Qbox.value == Qval;
	if (Qbut.disabled == false && estVide) {
		disableQbut(Qsbm);
	} else if (Qbut.disabled == true && !estVide) {
		enableQbut(Qsbm);
	}
}
function resetData(Qid,Qsbm,Qval) {
	var Qbox = document.getElementById(Qid);
	if (Qbox.value == null || Qbox.value == '') {
		Qbox.value = Qval;
		disableQbut(Qsbm);
	}
}
function enableQbut(Qsbm) {
	var Qbut = document.getElementById(Qsbm);
	Qbut.disabled = false;
	Qbut.style.cursor = 'pointer';
}
function disableQbut(Qsbm) {
	var Qbut = document.getElementById(Qsbm);
	Qbut.disabled = true;
	Qbut.style.cursor = 'default';
}
/* -- navigation -- */
var nav = {
	init : function() {
		if (!$('nav')) {return false;}
		this.menu = $('nav');
		this.buttons = [];
		$A(this.menu.getElementsByTagName('li')).each(
			function(item) {
				if (item.parentNode == this.menu) {
					this.buttons.push($(item));
				}
			}.bind(this)
		);
		this.buttons.each(
			function(button) {
				Event.observe(button,'mouseover',this.expand.bindAsEventListener(this));
				Event.observe(button,'mouseout',this.collapse.bindAsEventListener(this));
			}.bind(this)
		);
		return true;
	},
	expand : function(event) {
		var button = this.findButton(event.target || event.srcElement);
		if (!button.hasClassName('visu')) {
			button.addClassName('visu');
		}
	},
	collapse : function(event) {
		var button = this.findButton(event.target || event.srcElement);
		button.removeClassName('visu');
	},
	findButton : function(element) {
		var button = false;
		while (element.parentNode) {
			if (this.buttons.indexOf(element) != -1) {
				button = element;
			}
			element = element.parentNode;
		}
		return button;
	}
}
/* -- impression -- */
function okPrint() {
	var url = document.location.href;
	if (url.indexOf('#') > 0 ) {
		var cUrl = url.split('#');
		var fUrl = cUrl[0];
	} else {
		var fUrl = url;
	}
	if (fUrl.indexOf('?') > 0) {
		fUrl += '&print=true';
	} else {
		fUrl += '?print=true';
	}
	window.open(fUrl,'Impression','toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1');
}
function exePrint() {
	window.print();
}
/* -- reseau social -- */
var soNet = {
	init : function() {
		this.share = $('soNet');
		this.caller = '';
		this.newTimer = null;
		if (this.share != null) {
			Event.observe('soNet','mouseover',function() {soNet.showObj();return false;},false);
			Event.observe('soNet','mouseout',function() {soNet.initHide();return false;},false);
		}
		this.showIt = function() {soNet.showObj();return false;};
	},
	showObj : function() {
		if (this.share != undefined) {
			if (this.newTimer != null) {clearTimeout(this.newTimer)}
			this.share.setStyle({
				visibility : 'visible'
			})
		}
	},
	initHide: function(target) {
		if (target) this.caller = $(target.id);
		this.newTimer = setTimeout('soNet.hideObj()',250);
	},
	hideObj: function(target) {
		if (this.share != undefined) {
			this.share.setStyle({
				visibility : 'hidden'
			})
			this.caller.up(0).setStyle({
				backgroundColor : 'transparent'
			});
			Event.stopObserving(this.caller.id,'mouseover',this.showIt,false);
		}
	},
	getObj : function(target) {
		if (this.share != undefined) {
			this.caller = $(target.id);
			if (this.newTimer != null) {clearTimeout(this.newTimer)}
			var pos = Position.cumulativeOffset(this.caller);
			var xPos = pos[0];
			var yPos = pos[1] + this.caller.up(0).getHeight();
			this.share.setStyle({
				left: xPos +'px',
				top: yPos +'px'
			});
			this.share.setStyle({
				visibility:'visible'
			})
			Event.observe(this.caller.id,'mouseover',this.showIt,false);
		}
	},
	goToSN : function (el) {
		this.caller.href = this.caller.href.replace(/#/,'');
		var nuURL = el.href + URLEncode(this.caller.href);
		if (this.caller.rel && el.href.indexOf('technorati') == -1) {
			if (el.href.indexOf('newsvine') != -1) {
				nuURL = nuURL +'&h='+ URLEncode(this.caller.rel);
			} else {
				nuURL = nuURL +'&title='+ URLEncode(this.caller.rel);
			}
		}
		window.open(nuURL);
		return false;
	}
}
Event.observe(window,'load',function() {soNet.init();});
function URLEncode(plaintext) {
	var SAFECHARS = "0123456789" +
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";
	var HEX = '0123456789ABCDEF';
	var encoded = '';
	for (var i=0; i<plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
		if (ch == ' ') {
			encoded += '+';
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				alert("Unicode Character '" + ch 
					  + "' cannot be encoded using standard URL encoding.\n" +
					  "(URL encoding only supports 8-bit characters.)\n" +
					  "A space (+) will be substituted.");
				encoded += '+';
			} else {
				encoded += '%';
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;
}
/* -- taille du texte -- */
function textSize(size) {
	var divs = document.getElementsByTagName('DIV');
	var ts = document.getElementById('smallimage');
	var tm = document.getElementById('mediumimage');
	var tl = document.getElementById('largeimage');
	var strClass;
	var div;
	if (size == 0) {
		ts.src = '/themes/primipile/media/img/icn/tool/a_small_bold.gif';
		tm.src = '/themes/primipile/media/img/icn/tool/a_medium.gif';
		tl.src = '/themes/primipile/media/img/icn/tool/a_large.gif';
	}
	if (size == 1) {
		ts.src = '/themes/primipile/media/img/icn/tool/a_small.gif';
		tm.src = '/themes/primipile/media/img/icn/tool/a_medium_bold.gif';
		tl.src = '/themes/primipile/media/img/icn/tool/a_large.gif';
	}
	if (size == 2) {
		ts.src = '/themes/primipile/media/img/icn/tool/a_small.gif';
		tm.src = '/themes/primipile/media/img/icn/tool/a_medium.gif';
		tl.src = '/themes/primipile/media/img/icn/tool/a_large_bold.gif';
	}
	for (var i=0; i<divs.length; i++) {
		div = divs[i];
		strClass = div.className;
		if (strClass && strClass.indexOf('ubiText') > -1) {
			strClass = strClass.replace(new RegExp('MoyTai'),'');
			strClass = strClass.replace(new RegExp('TGdTai'),'');
			switch (size) {
				case 0 : 
					strClass = strClass.replace(new RegExp('ubiText'),'ubiText');
					break;
				case 1 : 
					strClass = strClass.replace(new RegExp('ubiText'),'ubiText MoyTai');
					break;
				case 2 : 
					strClass = strClass.replace(new RegExp('ubiText'),'ubiText TGdTai');
					break;
			}
			div.className = strClass;
		}
	}
}
function cntChar(str,maxChar,curId,maxId) {
	var curCnt = str.value.length;
	var curCntMax = maxChar;
	if (curCnt > maxChar) {
		str.value = str.value.substr(0,maxChar);
		curCnt = str.value.length;
	}
	if (maxId) {
		curCntMax = maxChar - curCnt;
		var maxDiv = document.getElementById(maxId);
		maxDiv.innerHTML = curCntMax;
	}
	var curDiv = document.getElementById(curId);
	curDiv.innerHTML = curCnt;
}
/* -- article -- */
function parseText(id,maxW) {
	var iDiv = document.getElementById(id);
	var aryLink = iDiv.getElementsByTagName('a');
	for (var i=0; i<aryLink.length; i++) {
		if (aryLink[i].href.indexOf(window.location.host) < 0) {
			if ((aryLink[i].href.indexOf('http://') > -1) || (aryLink[i].href.indexOf('https://') > -1)) {
				aryLink[i].target = '_blank';
				aryLink[i].setAttribute('target','_blank');
			}
		}
	}
	var aryImg = iDiv.getElementsByTagName('img');
	for (var i=0; i<aryImg.length; i++) {
		if (aryImg[i].src.indexOf('http://image.mabulle.com') > -1) {
			var w = aryImg[i].width;
			var h = aryImg[i].height;
			if (w > maxW) {
				aryImg[i].width = maxW;
				aryImg[i].setAttribute('width',maxW);
				var newH = parseInt((h * maxW) / w, 10);
				aryImg[i].height = newH;
				aryImg[i].setAttribute('height',newH);
			}
		}
	}
}
/* -- antispam -- */
function resetASpam(asn,ask) {
	document.images[asn].src = '/clef/'+ Math.floor(Math.random()*1000000000);
	document.forms[ask].ASkey.value = '';
}
/* -- focus -- */
var foCus = {
	init : function() {
		this.issue = $('foCus');
		this.imgs = $$('#foCus .bofImg');
		this.blrbs = $$('#foCus .bofText');
		this.imgs.each(function(item,indx) {
			Event.observe(item,'mouseover',function() {foCus.showBlrb(indx);return false;},false);
		});
	},
	showBlrb: function(indx) {
		this.imgs.each(function(item,indx) {
			item.removeClassName('onFoc');
		});
		this.blrbs.each(function(item) {
			item.hide();
		});
		this.imgs[indx].addClassName('onFoc');
		this.blrbs[indx].show();
		return true;
	}
}
/* -- onglet -- */
var onglet = new Object();
onglet.initiate = function(tab) {
	var tabul = tab.parentNode;
	var divul = tabul.getElementsByTagName('div');
	for (var i=0; i < divul.length; i++) {
		divul[i].className = 'tab';
	}
	tab.className += ' on';
	var id = tab.id;
	var box = id +'ox';
	var cTab = document.getElementById(box);
	var tabox = cTab.parentNode;
	var divox = tabox.getElementsByTagName('div');
	for (var i=0; i < divox.length; i++) {
		if (divox[i].className == 'tab on') {
			divox[i].className = 'tab';
		}
	}
	cTab.className += ' on';
};
onglet.init = function(tab) {
	onglet.initiate(tab);
};
onglet.crsOver = function(tab) {
	tab.style.cursor = 'pointer';
};
onglet.crsOut = function(tab) {
	tab.style.cursor = 'auto';
};
/* -- newsletter -- */
function nwsltDesc(id) {
	var Nid = document.getElementById(id);
	Nid.style.visibility = 'visible';
}
function nwsltClear(el) {
	if (el.value == 'votre adresse email') {
		el.value = '';
	}
}
function nwsltReset(el) {
	if (el.value == null || el.value == '') {
		el.value = 'votre adresse email';
	}
}
/* -- onAir -- */
var onAir = {
	init : function(flx) {
		this.nbCom = 5;
		this.flux = flx;
		onAir.wOA();
	},
	alea : function() {
		return (Math.round(Math.random()) - 0.5);
	},
	getOrd : function(nb) {
		this.flux.sort(onAir.alea);
		return this.flux.slice(0,nb);
	},
	wOA : function() {
		var com = onAir.getOrd(this.nbCom);
		for (var i = 0; i<com.length; i++) {
			var ubiDiv = document.createElement('div');
			var comDiv = document.createElement('div');
			var autDiv = document.createElement('div');
			ubiDiv.className = 'item souligne';
			if (i == (com.length - 1)) {
				ubiDiv.className = 'lastItem';
			}
			autDiv.className = 'isp';
			comDiv.innerHTML = '"'+ com[i].prt +'" <span class="subText">dit '+ com[i].aut +'</span>';
			autDiv.innerHTML = '&#8250; <a href="'+ com[i].url +'">lire ce fil de discussion</a><br /><span class="subText">au sujet de "<em>'+ com[i].pst +'</em>"</span>';
			comDiv = comDiv.cloneNode(true);
			ubiDiv.appendChild(comDiv);
			autDiv = autDiv.cloneNode(true);
			ubiDiv.appendChild(autDiv);
			ubiDiv = ubiDiv.cloneNode(true);
			document.getElementById('fil').appendChild(ubiDiv);
		}
	}
}
/* -- forms effect -- */
var inmotion = false;
var slide = {
	down:function(id) {
		document.getElementById(id).style.display = 'none';
		if (!inmotion) {
			inmotion = true;
			document.getElementById(id).style.display = 'block';
			//new Effect.BlindDown(id);
			var timer = setTimeout(function(){inmotion=false;slide.focus(id);},1000);
		}
	},
	focus:function(id) {
		if (id == 'addCom') {
			document.forms['comForm'].msag.focus();
			resetASpam('mASimg','comForm');
		} else if (id == 'addCit') {
			document.forms['citForm'].lib.focus();
			resetASpam('mASimg','citForm');
		}
	},
	reset:function(divId,formId) {
		if (formId == 'comForm' || formId == 'citForm') {
			document.getElementById('cCntr').innerHTML = 0;
			document.getElementById('cMax').innerHTML = 1500;
			document.forms[formId].copie.checked = false;
		}
		document.forms[formId].reset();
		slide.up(divId);
	},
	up:function(id) {
		if (!inmotion) {
			inmotion = true;
			document.getElementById(id).style.display = 'none';
			//new Effect.BlindUp(id);
			var timer = setTimeout(function(){inmotion=false;},1000);
		}
	},
	head:function(id) {
		document.getElementById('cHea').appendChild(document.getElementById(id));
		this.down(id);
	},
	foot:function(id) {
		document.getElementById('cFoo').appendChild(document.getElementById(id));
		this.down(id);
	}
}
/* -- recommandation -- */
var elo = function (id,titre,postId,type,url,name) {
	resetASpam(name,'amiForm');
	var elObj = document.getElementById(id);
	if (type == 'article') {
		var btTmp = 'bt_'+ postId;
		var myObj = document.getElementById(btTmp);
	} else {
		var myObj = document.getElementById('util');
	}
	var pos = Position.cumulativeOffset(myObj);
	var newTop = pos[1] + 40 + 'px';
	elObj.style.top = newTop;
	var newLeft = pos[0] + 'px';
	elObj.style.left = newLeft;
	elObj.style.display = 'block';
	if (type == 'blog') {
		var cTitre = '';
	} else {
		var cTitre = ' l\'article';
	}
	document.getElementById('titre').innerHTML = 'Recommander'+ cTitre +' <span class="def">'+ titre +'</span>';
	document.forms['amiForm'].obj.value = type.capitalize() +' à lire : '+ titre;
	document.forms['amiForm'].eloP.value = type;
	document.forms['amiForm'].eloT.value = titre;
	document.forms['amiForm'].pid.value = postId;
	document.forms['amiForm'].url.value = url;
}
function resetPO() {
	document.forms['amiForm'].de.value= '';
	document.forms['amiForm'].mel.value = '';
	document.forms['amiForm'].vers.value = '';
	document.forms['amiForm'].copie.checked = false;
	document.forms['amiForm'].obj.value = '';
	document.forms['amiForm'].msg.value = '';
	document.forms['amiForm'].ASkey.value = '';
	document.forms['amiForm'].eloT.value = '';
	document.forms['amiForm'].eloP.value = '';
	document.forms['amiForm'].pid.value = '';
	document.forms['amiForm'].url.value = '';
	document.forms['amiForm'].rand.value = '';
	var l = document.getElementById('cntr');
	l.innerHTML = 0;
	var m = document.getElementById('max');
	m.innerHTML = 250;
}