// JavaScript Document

function popupWindow(path,wWidth,wHeight) {
  posX = 1+(getWindowWidth()/2)-(wWidth/2);
  if(posX < 0)
    posX = 0;
  posY = 1+(getWindowHeight()/2)-(wHeight/2);
  if(posY < 0)
    posY = 0;
  fenster = window.open(path,"Fenster",("dependent=yes, statusbar=no, toolbar=no, menubar=no, location=no, directories=no, scrollbars=yes, height="+wHeight+", width="+wWidth+", screenX="+posX+", screenY="+posY));
}

function getWindowHeight(win)
{
    if (win == undefined)
        win = window;
    if (win.innerHeight) {
        return win.innerHeight;
    } else {
        if (win.document.documentElement 
                && win.document.documentElement.clientHeight) 
        {
            return win.document.documentElement.clientHeight;
        }
        return win.document.body.offsetHeight;
    }
}

function getWindowWidth(win)
{
    if (win == undefined)
        win = window;
    if (win.innerWidth) {
        return win.innerWidth;
    } else {
        if (win.document.documentElement 
                && win.document.documentElement.clientWidth) 
        {
            return win.document.documentElement.clientWidth;
        }
        return win.document.body.offsetWidth;
    }
}

function resizeLeftSide() {
  var leftSideHeight = getWindowHeight() - 200;
  var leftSideMiddleHeight = leftSideHeight - 300 - 40;
  if(leftSideHeight > 440) {
    document.getElementById('leftSide').style.height = leftSideHeight;
    document.getElementById('leftSideMiddle').style.height = leftSideMiddleHeight;
    document.getElementById('leftSide').setAttribute("style", "height:" + leftSideHeight + "px;", 0);
    document.getElementById('leftSideMiddle').setAttribute("style", "height:" + leftSideMiddleHeight + "px;", 0);
  }
}

function resizeMainWnd() {
  var wndHeight = getWindowHeight();
  document.getElementById('mainWnd').style.height = wndHeight;
  document.getElementById('mainWnd').setAttribute("style", "height:" + wndHeight + "px;", 0);
}

function displayHeadConditional() {       // für Pixelseite: Prüft, ob genug Platz:
  var wndHeight = getWindowHeight();      // wenn ja: Seitenkopf anzeigen
  //var newTop1 = 240;            // Höhe von head
  var newTop1 = 200;            // Höhe von head
  //var newTop2 = 240+29-5;       // Höhe von head + Abstand nach oben von pixelArea - Abstand nach oben von tabArea 
  var newTop2 = 200+29-5;       // Höhe von head + Abstand nach oben von pixelArea - Abstand nach oben von tabArea 
  //var newTop3 = 240+29-5+540+2; // Höhe von head + Abstand nach oben von pixelArea - Abstand nach oben von tabArea + Höhe von PixelArea + kl. Abstand
  var newTop3 = 200+29-5+540+2; // Höhe von head + Abstand nach oben von pixelArea - Abstand nach oben von tabArea + Höhe von PixelArea + kl. Abstand
  // TODO step for step more things showing up
  var limit = 805;              // Höhe von Head + Höhe von PixelArea + Abstand nach oben von pixelArea
  if(wndHeight > limit) {
    document.getElementById('head').style.display="block";
    if(document.getElementById('bigCityTabArea')) {           // Großstadt
      document.getElementById('bigCityTabArea').style.top = newTop1;
      document.getElementById('bigCityTabArea').setAttribute("style", "top:"+ newTop1 +"px;", 0);
    }
    if(document.getElementById('mediumCityTabArea')) {        // mittlere Stadt
      document.getElementById('mediumCityTabArea').style.top = newTop1;
      document.getElementById('mediumCityTabArea').setAttribute("style", "top:"+ newTop1 +"px;", 0);
    }
    if(document.getElementById('smallCityTabArea')) {         // Kleinstadt
      document.getElementById('smallCityTabArea').style.top = newTop1;
      document.getElementById('smallCityTabArea').setAttribute("style", "top:"+ newTop1 +"px;", 0);
    }
    document.getElementById('pixelArea').style.top = newTop2;
    document.getElementById('pixelArea').setAttribute("style", "top:"+ newTop2 +"px;", 0);
    document.getElementById('impressumLink').style.top = newTop3;
    document.getElementById('impressumLink').setAttribute("style", "top:"+ newTop3 +"px;", 0);
  }
}

