/*
	[DRAG]
	Gen.AdaptZone place aux noeuds possédant ValCle le comportement DD et 3 événements YAHOO dont clickValidator. DD rend l'él. déplaçable.
	Le formulaire fo_MoveFich est appelé si le transfert se fait entre zones. Sinon il s'agit simplement d'un tri.
	[EDIT]
	Quant à l'édition, elle est initialisée par GenClass.dblClic qui rempli App.ContentEdit. App.ContentEdit invalide le drad drop.
*/
// http://localhost/biblio/pgm/yui2.7/examples/container/keylistener.html
// var kl2 = new YAHOO.util.KeyListener(document, { ctrl:true, keys:89 }, { fn:YAHOO.example.container.panel1.show, scope:YAHOO.example.container.panel1, correctScope:true } );
// kl2.enable();

var Dom = YAHOO.util.Dom
var Event = YAHOO.util.Event
var YEC = YAHOO.namespace("example.container");

GenClass.prototype.dblClic = function(O) {
	var O, D, S
	if (window.Profil && Profil<=10) {
		if (!(D=ElemPClass(O,'TitIn')))  if (!(D=ElemPClass(O,'Descript')))  return;		//alert(Lout(D))
		new ClassEditZone(D)
		return true
	}
}

bodyKeyDown = function(e) {
	var key, O
	key = (document.all)?event.keyCode:e.which;
	O = EventCtrl(e);			//alert(key+crr+Lout(O))
	switch (key) {
		case 27:  if (App.ContentEdit)  App.ContentEdit.Annul();  break
		case  113: /*F2*/  DialogConnect();  AnnulEvent(e);  break;
		//case  113:  Profil=10;  LoadMenu();  AnnulEvent(e);  break;
	}
}


function setComboBox(CB, Liste, Opt) {
	var CB, Cod, Li, S, O, V, N, i, ValDef
	CB = LID(CB);  if (!CB)  return
	Opt = Opt || {}
	if (Opt.Clear) { delete(Opt.Clear);  CB.innerHTML='' }
	if (Opt.AjDeb)  setComboBox(CB, Opt.AjDeb)
	ValDef = Opt.ValDef

	for (Cod in Liste) {
		Li = Liste[Cod];			//alert(Cod+crr+V)
		if (typeof(Li)=='object') {
			i = 0
			for (N in Li) {
				S = Li[N]
				if (i==0)  Cod = S
				else if (i==1)  V = S
				i++
			}
		}else{
			V = Li
		}
		O = document.createElement('OPTION')
		O.value = Cod;  O.text = V
		//if (document.all)  CB.add(O,1);  else  CB.add(O,null)
		CB.add(O,Opt.pos)
	}
	nb = CB.options.length
	if (nb==1)  ValDef = Cod
	if (Opt.AjFin)  setComboBox(CB, Opt.AjFin)
	if (ValDef!==undefined)  PVal(CB,ValDef)
}

function YinitCms() {
	//DialogDragNod()
}

function DialogConnect() {
  if (!YEC.fo_Connect) {
		YEC.fo_Connect = new YAHOO.widget.Dialog("fo_Connect",
			{ fixedcenter : true,
				visible : false,
				constraintoviewport : true,
				buttons : [ { text:'Valider', handler:function() { form_Connect.submit() } , isDefault:true }, { text:'Quitter', handler:function() { this.cancel() } } ]
			}
		)
		YEC.fo_Connect.render();
	}
	Aff('fo_Connect')
	YEC.fo_Connect.show()
}

