// JavaScript Document

//init the rollover on the main menu items
var timeout;
function initMenu() {
  if (!document.getElementById) return

  var menus= document.getElementsByClassName("menuroll");
  for (var i = 0; i < menus.length; i++) {
    menus[i].onmouseover = function() {
          if (timeout) clearTimeout(timeout);
          hideAll();
          var id=this.id.substring(this.id.lastIndexOf('-')+1, this.id.length);
          showSubMenu(id);
    }
    menus[i].onmouseout = function() {
      timeout=setTimeout("hideAll()",500);
    }
  }

}

//make the submenu appear and the second submenu if exists
function initSubmenu() {
  if (!document.getElementById) return

  var lis=document.getElementsByTagName("li");
  for (var i = 0; i < lis.length; i++) {
    if (lis[i].parentNode.parentNode.className == 'submenu'){
        lis[i].onmouseover = function() {
            hideChildren();

            theId=this.parentNode.parentNode.id;
            showSubMenu(theId);
            if (this.className=='submenuroll'){
                this.childNodes[2].style.display="block";

            }
        }
        lis[i].onmouseout = function() {
            if (this.className=='submenuroll'){
                this.childNodes[2].style.display="none";
            }
            timeout=setTimeout("hideAll()",500);
        }
    }
  }
}

//once on the second level submenu (problem with this function with old version of firefox.
function initSubmenu2() {
  if (!document.getElementById) return
  var lis=document.getElementsByTagName("li");
  for (var i = 0; i < lis.length; i++) {
    if (lis[i].parentNode.parentNode.className == 'submenuroll'){

        lis[i].onmouseover = function() {
            //$('test1').innerHTML="test: in " +this.innerHTML+"\n";
            this.parentNode.style.display.block;
            //$('test1').innerHTML=this.parentNode.parentNode.innerHTML;
            //this.parentNode.parentNode.style.color= "#333";
            //this.parentNode.parentNode.style.background="#c4ccdf";
        }

        lis[i].onmouseout = function() {
            //$('test2').innerHTML="test: out " +this.innerHTML+"\n";
                this.parentNode.style.display="none";
                //this.parentNode.parentNode.style.color= "#666";
                //this.parentNode.parentNode.style.background="#fff";
//hideAll();
            //timeout=setTimeout("hideAll()",500);

        }

    }
  }

}


function showSubMenu(theId){
 if (timeout) clearTimeout(timeout);
  $(theId).style.display="block";

}

function showSubMenu2(theNode){
 if (timeout) clearTimeout(timeout);
  theNode.element.style.display="block";

}




function hideAll(){
  var subMenus= document.getElementsByClassName("submenu");
  for (var i = 0; i < subMenus.length; i++) {
    subMenus[i].style.display="none";
  }
  var subMenus2= document.getElementsByClassName("submenuroll");
  for (var i = 0; i < subMenus2.length; i++) {
    subMenus2[i].childNodes[2].style.display="none";

  }

}

function hideChildren(){
  var subMenus2= document.getElementsByClassName("submenuroll");
  for (var i = 0; i < subMenus2.length; i++) {
    subMenus2[i].childNodes[2].style.display="none";
                    //subMenus2[i].childNodes[0].style.color= "#666";
                //subMenus2[i].childNodes[0].style.background="#fff";
  }

}

function setOtherStyleSheet() {
    var currentTitle = getActiveStyleSheet();
    if (currentTitle =="small"){
        setActiveStyleSheet("large");
    }
    else {
        setActiveStyleSheet("small");
    }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function detect() {
    if(screen.width<1280||screen.height<1024){
        return "small";
    }
    else{
        return "large";
    }
}


function setTextSize(size) {
  var a = document.getElementsByTagName("body")[0];
  if(a){
    a.style.fontSize=size;
  }
}

function getTextSize() {
  var a = document.getElementsByTagName("body")[0];
   return a.style.fontSize;
}

function getPreferredTextSize() {
  var a = document.getElementsByTagName("body")[0];
  if(a){
    return a.style.fontSize;
  }
}






function createCookie(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=/";
}

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;
}

window.onload = function(e) {
    initMenu();
    initSubmenu();
    initSubmenu2();
  var cookie = readCookie("style");
  var title = cookie ? cookie : detect();//getPreferredStyleSheet();
  setActiveStyleSheet(title);

  var cookie2 = readCookie("size");
  var size = cookie2 ? cookie2 : getPreferredTextSize();
  setTextSize(size);


}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);

  var size = getTextSize();
  createCookie("size", size, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

var cookie2 = readCookie("size");
var size = cookie2 ? cookie2 : getPreferredTextSize();
setTextSize(size);

/*
function initAll(){
    initMenu();
    initSubmenu();
    initSubmenu2();

}
window.onload = initAll;
*/

