/**
 * Script affichant un menu au survol de la souris
 */
function positionInfobulle(e) {
	// On repère la position de la souris et on ajuste selon le scroll, de la
	// façon adaptée au navigateur

	$('divinfo').hide();

	var scrOfY = 0;
	if (typeof (window.pageYOffset) == 'number') {
		scrOfY = window.pageYOffset;
	} else if (document.body
			&& (document.body.scrollLeft || document.body.scrollTop)) {
		scrOfY = document.body.scrollTop;
	} else if (document.documentElement
			&& (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		scrOfY = document.documentElement.scrollTop;
	}

	var scrOfX = 0;
	if (typeof (window.pageXOffset) == 'number') {
		scrOfX = window.pageXOffset;
	} else if (document.body
			&& (document.body.scrollLeft || document.body.scrollTop)) {
		scrOfX = document.body.scrollLeft;
	} else if (document.documentElement
			&& (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		scrOfX = document.documentElement.scrollLeft;
	}

	if (e) {
		x = e.clientX + scrOfX;
		y = e.clientY + scrOfY;
	} else {
		x = event.clientX + scrOfX;
		y = event.clientY + scrOfY;
	}

	if (window.innerHeight) {
		wiw = window.innerWidth;
		wih = window.innerHeight;
	} else if (document.documentElement.clientWidth) {
		wiw = document.documentElement.clientWidth;
		wih = document.documentElement.clientHeight;
	} else {
		wiw = document.body.clientWidth;
		wih = document.body.clientHeight;
	}

	if ((e ? e.clientX : event.x) < wiw / 2) {
		x = x + 15;
	} else {
		x = x - $('divinfo').getWidth() - 15;
	}
	if ((e ? e.clientY : event.y) < wih / 2) {
		y = y + 15;
	} else {
		y = y - $('divinfo').getHeight() - 15;
	}

	$('divinfo').style.left = x + "px";
	$('divinfo').style.top = y + "px";
	$('divinfo').show();
}

/**
 * afficher l'infobulle
 */
function afficherDivInfo(contenu, afficherdiv) {
	if (afficherdiv == 1) {
		$('divinfo').innerHTML = contenu;
		if (navigator.appName.substring(0, 3) == "Net") {
			addEventListener('MOUSEMOVE', afficherDivInfo, true);
		}
		//positionInfobulle();
		document.onmousemove = positionInfobulle;

	} else {
		document.onmousemove = '';
		$('divinfo').hide();
	}
}
/*******************************************************************************
 * Met en évidence le champs de formulaire qui a le focus
 ******************************************************************************/
function inputOnFocus() {
	var arrayInputs = new Array();

	var inputs = document.getElementsByTagName('input');
	for ( var i = 0; i < inputs.length; i++) {
		if (inputs[i].type == 'text' || inputs[i].type == 'password')
			arrayInputs[arrayInputs.length] = inputs[i];
	}
	inputs = document.getElementsByTagName('select');
	for ( var i = 0; i < inputs.length; i++)
		arrayInputs[arrayInputs.length] = inputs[i];
	inputs = document.getElementsByTagName('textarea');
	for ( var i = 0; i < inputs.length; i++)
		arrayInputs[arrayInputs.length] = inputs[i];
	for ( var i = 0; i < arrayInputs.length; i++) {
		var onLoad = arrayInputs[i].onfocus
		$(arrayInputs[i]).observe('focus', function(event) {
			this.className += (this.className ? ' ' : '') + 'onFocus';
		});

		arrayInputs[i].onblur = function() {
			var newClassName = '';
			var classNames = this.className.split(' ');
			for ( var i = 0; i < classNames.length; i++) {
				if (classNames[i] != 'onFocus')
					newClassName += (newClassName ? ' ' : '') + classNames[i];
			}
			this.className = (newClassName ? newClassName : '');
		}
	}
}
/*
 * function addEvent(element, event, func) { if (element.addEventListener)
 * element.addEventListener(event, func, func); else element.attachEvent('on' +
 * event, func); }
 */
function addTarget_blank() {
	var listTarget_blank = $$('div#kesako a');
	/*
	 * tabs[i].onclick = ( function(tabs, zones, i) { return function() {
	 * switchDisplay(tabs, zones, i); } })(tabs, zones, i);
	 * 
	 */
	listTarget_blank.each( function(elem) {
		elem.onclick = ( function(elem) {
			return function() {
				window.open(elem.href);
				return false;
			}
		}(elem));
	});

}
function addPriceLine(){
	var numrows = $$('#priceList tr').length;
	var id = 'row'+(numrows-2);
	var nextid = numrows-1;
	$(id).insert({after:'<tr id="row'+nextid+'"><td><input type="text" maxlength="2" size="2" name="prices['+nextid+'][mois_min]" value=""/></td>'
		+'	<td><input type="text" maxlength="2" size="2" name="prices['+nextid+'][mois_max]" value=""/></td>'
		+'	<td><input type="text" maxlength="4" size="4" name="prices['+nextid+'][1]" value=""/></td>'
		+'	<td><input type="text" maxlength="4" size="4" name="prices['+nextid+'][2]" value=""/></td>'
		+'	<td><input type="text" maxlength="4" size="4" name="prices['+nextid+'][pack]" value=""/></td></tr>'});
}

function displayEditSubscribeBox(id_inscr){
	
	$('editSubscribeBy').update('<form name="editSubscribeBy">'
		+'<input type="hidden" name="id_inscription" value="'+id_inscr+'"/>'
		+'<input onKeyUp="updateSubscribeBox($F(\'subscriber\'))" type="text" id="subscriber" name="subscriber" value=""/>&nbsp;<span id="counted"></span><span id="selectChoice"> <img width="16px" src="/images/gestion/loading_100x100.gif"/></span><br />'
		+'<p><select style="width:300px;" onclick="$(\'validate\').enable();" name="subscribeByChoices" size="5" id="subscribeByChoices" class="selectBox"></select></p>'
		+'<p><input id="validate" type="button" onclick="saveSubscriber('+id_inscr+',$F(\'subscribeByChoices\'))" value="valider" disabled/></p></form><div id="sub_message"></div>'
	);
	updateSubscribeBox('');
	$('subscribeInfos').show();
	
}
function saveSubscriber(id_inscr, id_sub){
	new Ajax.Request('/javascript/edit_user.php?action=save', {
		method: 'post',
		parameters: {id_inscr: id_inscr, id_sub: id_sub},
		onSuccess: function(transport) {
			var data = transport.responseText.evalJSON();
			$('sub_message').update(data.message);
			var id_temp = 'subscribe_by_'+id_inscr;
			$(id_temp).update(data.subscriber);
		}
	});	
	
}
function updateSubscribeBox(name){
	new Ajax.Request('/javascript/edit_user.php?action=update', {
			method: 'post',
			parameters: {name: name},
			onCreate: function(){
				$('selectChoice').show();
				$('subscribeByChoices').disable();
				$('validate').disable();
				
			},
			onSuccess: function(transport) {
				var data = transport.responseText.evalJSON();
				$('selectChoice').hide();
				$('subscribeByChoices').update(data.liste);
				$('counted').update('<b>'+data.total+'</b>');
				$('subscribeByChoices').enable();
			}
		});	
}

Event.observe(window, 'load', inputOnFocus);
Event.observe(window, 'load', addTarget_blank);

