/*
	A simple class for displaying file information and progress
	Note: This is a demonstration only and not part of SWFUpload.
	Note: Some have had problems adapting this class in IE7. It may not be suitable for your application.
*/

// Constructor
// file is a SWFUpload file object
// targetID is the HTML element id attribute that the FileProgress HTML structure will be added to.
// Instantiating a new FileProgress object with an existing file will reuse/update the existing DOM elements
function FileProgress(file, targetID, categorieUpload) {
	
	this.fileProgressID = file.id;

	this.opacity = 100;
	this.height = 0;

	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));

		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(file.name));

		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";
		progressBar.style.height = '5px';
		progressBar.style.backgroundColor = 'blue';

		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";

		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);

		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
	}

	this.height = this.fileProgressWrapper.offsetHeight;

}
FileProgress.prototype.setProgress = function (percentage, categorieUpload) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";

	
	switch(categorieUpload)
	{
		case 'musique': $('myprcMusique').update(percentage + " %"); 
		break;
		
		case 'pub': $('myprcPub').update(percentage + " %");
		break;
		
		case 'cv': $('myprcCv').update(percentage + " %");
		break;
		
		case 'doc': $('myprcDoc').update(percentage + " %");
		break;
		
		case 'photo': $('myprcPhoto').update(percentage + " %");
		break;
		
		case 'offreEmploi': $('myprcOffreEmploi').update(percentage + " %");
		break;
		
		case 'demande': $('myprcDemande').update(percentage + " %");
		break;
		
	}
	
};
FileProgress.prototype.setComplete = function (categorieUpload) {

	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";
	

	
	switch(categorieUpload)
	{
		case 'musique': $('myprcMusique').update('Merci de patienter...');
		break;
		
		case 'pub': $('myprcPub').update('Merci de patienter...');
		break;
		
		case 'cv': $('myprcCv').update('Merci de patienter...');
		break;
		
		case 'doc': $('myprcDoc').update('Merci de patienter...');
		break;
		
		case 'photo': $('myprcPhoto').update('Merci de patienter...');
		break;
		
		case 'offreEmploi': $('myprcOffreEmploi').update('Merci de patienter...');
		break;
		
		case 'demande': $('myprcDemande').update('Merci de patienter...');
		break;
		
	}

	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear(categorieUpload);
	}, 5000);
};
FileProgress.prototype.setError = function (categorieUpload) {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
	
	switch(categorieUpload)
	{
		case 'musique': $('myprcMusique').update("0 %");
		break;
		
		case 'pub': $('myprcPub').update("0 %");
		break;
		
		case 'cv': $('myprcCv').update("0 %");
		break;
		
		case 'doc': $('myprcDoc').update("0 %");
		break;
		
		case 'photo': $('myprcPhoto').update("0 %");
		break;
		
		case 'offreEmploi': $('myprcOffreEmploi').update("0 %");
		break;
		
		case 'demande': $('myprcDemande').update("0 %");
		break;
	}

	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear(categorieUpload);
	}, 5000);
};
FileProgress.prototype.setCancelled = function (categorieUpload) {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
	
	switch(categorieUpload)
	{
		case 'musique': $('myprcMusique').update("0 %");
		break;
		
		case 'pub': $('myprcPub').update("0 %");
		break;
		
		case 'cv': $('myprcCv').update("0 %");
		break;
		
		case 'doc': $('myprcDoc').update("0 %");
		break;
		
		case 'photo': $('myprcPhoto').update("0 %");
		break;
		
		case 'offreEmploi': $('myprcOffreEmploi').update("0 %");
		break;
		
		case 'offreDemande': $('myprcDemande').update("0 %");
		break;
		
	}

	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear(categorieUpload);
	}, 2000);
	

};


FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};

// Show/Hide the cancel button
FileProgress.prototype.toggleCancel = function (show, swfUploadInstance) {
	
	
	return;
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfUploadInstance) 
	{
		var fileID = this.fileProgressID;
		/*
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfUploadInstance.cancelUpload(fileID);
			return false;
		};
		*/
	}
};

