jQuery.fn.exists = function(){return jQuery(this).length>0;}


$(document).ready(function(){
    
    //$.validator.defaults.debug = true;


   /* log box */
   
   $.validator.setArrowTemplate({ textColor:"white",boxColor:"red",cssBoxTop:"-3",cssBoxLeft:"170"});
    
    $("#formLogBox").validate({

	rules: {
	    login: {required:true},
	    pass: {required:true}
		
	}

    });
    
    $("#login_input").attr("autocomplete","off");
    $("#password_input").attr("autocomplete","off");
    
    //make the login block appear on top of the page when mouse enter "connexion"
    $("div.login a.login").mouseenter(function(){
        var test = ($('div.login_block').css("display")=="none")?true:false;
        if(test) $('div.login_block').show();
    });
    
    //make the login block appear on top of the page when mouse enter the connexion icon
    $("#login_icon").mouseenter(function(){
        var test = ($('div.login_block').css("display")=="none")?true:false;
        if(test) $('div.login_block').show();        
    });
    
    delayHide =5;
    timeOutId=1;
    noHide = false;
    
    function logBoxHide(){
      if(!noHide)$('div.login_block').hide();
    }

    clicNotTriggered=false;
    $("div.login_block").click(function(){ clicNotTriggered=true;});
    $("body").click(function(){
         if(!clicNotTriggered) logBoxHide();
         else clicNotTriggered=false;
                    
     });
    
    
    function stopDelayHide(){
      clearTimeout(timeOutId);
    }
   
    function HideWithDelay(obj){
      clearTimeout(timeOutId);
      timeOutId = setTimeout(function(){ logBoxHide()},delayHide*1000);
    }

    //hide the login block when getting out of the area
    $('div.login_block').hover(function(){ stopDelayHide();},function(){HideWithDelay()   });
    
    //hide the placeholders if the login fields are not empty
    $("div.login input").each(function(){
        if($(this).val() != "")
        {
            var idPlaceholder = this.id.replace("input","placeholder");
            if(idPlaceholder!='' && idPlaceholder!='memor' && idPlaceholder!='button_submit') $("#"+idPlaceholder).hide();
        }
    });
    
    //hide the placeholders when focus entering login or password
    $("div.login input").focus(function(){
            var idPlaceholder = this.id.replace("input","placeholder");
            delayHide=15;
            
            if(idPlaceholder!='' && idPlaceholder!='memor' && idPlaceholder!='button_submit') $("#"+idPlaceholder).hide();
    });
    
    //manage the placeholder for login area on top of the page
        //hide the placeholder on top of the input box
        $("div.login div.placeholder").click(function(){
            var idInput = this.id.replace("placeholder","input");
            
            $(this).hide();
            $("#"+idInput).focus();
        });
        
        //put back placeholders if necessary when exiting input box        
        $("div.login input").blur(function(){
            var idPlaceholder = this.id.replace("input","placeholder");
            
            if($(this).val() == "")
            {
                if(idPlaceholder!='') $("#"+idPlaceholder).show();
            }
        });
    ////////////
    
    /* end logbox */
    
    //show and hide the hints appropriate depending on icon we display
    $('#subheader ul.menu li').mouseenter(function(){
          
	$(this).addClass('on');
        
        //exception for "comment" (li with class "nobg")
        if(!$(this).hasClass("nobg"))
        {
            //show red strip
            $("#redStrip").show();
	    
            //show associated hint
            var item_class = $(this).find('a').attr('rel');
	    $('ul.menu-hints li[class="'+item_class+'"]').show();
        }
    }).mouseleave(function(){
        $(this).removeClass('on');
        
        if(!$(this).hasClass("nobg"))
        {
            //hide hint
	    var item_class = $(this).find('a').attr('rel');
	    $('ul.menu-hints li[class="'+item_class+'"]').hide();
                
            //hide red strip
            $("#redStrip").hide();
        }
    });
        
    
    //temp patch for links in header not redirecting anywhere
    /*$('#subheader ul.menu li a').click(function(){
        
        //exception for "comment"
        if(!$(this).hasClass("comment")) return false;    
    });*/
});
