// JavaScript Document



$( function () {

	
	/* modif super header */
	
	/* Box Intranet */
	var boxIntranetTimeout = setTimeout('',0);

	$('.box-intranet .lien a').click ( function () {
		$('.box-intranet .box-content').slideToggle ();
		$(this).toggleClass("link-hover");
		return false;
	});

	$('.box-intranet .lien a, .box-intranet .box-content').hover ( function () {
		clearTimeout(boxIntranetTimeout);

	}, function () {
		boxIntranetTimeout = setTimeout( function () {
			$('.box-intranet .box-content').slideUp ();
			$('.box-intranet .lien a').removeClass("link-hover");
		}, 1000);
	});
	
	/* Mon profil */
	var monProfilTimeout = setTimeout('',0);
	var isConnected = $('.mon-profil-content').length > 0 ? true : false ;
	
	if ( isConnected ) {
	
		$('.mon-profil-btn a, .mon-profil .mon-profil-content').hover ( function () {
			clearTimeout(monProfilTimeout);
			$('.mon-profil .mon-profil-content').slideDown();
			$(this).addClass("active");
	
		}, function () {
			monProfilTimeout = setTimeout( function () {
				$('.mon-profil .mon-profil-content').slideUp ();
				$('.mon-profil-btn a').removeClass("active");
			}, 1000);
		});
	
	}

	/* fin modif super header */

	/* Champs recherche */

	var blurTimeout = setTimeout('',0);

	var searchSent = false;

	var clearField = function ( field ) {

		if ( $(field).attr('title') == $(field).val()) {

			$(field).val('');

			$(field).removeClass('inittext');

		}

	}

	

	var restoreField = function ( field ) {

		if ( $(field).val() == '') {

			$(field).val( $(field).attr('title') );

			$(field).addClass('inittext');

		}

	}

	

	$('.inputsubmit').hover ( function () {

		$(this).addClass ('inputsubmithover'); 

	}, function () {

		$(this).removeClass ('inputsubmithover');

	});

	

	$('input.main-search').focus ( function () {

		$(this).parent('p').addClass('focus');

		clearField (this);

	});

	

	$('input.main-search').blur ( function () {

		if (!searchSent && $('input.main-search').val() == '') {

			blurTimeout = setTimeout( function () {

				$('input.main-search').parent('p').removeClass('focus');

				restoreField ('input.main-search');

			}, 500);

		}

	});

	

	$('.form-main-search').submit ( function () {

		if ( $('input.main-search').val() != '' && $('input.main-search').val() != $('input.main-search').attr('title')  ) {

			clearTimeout(blurTimeout);

			searchSent = true;		

			$('.form-main-search').find('.inputsubmit').addClass ('inputsend');
            
            jQuery(this).submit();

		}

		return false;

	});

	function getCookie(c_name)
    {
        if (document.cookie.length>0){
            var c_start=document.cookie.indexOf(" " + c_name + "=");
            if (c_start!=-1){
                c_start=c_start+1 + 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 "";
    }
    
	var espaces_autorises = getCookie('espace_autorises');    
	var recognisedIP = getCookie('recognisedIP');   
    var afficheBox = false;
    if(recognisedIP == '' || espaces_autorises == '') {
        jQuery.ajax({
            url: "/wp-content/GM/gm-authentification-ajax.php",
            cache: false,
            success: function(data){
                espaces_autorises = getCookie('espace_autorises'); 
                recognisedIP = getCookie('recognisedIP');
                if(data == "1") {
                    jQuery('.super-entete .box-intranet').show();
                }
            }
        });
    }
    if(recognisedIP == '1' && espaces_autorises != '')
    {
        jQuery('.super-entete .box-intranet').show();
	}
    
    if(getCookie('userId') != '' /*&& getCookie('nom') != '' && getCookie('prenom') != ''*/){
        jQuery('.super-entete .connecte').show();
        jQuery('.super-entete .connecte').find('li').eq(0).html("Bonjour <strong>"+Utf8.decode(getCookie('prenom'))+" "+Utf8.decode(getCookie('nom')));
    }
    else{
        jQuery('.super-entete .nonconnecte').show();
    }    

});

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;
	}
 
}
