function setWaitingCursor(mode){
	var bodys=document.getElementsByTagName("body");
	//alert("bodys "+bodys);
	var body=bodys[0];
	if(mode)
		body.style.cursor="wait";
	else
		body.style.cursor="";
} 
function submitForm(theform,  confirme) {
	if(confirme==null || confirme==false || confirm(confirme)){
    url = theform.action;
    pars = Form.serialize(theform);
		if(pars!="")
   	 pars+="&ajax=1";
   	else
   	 pars="ajax=1";
    var options={method: "post",parameters : pars };
    options.onComplete=ajaxEngine._onRequestComplete.bind(ajaxEngine);
    setWaitingCursor(true);
    var status = new Ajax.Request(url,options);
  }
  return false;
}
function clearElements(){
	for (var i = 0 ; i < arguments.length ; i++ ) {
		element=ajaxEngine.ajaxElements[ arguments[i] ];
		element.innerHTML="";
	}
}

function ricoActionBroker(){
	
}
function showEnchere(perso,enchere) {
	   ajaxEngine.sendPostRequest('showEnchere',"id="+perso,"ajax=1","enchere="+enchere);
	   return false;
}
function showSondage(perso,sondage,viewType) {
	   ajaxEngine.sendPostRequest('showSondage',"id="+perso,"ajax=1","sondage="+sondage,"viewType="+viewType);
	   return false;
}
function showMessage(perso,id,mbox,dir) {
	   ajaxEngine.sendPostRequest('showMessage',"id="+perso,"ajax=1","showmessage="+id,"dir="+dir,"curMboxId="+mbox);
	   return false;
}
function setHumeur(perso,humeur) {
	   ajaxEngine.sendPostRequest('changeHumeur',"id="+perso,"ajax=1","humeur="+humeur);
	   return false;
}
function showRelation(perso,id, gen) {
		if(gen!=1)
			gen=0;
	   ajaxEngine.sendPostRequest('showFriend',"id="+perso,"ajax=1","ShowFriend="+id,"Gen="+gen);
	   return false;
}
function setCacheMort(cache,page) {
	   ajaxEngine.sendPostRequest('orderPerso',"ajax=1","cache="+cache,"page="+page);
	   return false;
}
function showPlayer(perso,id) {	
	   ajaxEngine.sendPostRequest('showFriend',"id="+perso,"ajax=1","ShowPlayer="+id);
	   return false;
}
function showSac(perso,id) {	
	   ajaxEngine.sendPostRequest('aventure',"id="+perso,"ajax=1","showInventaire="+id,"section=1");
	   return false;
}
function showRune(perso,id) {	
	   ajaxEngine.sendPostRequest('aventure',"id="+perso,"ajax=1","rune="+id,"section=1");
	   return false;
}
function showDonjon(perso,id) {	
	   ajaxEngine.sendPostRequest('aventure',"id="+perso,"ajax=1","showDonjon="+id,"section=0");
	   return false;
}
function showMonstre(perso,id) {	
	   ajaxEngine.sendPostRequest('aventure',"id="+perso,"ajax=1","showMonstre="+id,"section=0");
	   return false;
}
function showEtablissement(perso,id,classe) {	
	   ajaxEngine.sendPostRequest('showAgenda',"id="+perso,"ajax=1","showetab="+id,"classe="+classe);
	   return false;
}
function showActivite(perso,etab,act,classe) {

	   ajaxEngine.sendPostRequest('showAgenda',"id="+perso,"ajax=1","showetab="+etab,"showactivite="+act,"classe="+classe);
	   return false;
}
function sortInventaire(perso,id){
		  text=Sortable.serialize("sortable-"+id,{name:"sub"});
		  text+="&id="+perso;  
		 	options = {method: "post" ,parameters: text};
      ajaxEngine.sendRequestWithData('orderInventaire', null, options);
}
function registerInventaireSortable(perso,id){
		fnct=function(){sortInventaire(perso,id);}	
		Sortable.create("sortable-"+id,{"onUpdate":fnct});	
}
function sortPersonnage(){	   		  
		    text=Sortable.serialize("persoListe",{name:"perso"});	
		    options = {method: "post" ,parameters: text};
      	ajaxEngine.sendRequestWithData('orderPerso', null, options);			
}
function sortSignet(perso){	   		  
		    text=Sortable.serialize("signetListe",{name:"signet"});	
		    text+="&id="+perso+"&section=3";  
		    options = {method: "post" , parameters: text};
      	ajaxEngine.sendRequestWithData('orderSignet', null, options);			
}
function registerSortPersonnage(){
		Sortable.create("persoListe",{"onUpdate":sortPersonnage});	
}
function registerSortSignet(perso){
		fnct=function(){sortSignet(perso);}	
		Sortable.create("signetListe",{"onUpdate":fnct});	
}
function destroyDrag(drag){
		alert("this "+this+" drag "+drag);
		
}
function registerDrag(id){
		drag=new Draggable(id,{constraint:'vertical',revert:true,onEnd:destroyDrag});
		
}
MessageLister=function(){};
MessageLister.prototype.setRcvSize=function(len){
	this.rcvSize=len;
}
MessageLister.prototype.setSentSize=function(len){
	this.sentSize=len;
}
MessageLister.prototype.changeSize=function(small){
	this.small=small;
}
MessageLister.prototype.redraw=function(){
	//alert("redraw: "+	this.small+" in "+this.rcvSize+" out "+this.sentSize);
	var rcvDiv=document.getElementById("inner_message_list_1");
	var sentDiv=document.getElementById("inner_message_list_2");
	if(rcvDiv!=null && sentDiv!=null){
	//	alert("small "+this.small+" rcv="+this.rcvSize+" sent="+this.sentSize);
		if(this.small){
			if(this.rcvSize>10){
				rcvDiv.style.height="230px";
				rcvDiv.style.overflow="auto";
			}
			else{
				rcvDiv.style.height="";
				rcvDiv.style.overflow="visible";
			}
			if(this.sentSize>10){
				sentDiv.style.height="230px";
				sentDiv.style.overflow="auto";
			}
			else{
				sentDiv.style.height="";
				sentDiv.style.overflow="visible";
			}
		}
		else{
			if(this.rcvSize>18){
				rcvDiv.style.height="391px";
				rcvDiv.style.overflow="auto";
			}
			else{
				rcvDiv.style.height="";
				rcvDiv.style.overflow="visible";
			}
			if(this.sentSize>18){
				sentDiv.style.height="391px";
				sentDiv.style.overflow="auto";
			}
			else{
				sentDiv.style.height="";
				sentDiv.style.overflow="visible";
			}
		}
	}
}
var messageLister= new MessageLister();

