
var token = 1 ;
var AjaxWorking = 0;
var AjaxLoadBlock = '';
var AjaxReady = '';

var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) <= 8);

$(document).ready(function(){
	
	if(window.pngFixCharged){
		$('.jsPngFix').each(function(){
			$(this).parent().pngFix();
		});
	}

	//Gestion des onglets
	$("#annuaireHeader a").click( function() {
		if(!$(this).hasClass("active")){
			var item = $(this).attr("id");
			$("#annuaireHeader a").removeClass("active");
			$(this).addClass("active");
			if(item == 'tab-2'){
				$("#annuaireContent .tab-1").animate({marginLeft: "-729px"}, 400);
			}
			else if(item == 'tab-1'){
				$("#annuaireContent .tab-1").animate({marginLeft: "0"}, 400);
			}
			//$("#annuaireContent div[class^='tab']").hide();
			//$("#annuaireContent ."+item).show();
		};
	});

	//Gestion des inputs
	var valeur = new Array();
	var modify = new Array();
	$("input.searchInput").focus(function(){
		var id = $(this).attr("id");

		if(!valeur[id] || valeur[id] == ''){
			valeur[id] = $(this).val();
		}

		if(!modify[id] || modify[id] == '' || modify[id] == 0){
			$(this).val('');
			modify[id] = 0;
		}

		$(this).keypress(function(){
			modify[id]++;
		});
	});
	$("input.searchInput").blur(function(){
		var id = $(this).attr("id");

		if($(this).val() == '') $(this).val(valeur[id]);
	});

	//Gestion des checkbox
	$("input:checkbox[checked!='checked']").click(function(){
		var value = $(this).val();
		var name  = $(this).attr("name");
		$("input:checkbox:checked[name='"+name+"'][value!='"+value+"']").removeAttr("checked");
	});	
	$("input[name='accepter']").click(function(){	
		if($(this).attr("checked")){
			$("input:checkbox[value='1']").attr("checked","checked");
			$("input:checkbox:checked[value='-1']").removeAttr("checked");
			$("input:checked[name='rejeter']").removeAttr("checked");
		}else{
			$("input:checkbox[value='1']").removeAttr("checked");
		}
	});
	$("input[name='rejeter']").click(function(){
		if($(this).attr("checked")){
			$("input:checkbox[value='-1']").attr("checked","checked");
			$("input:checkbox:checked[value='1']").removeAttr("checked");
			$("input:checked[name='accepter']").removeAttr("checked");
		}else{
			$("input:checkbox[value='-1']").removeAttr("checked");
		}
	});

	// constuire les tooltips de maniere auto
	constructAdvancedTitle();
	// ajoute les smoothScroll automatiquement
	constructSmoothScrollLink();
	constructMostSearch();
	constructExternalLink();
	constructFoldCategoryMenu();

	if(AjaxReady){
		AjaxReady.click(function(){
			constructAdvancedTitle();
			constructSmoothScrollLink();
		});
	}
});

function smoothScroll(destinationY,callbackFunction,timing){
	
	if(timing == undefined || timing == '') timing = 550 ;

	var MaxScroll = window.innerHeight || document.documentElement.clientHeight;
	MaxScroll = $('body').height() - MaxScroll;
	if(MaxScroll > 1 && destinationY > MaxScroll) destinationY = MaxScroll ;
	
	if(destinationY >= 1){
		$('html,body').animate({
			scrollTop: destinationY
		},timing,'easeOutExpo',function(){
			if( $.isFunction(callbackFunction) ) callbackFunction() ;
		});
	}else{
		if( $.isFunction(callbackFunction) ) callbackFunction() ;
	}
	return false;
}


function constructSmoothScrollLink(){

	$('a.jsSmoothScroll[href^=#]').each(function(){
		var linkEl = $(this);
		var destination = linkEl.attr('href');
		if( $(destination).length >= 1){
			linkEl.unbind('click').click(function(){
				smoothScroll($(destination).offset().top ,function(){
					window.location.hash = destination;
				},700);
				return false;
			});
		}
	});
}


function constructAdvancedTitle(){

	$('a.jsAdvancedTitle').each(function(){
		
		$(this).tooltip({
			track: true,
			delay: 50,
			extraClass: 'advancedTitleTooltip',
			showURL: false, 
			left: -100,
			top: 23
		});
		
	});
}

function constructMostSearch(){
	$('.jsMostSearch a').each(function(){
		//$(this).attr('href', $(this).attr('href')+'?h='+parseInt((Math.random() * 9999999)+1) );
	});
}

function constructExternalLink(){
	$('a[rel="external"]').attr('target','_blank');
}

function constructFoldCategoryMenu(){

	$('#ConsoleCategoryMenu .jsFoldCategory').each(function(){
			
		var hEl = $(this);
		var divEl = hEl.closest('div');
		var ulEl = hEl.nextAll('ul:first');

		var ulHeight = ulEl.height();
		var ulPadding = ulEl.css('padding');

		ulEl.css({
			display: 'block',
			height: 0
		}).find('li').css('opacity','0');

		divEl.unbind('hover').hover(showCat,showCat);
			
		function showCat(){
			if(ulEl.hasClass('activ')){
				ulEl.removeClass('activ');
				destH = 0;
				destOpacity = 0;
			}else{
				ulEl.addClass('activ');
				destH = ulHeight;
				destOpacity = 1;
			}



			ulEl.stop().animate({
				height: destH
			},300,function(){
				if(destH == 0){
				}
			}).find('li').stop().animate({
				opacity: destOpacity
			},500);
		}
	
	});


}



