$(document.body).ready(function () {
	$('.guideBox').tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    extraClass: "right" 
	});
	$('.temoin').tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    extraClass: "right" 
	});
	$('.talkingTemoin').tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    extraClass: "smallTootip"
	});
	$('.person').tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    extraClass: "smallTootip" 
	});
	
	/* Filtre Etudes & analyses */
	
	$('#filterCol2 input').click(function(){
		if ($('#filterCol2 input:checked').length==0) $('#t_all').attr('checked','checked');
		else if ($(this).attr('id')=='t_all') {
			$('#filterCol2 input').attr('checked',false);
			$('#t_all').attr('checked','checked');
		}
		else if ($(this).attr('id')!='t_all') $('#t_all').attr('checked',false);
	});
	
	$('#filter input').click(function(){
		$('.article').attr('rel','').hide();
		
		var filter1 = false;			
		var filter2 = false;
		$('#filterCol1 input:checked, #filterCol3 input:checked').each(function(i,elm) {				
			$('.article.'+$(elm).attr('id')).attr('rel','1');
		});
		
		$('#filterCol2 input:checked, #filterCol4 input:checked').each(function(i,elm) {
			if ($(elm).attr('id')=='t_all') $('.article[rel=1]').attr('rel','12');
			else $('.article[rel=1].'+$(elm).attr('id')).attr('rel','12');
		});
		
		$('#filterCol0 input:checked, #filterCol5 input:checked').each(function(i,elm) {
			$('.article[rel=12].'+$(elm).attr('id')).attr('rel','123');
		});
		
		$('#filterTotal span').html($('.article[rel=123]').length);
		$('#filterTotal').show();
		$('.article[rel=123]').show();
	});
	
	$('.selectFilter').each(function(i,elm){
		$(elm).click(function(){
			if($(elm).children('.listeFilter').css('display')=='none'){
				$('.listeFilter:visible').slideUp();
				$('.listeFilter:visible').prev().css('border-bottom-width', '1px');
				
				$(elm).children('.labelFilter').css('border-bottom-width', '0px');
				$(elm).children('.listeFilter').slideDown();
				
			}
		});
		$(elm).children('.labelFilter').click(function(){
			$(elm).children('.listeFilter:visible').slideUp(function(){
				$(elm).children('.labelFilter').css('border-bottom-width', '1px');	
			});
			
		});
		
	});
	$('*').click(function(evt){
		elm =  $(evt.target);
		
		if( elm.parents('.selectFilter').length==0){
			$('.listeFilter:visible').slideUp();
			$('.listeFilter:visible').prev().css('border-bottom-width', '1px');
					
		}
	});
	
	/* INTRANET */
	
	$('.editable').click(function() {
		var link = $(this);
		var boxHeight = 150;
		if ($(this).hasClass('big')) boxHeight = 400;
		
		$('<div id="updateDiv"></div>').dialog({
		modal: true,
		open: function ()
		{				
			$(this).load(domain+'/do/editUfapec.php?info='+$(link).attr('rel'),'',function(){});
		},							
		height: boxHeight,
		width: 350,
		title: 'Mise-à-jour : '+$(link).prevAll('.label:first').html(),
		buttons: [				
			{
				text: 'Mettre-à-jour',
				click: function() { 
					$.ajax({
					   type: "POST",
					   url: domain+'/do/editUfapec.php?action=update&info='+$(link).attr('rel'),
					   data: $('#updateDiv form').serialize(),
					   success: function(msg){							
							if (!msg) // success
							{
								var curVal = $(link).prevAll('.currentValue:first').html();
								var newVal = $('#updateDiv input').val();
								if ($('#updateDiv select').length>0) newVal = $('#updateDiv select option:selected').html();  //$('#updateDiv select').val();
								
								if (curVal!=newVal) {
									$(link).prevAll('.currentValue:first').addClass('old');
									$(link).prevAll('.suggestedValue:first').html(newVal);
								}
								else {
									$(link).prevAll('.currentValue:first').removeClass('old');
									$(link).prevAll('.suggestedValue:first').html('');
								}
								
								$('#updateDiv').dialog('close');
							}
							else // fail
							{
								if (msg=='reload') document.location.reload();
								else alert(unescape(msg));
							}
					   }
					});	
					
					
				}
			}
		],
		close: function() {
			$('#updateDiv').remove();
		}
		});
		
		return false;
	});
});

function printGuide(full)
{
	if (full)
	{
		$('.guideBox').addClass('boxToPrint');
		$('#guidePic').addClass('picToPrint');
	}
	else
	{
		$('.guideBox').removeClass('boxToPrint');
		$('#guidePic').removeClass('picToPrint');
	}
	
	window.print();
}

function printGuide_all(url,name)
{	
	window.open(url,'','resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=yes,width=600,height=500,left=100,top=160');
	//if (w==null) alert('Votre navigateur semble avoir bloqué l\'ouverture du popup');
}