function DialogDragNod() {
  if (!YEC.fo_MoveFich) {
		YEC.fo_MoveFich = new YAHOO.widget.Dialog("fo_MoveFich",
			{ width : "32em",
				fixedcenter : true,
				visible : false,
				constraintoviewport : true,
				buttons : [ { text:"Valider", handler:handleSubmit, isDefault:true }, { text:"Quitter", handler:function() { delete App.Drag;  this.cancel() } } ]
			}
		)
		YEC.fo_MoveFich.render();
		var kl2 = new YAHOO.util.KeyListener(document, { keys:27 }, { fn:function() { delete App.Drag;  this.cancel() }, scope:YEC.fo_MoveFich, correctScope:true } );
		kl2.enable();
	}
	Aff('fo_MoveFich')
	YEC.fo_MoveFich.show()
}

var handleSubmit = function() {
	var Place = LVal('Place'), Mode = LVal('mode[]'), Ou = LVal('ou[]');		//alert(Place+cr+Mode+cr+Ou)
  OD = App.Drag.OD['c'+Place]
  MoveFiche(App.Drag.OS, OD, App.Drag.OSP, Mode, Ou)
	this.cancel()
}

YAHOO.util.Event.onDOMReady(YinitCms);



// ====================================      DRAG / DROP       ===============================
function clickValidator(e) {
	if (App.ContentEdit)  return
	var el = this.getEl();
	this.xy = Dom.getXY(el)
	App.DragFich = null
	//Event.preventDefault(e);
	return 1
}

function onDragDrop(e, id) {
	var OS, OD, cl1, Tit
	App.AnnulEvent = 1
	if (!App.Drag) { App.Drag = {};  App.Drag.Param = App.DragParam || {};  App.Drag.OD = [];  App.Drag.Data = [] }
	OS = this.getEl();		//alert(id+crr+Dom.getXY(id)+crr+Lout(OS));
	Dom.setXY(OS, this.xy)
	OD = LID(id)
	if (!ElemPAt(id,'drop'))  return;
	if (LX(OS,'cl1')==LX(OD,'cl1'))  return;	if (LX(OS,'clp')==LX(OD,'cl1'))  return
	//OD.style.border = '1px dotted #888'
	// Constitution dans App.Drag.Data du tableau des noeds parents de destination (car onDragDrop est exécuté autant de fois qu'il y a de parents du nod dest + 1 (le nod dest))
	cl1 = LX(OD,'cl1');  if (!(Tit=LX(OD,'Titre')))  return
	App.Drag.OD['c'+cl1] = OD;					//wh('cl1='+cl1+', Titre='+LX(OD,'Titre'))
	App.Drag.Data.push({'cl1':cl1, 'Titre':Tit})
	if (App.Drag.Data.length==1) {
		App.Drag.OS = OS;
		App.Drag.OSP = ElemPAt(OS.parentNode,'ValCle');		//alert(Lout(App.Drag.OSP))
		setTimeout("SuiteDragDrop()",100)
	}
}

function SuiteDragDrop() {
	var cl1, clp, OD
	//alert(JSW(App.Drag.Data))
	App.Drag.Data.reverse()
	clp = App.Drag.Data[0].cl1;  OD = App.Drag.OD['c'+clp]
	//alert(LX(OD,'clp')+crr+LX(App.Drag.OS,'clp'))
	if (LX(OD,'clp')==LX(App.Drag.OS,'clp') && (App.Drag.Param.Tri!='ask')) {    // même père
		MoveFiche(App.Drag.OS, OD, App.Drag.OSP, '', 'ap')
	}else{
		setComboBox('Place', App.Drag.Data, {Clear:1})
		PVal('TitreMov','<b>Fiche concernée</b> : ' + LX(App.Drag.OS,'Titre'), 1)
		DialogDragNod()
	}
}

