jQuery().ready(function(){
	var $ = jQuery;
	
	$.gm_authentification = {
			
		espaces_autorises:'',
		checkedIP:'',
		recognisedIP:'',
		userId:'',
		nom:'',
		prenom:'',
		
		init:function(){
		
			$.gm_authentification.espaces_autorises = $.gm_authentification.getCookie('espace_autorises');
			$.gm_authentification.checkedIP = $.gm_authentification.getCookie('checkedIP');
			$.gm_authentification.recognisedIP = $.gm_authentification.getCookie('recognisedIP'); 
			$.gm_authentification.userId = $.gm_authentification.getCookie('userId');
			$.gm_authentification.nom = Utf8.decode($.gm_authentification.getCookie('nom'));
			$.gm_authentification.prenom = Utf8.decode($.gm_authentification.getCookie('prenom'));
			
			if( $.gm_authentification.checkedIP == '' || $.gm_authentification.checkedIP == '0' 
				|| ($.gm_authentification.recognisedIP == '1' && $.gm_authentification.espaces_autorises == '' ) ) {
				
		        $.ajax({
		            url: gm_to_js.wp_site_url + "/wp-content/GM/gm-authentification-ajax.php",
		            cache: false,
		            success: function(data){
		        		$.gm_authentification.espaces_autorises = $.gm_authentification.getCookie('espace_autorises'); 
		        		$.gm_authentification.recognisedIP = $.gm_authentification.getCookie('recognisedIP');
		        		$.gm_authentification.checkedIP = $.gm_authentification.getCookie('checkedIP');
		        		
		        		$.gm_authentification.update_connexion_bar();
		            }
		        });
		        
		    }
		    
			$.gm_authentification.update_connexion_bar();
		    
		},
		update_connexion_bar:function(){
			
			if( $.gm_authentification.recognisedIP == '1' && $.gm_authentification.espaces_autorises != ''){
		        $('.super-header .box-intranet').show();
			}else{
				$('.super-header .box-intranet').hide();
			}
		    
		    if( $.gm_authentification.userId != '' /*&& $.gm_authentification.nom && $.gm_authentification.prenom != ''*/ ){
		    	$('a#mon-profil-connected').show();
		    	$('.user-status').addClass('user-connect');
		    	$('.nav-disconnect').hide();
		    	$('.nav-connect').show();
		        $('.nav-connect').html("Bonjour <strong>"+$.gm_authentification.prenom+" "+$.gm_authentification.nom+'</strong>');
		    }
		    else{
		    	$('a#mon-profil-not-connected').show();
		        $('.nav-disconnect').show();
		        $('.nav-connect').hide();
		        $('.mon-profil-content').remove();
		    }  
		
		    $('.social-link').show(); //Show it after the battle or we will see it magically moving from right to left ...
		    
		},
		getCookie:function (c_name){
	        if (document.cookie.length>0){
	            var c_start=document.cookie.indexOf(" " + c_name + "=");
	            
	            if( c_start == -1 ){
	            	c_start=document.cookie.indexOf(c_name + "=");
	            	if( c_start != 0 ){
	            		c_start = -1;
	            	}
	            }
	            
	            if (c_start!=-1){
	            	if( c_start != 0 ){
	            		c_start= c_start+1;
	            	}
	            	c_start = c_start + c_name.length+1;
	                var c_end=document.cookie.indexOf(";",c_start);
	                if (c_end==-1) c_end=document.cookie.length;
	                return unescape(document.cookie.substring(c_start,c_end));
	            }
	        }
	        return "";
	    }
	    
	}
	
	$.gm_authentification.init();
    
});

var Utf8 = {
		 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