function fixIEMenuBug() {
  if (document.all&&document.getElementById) {
    navRoot = document.getElementById("navList");
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (node.nodeName=="LI") {
        node.onmouseover=function() {
          this.className+=" over";
        }
        node.onmouseout=function() {
          this.className=this.className.replace(" over", "");
        }
      }
    }
  }
}

function getBgColorFromFgColor(color) {
  var iRed = parseInt("0x"+color.substring(1,3));
  var iGreen = parseInt("0x"+color.substring(3,5));
  var iBlue = parseInt("0x"+color.substring(5,7));
  bgRed = (0.2*iRed+0.8*255);
  bgGreen = (0.2*iGreen+0.8*255);
  bgBlue = (0.2*iBlue+0.8*255);
  bgColor = stringInt(bgRed,16,2)+stringInt(bgGreen,16,2)+stringInt(bgGreen,16,2);
  return ('#'+bgColor);
}

/* With this method each tab is connected to a certain colour */
function getTabColor(categoryFolder) {
  var color = '#428cfe';
    if(categoryFolder == "Auto")
      color='#428cfe';
    if(categoryFolder == "Bildung")
      color='#3366ff';
    if(categoryFolder == "Bringdienste")
      color='#6045fd';
    if(categoryFolder == "Finanzen")
      color='#9933ff';
    if(categoryFolder == "Gesundheit")
      color='#d445fd';
    if(categoryFolder == "Handwerk")
      color='#ff33ff';
    if(categoryFolder == "Hotels")
      color='#ff3399';
    if(categoryFolder == "Immobilien")
      color='#ff3366';
    if(categoryFolder == "Industrie")
      color='#ff3333';
    if(categoryFolder == "IT")
      color='#ff9933';
    if(categoryFolder == "Medien")
      color='#ffcc33';
    if(categoryFolder == "Nightlife")
      color='#ffff33';
    if(categoryFolder == "Notdienste")
      color='#ccff33';
    if(categoryFolder == "Restaurants")
      color='#99ff33';
    if(categoryFolder == "Shopping")
      color='#4efd44';
    if(categoryFolder == "Staedtisches")
      color='#33ff66';
    if(categoryFolder == "Tourismus")
      color='#46fdea';
    if(categoryFolder == "Unterhaltung")
      color='#33ff99';
    if(categoryFolder == "Vereine")
      color='#33ccff';
    if(categoryFolder == "Auto_Mobilitaet")
      color='#428cfe';
    if(categoryFolder == "Essen_Schlafen")
      color='#9933ff';
    if(categoryFolder == "Finanzen_Immobilien")
      color='#ff33ff';
    if(categoryFolder == "Freizeit_Tourismus")
      color='#ff3366';
    if(categoryFolder == "Gesundheit_Wellness")
      color='#ffcc33';
    if(categoryFolder == "Industrie_Handwerk")
      color='#4efd44';
    if(categoryFolder == "Shopping_Lifestyle")
      color='#33ff99';
    if(categoryFolder == "Oeffentliches_Vereine")
      color='#33ccff'; 
  return color;
}

function setPixelareaBorder(categoryFolder) {
  var color = getTabColor(categoryFolder);
  document.getElementById("pixelArea").style.border = "solid "+color+" 1px";
}

function showImage(categoryFolder) {
  document.getElementById(categoryFolder).getElementsByTagName("img")[0].style.display="block";
  document.getElementById(categoryFolder).style.height = 25;
  document.getElementById(categoryFolder).setAttribute("style", "height:25px;", 0);
}

function stringInt(num,base,size) {
  var i, q, sign, result="", baseTable="0123456789ABCDEF";
  num=parseInt(num);
  sign=(num<0)?"-":"";
  base=parseInt(base);
  size=(size)?size:0;
  if(!base || base<=1 || base>baseTable.length) {
    base=10;
  }
  while(true) {
    q=num/base;
    result=baseTable.charAt(parseInt(getFrac(q)*base))+result;
    num=parseInt(q);
    if(num==0) {
      break;
    } 
  }
  q="";
  size-=result.length;
  for(i=0;i<size;i++) {
    q+="0";
  }
  return sign+q+result;
}

function getFrac(num) {
  num=""+num;
  return parseFloat("0."+num.substring(num.length-((num.indexOf(".")>=0)?num.length-num.indexOf(".")-1:0),num.length));
}
