/*****************************************************************************************
** © 2008 POULAIN Nicolas – nicolas.poulain@ouvaton.org **
** **
** Ce fichier est une partie du logiciel libre CollectionLibre, licencié **
** sous licence "CeCILL version 2". **
** La licence est décrite plus précisément dans le fichier : LICENSE.txt **
** **
** ATTENTION, CETTE LICENCE EST GRATUITE ET LE LOGICIEL EST **
** DISTRIBUÉ SANS GARANTIE D'AUCUNE SORTE **
** ** ** ** **
** This file is a part of the free software project CollectionLibre,
** licensed under the "CeCILL version 2". **
**The license is discribed more precisely in LICENSES.txt **
** **
**NOTICE : THIS LICENSE IS FREE OF CHARGE AND THE SOFTWARE IS DISTRIBUTED WITHOUT ANY **
** WARRANTIES OF ANY KIND **
*****************************************************************************************/

function affiche_image(objet,image) {
	montre2('opaque') ;
	montre2('deux') ;
	image = document.images['image_principale'].src ;
	image = image.replace('220x220','640x480') ;
	change_image('zoom',image) ;
}

function change_image(nom,source) {
	document.images[nom].src = source ;
}
function montre2(id) {
	//var d = document.getElementById(id);
	//if ( d.style.display == "block" ) d.style.display = "none" ;
	//else d.style.display = "block" ;

	var d = document.getElementById(id);
	if ( d.style.display == "block" ) $(d).fadeOut("slow") ;
	else $(d).fadeIn("slow") ;

}

function switch_display(id) {
	// nécesssite jquery
	if ( $('#'+id).css("display") == "none" ) $('#'+id).show();
	else $('#'+id).hide();
	return false;
}

// a jeter
function montre_back(id) {
	for ( var i=1 ; i<6 ; i++ ) {
		var nom = "onglet"+i ;
		document.getElementById(nom).style.display = "none" ;
	}
	document.getElementById(id).style.display = "block" ;
}

function montre(id) {
	// appliqué aux onglets
	// cache les class=onglet
	$('.onglet').hide() ;
	$('.li_onglet').css({backgroundColor: '#FFF' }) ;
	$('#li_'+id).css({backgroundColor: '#E2E2E2' }) ;
	// montre celui passé en argument
	//$('#'+id).show() ;
	$('#'+id).fadeIn() ;
}

var fenetre ;
function Ouvre_Fenetre(adresse) {
	var f_width=450 ; var f_height=270 ;
	var dist_left = (screen.width - f_width)/2 ;
	var dist_top = (screen.height - f_height)/2 ;
	var Options = "scrollbars=yes,resizable,height="+f_height+",width="+f_width+",top="+dist_top+",left="+dist_left+",screenX="+dist_left+",screenY="+dist_top ;
	// fermeture si déjà ouverte pour pas que ça déconne
	if ( fenetre && !fenetre.closed ) fenetre.close() ;
	fenetre = window.open(adresse,'fenetre',Options);
	//return fenetre ;
}
// même que ci-dessus mais taille paramétrable
function Ouvre_Fenetre2(adresse,f_width,f_height) {
	var dist_left = (screen.width - f_width)/2 ;
	var dist_top = (screen.height - f_height)/2 ;
	var Options = "scrollbars=yes,resizable,height="+f_height+",width="+f_width+",top="+dist_top+",left="+dist_left+",screenX="+dist_left+",screenY="+dist_top ;
	// fermeture si déjà ouverte pour pas que ça déconne
	if ( fenetre && !fenetre.closed ) fenetre.close() ;
	fenetre = window.open(adresse,'fenetre',Options);
	//return fenetre ;
}
function enlarge(indice) {
	var new_height ;
	temp_height = $('textarea#valeur'+indice).css("height") ;
	var pattern = /^([0-9]*)(\.[0-9]*){0,1}px$/ ;
	var result = pattern.exec(temp_height) ;
	//alert(result[0]+' '+result[1]+' '+result[2]+' '+result[3]) ;
	var new_height = parseInt(result[1]) + 50 + 'px' ;
	$('textarea#valeur'+indice).css({height: new_height }) ;
}
function non_enlarge(indice) {
	$('textarea#valeur'+indice).css({height:'40.53px'}) ;
}
function CookieLire(Nom) {
	var Paires = document.cookie.split('; ');
	for (var i = 0; i < Paires.length; i++){
		Paires[i] = Paires[i].split('=') ;
		if (Paires[i][0] == Nom) {
			return unescape(Paires[i][1]);
		}
	}
	return null;
}

function save_position_image(indice) {
	var i = 0 ;
	var data = "" ;
	while ( $("#"+i).css("zIndex") ) {
		data += $("#"+i).css("zIndex") +
			"," +
			$("#"+i).css("top") +
			"," +
			$("#"+i).css("left") +
			"," ;
		// par sécurité
		if ( i>40 ) break ;
		i++ ;
	}
	//alert(data) ;
	document.cookie = "position"+indice+"="+data ;
}

// pas utilisé, car traité en php
/*function load_position_image() {
	var tab = Array ;
	var position = CookieLire("position") ;

	if ( position != "" ) {
		var i = 0 ;
		tab = position.split(",") ;
		while ( $("#"+i).css("zIndex") ) {
			// attribution des valeurs
			$("#"+i).css({zIndex: tab[3*i] }) ;
			$("#"+i).css({top: tab[3*i + 1] }) ;
			$("#"+i).css({left: tab[3*i + 2] }) ;

			// par sécurité
			if ( i>40 ) break ;
			i++ ;
		}
	}
	document.cookie = "position=" ;
}*/

// à faire : fusionner les deux fonctions
function add_selection(separateur,champs, valeur) {
	with ( opener.document.formulaire[champs] ) {
		if ( value == "" ) value = valeur ;
		else {
			value += " " + separateur + " " + valeur ;
		}
	}
}
// champs : champs de destination
// valeur : valeur à insérer dans le champ de destination
// separateur : élément séparateur si le champs contient déjà une donnée
function add_num_inventaire(before,after,champs,origine,incremente) {
	var valeur = origine.value ;
	if (incremente) {
		var pattern = /^(.*[^0-9])([0-9]+$)/ ;
		var result = pattern.exec(valeur) ;
		var new_indice = parseInt(result[2]) + 1 ;
		origine.value = result[1]+new_indice ;
	}
	champs.value += before + valeur + after ;
	return true ;
}

function rotate_infos_image(texte,div) {
	$(div).fadeOut(function(){
		$(div).empty() ;
		$(div).append(texte);
	});
	$(div).fadeIn('slow') ;
	return false ;
}

// fonction spéciale pour formulaire_image
function infos_auto(formulaire,mode) {
	var chaine = formulaire['file'].value ;
	// découpe sur \ ou / suivant OS win ou Linux
	var reg = new RegExp("[\\\\/]+","g");
	var tableau = chaine.split(reg) ;
	if ( mode == 'document' ) {
		formulaire['original_document'].value = tableau[tableau.length-1] ;
		formulaire['dossier_document'].value = tableau[tableau.length-2] ;
	}
	else {
		formulaire['original_image'].value = tableau[tableau.length-1] ;
		formulaire['dossier_image'].value = tableau[tableau.length-2] ;
	}
}

function maj_help_structure(prefix,ouv,ferm) {
	document.getElementById(prefix+'_help_structure_result').value = ouv + document.getElementById(prefix+'_help_code').value + ferm ;
}