function uiReady(){

	if($("#trip").length) tripLoad();
	$(window).load(uiLoad);

	// MENU
	$("#menu ul.ssmenu").prev().css('background','url(/img/fond-menuArrow.png) no-repeat 155px 50%');
	$("#menu li").find("ul.ssmenu a:last").css('border-bottom','none');
	$("#menu li").hoverIntent({    
		sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)    
		interval: 100, // number = milliseconds for onMouseOver polling interval    
		over: function(){$(this).children("ul").show('fast');},
		timeout: 100, // number = milliseconds delay before onMouseOut    
		out: function(){$(this).children("ul").hide('fast');}
	});
	
	// H1
	$("h2").wrap($("<div/>").addClass("ombre"));
	
	//CLOUD
	$(".tagCloud a:odd").css('color','#d6e0ef');
	
	// MENU PIECES (INVENTAIRE)
	if($("#menuPieces").length) initMenuPieces();
	
	// SELECT PAYS (DEVIS)
	if($(".input.pays").length) addPays(".input.pays");
	
}


// !LOAD
function uiLoad(){

	// MENU
	var menuUlTopPosition = $("#menu ul:first").offset().top;
	var menuWidth = $("#menu").outerWidth();
	var menuHeight = $("#menu").outerHeight();

	$("#menu").css({top:$("#contenu").offset().top});
	$("#menu li").corner("5px tl bl cc:#0388c7");
	$("#menu ul.ssmenu li").uncorner();

	function updateMenuPosition(){
	    if($("#piedWrapper").offset().top - (menuHeight + $(window).scrollTop()) > 30 ){
	    	if($(window).scrollTop() > $("#contenu").offset().top -10 ){
	    		$("#menu").animate({
	    			top: $(window).scrollTop() - $("#pageWrapper2").offset().top + 10
	    		},250);
	    	}
	    	else {
	    		$("#menu").animate({
	    			top: $("#contenu").offset().top - $("#pageWrapper2").offset().top
	    		},250);
	    	}
	    }
	    else {
	    	$("#menu").animate({
	    		top: $("#piedWrapper").offset().top - menuHeight - 20 - $("#pageWrapper2").offset().top
	    	},200)
	    }
	}
	
	function fixMenuZindex(){
	    var z = 10000;
	    jQuery.each($("#menu li"),function(i,li){
	    	$(li).css('z-index',z-i);
	    });
	}
	
	fixMenuZindex();
	setInterval(updateMenuPosition,300);


	// CORNER
	$("h2").corner("5px br").corner("5px top bl cc:#fff");
	$(".ombre").corner("5px");
	
	$("#menuPieces li a").corner("5px top");
	$(".piece").corner("5px bottom tr");
	$(".piece .imgWrapper").corner("5px");
	


	// GEO LOCALISATION
	$.get("/ajax/geoLoc.php",function(){

		$.get("/ajax/question.php",function(data){
		    if(data){
		    	$("body").prepend($("<div/>").attr('id','question'));
		    	$("#question").html(data).slideDown('slow',function(){
		    		$(this).find("a").click(function(e){
		    		    e.preventDefault();
		    		    var reponse = $(this).html();
		    		    var url = $(this).attr('href');
		    		    $.get("/ajax/repondre.php?r=" + reponse, function(){
		    		    	if(reponse == 'NON'){
		    		    		$("#question").slideUp('slow');
		    		    		$("#appendiceQuestion").fadeOut('slow');
		    		    	}
		    		    	else window.location.href = url;
		    		    });
		    		});
		    	});
		    	$('<img src="/img/appendice-question.png" alt="" id="appendiceQuestion" />').prependTo("#entete").fadeIn('slow');
		    }
		});
	
	});
	
}

// !TRIP
function tripLoad() {

	var defaultValue = 'Saisissez le nom d\'une ville...';
	$("#trip input.ac[value='']").val(defaultValue);
	
	$("#trip img.delete").click(function(){
		$(this).parent().find(":input").val('');
		ac_reset($(this).parent().find(":input.ac").attr('id'));
		$(this).parent().find(":input.ac").focus();
	});


	$("#trip input.ac").autocomplete("/ajax/findVille.php", {
	   	valueAtStart: true,
	   	cacheLength:10,
	    minChars:1,
	   	maxItemsToShow:14,
	   	mustMatch:false,
	   	selectFirst:true,
	   	delay:500,
	   	noResultMessage:'VILLE INCONNUE :-(',
	   	formatItem:ac_format,
	   	onSelectItem:ac_select,
	   	onChange:ac_reset
	})
	.focus(function(){
		if($(this).val() == defaultValue){
			$(this).val('');
			$(this).nextAll("img.delete:first").fadeOut('fast');
		}
	});
	
	function ac_format(row) {
	    if(row.length > 1 && row[1]!='') return row[0] + " (" + row[2] + ")";
	    else return row[0];
	}
	
	function ac_select(li) {
	    var selectId = li.attr('selectId');
	    var inputId = li.attr('inputId');
	    if(inputId) $('#' + inputId).nextAll(":input[type='hidden']:first").val('');
	    if(selectId) {
	    	$('#' + inputId).nextAll(":input[type='hidden']:first").val(selectId);
	    	$('#' + inputId).nextAll("img.tick:first").fadeIn('fast');
	    }
	}
	
	function ac_reset(id) {
	    if(id) {
	    	$('#' + id).nextAll(":input[type='hidden']:first").val('');
	    	$('#' + id).nextAll("img.tick:first").fadeOut('fast');
	    	if($('#' + id).val() == '') $('#' + id).nextAll("img.delete:first").fadeOut('fast');
	    	else $('#' + id).nextAll("img.delete:first").fadeIn('fast');
	    }
	}

}

// !MENU PIECES
function initMenuPieces(){
    
    $(".piece").hide();
    $("#menuPieces").show();
    
    $("#menuPieces a").click(function(e){
    	e.preventDefault();
    	$(this.hash).fadeIn("fast");
    	$(".piece[id!='"+this.hash.substring(1)+"']").hide();
    	$("#menuPieces a").removeClass('selected');
    	$(this).addClass('selected');
    });
    
	var firstTab = $("#menuPieces a:first").attr('href');
    $("#menuPieces a:first").addClass('selected');
    $(firstTab).fadeIn("fast");

}


// !SELECT PAYS
function addPays(select){
	$.getJSON("/ajax/jsonPays.php", function(j){
		var options = '';
		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		}
		$(select).append(options);
    })
}