/**
 * Bibliothèque Javascript
 * @package Metz Judo
 * @copyright 2009 bt-c enter - Tous droits réservés
 * @version 1.0 of 01/08/2009 
 * @since 1.0 of 01/08/2009 : création
 */

/*
 * Fonction d'initialisation pour l'affichage de la carte Google
 */
function initialize() {
	if (GBrowserIsCompatible()) {
		if (document.getElementById("map_canvas")) {
        var map = new GMap2(document.getElementById("map_canvas"));
        var latlng = new GLatLng(49.108236, 6.184659);
        var marker = new GMarker(latlng)
        map.setCenter(latlng, 14);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.addOverlay(marker);
        marker.openInfoWindowHtml("<img src='/img/logoarenes.jpg' />"+
        "<br / >"+
        "<a href='http://maps.google.fr/maps?f=q&source=s_q&hl=fr&geocode=&q=5+avenue+louis+le+d%C3%A9bonnaire,+metz&sll=49.106292,6.183769&sspn=0.01357,0.027423&ie=UTF8&ll=49.107618,6.183758&spn=0.013569,0.027423&t=h&z=15&iwloc=A' target='_blank'>Itineraire avec Google Maps</a>");
      }
   }
}


/*
 * Ecriture d'un cookie
 */
function writeCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

/*
 * Lecture d'un cookie
 */
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

/* 
 * Gestion de la taille des caractères
 */
function setSize(size) {
	bodysize = size;
	writeCookie("bodysize", bodysize, 300);
	document.getElementsByTagName("body")[0].style.fontSize = bodysize + "em";
}

/*
 * Fonction de changement de la taille d'affichage
 */
function switchSize() {
	switch (bodysize) {
		case "0.7": size="0.8"; break;
		case "0.8": size="0.9"; break;
		case "0.9": size="0.7"; break;
	}
	setSize(size);
}

/*
 * Fonction de changement de la taille d'affichage
 * La taille la plus grande n'est pas affichée car elle provoque
 * un décalage de la colonne de gauche vers le bas
 */
function switchSizeIE6() {
	switch (bodysize) {
		case "0.7": size="0.8"; break;
		case "0.8": size="0.7"; break;
	}
	setSize(size);
}

/* 
 * Initialiation avec la lecture de la taille
 */
var bodysize= "0.7";
window.onload = function(dummy) {
	var cookie = readCookie("bodysize");
	if (cookie) {setSize(cookie);}
}

/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
 
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;
	}
 
}

/*
 * Fonction d'appel de la page de recherche en passant l'argument en clair dans l'URL
 */
function gogooglesearch (lang) {
	q = document.getElementById("cse-search-box").q.value;
	if (lang == 'fr') {racine = 'recherche'; q = q.replace ("&", "et");}
	q = trim(q);
	document.getElementById("cse-search-box").q.value = q
	if (q.length > 0) {
		f = document.getElementById("cse-search-box");
		f.action = "/"+ racine + ".html?q=" + q;
		f.submit();
	}
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


