var actionPublication = 'main.php?action=ajx_publication';
var animation1Over = true;
var animation2Over = true;
var animationTimer;
var pubHtml = '';
var pubJS = '';


function showPub(pubId) {
	animation1Over = false;
	animation2Over = false;
	
	// On fixe la taille du bloc avant de mettre le wait (pour éviter que la page ne rétrécisse)
	$('projetDetail').style.height = getSize($('projetDetail'))[1] + 'px';
	
	// Animation 1 : fade out du projet actuel, fade in du wait et projet futur
	JQ("#projetDetail").fadeOut(500,
			function() {
				// Affichage du wait
				$('projetDetail').innerHTML = getAjaxWait();
				$('projetDetail').className = '';
				JQ("#projetDetail").fadeIn(2000);
				animation1Over = true;
			});
	
	// Requête Ajax de récupération de la publication
	var params = 'pub_id=' + pubId;
	AJAXRequest(actionPublication, params, showPubReturn);
	
	// Animation 2 : copie du div sélectionné et déplacement vers la div de détail
	var detailLeft = getOffset($('projetDetail'))[0];
	var detailTop = getOffset($('projetDetail'))[1];
	var detailWidth = getSize($('projetDetail'))[0];
	
	// Positionnement du div qui se déplace sur le div d'origine
	var divBloc = $('projetBloc' + pubId);
	$('projetBlocMove').innerHTML = $('projetBloc' + pubId).innerHTML;
	$('projetBlocMove').style.position = 'absolute';
	$('projetBlocMove').style.left = getOffset(divBloc)[0] + 'px';
	$('projetBlocMove').style.top = getOffset(divBloc)[1] + 'px';
	$('projetBlocMove').style.width = getSize(divBloc)[0] + 'px';
	$('projetBlocMove').style.display = '';
	
	// Déplacement du div
	JQ("#projetBlocMove").animate({
			width: detailWidth,
			top: detailTop,
			left: detailLeft,
			opacity: 0.25
		}, 
		800, 
		'easeOutQuad', 
		function() {
			hideDiv('projetBlocMove');
			animation2Over = true;
		});
}

function showPubReturn(xhr) {
	var html = getXhrValueHTML(xhr);
	
	if (html != undefined && html != '' && html.indexOf('Fatal error') < 0) {
		// Parsing de la réponse Ajax
		ajaxResult = parseAjaxScript(html);

		pubHtml = ajaxResult[0];
		pubJS = ajaxResult[1];
		                                
		// On attend la fin de l'animation
		animationTimer = setInterval('displayPub()', 100);
	}
}

function displayPub() {
	if (animation1Over && animation2Over) {
		// Code HTML
		$('projetDetail').style.height = '';
		$('projetDetail').className = 'projetBloc';
		$('projetDetail').innerHTML = pubHtml;
		/*JQ("#projetDetail").fadeIn(0);*/
	
		// Exécution du code javascript
		executeJS(pubJS);

		clearInterval(animationTimer);
	}
}


/*******************************************************
********************************************************
* Publication
********************************************************
*******************************************************/
function publicationClose(action) {
	document.location = 'main.php?action=' + action;
}

function manageArpTypePieceJointe(pupId){
	// Première partie : vérification de la validité de l'action
	if ($('pup_objet_externe_' + pupId).value != '' 
			&& $('pup_type_piece_jointe_' + pupId).value != _prmIdArticleParagrapheTypePieceJointeObjetExterne) {
		$('pup_type_piece_jointe_' + pupId).value = _prmIdArticleParagrapheTypePieceJointeObjetExterne;
		showAlert($('publication_erreur_objet_externe_renseigne').value);
	} else if(($('doc_fichier_par_' + pupId).value !='' 
				|| parseInt($('doc_id_' + pupId).value) > 0) 
			&& $('pup_type_piece_jointe_' + pupId).value != _prmIdArticleParagrapheTypePieceJointeFichierJoint){
		$('pup_type_piece_jointe_' + pupId).value = _prmIdArticleParagrapheTypePieceJointeFichierJoint;
		showAlert($('publication_erreur_fichier_renseigne').value);
	}
	
	// Seconde partie : arrangement des éléments à afficher/masquer
	$('trFic_' + pupId + '_0').style.display = 'none';
	$('trFic_' + pupId + '_1').style.display = 'none';
	$('trFic_' + pupId + '_2').style.display = 'none';
	$('trFic_' + pupId + '_3').style.display = 'none';
	$('trFic_' + pupId + '_4').style.display = 'none';
	$('trFic_' + pupId + '_5').style.display = 'none';
	$('trDocLien_' + pupId + '_0').style.display = 'none';
	$('trDocLien_' + pupId + '_1').style.display = 'none';
	$('trObj_' + pupId + '_0').style.display = 'none';
	if($('pup_type_piece_jointe_' + pupId).value == _prmIdArticleParagrapheTypePieceJointeFichierJoint){
		$('trFic_' + pupId + '_0').style.display = '';
		$('trFic_' + pupId + '_1').style.display = '';
		$('trFic_' + pupId + '_2').style.display = '';
		$('trFic_' + pupId + '_3').style.display = '';
		$('trFic_' + pupId + '_4').style.display = '';
		$('trFic_' + pupId + '_5').style.display = '';
		if($('pup_doc_lien_type_' + pupId).value == _prmIdArticleParagrapheDocTypeLienUrl){
			$('trDocLien_' + pupId + '_0').style.display = '';
		} else if($('pup_doc_lien_type_' + pupId).value == _prmIdArticleParagrapheDocTypeLienArticleInterne){
			$('trDocLien_' + pupId + '_1').style.display = '';
		}
	} else if($('pup_type_piece_jointe_' + pupId).value == _prmIdArticleParagrapheTypePieceJointeObjetExterne) {
		$('trObj_' + pupId + '_0').style.display = '';
	}
}

function manageArpTypeDocLien(pupId){
	$('trDocLien_' + pupId + '_0').style.display = 'none';
	$('trDocLien_' + pupId + '_1').style.display = 'none';
	if($('pup_doc_lien_type_' + pupId).value == _prmIdArticleParagrapheDocTypeLienUrl){
		$('pub_id_externe_' + pupId).value = 0;
		$('trDocLien_' + pupId + '_0').style.display = '';
	} else if($('pup_doc_lien_type_' + pupId).value == _prmIdArticleParagrapheDocTypeLienArticleInterne){
		$('pup_url_externe_' + pupId).value = '';
		$('trDocLien_' + pupId + '_1').style.display = '';
	} else {
		$('pup_url_externe_' + pupId).value = '';
		$('pub_id_externe_' + pupId).value = 0;
	}
}