// Fades out and clips away the FileProgress box.
FileProgress.prototype.disappear = function (categorieUpload) {
	
	
	// Identification de l'appellant

	/*---------*/
	
	switch(categorieUpload)
	{
		case 'musique': 
			
				$('new_musique').hide();
		
				$$('.progressWrapper').first().remove();
								
				$('myprcMusique').update('');
				
				$('conteneur').insert({bottom: $('new_musique')});
				
			
				new Ajax.Request('js/ajax.php', {asynchronous: 'false', method: 'get', parameters:'id_appel=14', onComplete: function(req){
					if (200 == req.status){
						
						var idLigne = req.responseText;
						
						var td_un = new Element('td').setStyle({height: '20px'}).update('<span class="auteurMusique" title="Clic pour modifier">Nom de l\'auteur</span>');
						var td_de = new Element('td').setStyle({height: '20px'}).update('<span class="titreMusique" title="Clic pour modifier">Titre de la musique</span>');
						var td_tr = new Element('td').setStyle({height: '20px'}).update('<span class="actifMusique" title="Clic pour modifier">oui</span>');
						var td_qu = new Element('td').setStyle({height: '20px'}).addClassName('imageSupp').update('<img style="cursor: pointer; display: none;" alt="Supprimer" soundid="'+idLigne+'" src="img/cancel.jpg"/>');
						var tr = new Element('tr', {id: 'ligne_'+idLigne}).setStyle({backgroundColor: '#ffffff'}).insert({top: td_qu});
						    tr.insert({top: td_tr});
						    tr.insert({top: td_de});
						    tr.insert({top: td_un});
					
						$('musiqueTab').down('tr').insert({after: tr});
						
						new Effect.Highlight(tr, {startcolor: '#ffff99',endcolor: '#fffffff' });
				
							 tr.observe('mouseover',function(e){
							 	
							 	tr.setStyle({backgroundColor: '#EDECEC'}); 
							 	tr.down('.imageSupp').down('img').show(); 
							 	
							 	
							});
								
						
							tr.observe('mouseout',function(e){
								tr.setStyle({backgroundColor: '#ffffff'});  
								tr.down('.imageSupp').down('img').hide();
								
							});
						 	 
						 	new Ajax.InPlaceEditor(tr.down('.auteurMusique'), 'js/ajax.php?id_appel=11&idSound='+idLigne,
							{
						 	 	loadingText: 'Chargement...',
								savingText: 'Enregistrement...',
								cancelText: 'Annuler',
								clickToEditText: 'Clic pour modifier',
								hoverClassName: 'cursor'
						 	}); 
					
						 	new Ajax.InPlaceEditor(tr.down('.titreMusique'), 'js/ajax.php?id_appel=12&idSound='+idLigne,
							{
						 	 	loadingText: 'Chargement...',
								savingText: 'Enregistrement...',
								cancelText: 'Annuler',
								clickToEditText: 'Clic pour modifier',
								hoverClassName: 'cursor'
						 	}); 
					
						 	new Ajax.InPlaceEditor(tr.down('.actifMusique'), 'js/ajax.php?id_appel=13&idSound='+idLigne,
							{
								collection:[[0, 'non'],[1, 'oui']],
						 	 	loadingText: 'Chargement...',
								savingText: 'Enregistrement...',
								cancelText: 'Annuler',
								clickToEditText: 'Clic pour modifier',
								hoverClassName: 'cursor'
						 	}); 
					
						 	var soundNum = tr.down('img').getAttribute("soundId");
						 	tr.down('img').observe('click',function(e){confirm('Etes vous sûr de vouloir supprimer cette entrée?', 'deleteSound("'+soundNum+'")')});
					
						
				}}});	
		
				
		
		
		
		break;
		
		case 'pub': 
			
				$('new_pub').hide();
		
				$$('.progressWrapper').first().remove();
								
				$('myprcPub').update('');
				
				$('conteneur').insert({bottom: $('new_pub')});
					
				new Ajax.Request('js/ajax.php', {asynchronous: 'false', method: 'get', parameters:'id_appel=22', onComplete: function(req){
					if (200 == req.status){
						
						var idLigne = req.responseText;
						
						var td_un = new Element('td').setStyle({height: '20px'}).update('<span class="nomSocietePub">Nom de la société</span>');
						var td_de = new Element('td').setStyle({height: '20px'}).update('<span class="lienPub">Lien site web</span>');
						var td_tr = new Element('td').setStyle({height: '20px', textAlign: 'center'}).update('<span class="isActifPub">Non</span>');
						var td_qt = new Element('td').setStyle({height: '20px', textAlign: 'center'}).update('<span class="emplacementPub">Haut</span>');
						var td_ci = new Element('td').setStyle({height: '20px'}).addClassName('imageSupp').update('<img style="cursor: pointer; display: none;" alt="Supprimer" pubId="'+idLigne+'" src="img/cancel.jpg"/>');
						var tr = new Element('tr', {id: 'ligne_'+idLigne}).setStyle({backgroundColor: '#ffffff'}).insert({top: td_ci});
						    tr.insert({top: td_qt});
							tr.insert({top: td_tr});
						    tr.insert({top: td_de});
						    tr.insert({top: td_un});
					
						$('contactTab').down('tr').insert({after: tr});
						
						new Effect.Highlight(tr, {startcolor: '#ffff99',endcolor: '#fffffff' });
				
							 tr.observe('mouseover',function(e){
							 	
							 	tr.setStyle({backgroundColor: '#EDECEC'}); 
							 	tr.down('.imageSupp').down('img').show(); 
							 	
							 	
							});
								 
							tr.observe('mouseout',function(e){
								tr.setStyle({backgroundColor: '#ffffff'});  
								tr.down('.imageSupp').down('img').hide();
								
							});
						 	
						 	new Ajax.InPlaceEditor(tr.down('.nomSocietePub'), 'js/ajax.php?id_appel=23&idPub='+idLigne,
							{
							 	loadingText: 'Chargement...',
								savingText: 'Enregistrement...',
								cancelText: 'Annuler',
								clickToEditText: 'Clic pour modifier',
								hoverClassName: 'cursor'
							}); 

						 	new Ajax.InPlaceEditor(tr.down('.lienPub'), 'js/ajax.php?id_appel=24&idPub='+idLigne,
							{
						 	 	loadingText: 'Chargement...',
								savingText: 'Enregistrement...',
								cancelText: 'Annuler',
								clickToEditText: 'Clic pour modifier',
								hoverClassName: 'cursor'
						 	}); 
					
						 	new Ajax.InPlaceCollectionEditor(tr.down('.isActifPub'), 'js/ajax.php?id_appel=25&idPub='+idLigne,
							{
								collection:[[0, 'Non'],[1, 'Oui']],
						 	 	loadingText: 'Chargement...',
								savingText: 'Enregistrement...',
								cancelText: 'Annuler',
								clickToEditText: 'Clic pour modifier',
								hoverClassName: 'cursor'
						 	}); 
						 	
						 	new Ajax.InPlaceCollectionEditor(tr.down('.emplacementPub'), 'js/ajax.php?id_appel=26&idPub='+idLigne,
							{
								collection:[['H', 'Haut'],['B', 'Bas']],
						 	 	loadingText: 'Chargement...',
								savingText: 'Enregistrement...',
								cancelText: 'Annuler',
								clickToEditText: 'Clic pour modifier',
								hoverClassName: 'cursor'
						 	}); 
					
						 	var soundNum = tr.down('img').getAttribute("pubId");
						 	tr.down('img').observe('click',function(e){confirm('Etes vous sûr de vouloir supprimer cette entrée?', 'deletePub("'+soundNum+'")')});
					
					
				}}});			
		
		
		
		
		break;
		
		case 'cv': $('myprcCv').update("");
		break;
		
		case 'photo': 
		
				$('new_photo').hide();
		
				$$('.progressWrapper').first().remove();
								
				$('myprcPhoto').update("");
				
				$('conteneur').insert({bottom: $('new_photo')});
				
				new Ajax.Request('js/ajax.php', {asynchronous: 'false', method: 'get', parameters:'id_appel=46', onComplete: function(req){
					if (200 == req.status)
						$('myPic').src = req.responseText;
						
						if(!$('deletePhoto'))
						{
							$('addPhoto').insert({after: '&nbsp;|&nbsp;<a href="javascript:void(0);" id="deletePhoto" class="actuLien">Supprimer la photo</a>'});						
							
							$('deletePhoto').observe('click',function(e){			
								$('myPic').src='img/nophoto.jpg';
								
								new Ajax.Request('js/ajax.php', {method: 'get', parameters:'id_appel=47'});	
								$('deletePhoto').remove();		
							});
						
						}
						
					}
				});

		
		break;
		
		case 'doc': $('myprcDoc').update("");
		
				$('new_doc').hide();
		
				$$('.progressWrapper').first().remove();
								
				$('myprcDoc').update('');
				
				$('conteneur').insert({bottom: $('new_doc')});
					
				new Ajax.Request('js/ajax.php', {asynchronous: 'false', method: 'get', parameters:'id_appel=36', onComplete: function(req){
					if (200 == req.status){
						
						var idLigne = req.responseText;
						
						var td_un = new Element('td').setStyle({height: '20px'}).update('<span class="titreDocument">Nom</span>');
						var td_de = new Element('td').setStyle({height: '20px'}).update('<span class="motcleDocument">mots, clés, séparés, par, une, virgule</span>');
						var td_tr = new Element('td').setStyle({height: '20px'}).addClassName('imageSupp').update('<img style="cursor: pointer; display: none;" alt="Supprimer" docId="'+idLigne+'" src="img/cancel.jpg"/>');
						var tr = new Element('tr', {id: 'ligne_'+idLigne}).setStyle({backgroundColor: '#ffffff'}).insert({top: td_tr});
						    tr.insert({top: td_de});
						    tr.insert({top: td_un});
					
						$('contactTab').down('tr').insert({after: tr});
						
						new Effect.Highlight(tr, {startcolor: '#ffff99',endcolor: '#fffffff' });
				
							 tr.observe('mouseover',function(e){
							 	
							 	tr.setStyle({backgroundColor: '#EDECEC'}); 
							 	tr.down('.imageSupp').down('img').show(); 
							 	
							 	
							});
								 
							tr.observe('mouseout',function(e){
								tr.setStyle({backgroundColor: '#ffffff'});  
								tr.down('.imageSupp').down('img').hide();
								
							});
						 	
						 	new Ajax.InPlaceEditor(tr.down('.titreDocument'), 'js/ajax.php?id_appel=37&idDoc='+idLigne,
							{
							 	loadingText: 'Chargement...',
								savingText: 'Enregistrement...',
								cancelText: 'Annuler',
								clickToEditText: 'Clic pour modifier',
								hoverClassName: 'cursor'
							}); 

						 	new Ajax.InPlaceEditor(tr.down('.motcleDocument'), 'js/ajax.php?id_appel=38&idDoc='+idLigne,
							{
						 	 	loadingText: 'Chargement...',
								savingText: 'Enregistrement...',
								cancelText: 'Annuler',
								clickToEditText: 'Clic pour modifier',
								hoverClassName: 'cursor'
						 	}); 
					
						 	var id = tr.down('img').getAttribute("docId");
						 	tr.down('img').observe('click',function(e){confirm('Etes vous sûr de vouloir supprimer cette entrée?', 'deleteDoc("'+id+'")')});
					
					
				}}});	
		
		break;
		
		case 'offreEmploi': 
		
				$('new_offreEmploi').hide();
		
				$$('.progressWrapper').first().remove();
								
				$('myprcOffreEmploi').update("");
				
				$('conteneur').insert({bottom: $('new_offreEmploi')});
				
				$('addOffreEmploipj').hide();	
				
				$('uploadCV').update('1 Fichier joint [<a href="javascript:void(0);" class="actuLien" id="supprimeOffreEmploi">supprimer</a>]');	
				
				$('supprimeOffreEmploi').observe('click', function(event){
					new Ajax.Request('js/ajax.php', {method: 'get', parameters:'id_appel=55'});
					$('uploadCV').update('');
					$('addOffreEmploipj').show();
				});			
						
		break;
		
		case 'demande': 
		
				$('new_demande').hide();
		
				$$('.progressWrapper').first().remove();
								
				$('myprcDemande').update("");
				
				$('conteneur').insert({bottom: $('new_demande')});
				
				$('addDemandepj').hide();	
				
				$('uploadCV').update('1 Fichier joint [<a href="javascript:void(0);" class="actuLien" id="supprimeDemande">supprimer</a>]');	
				
				$('supprimeDemande').observe('click', function(event){
					new Ajax.Request('js/ajax.php', {method: 'get', parameters:'id_appel=57'});
					$('uploadCV').update('');
					$('addDemandepj').show();
				});			
						
		break;
		
		
	}

};