var detect = navigator.userAgent.toLowerCase();
var OS;
var browser;
var version;
var total;
var thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
		
/* Affichage du plus d'infos */
function plusInfos(article) {
	if (document.getElementById(article).style.display=="block") {
		document.getElementById(article).style.display="none";
	}else{
		document.getElementById(article).style.display="block";
	}
	win=article;
	return;
}

/* Recherche des mots passés en paramètre */
function getParams() {
	if (browser == 'Internet Explorer') {
		id = "" + window.location + "";
		idx = id.indexOf('?');
	}else{
		idx = document.URL.indexOf('?');
	}
	var params = new Array();
	if (idx != -1) {
		if (browser == 'Internet Explorer') {	
			pairs = id.substring(idx+1, id.length).split('&');
		}else{
			pairs = document.URL.substring(idx+1, document.URL.length).split('&');
		}
		for (var i=0; i<pairs.length; i++) {
			nameVal = pairs[i].split('=');
			params[nameVal[0]] = nameVal[1];
		}
	}
	return params;
}

//params = getParams();
/* Fonction appellée par index.html lors d'un appel depuis une page
		TEI, EAC ou EAD. Elle permet d'analyser l'url de la page demandeuse
		puis de charger la page dans la frame "main".
		params['do']='aller' si l'appel ne vient pas de index.html
		params['page'] page à charger
		params['stop'] id ou sauter.
*/


function allerNouvPage(){
	var params = getParams();
	if (params['do']=='aller'){
		allerNouvPage = params['page'] + '#' + params['stop'];
		window.top.frames[1].location.href=allerNouvPage;
	}else{
		//alert('Ce navigateur ne sais pas lire les paramètres.\n' + browser);
	}
}


/* Eclatement des mots passés  en paramètre */
function splitwords(words) {
	//words = words.replace('e', '[eéèêë]');
	//words = words.replace('a', '[aàâ]');
	//words = words.replace('i', '[iïî]');
	//words = words.replace('u', '[uù]');
	//words = words.replace('c', '[cç]');
		
	arrayOfTerms = new Array(0);
		
	reQuoted = /"([^"]+)"/g; // version simple interdisant les apostrophes échappées
	arrayOfQuoted = words.match(reQuoted)
	if (arrayOfQuoted) {
		for (i=0; i< arrayOfQuoted.length; ++i) {
			arrayOfTerms.push(arrayOfQuoted[i].substr(1,arrayOfQuoted[i].length-2));
			// On transforme les termes contenant des ? ou des *
			arrayOfTerms[i] = arrayOfTerms[i].replace(/\?/g, "\\w?");
			arrayOfTerms[i] = arrayOfTerms[i].replace(/\*/g, "\\w*");
		}
		words = words.replace(reQuoted, "");
	}
	reSpace = /[^\s]+/g; // Séparation des mots par des espaces
	arrayOfWords = words.match(reSpace);
	if (arrayOfWords) {
		reJoker = /[?*]/;
		reNotJoker = /[^?*]/;
		// On transforme les termes contenant des ? ou des *
		for (i=0; i< arrayOfWords.length; ++i) {
			term = arrayOfWords[i];
			if (term.match(reJoker)) {
				if (term.match(reNotJoker)) {
					term = term.replace(/\?/g, "\\w?");
					term = term.replace(/\*/g, "\\w*");
				}
				else {
					// Le terme ne contient que des joker, on le supprime
					break;
				}
			}
			else {
				term += "\\w?";
			}
			term = "\\b" + term + "\\b";
			arrayOfTerms.push(term);
		}
	}
	return arrayOfTerms;
}

