var Menu = {};
Menu.add = function() {};
Menu.add.prototype = {
  init: function(menuId, pars) {
    // this.menu = this;
    this.debug = (typeof pars.debug !== 'undefined') ? pars.debug : true;
    this.menuContainer = document.getElementById(menuId);
    if(!this.menuContainer && this.debug === true) { return false; }
    this.menuLis = this.menuContainer.getElementsByTagName('li');
    this.overName = pars.overName ? pars.overName : 'over';
    this.timeOut = (typeof pars.timeOut !== 'undefined' && isInt(pars.timeOut)) ? pars.timeOut : 100;
    this.ie = getIeType();
    this.ieFrame = (typeof pars.ieFrame !== 'undefined') ? pars.ieFrame : true;
    this.subMenuFunc = (typeof pars.subMenuFunc === 'function') ? pars.subMenuFunc : false;
    
    for(var i = 0; i < this.menuLis.length; i++) {
      // this.menuLis[i].menu = this.menu;
      this.menuLis[i].menu = this;
      this.addOver(this.menuLis[i]);
      this.menuLis[i].id = menuId+'lis'+i;
      if(getFirstUl(this.menuLis[i]) && (typeof this.subMenuFunc == 'function')) {
        this.subMenuFunc(this.menuLis[i]);
      }
    }
  },
  
  addOver: function(obj) {
    obj.onmouseover = function() {
      if(this.timer) {
        clearTimeout(this.timer);
      }
      if(!hasClassName(this.id)) {
        addClassName(this, this.menu.overName);
      }
      if(this.menu.ieFrame) {
      if(window.ie !== false && window.ie === 'ie6' && getFirstUl(this) && !getFirstIframe(this)) {
        /*
         * add an iframe if ie6 or earlier - select|z-index bug
         */
        var ieUL = getFirstUl(this);
        var ieMat = document.createElement('iframe');
		    ieMat.style.height = ieUL.offsetHeight;
		    ieMat.style.width = ieUL.offsetWidth;
        ieUL.insertBefore(ieMat, ieUL.firstChild);
      }
      }
    };
    obj.onmouseout = function(obj) {
       this.timer = setTimeout("removeClassName('"+this.id+"')", this.menu.timeOut);
    };
  }  
};

/*
 * checks that the browser is Microsoft Internet Explorer 6
 */
getIeType = function() {
  ie = (navigator.appName == 'Microsoft Internet Explorer' && !window.opera && document.all) ? Object() : false;
  if(ie !== false) {
    window.ie = (/MSIE 7/i.test(navigator.userAgent)) ? 'ie7' : 'ie6';
  }
};

/*
 * Special function to indicate that the object have child list items
 */
subMenuFunc = function(obj) {
  obj.innerHTML += ' >';
  // addClassName(obj, 'sub');
};
subMenuFuncRight = function(obj) {
  addClassName(obj, 'sub');
};
/*
 * Get the objects first ul element
 */
getFirstUl = function(obj) {
  return obj.getElementsByTagName('ul')[0];
};

/*
 * Get the object first child iframe element
 */
getFirstIframe = function(obj) {
  iframes = obj.getElementsByTagName('iframe');
  for(var i=0; i<iframes.length; i++)
  {
    if(iframes[i].parentNode.parentNode == obj) {
      return iframes[i];
    }
  }
};

/*
 * Add the specified class name to the given object
 */
addClassName = function(obj, cln) {
  obj.className += ' '+cln;
};

/*
 * Remove the class name of the given object
 */
removeClassName = function(objId) {
  obj = document.getElementById(objId);
  cRex = new RegExp(' ?'+obj.menu.overName, 'g');
  obj.className = obj.className.replace(cRex, '');
};
/*
 * Check that the obj have the classname already
 */
hasClassName = function(objId) {
  obj = document.getElementById(objId);
  cRex = new RegExp(' ?'+obj.menu.overName, 'g');
  return obj.className.match(cRex);
}

/*
 * Check, that the given string is an interval number
 */
isInt = function(str) {
  n = new RegExp('^[0-9]+$');
  if(n.test(str)) {
    return true;
  }
  return false;
};

addEvent = function(eventType, func, obj) {
  if (window.attachEvent) { obj.attachEvent('on'+eventType, func); }
  else if (window.addEventListener) { obj.addEventListener(eventType, func, false); }
}

//fonction de la validation de la recherche

function parseSearchQuery(theForm, message) {
	var queryValue = theForm.elements["query"].value;
	var testValue = queryValue.replace(/ /g, "");
	if (testValue.length < 3) {
		alert(message);
		return (false);
	}
	queryValue = queryValue.replace(/\+/g, "%2b");
	queryValue = queryValue.replace(/\-/g, "%2d");
	theForm.elements["query"].value = queryValue;
	return (true);
}