function clearMessageWindow(){
	clearElements("showRelation");
	messageLister.changeSize(false);
	messageLister.redraw();
}

ricoActionBroker.prototype.ajaxUpdate=function(ajaxResponse){
	//alert("ajaxUpdate "+ajaxResponse.childNodes.length);
	 for(var i=0; i<ajaxResponse.childNodes.length; i++){
	 
	 		this.execute(ajaxResponse.childNodes[i]);
	 }
}
ricoActionBroker.prototype.execute=function(node){
	//alert("execute "+node.nodeType+" "+node.nodename+" "+node.tagName);
	if(node.nodeType==1){
	//	alert("element");
		if(node.tagName=="browse"){
			var url=node.getAttribute("url");
			top.location.replace(url);
		}
		else if(node.tagName=="scroll"){
			//alert("scroll");
			var element=node.getAttribute("element");
			//alert("scroll "+element);
			var toTop=(node.getAttribute("top")=="true")?true:false;
				//alert("scroll "+element+" top "+toTop);
			var elt=document.getElementById(element);
			//alert("scroll "+element+" "+elt+" top "+toTop);
			elt.scrollIntoView(toTop);
			//alert("scrolled");
		}
		else if(node.tagName=="message_liste"){
			var rcv=node.getAttribute("rcv");
			var sent=node.getAttribute("sent");
			var small=node.getAttribute("small");
			if(rcv!=null)
				messageLister.setRcvSize(rcv);
			if(sent!=null)
				messageLister.setSentSize(sent);
			if(small!=null)
				messageLister.changeSize(small==1);
			messageLister.redraw();
		}
		else if(node.tagName=="sortPersonnage"){
			registerSortPersonnage();
		}
		else if(node.tagName=="registerFriendList"){
			var perso=node.getAttribute("perso");
			registerFriendList(perso);			
		}
		else if(node.tagName=="unRegisterFriendList"){
			unRegisterFriendList();		
		}
		else if(node.tagName=="resetAction"){
			actionSwitch.clear();		
		}
		else if(node.tagName=="foldGroup"){			
			var groupe=node.getAttribute("groupe");
			fold_group(groupe,true);		
		}
	}
}
function unRegisterFriendList(){
	  jQuery(".draggable-rel").draggableDestroy();
	  jQuery(".drop-rel").droppableDestroy();
	  jQuery(".draggable-amis").draggableDestroy();
	  jQuery(".drop-amis").droppableDestroy();
}
function cloneIE6image(){
			var myHelper = document.createElement("img");
			var filter=this.style.filter;
			var src=this.src;
			//alert("filter ->"+filter+"<-");
			if(filter!=null && filter!=""){
				var tab=filter.split("'");
				src=tab[1];
			}
			myHelper.src=src;
			jQuery(myHelper).css({
				width: this.offsetWidth + 'px',
				height: this.offsetHeight + 'px',
				opacity: 0.7,
				position: 'absolute'
			});
			return myHelper;
}

function registerFriendList(perso){
  jQuery(".draggable-rel").draggable({
	  	helper: cloneIE6image,
	  	opacity: 0.7,
	  	axis: "y",
	  	zIndex: 1000});
	 	
	 jQuery(".draggable-amis").draggable({
	  	helper: cloneIE6image,
	  	opacity: 0.7,
	  	axis: "y",
	  	zIndex: 1000});
		  	
	 	jQuery(".drop-rel").droppable({
		accept: '.draggable-amis',
		tolerance: 'pointer', 
		drop: function(ev,ui) {
				setAmitie(perso,ui.draggable.element.id,0)
			}
		});

 	jQuery(".drop-amis").droppable({
		accept: '.draggable-rel',
		tolerance: 'pointer', 
		drop: function(ev,ui) {
				setAmitie(perso,ui.draggable.element.id,1)
			}
		});
	}
function setAmitie(perso,ami,favori) {
		 dest=ami.split("_");
		 id=dest[2];
		//alert("setAmitie "+id+" "+favori);
	   ajaxEngine.sendPostRequest('showFriend',"id="+perso,"ajax=1","ShowFriend="+id,"Favori="+favori,"show=0");
	   return false;
}


