// Stop words taken from search.properties
var stopWordsList = [
    "a", "\u00e0", "afin", "ailleurs", "ainsi", "alors", "apr\u00e8s", "attendant", "au", "aucun", "aucune", "au-dessous", "au-dessus", "aupr\u00e8s", "auquel", "aussi", "aussit\u00f4t", "autant", "autour", "aux", "auxquelles", "auxquels", "avec",
    "b", "beaucoup",
    "c", "\u00e7a", "car", "ce", "ceci", "cela", "celle", "celles", "celui", "cependant", "certain", "certaine", "certaines", "certains", "ces", "cet", "cette", "ceux", "chacun", "chacune", "chaque", "chez", "ci", "combien", "comme", "comment", "concernant", "contre",
    "d", "dans", "de", "dedans", "dehors", "d\u00e9j\u00e0", "del\u00e0", "depuis", "des", "d\u00e8s", "desquelles", "desquels", "dessus", "donc", "donn\u00e9", "dont", "du", "duquel", "durant",
    "e", "elle", "elles", "en", "encore", "entre", "\u00e9taient", "\u00e9tait", "\u00e9tant", "etc", "eux",
    "f", "furent",
    "g",
    "h", "hormis", "hors",
    "i", "ici", "il", "ils",
    "j", "jadis", "je", "jusqu", "jusque",
    "k",
    "l", "la", "l\u00e0", "laquelle", "le", "lequel", "les", "lesquelles", "lesquels", "leur", "leurs", "lors", "lorsque", "lui",
    "m", "ma", "mais", "malgr\u00e9", "me", "m\u00eame", "m\u00eames", "mes", "mes", "mien", "mienne", "miennes", "miens", "moins", "moment", "mon", "moyennant",
    "n", "ne", "ni", "non", "nos", "notamment", "notre", "n\u00f4tre", "notres", "n\u00f4tres", "nous", "nous",
    "o", "on", "o\u00f9",
    "p", "par", "parce", "parmi", "pas", "plus", "plusieurs", "pour", "pourquoi", "puis", "puisque",
    "q", "qu", "quand", "quant", "que", "quel", "quelle", "quelque", "quelques-unes", "quelques-uns", "quelqu", "quels", "qui", "quiconque", "quoi", "quoique",
    "r",
    "s", "sa", "sans", "se", "selon", "ses", "si", "sien", "sienne", "siennes", "siens", "soi", "soi-m\u00eame", "soit", "son", "sont", "suis", "sur",
    "t", "ta", "tandis", "tant", "te", "telle", "telles", "tes", "tienne", "tiennes", "tiens", "toi", "ton", "toujours", "tous", "tout", "toute", "toutes", "tr\u00e8s", "trop", "tu",
    "u", "un", "une",
    "v", "vos", "votre", "v\u00f4tre", "v\u00f4tres", "vous", "vu",
    "w",
    "x",
    "y",
    "z"];
var stopWordHash = {};
for (var i in stopWordsList) stopWordHash[stopWordsList[i]] = true;
stopWordsList = null;