/* Recherche et mise en surbrillance des mots */
function wordhighlight(aSourceObject, aWords){
	//alert(params['jump']);
	aWords = decodeURI(aWords)
	HTML = document.getElementById(aSourceObject).innerHTML;
	if (HTML.length > 64*1024) {
		return;
	}
	//Extract HTML Tags
	regexp=/<[^<>]*>/ig;
	vHTMLArray = HTML.match(regexp);
	//Replace HTML tags
	vStrippedHTML = HTML.replace(regexp,"$!$");
	// Prepare Array of highlightings
	highlight = new Array;
	for (i = 1; i <= 6; ++i) {
		highlight.push('highlight' + i);
	}
	//Replace search words
	arrayOfTerms = splitwords(aWords);
	for (i=0; i < arrayOfTerms.length; ++i) {
		nbLight = (i % highlight.length);
		regexp= new RegExp ("(" + arrayOfTerms[i] + ")", "gi");
		vStrippedHTML = vStrippedHTML.replace(regexp,'%!' + nbLight + '%$1%!%');
	}
	for (light = 0; light < highlight.length; ++light ) {
		searchedStr = "%!" + light + "%";
		for(i=0;vStrippedHTML.indexOf(searchedStr) > -1;i++){
			//vStrippedHTML = vStrippedHTML.replace(searchedStr,'<span style="background-color:yellow;" class="' + highlight[light % highlight.length] + '">');
			vStrippedHTML = vStrippedHTML.replace(searchedStr,'<span class="surbrillance">');
		}
	}
	for(i=0;vStrippedHTML.indexOf("%!%") > -1;i++){
		vStrippedHTML = vStrippedHTML.replace("%!%",'</span>');
	}
	//Reinsert HTML
	for(i=0;vStrippedHTML.indexOf("$!$") > -1;i++){
		vStrippedHTML = vStrippedHTML.replace("$!$", vHTMLArray[i]);
	}
	//Display Result
	document.getElementById(aSourceObject).innerHTML = vStrippedHTML;
}

/* Au démarrage de la page sur body onload */
function startHighlight() {
	var params = getParams();
	if(document.getElementById(params['objet'])) {
		if (!params['do']) {
			wordhighlight(params['objet'], params['texte']);
			}
	}
	location.href= '#' + params['jump'];
}

/* Plier et déplier la zone de recherche */
var action ='deplier';
function plier_deplier() {
	var dbRes = [[]];
	var dbEl = document.body.getElementsByTagName('div');
	for (e = 0; e < dbEl.length; e++) {
		if (dbEl[[e]].className == 'plusInfos') {
			if (action == 'deplier') {
				dbEl[[e]].style.display="block";
			}
			if (action == 'plier') {
				dbEl[[e]].style.display="none";
			}
		}
	}
	if (action == 'deplier') {
		action = 'plier';
	}else{
		action = 'deplier';
	}
}

/* Ouvre une image dans une nouvelle fenêtre */
function ouvre_image(image){
	var theWindow = "";
	if (image == '') return;
	image = image.toLowerCase();
	theWindow=open("","Image", "menubar=0, location=no, status=no, scrollbars=yes, resizable=yes, width=1053, height=800");
	theWindow.document.write("<html><head><title>Archives du Morbihan</title></head><body bgcolor='#000000' color='#FFFFFF'>");
	theWindow.document.write('<div align="center"><a style="color:#FFFFFF;" href="javascript: self.close()">- Fermer -</a></div>');
	if (image.indexOf(".pdf") != -1){
		theWindow.document.write("<object  type='application/pdf' data='" + image + "' style='margin-top: 20px;border: 2px solid #8b0000; width:1024; height:768;'>");
		theWindow.document.write("<param  name='src' value='" + image + "' /></object>");
	}else{
		theWindow.document.write("<div style='text-align: center;margin-top:auto; margin-bottom:auto:'><img src='" + image + "'></div>");
	}
	theWindow.document.write('<div align="center"><a style="color:#FFFFFF;" href="javascript: self.close()">- Fermer -</a></div>');
	theWindow.document.write("</body>\n</html>");
	theWindow.focus();
	return;
}

/* Plier et déplier le plan de classement */
old = '';
function deplierPlan(lien){
	//alert(old);
	if (document.getElementById(lien).style.display=="none" && old != lien){
		document.getElementById(lien).style.display="block";
		old=lien;
		return;
	}
	if (document.getElementById(lien).style.display=="block"){
		document.getElementById(lien).style.display="none";
		old='';
		return;
	}
}

/* Fonction d'affichage du média */
var mediaSelect = '';
function getMedia(type, id, path, viewer){
var rendu = '';
if (document.getElementById(mediaSelect)) {
	document.getElementById(mediaSelect).innerHTML = '<img border="0" src="../tools/HP.png"/>';
}
switch (type)
{
case 'mp3' :
	rendu = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="200" height="20" id="dewplayer" align="middle">';
	rendu = rendu + '<param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="' + viewer + '?mp3=' + path ;
	rendu = rendu + '&autostart=1&showtime=1&bgcolor=FFFFFF" /><param name="quality" value="high" />';
	rendu = rendu + '<param name="bgcolor" value="FFFFFF" /><embed src="' + viewer + '?mp3=' + path;
	rendu = rendu + '&autostart=1&showtime=1&bgcolor=FFFFFF" quality="high" bgcolor="FFFFFF" width="200" height="20" name="dewplayer"  align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
	break;
}
document.getElementById(id).innerHTML = rendu;
mediaSelect = id;
}				
					
	
