/*
* lt_up : fonction de base du systeme. Permet d'actualiser intelligemment un elt de la page
 * 
 * fonction jajax <1.1 stable 18/04/07>
 * author Pascal Menut <pascal@petitegraine.com>
 *
 * params :
 *  idElt : identifiant html de l'element qui subi la requete ajax
 *  jdata : un blabla de commande par ex : 'package: sandbox, composant: helloworld' va appeler le script situé dans l'arborescence dans /packages/sandbox/jax/helloworld.php
 *  lanceOnComplete : TODO améliorer le passage de fonction. fonction(s) à executer lors de l'événement 'onComplete'
 *  highlight : true/false , true par défaut
 *  reloadbehaviours true/false , true par défaut
 */
function lt_up(idElt, jdata, lanceOnComplete, highlight, reloadbehaviours) {
	
	if(highlight == null) {
		highlight = true;
	}
	if(reloadbehaviours == null) {
		reloadbehaviours = true;
	}
	
	// recuperation des options.
	// pour compatibilite. A virer en temps opportun, ou a remplacer par un pattern, dans le genre des templates exogen.
	if((lanceOnComplete == null) || (lanceOnComplete == 'noeffect') || (lanceOnComplete == 'APPEAR') || (lanceOnComplete == 'GROW')) {
		lanceOnComplete = '';
	}
	if(reloadbehaviours == true) {
		lanceOnComplete += "; Behaviour.apply()";
	}
	if(highlight == true) {
		lanceOnComplete += "; new Effect.Highlight(idElt, { duration: 0.2 } )";
	}
	new Ajax.Updater(idElt, '?io=get&elt=' + encodeURIComponent(idElt) + '&jdata=' + encodeURIComponent(jdata), {method:'get', evalScripts:true, asynchronous: true, onComplete: function (request) { eval(lanceOnComplete); }});
}

function lt_autocompletion(idElt, jdata) {
	
	lt_up(idElt, jdata);
	
}

function lt_download(file, type) {
	//startLoading('xset', 'chargement ...');
	
	top.frames['dl'].document.location.href = '?io=download&elt=set&file=' + file + '&type=' + type;
	// + '&jdata=' + encodeURIComponent(jdata), {method:'post', evalScripts:true, asynchronous: true, onComplete: function (request) {  }});
}

function lt_set(jdata, inhibeStopLoading, inhibeStartLoading, idElt) {

	if(idElt != null) {
		// a la charge du développeur de donner un idElt qui existe.
		// TODO faire une vérif, ou mieux : faire un addChild si jamais..
		
	
		idxElt = 'xset';
	}
	else {
		idElt = 'set';
		idxElt = 'xset';
		 // par défaut on active la div xset
	}
	
	if(inhibeStartLoading != null && inhibeStartLoading != true && inhibeStartLoading != 'true') {
		
		startLoading(idxElt, 'chargement ...');
	}
	
	if(inhibeStopLoading != null && inhibeStopLoading != true && inhibeStopLoading != 'true') {
		new Ajax.Updater(idElt, '?io=set&elt=' + idElt + '&jdata=' + encodeURIComponent(jdata), {method:'get', evalScripts:true, asynchronous: true, onComplete: function (request) { stopLoading(idxElt); }});
	}
	else {
		// le stopLoading est a la charge du script.
		new Ajax.Updater(idElt, '?io=set&elt=' + idElt + '&jdata=' + encodeURIComponent(jdata), {method:'post', evalScripts:true, asynchronous: true});
	}
}

function lt_notify(jdata, idElt) {
	if(idElt != null) {
		
	}
	else {
		idElt = 'listener';
	}
	new Ajax.Updater(idElt, '?io=set&elt=' + idElt + '&jdata=' + encodeURIComponent(jdata), {method:'get', evalScripts:true, asynchronous: true});
}

function lt_post(idElt, jdata, form) {
	 var ajax = new Ajax.Request('index.php', {
	 		 method: 'post', 
	 		 parameters: 'io=set&elt=' + encodeURIComponent(idElt) + '&jdata=' + encodeURIComponent(jdata) + '&'+Form.serialize(form),
	 		 onSuccess: function() { new Effect.Highlight(idElt, { duration: 0.4 } ) },
	 		 onFailure: function() { alert('Erreur ! Données non transmises'); }
	 });
	 return false;
}

function highlight(idElt, delai) {
	if(delai == null) {
		delai = 0;
	}
	new Effect.Parallel(
				 [ new Effect.Opacity(idElt, 
								{ sync: true, to: 1, from: 1 } ) 
				 ],
				 { 	duration: delai, 
					afterFinish: function(effect) { 
								
						new Effect.Highlight(idElt, { duration: 0.4 } )
					}
				 }
				);
}


function lt_minimize(idElt) {
	
	if(document.getElementById('max_' + idElt).style.display != 'none') {
		if(document.all) {
			document.getElementById('content_' + idElt).style.display = 'none';
		}
		else {
			new Effect.Shrink('content_' + idElt, {duration: 0.25});
		}
		//new Effect.BlindUp('content_' + idElt, {duration: 0.25});
		//document.getElementById.style.display = 'none';
		document.getElementById('max_' + idElt).style.display = 'none';
		document.getElementById('min_' + idElt).style.display = 'block';
		document.getElementById(idElt).className = idElt + '_min';
		new Effect.ShakeUp(idElt);
		
		if(document.getElementById('help')) {
			new Effect.Appear('help', {duration: 0.1});
		}
	}
}

