$(document).ready(
    function () {

         setTimeout (function() {$(".flashes").fadeOut("slow")}, 8000 );

         jQuery(".datepicker").datepicker({
                showOn: 'button',
                closeText: 'X',
                constrainInput: false,
                changeMonth: true,
                changeYear:true,
                dateFormat: 'dd-mm-yy',
                buttonText: ""
              });
	
	   var overChildren = false;
	
       $("#top_nav li").bind("mouseover",function(){

			this.overChildren = false;
			
			// Extract children from parent
			if($("#"+$(this).attr("id")+" ul:first") != null) {
				var ul = $("#"+$(this).attr("id")+" ul:first").clone();
				$("#"+$(this).attr("id")+" ul:first").remove();
				$(ul).addClass("nav_children");
				$(ul).attr("id", $(this).attr("id") + "_children");
				$(ul).appendTo($("#nav"));
				 $("#" + $(this).attr("id") + "_children" + " li").each(function(i){
				   $(this).addClass("nav_children_li");
				 });
			}

			// Display an item with subitems
            if($(this).hasClass("has_children")) {
                   if ($(this).hasClass("mouse-over")==false){

                       var position = $(this).position();
                       var borderTopWidth = parseInt($("#"+$(this).attr("id")+"_children").css("borderTopWidth"), 10);

                       if ($(this).hasClass("parent_of_current")){
                           $("#"+$(this).attr("id")+"_children").css("top", position.top + $(this).height() - borderTopWidth +5);
                       }else {
                           $("#"+$(this).attr("id")+"_children").css("top", position.top + $(this).height() - borderTopWidth +1);
                       }
                       $("#"+$(this).attr("id")+"_children").css("left", position.left + 5);

						// Subitems have to be displayed in a box at least as large as parent item
						parentWidth = $("#"+$(this).attr("id")).width();
						if($("#"+$(this).attr("id")+"_children").width() < parentWidth) {
							$("#"+$(this).attr("id")+"_children").css("width", parentWidth + borderTopWidth / 2);
						}

                       $(this).addClass("mouse-over");
                       $("#"+$(this).attr("id")+"_children").show();
                   }
			// Display an item with no subitems
            } else {
                       $(this).addClass("mouse-over-nochild");
                       $("#"+$(this).attr("id")+" a").addClass("mouse-over-nochild");
            }

		// Binding mouse leave event
        }).bind("mouseleave",function(){
            if ($(this).hasClass("mouse-over")==true){
				// Binds mouse movements on the children Div
				$("#"+$(this).attr("id")+"_children").mouseenter(function() {
                	$("#"+$(this).attr("id").replace("_children","")).addClass("mouse-over");
                    $(this).show()
					this.overChildren = true;
				}).mouseleave(function() {
                	$("#"+$(this).attr("id").replace("_children","")).removeClass("mouse-over");
					$(this).hide();
				});
				// If user directly leaves zone without navigating on the children div, hide everything
				if (!this.overChildren) {
					$(this).removeClass("mouse-over");
					$("#"+$(this).attr("id")+"_children").hide();
				}

            } else if ($(this).hasClass("mouse-over-nochild")==true){
                $(this).removeClass("mouse-over-nochild");
                $("#"+$(this).attr("id")+" a").removeClass("mouse-over-nochild");
            }
        });

    }
    
   


);

function go_timer_flashes(){
setTimeout (function() {
        $(".flashes").fadeOut("slow");
         $(".flashes").html('');
    }, 10000 );
}

function initialize_tags(url){
    $('.tags_autocomplete').each(function() {
      id = $(this).attr("id").split("_")[3];
      $(this).children().attr('class', 'selected');
      $(this).fcbkcomplete({
            json_url: url+"?category_id="+id,
            complete_text:'Commencez à taper (* pour voir la liste de mots clés)...',
            filter_case: true,
            filter_hide: true,
            filter_selected: false,
            newel: false
        });
    });
}

function initialize_new_tags(url){
    $('.new_tags_autocomplete').each(function() {     
      $(this).children().attr('class', 'selected');
      $(this).fcbkcomplete({
            json_url: url,
            complete_text:'Commencez à taper (* pour voir la liste de mots clés)...',
            filter_case: true,
            filter_hide: true,
            filter_selected: false,
            newel: true
        });
    });
}


function initialize_datepicker() {
    jQuery(".datepicker").datepicker({
                showOn: 'button',
                closeText: 'X',
                constrainInput: false,
                changeMonth: true,
                changeYear:true,
                dateFormat: 'dd-mm-yy',
                buttonText: ""
              });       
}

