///////////////////////////////////////////////////
///		Drag & Drop	Plugin						
///		Basé sur jQuery et Interface 			
///		Modifié par SPENDER Maël			
///		Team Projet -- www.teamprojet.com		
///		Août 2009								
///////////////////////////////////////////////////

$(document).ready(
	function()
	{
		tree = $('#myTree');
		$('li', tree.get(0)).each(
			function()
			{
				subbranch = $('ul', this);
				if (subbranch.size() > 0) {
					
				} else {
					//$(this).prepend('<img src="images/spacer.gif" width="16" height="16" class="expandImage" />');
				}
			}
		);

		$('span.textHolder').Droppable(
			{
				accept			: 'treeItem',
				hoverclass		: 'dropOver',
				activeclass		: 'fakeClass',
				tollerance		: 'pointer',
				onhover			: function(dragged)
				{
					if (!this.expanded) {
						subbranches = $('ul', this.parentNode);
						if (subbranches.size() > 0) {
							subbranch = subbranches.eq(0);
							this.expanded = true;
							if (subbranch.css('display') == 'none') {
								var targetBranch = subbranch.get(0);
								this.expanderTime = window.setTimeout(
									function()
									{
										$(targetBranch).show();
										$.recallDroppables();
									},
									500
								);
							}
						}
					}
				},
				onout			: function()
				{
					if (this.expanderTime){
						window.clearTimeout(this.expanderTime);
						this.expanded = false;
					}
				},
				ondrop			: function(dropped)
				{
					if(this.parentNode == dropped)
						return;
					if (dropped.id == "racine" )
						return;
					if (dropped.id.match (/sds_.*/)) {
						dropped.id=dropped.id.substr (4);
					}
					if (this.expanderTime){
						window.clearTimeout(this.expanderTime);
						this.expanded = false;
					}
					subbranch = $('ul', this.parentNode);
					if (subbranch.size() == 0) {
						$(this).after('<ul></ul>');
						subbranch = $('ul', this.parentNode);
					}
					oldParent = dropped.parentNode;
					subbranch.eq(0).append(dropped);
					oldBranches = $('li', oldParent);
					if (oldBranches.size() == 0) {
						$(oldParent).remove();
					}
					//expander = $('img.expandImage', this.parentNode);
					
					pere = this.parentNode;
				
					if (dropped.id.match (/fich_.*/))var fichier = true;
					else var fichier = false;

					var transfert = Array ();
					
					var i;
					var tabInput = document.getElementsByTagName("input");
					var n = tabInput.length;
					 
					for (i=0; i<n; i++)
					{
						if (tabInput[i].type.toLowerCase()=="checkbox")
							if (tabInput[i].checked==true)
					 			if (tabInput[i].value != "on")
									if (fichier && tabInput[i].name.match (/id_f/)) transfert[transfert.length] = tabInput[i].value;
									else if (!fichier) transfert[transfert.length] = tabInput[i].value;
					}
								
					var listParam = "";
					for (i = 0; i<transfert.length; i++) {
						listParam += "&" + ((fichier) ? "fichiers[]=" : "id_sds[]=" ) + transfert[i];	
					}
					if (transfert.length==0) listParam += ((fichier) ? '&fichiers=' + dropped.id.substr(5) : '&id_sds=' + dropped.id);
					
					if (fichier) {

						if (pere.id.match (/sds_.*/)) pere.id = pere.id.substr (4);
						if (pere.id == "") {
							alert ('Vous ne pouvez pas glisser un fichier à la racine.');
							location.href='index.php?page=projet&action=trid&projet='+getProjet();
						}
						else {
							location.href='index.php?page=fichier&projet='+getProjet()+'&action=deplacerPlusieurs&vers='+pere.id+listParam;
						}
					}
					else if (pere.id.match (/sds_.*/)) {
						alert ('Vous ne pouvez pas glisser un sous-dossier dans un autre sous-dossier.');
						location.href='index.php?page=projet&action=trid&projet='+getProjet(); }
					else {
						if (pere.id == "") {
							location.href='index.php?page=projet&action=SdsVersDs'+listParam+'&projet='+getProjet();
						}
						else if (pere.id == "sommet")  {
							location.href='index.php?page=projet&action=deplacerDs&nouveauDs=0'+listParam+'&projet='+getProjet();
						}
						else {
							location.href='index.php?page=projet&action=deplacerSds&nouveauDs='+pere.id+listParam+'&projet='+getProjet();
						}
					}
					/*
					$(document).ready(function () { // On verifie que la page est chargée
							$.post("ged/index.php.php",{nouveauDs: pere.id, id_sds: dropped.id, projet: getProjet(), page:"projet", action:"deplacer"},
							function success(data){ // Au succès on renvoie le résultat de la requête
							alert (réussi);
							});
					});*/
					
				}
			}
		);
		
		$('span.textHolderSub').Droppable(
			{
				accept			: 'treeItem',
				hoverclass		: 'dropOver',
				activeclass		: 'fakeClass',
				tollerance		: 'pointer',
				onhover			: function(dragged)
				{
					if (!this.expanded) {
						subbranches = $('ul', this.parentNode);
						if (subbranches.size() > 0) {
							subbranch = subbranches.eq(0);
							this.expanded = true;
							if (subbranch.css('display') == 'none') {
								var targetBranch = subbranch.get(0);
								this.expanderTime = window.setTimeout(
									function()
									{
										$(targetBranch).show();
										$.recallDroppables();
									},
									500
								);
							}
						}
					}
				},
				onout			: function()
				{
					if (this.expanderTime){
						window.clearTimeout(this.expanderTime);
						this.expanded = false;
					}
				}
			}
		);
		$('li.treeItem').Draggable(
			{
				revert		: true,
				autoSize		: true,
				ghosting			: true
			}
		);
	}
);

/// Fonction diverses de fonctionnement du site.

function formulaireGraphAjout () {
	
	if(document.getElementById('radioPrestataire').checked == true) {
		document.getElementById('lot').disabled='disabled';
		document.getElementById('prestataire').disabled='';	
	}
	else if (document.getElementById('radioLot').checked == true){
		document.getElementById('prestataire').disabled='disabled';
		document.getElementById('lot').disabled='';
	}
	else {
		document.getElementById('lot').disabled='disabled';
		document.getElementById('prestataire').disabled='disabled';
	}
}

function remplissageFormSoc() {
	
	soc = document.getElementById('societe');
	infos = document.getElementById(soc.value);
	document.getElementById('lot').value='';
	if (infos != null) {
		var info = infos.value.split('/');
		document.getElementById('lot').value=info[2];
		document.getElementById('nouvelleSociete').value='';
	}
	else {
		info = new Array ('', '');
	}
	
	document.getElementById('tel').value=info[0];
	document.getElementById('fax').value=info[1];
	document.getElementById('activite').value=info[3];
}