function lt_maximize(idElt, cdata) {
	if(cdata != null) {
		lt_up(idElt, cdata, '');
	}
	else {
		
		// ces lignes sont spÃ©cifiques
		document.getElementById('bloc2').className = 'nonestompe';
		
		new Effect.Appear('content_' + idElt, {duration: 0.5});
		document.getElementById('max_' + idElt).style.display = 'block';
		document.getElementById('min_' + idElt).style.display = 'none';
		document.getElementById(idElt).className = idElt + '_max';
		
		if(document.getElementById('help')) {
			new Effect.Fade('help', {duration: 0.1});
		}
	}
}


function stopLoading(idElt, msg) {
	if(msg == null) {
		msg = '';
	}
	switch(idElt) {
		case 'xset':
			
			new Effect.Fade('xset', {	duration: 0.4, 
										delay: 0.5,
										beforeStart: function(effect) {
														new Effect.Morph('cset', {style: 'width: 200px; height: auto;', duration: 0.3, beforeStart: function() { $('xset-loader').style.visibility = 'hidden'; $('set-label').innerHTML = 'Termin&eacute;'; $('set').innerHTML = msg; }});
													},
										afterFinish: function(effect) { 
														$('xset-loader').style.visibility = 'visible';
														$('set-label').innerHTML = 'Patientez...';
													 }
								});
			
			
			break;
		default:
			// arret de l'affichage du petit bitoniau
			$(idElt).src = 'themes/images/toolDown.gif';
			break;
	}
}

function startLoading(idElt, msg) {
	if(msg == null) {
		msg = '';
	}
	switch(idElt) {
		case 'xset':
			$('set').innerHTML = msg;
			new Effect.Appear('xset', {duration: 0.1});
			break;
		default:
			$(idElt).src = 'themes/images/toolLoader.gif';
			break;
	}
}

function suspendLoading(idElt, msg) {
	if(msg == null) {
		msg = '';
	}
	switch(idElt) {
		case 'xset':
			$('set-label').innerHTML = 'Termin&eacute;';
			$('xset-loader').style.visibility = 'hidden';
			$('set').innerHTML = msg + '<p style="text-align: center; padding-top: 10px;"><a style="border: 1px solid gray; padding: 2px 10px;" href="#" onclick="stopLoading(\'' + idElt + '\'); return false;" title="fermer">Ok</a></p>';
			break;
		default:
			stopLoading(idElt);
			xalert(msg);
			break;
	}
	
}

function accordeon(el, racineAccordeon) {
    if (el.id == racineAccordeon + '-visible') {
        return false;
    }
    eltVisible = $(racineAccordeon + '-visible');
    if (eltVisible) {
    	//startLoading(el.parentNode.id+'-state');
        var eldown = el.parentNode.id+'-body';
        var elup = eltVisible.parentNode.id+'-body';
        
    	//new Ajax.Updater(eldown, '?io=get&elt=' + el.parentNode.id, { asynchronous:true, evalScripts:true, onComplete: function () {
	        new Effect.Parallel(
	        [
	            new Effect.BlindUp(elup),
	            new Effect.BlindDown(eldown)
	        ], {
	            duration: 0.3
	        });
	    	//stopLoading(el.parentNode.id+'-state');
    		//document.getElementById(eltVisible.parentNode.id+'-state').src = 'themes/images/toolUp.gif';
    		//eltVisible.id = '';
    	//}});
    	// el.id = 'acc-visible';*/
    	 el.id = racineAccordeon + '-visible';
    	 eltVisible.id = '';
    }
    else {
    	var eldown = el.parentNode.id+'-body';
    	new Effect.BlindDown(eldown, {duration: 0.3});
        el.id = racineAccordeon + '-visible';
    }
    
    
}

function cocherdecocherTout(classename, cocher) {
		$A(document.getElementsByClassName(classename)).each(function(elt) {
			if(elt.disabled) {
			
			}
			else {
				elt.checked = cocher;
			}
		});
	
	}

function addEvent(elm, evType, fn, useCapture) {
    elm["on"+evType]=fn;return;
}

function xalert(message) {
	return alert(message);
}

function xconfirm(message, action) {
	if(confirm(message)) {
		lt_set(action);
	}
}

function xnotice(message) {
	// affichage d'une notice. Et vivent les zeffets scriptaculous
	$('notice').innerHTML = message;
	
	new Effect.Parallel(
		[ new Effect.Appear('xnotice', {duration: 0.5}  ),
		  new Effect.Pulsate('notice', {duration: 2, pulses: 2}) ],
		  { duration: 2,
	      	afterFinish: function(effect) { new Effect.Fade('xnotice', {duration: 0.5}); }
	      }
	  );
}

/**
 * effets
**/

function shakeitbaby(idelt) {
	$(idelt).shake();
}


/**
  * chargement de l'arrosoir.
 **/
 
function arrShowLoginBox() {
	$('arrosoirbox').className = 'arrosoirbox_positionDessus';
	lt_up('arrosoir-loginbox', 'package: arrosoir, composant: loginbox', 'new Effect.Appear(\'arrosoir-loginbox\', {duration: 0.1});');
					
}

/**
 * fonctions de debug
*/


function swappeDiv(iddiv) {
	$(iddiv).toggle();

}
