/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function() {

	/* Tooltip */
	$('.tips').cluetip({
		tracking: true,
		//		positionBy: 'mouse',
		topOffset: -10,
		leftOffset: 25,
		fx:{
			open:'fadeIn',
			openSpeed:'500'
		},
		clickThrough: true,
		cursor: 'pointer',
		showTitle: false
	});
	

	/* accordeon */
	// On les caches tous des le départ.
	$('.div_toggle').hide();

	// Quand on demande d'fficher ou de cacher.
	$('.toggle').click(function() {
		/* On récupère l'id et le no du bloc*/
		var toggle_id = $(this).attr('id');
		var id = getId(toggle_id);
		var noBloc = getNoBloc(toggle_id);

		/* On modifie l'icon */
		$('.fugue-control-270').removeClass('fugue-control-270').addClass('fugue-control');
		if($('#div_'+id+'_'+noBloc).is(':visible')){
			$('#span_'+id+'_'+noBloc).removeClass('fugue-control-270').addClass('fugue-control');
		// S'il est visible, c'est qu'on va le fermer! Donc il reprend son état initial.
		} else {
			$('#span_'+id+'_'+noBloc).removeClass('fugue-control').addClass('fugue-control-270');
		// S'il n'est pas visible, on va l'ouvrir, donc on change l'icon
		}

		/* On affiche ou cache le contenu */

		// Si c'est safari ou chrome on désactive le slow.
		var slow = 'slow';
		if ($.browser.webkit) slow = '';

		var actuelOuvert = false;
		if ($('#div_'+id+'_'+noBloc).is(':visible')) actuelOuvert = true;

		$('.div_toggle:visible').hide(slow);
		if(actuelOuvert) $('#div_'+id+'_'+noBloc).hide(slow);
		else $('#div_'+id+'_'+noBloc).show(slow);
		
		
		/* On annule le lien */
		return false;
	})

	loadPreview();
	

});
function getNoBloc(bloc){
	// Forme d'un id:
	//     xxx_id_noBloc
	// ex: aPreview_186_6340
	var debutId = bloc.indexOf('_') + 1;
	var finId = bloc.length;
	var semiId = bloc.substring(debutId, finId);

	debutId = semiId.indexOf('_') + 1;
	var id = semiId.substring(debutId, finId);
	return id;
}
function getId(myId){
	// Forme d'un id:
	//     xxx_id_noBloc
	// ex: aPreview_186_6340
	var debutId = myId.indexOf('_') + 1;
	var finId = myId.length;
	var semiId = myId.substring(debutId, finId);

	debutId = 0;
	finId = semiId.indexOf('_');
	var id = semiId.substring(debutId, finId);
	return id;
}

function loadPreview(){
	$('.preview').click(function(){
		setPreview($(this).attr('id'));
		
	});
	return false;
}

function setPreview(new_id){
	/* On récupère l'id */
	var id = getId(new_id);
	var link = '?id=78&type=3417&tx_lsdocuments_pi1[action]=previewDoc&tx_lsdocuments_pi1[docId]='+id;
	$.ajax({
		url : link,
		success: function(data) {
			/* On charge le fichier */
			$('.imagesPreview').html(data);
			$('.imagesPreview a').colorbox({height:'95%',photo:true});
			$("#imagePreview_0").click();
		}
	});
	return false;
}