function MoveFiche(OS, OD, OSP, Op, Ou) {
	var S, Li, cl1P, cl1, clp, Cle, Lien, oldLien, Cles
	//alert(Op+','+Ou+crr+Lout(OS)+crr+Lout(OD));  return
	cl1 = LX(OS,'cl1');  Cle = LX(OS,'Cle')
	clp = LX(OD,'cl1');  Lien = LX(OD,'Cle');  cl1P = LX(OD,'clp')
	//oldLien = LX(OS,'Lien');
	if (cl1==clp) { alert("Destination identique"); return }    //alert(cl1+cr+clp+cr+Cle)
	if (Op=='cpy' || Op=='dup') { oldLien='';  cl1='' }
	Cles = "&cl1P=" + cl1P + "&clp=" + clp + "&cl1=" + cl1

	var Param = {OS:OS, OD:OD, OSP:OSP, Op:Op, Ou:Ou, Cles:Cles}
	if (Op=='dup') {
		Li = "Pgm=DuplicNod" + Cles + "&Cle=" + Cle + "&Lien=" + Lien + AjUrl;	 status = Li    //"&oldLien=" + oldLien +
  	XmlPost2(Li, '', SuiteMoveFiche, Param)
	}else if (Ou=='in' || Op=='cpy') {
		Li = "Pgm=SovNod" + Cles + "&Cle=" + Cle + "&Lien=" + Lien + "&avecFils=1" + AjUrl;	 status = Li    //"&oldLien=" + oldLien +
  	XmlPost2(Li, '', TriFiche, Param)
	}
	if (Ou!='in') {
		TriFiche('',Param)
	}
	delete App.Drag
}

function TriFiche(re, Param) {
  var S, V
	S = "Pgm=TriNod" + Param.Cles + "&Ou=" + Param.Ou + AjUrl;  status = S
	XmlPost2(S,'',SuiteMoveFiche, Param);
}

function SuiteMoveFiche(re, Param) {
  var S, V, Li, W
	PVal('DivOther',re,1);
	if (Param.OSP.id.substr(0,4)=='menu') {
		location.reload()
	}else{
		//setTimeout("Fiche('"+Param.OS.id+"')",1000)
		Fiche(Param.OSP);		//alert(Lout(Param.OSP))
		Fiche(Param.OD)
	}
	//var OS = Param.OS;
	//V = VarListe(re, 'ValCle')
}


// ====================================      EDIT       ===============================
function ClassEditZone(Div) {
	var D
	this.Div	= D = LID(Div)
	this.DivP = ElemPAt(D,'ValCle')
	this.Cle	= LX(this.DivP,'Cle')
	this.NomCh = ''
	this.SiTxt = 0
	//wh('+++'+this.Cle)
	if (this.Cle) {
		this.Init()
		App.ContentEdit = this
	}
	this.oldV = this.Get()
}

ClassEditZone.prototype.Init = function() {
	var D, cl
	D = this.Div
	cl = D.className;  this.NomCh = cl;  if (cl=='TitIn') { this.NomCh = 'Titre';  this.SiTxt = 1 }
	D.contentEditable = true;  D.style.border = "1px dotted #888"
	D.focus();  D.attachEvent('onblur', BlurEdit)
}

ClassEditZone.prototype.Get = function() { var D, V;  D = this.Div;  if (this.SiTxt)  V = LText(D);  else V = D.innerHTML;  return V }
ClassEditZone.prototype.Put = function(V) { if (this.SiTxt)  PText(this.Div,V);  else this.Div.innerHTML = V }

ClassEditZone.prototype.Sov = function() {
	var V = this.Get()
	if (V!=this.oldV) {
		XmlPost2("Pgm=Sov&NomT=base&NomC=Cle&TypCle=Compteur&Cle=" + this.Cle + '&' + this.NomCh + '=' + encodeURIComponent(V) + AjUrl,'',_AffMsg)
	}
  this.Exit()
}

ClassEditZone.prototype.Annul = function() {
	var D, S, cl, V
	this.Put(this.oldV)
  this.Exit()
}

ClassEditZone.prototype.Exit = function() { var D = this.Div;  D.style.border = '';  D.contentEditable = false;  D.detachEvent('onblur', BlurEdit);  delete App.ContentEdit }

BlurEdit = function(e) { if (App.ContentEdit)  App.ContentEdit.Sov();  else  alert("Erreur de mémorisation. (BlurEdit)") }