function getHiliteWords(words) {
    var result = new Array();
    for(var i = 0; i < words.length; ++i) {
        var w = words[i];
        // Remove quotes everywhere
        w = w.replace(/["]/g, '');
        if(w.length > 0) {
            // Cheap inflection algorithm :-)
            if(w.charAt(w.length - 1) == 's') {
                w = w.substring(0, w.length - 1);
                // word without 's' is not a stop word
                if(w.length > 0)
                	if(!stopWordHash[w])
                		result.push(w);
                // word with 's' is not a stop word
                w = w + 's';
                if(!stopWordHash[w])
                    result.push(w);
            } else {
                if(!stopWordHash[w])
                    result.push(w);
            }
        }
    }
    return result;
}

Hilite.onload=false;
Hilite.style_name_suffix=false;
function plus(id) {
	var mySize = $("#"+id).css("font-size");
	var iMySize, iSizeLib;
	if( mySize.indexOf("px") > 0 ) {
		iMySize = mySize.replace("px", "");
		iSizeLib = (iMySize * 1.2)+"px";
	}
	else if( mySize.indexOf("em") > 0 ) {
		iMySize = mySize.replace("em", "");
		iSizeLib = (iMySize * 1.2)+"em";
	}
	$("#"+id).css("font-size", iSizeLib);
}
function moins(id) {
	var mySize = $("#"+id).css("font-size");
	var iMySize, iSizeLib;
	if( mySize.indexOf("px") > 0 ) {
		iMySize = mySize.replace("px", "");
		iSizeLib = (iMySize * 0.8)+"px";
	}
	else if( mySize.indexOf("em") > 0 ) {
		iMySize = mySize.replace("em", "");
		iSizeLib = (iMySize * 0.8)+"em";
	}
	$("#"+id).css("font-size", iSizeLib);
}
function search(id) {
	var searchText = prompt("Votre recherche:", "");
	if (searchText != '' ) {
		doSearch(searchText,id);
	}
}

var searchOccsText;
var searchOccs;
var searchOccsIndex;
 
function doSearch(searchText,id, noScroll, endCB){
    //Hilite.debug_referrer = 'http://www.google.com/search?q='+escape(searchedtext)+'';
    resetSearch();
    searchOccsText = searchText;
    var words = getHiliteWords(searchText.split(' '));
    if(words.length == 0)
        return;
    Hilite.exact = false;
    Hilite.hiliteElement(document.getElementById(id), words);
	//Hilite.hilite();
    if(!noScroll){
    	// Hack: wait 200ms because se_hilite is asynchronous. It still might not scroll.
		setTimeout(function(){
			var elems = new Array();
			$("span.hilite").each(function(i){
				var pos = jQuery.iUtil.getPosition(this);
				if(pos.x || pos.y)
					elems.push(this);
			});
			searchOccs = jQuery(elems);
			searchOccsIndex = 0;
			if(searchOccsIndex < searchOccs.length)
				searchOccs.eq(searchOccsIndex).IdmScrollTo(800,undefined,undefined,-50);
			if(endCB)
				endCB();
		}, 200);
    }
}

function searchPrev(searchText, id, noScroll) {
	if (searchOccsText != searchText) {
		doSearch(searchText, id, noScroll, function(){ searchPrev(searchText, id, noScroll); })
		return;
	}
	var length = searchOccs ? searchOccs.length : 0;
	if(length > 0) {
		var index = searchOccsIndex - 1;
		if(index < 0)
			index = length - 1;
		searchOccsIndex = index;
		searchOccs.eq(searchOccsIndex).IdmScrollTo(800,undefined,undefined,-50);
	}
}

function searchNext(searchText, id, noScroll) {
	if (searchOccsText != searchText) {
		doSearch(searchText, id, noScroll, function(){ searchNext(searchText, id, noScroll); })
		return;
	}
	var length = searchOccs ? searchOccs.length : 0;
	if(length > 0) {
		var index = searchOccsIndex + 1;
		if(index >= length)
			index = 0;
		searchOccsIndex = index;
		searchOccs.eq(searchOccsIndex).IdmScrollTo(800,undefined,undefined,-50);
	}
}

function resetSearch() {
    $("span.hilite").removeClass();
    searchOccs = undefined;
    searchOccsIndex = undefined;
    searchOccsText = undefined;
}

function swapNote(id){
	$(".noteContent"+id).toggle();
	var src = $("#imgBook"+id).attr("src");
	var altsrc = $("#imgBook"+id).attr("altsrc");
	$("#imgBook"+id).attr("src", altsrc);
	$("#imgBook"+id).attr("altsrc", src);
}

function getOffsetTopFromBody(elem)
{
    if (!elem)
        return null;
        
    if(!elem.parentNode || elem.parentNode.nodeType == 11)
    {
        elem = elem.ownerDocument.all(elem.id)
    }
    
    var topFromBodyStart = elem.offsetTop;
    var body = elem.ownerDocument.body;
    while (elem.offsetParent && elem.offsetParent != body)
    {
        elem = elem.offsetParent;
        topFromBodyStart += elem.offsetTop;
    }

    return topFromBodyStart;
}

function moveToolBar(minTop) {
    var scrollTop = document.body.scrollTop;
    if (scrollTop > minTop) {
        if($("#browseDataToolBar").attr("class") != "toolBarTop") {
           $("#browseDataToolBar").attr("class","toolBarTop");
           $("#browseDataToolBar").attr("style","max-width:695px");
           
        }
        /*
        var maxwidth = "100%";
        if (document.width > 1024)
        	maxwidth = document.width - 1024 + 695 + "px";
       	$("#browseDataToolBar").attr("style","max-width:" + maxwidth);*/
    }
    else {
        if($("#browseDataToolBar").attr("class") != "toolBarInitialPosition") {
            $("#browseDataToolBar").attr("class","toolBarInitialPosition");
        	$("#browseDataToolBar").attr("style","max-width:100%");
        }
    }
    
}
