//----------------------------------------------
//  Main Navigation
//----------------------------------------------

var positionReferenceLayer = 'divrubric4';
var countLangSelector      = confCountryTopic+'/'+confLanguageTopic;
switch (countLangSelector) {
  case "gr/el":
    dropperGif = buildValidServerRelativeUrl("/_common/img/modul_navigation/dropper_gr-el.gif");
    break;
  case "nl/nl":
    positionReferenceLayer = 'divrubric3';
    break;
  default:
    positionReferenceLayer = 'divrubric4';
    break;
}

var menuOpen = false;
var  currentHighLight;
function closeMenu(menuID) {
  if (menuID == 'close') {
   if (menuOpen) {
      resizeLayer('firstNavi', null, 101)
      resizeLayer(parent.frames['mainNavigationIFrame'].parent.document.getElementById('mainNavigationIFrame'), null, 101)
      setZIndex(parent.frames['mainNavigationIFrame'].parent.document.getElementById('mainNavi'), 919);
      setVisibility('bmw' + menuOpen,0);
      setCurtain(0);
//      if ((menuOpen == 'rubric4') || (menuOpen == 'rubric5') || (menuOpen == 'rubric6')){
//        setClassName('nav'+menuOpen,'mainNavi');
//      } else {
        setClassName('nav'+menuOpen,'mainNaviVehicles');
//      }
      menuOpen = false;
    }
  }
}
function openMenu(menuID) {
  if (parent.frames['mainNavigationIFrame'].parent.document.getElementById('changeToHighend')) { // remove Layer to highendVersion
    oldDiv=parent.frames['mainNavigationIFrame'].parent.document.getElementById('changeToHighend');
    parent.frames['mainNavigationIFrame'].parent.document.getElementsByTagName("body")[0].removeChild(oldDiv);
  }
  readLeft  = getDivInformation('div'+menuID,'offsetLeft');
  readLeft2 = getDivInformation(positionReferenceLayer,'offsetLeft');
  readTop   = getDivInformation('div'+menuID,'offsetTop');
  navioffsetTop = 49;
  navioffsetLeft = 0;
  if (browserId == 'MSIE') {
    navioffsetLeft = -1;
  }
  for(i=0;i < 2;i++){
    if ((menuID == 'rubric4') || (menuID == 'rubric5') || (menuID == 'rubric6')){
       moveObject('bmw'+menuID,readLeft2 +navioffsetLeft, navioffsetTop);
    } else {
       moveObject('bmw'+menuID,readLeft + navioffsetLeft, navioffsetTop);
    }
  }
  if (menuOpen != menuID) {
    resizeLayer('firstNavi', null, 344);
    resizeLayer(parent.document.getElementById('mainNavigationIFrame'), null, 344);
    aktuallyHi = document.getElementById('nav'+menuID).className;
    if (aktuallyHi == 'mainNaviHighlight') {
      currentHighLight = menuID;
    }
    setVisibility('bmw'+menuID,1);
    setCurtain(1);
    setZIndex(parent.document.getElementById('mainNavi'),920);
    if (menuOpen) {
      setVisibility('bmw'+menuOpen,0);
//      if ((menuOpen == 'rubric4') || (menuOpen == 'rubric5') || (menuOpen == 'rubric6')){
//        setClassName('nav'+menuOpen,'mainNavi');
//      } else {
        setClassName('nav'+menuOpen,'mainNaviVehicles');
//      }
	    if (currentHighLight == menuOpen){
        setClassName('nav'+menuOpen,'mainNaviHighlight');
      }
    }
    setClassName('nav'+menuID,'mainNaviClicked');
    menuOpen = menuID;
  } else{
    resizeLayer('firstNavi', null, 101);
    resizeLayer(parent.document.getElementById('mainNavigationIFrame'), null, 101);
    setVisibility('bmw'+menuID,0);
    setCurtain(0);
    setZIndex(parent.document.getElementById('mainNavi'),919);
//    if ((menuID == 'rubric4') || (menuID == 'rubric5') || (menuID == 'rubric6')){
//      setClassName('nav'+menuID,'mainNavi');
//    } else {
      setClassName('nav'+menuID,'mainNaviVehicles');
//    }
	  if (currentHighLight == menuOpen){
      setClassName('nav'+menuOpen,'mainNaviHighlight');
    }
    menuOpen = false;
  }
}
function setCurtain(mode){
  // please modificate also the setCurtain function in the narrowband_bottom.html
  if ((parent.useCurtain) && (parent.useCurtain == "true")){
     if (mode == 1){
       setVisibility(parent.document.getElementById('iFrameContainer'),0);
       moveObject(parent.document.getElementById('iFrameContainer'),((window.screen.width*2)*(-1)));
       setVisibility(parent.document.getElementById('curtain'),null,'block');
    } else {
       setVisibility(parent.document.getElementById('curtain'),null,'none');
       if (typeof parent.leftPos !='undefined') {
         moveObject(parent.document.getElementById('iFrameContainer'),parent.leftPos);
       }
       else {
         moveObject(parent.document.getElementById('iFrameContainer'),0);
       }
       setVisibility(parent.document.getElementById('iFrameContainer'),1);

    }
  }
}
function openMenuOver(menuID) {
  if ((menuOpen) && (menuOpen != menuID)){
    openMenu(menuID)
  }
}
function highlightMainNavigation(){
  if(parent.name=='frameContent'){
    var links=buildLinkList();
    var evaluatedLinks = evaluateHighlighting(parent.location.href,links);
    var highlightedIndex=-1;
    var bestmatch=999;
    for(i=0;i < evaluatedLinks.length;i++){
      if(evaluatedLinks[i] < bestmatch) {
        bestmatch=evaluatedLinks[i];
        highlightedIndex=i;
        if(evaluatedLinks[i]==-2) {
          break;
        }
      }
    }
    if(highlightedIndex!=-1){
      document.getElementsByTagName('a')[highlightedIndex].className='mainNaviHighlight';
      if(document.getElementsByTagName('a')[highlightedIndex].getAttribute('navParent')){
        setClassName(document.getElementsByTagName('a')[highlightedIndex].getAttribute('navParent'),'mainNaviHighlight');
      }
      if(document.getElementsByTagName('a')[highlightedIndex].getAttribute('navHeader')){
        setClassName(document.getElementsByTagName('a')[highlightedIndex].getAttribute('navHeader'),'mainNaviHighlight');
        if (document.getElementsByTagName('a')[highlightedIndex+1]) {
          document.getElementsByTagName('a')[highlightedIndex+1].className='mainNaviHighlight';
        }
      }
    }
  }
}
function positionLayers() {
  var layerList = new Array();
      layerList = new Array('bmwrubric1','bmwrubric2','bmwrubric3');
  var layerList2 = new Array();
      layerList2 = new Array('bmwrubric4','bmwrubric5','bmwrubric6');
  for(i=0;i < layerList.length;i++){
    if (document.getElementById(layerList[i])) {
      readLeft = getDivInformation(layerList[i],'offsetLeft');
      moveObject(layerList[i],readLeft+24,49);
    }
  }
  for(i=0;i < layerList2.length;i++){
    if (document.getElementById(layerList2[i])) {
      readLeft2 = getDivInformation(layerList2[0],'offsetLeft');
      moveObject(layerList2[i],readLeft2+24,49);
    }
  }
}
//----------------------------------------------
//  Module Navigation
//----------------------------------------------
var oldOne = '';
var oldSubOne = '';
var ulHeight = undefined;
var naviSummaryHeight;
checkClient();
if (browserId == 'MSIE') {
  naviSummaryHeight = 436;
} else{
  naviSummaryHeight = 440;
}
var myTabIndex = 1000;
function writeModuleNavigation(){
  ulHeight = (naviSummaryHeight);
  var contentUrl=getServerRelativeContentUrl();
  writeContent  = '<div id="navigation2">';
  writeContent += '  <ul>';
  for (i = 0; i < moduleLink.length; i++) {
    if (i==moduleLink.length-1) {
      //writeContent += '    <li style="border-bottom:1px solid #ffffff;">';
	  writeContent += '    <li>';
    } else {
      writeContent += '    <li>';
    }
	writeContent += '      <div id="div'+i+'ScrollParent" style="position:relative; left:0px; top:0px; width:191px; height:'+ulHeight+'px; overflow:hidden;display:block; background-image: url('+bgNaviGif+');">';
    writeContent += '        <div id="div'+i+'ScrollControl" style="position:absolute; right:0px;  top:0px;  width:12px;  height:'+ulHeight+'px;  visibility:hidden;">';
	 //writeContent += '        <div id="div'+i+'ScrollControl" style="position:absolute; right:0px;  top:0px;  width:12px;  height:'+ulHeight+'px;  visibility:hidden; background-image: url('+backgroundGif+');">';
    writeContent += '          <div style="position:absolute;top:'+(ulHeight/2-12)+'px"><a href="javascript:stop();" onMouseDown="up(2,'+i+');" onMouseUp="stop();" ><img src="'+scrollerUpGif+'"   width="12" height="12" border="0" id="ScrollerArrowUp"></a></div>';
    writeContent += '          <div style="position:absolute;top:'+(ulHeight/2)+'px"><a href="javascript:stop();" onMouseDown="down(2,'+i+');" onMouseUp="stop();"  ><img src="'+scrollerDownGif+'" width="12" height="11" border="0" id="ScrollerArrowDown"></a></div>';
    writeContent += '        </div>';
    writeContent += '        <div id="div'+i+'ScrollChild" onclick="getLastHeight(this,'+i+');" style="position:absolute; left:0px; top:0px; width:250px; visibility:inherit;">';
    if (moduleLink[i][0][0][1] != '') {
      writeContent += '          <a href="'+moduleLink[i][0][0][1]+'"  id="link'+i+'" onclick="blockFolding();">';
    } else if (moduleLink[i][1]) {
      writeContent += '          <a href="javascript:setBlock(\''+i+'\');"  id="link'+i+'">';
    } else {
      writeContent += '          <a href="javascript://"  id="link'+i+'">';
    }
    writeContent +=              moduleLink[i][0][0][0]+'<br><span id="copyLink'+i+'" style="-line-height:13px;">'+moduleLink[i][0][0][2]+'</span>';
    writeContent += '          </a>';
    writeContent += '          <ul id="ulId'+i+'">';
    for (j = 1; j < moduleLink[i].length; j++) {
      if (moduleLink[i][j][0][2] != '') {
        writeContent += '            <li class="firstData">'+moduleLink[i][j][0][2]+'</li>';
      }
      if (j==1) {
       liClass =   ' class="firstData"';
      } else {
       liClass =   '';
      }
      writeContent += '            <li'+liClass+'>';
      if (moduleLink[i][j][0][1] != '') {
        writeContent += '              <a href="'+moduleLink[i][j][0][1]+'"  id="link'+i+'-'+j+'" onclick="blockFolding();">'+moduleLink[i][j][0][0]+'</a>';
      } else if (moduleLink[i][j][1]) {
        writeContent += '              <a href="javascript:setSubBlock(\''+i+'-'+j+'\');"  id="link'+i+'-'+j+'">'+moduleLink[i][j][0][0]+'</a>';
      } else {
        writeContent += '              <a href="javascript://" id="link'+i+'-'+j+'"  onclick="blockFolding();">'+moduleLink[i][j][0][0]+'</a>';
      }
      if (moduleLink[i][j][1]) {
        writeContent += '              <ul id="ulId'+i+'-'+j+'" style="display:none;">';
      }
      for (k = 1; k < moduleLink[i][j].length; k++) {
        if (moduleLink[i][j][k][2] == 'newGroup'){
          liSubClass = ' class="newGroup"';
        } else {
          liSubClass = '';
        }
        writeContent +=   '              <li'+liSubClass+'><a href="'+moduleLink[i][j][k][1]+'"  id="link'+i+'-'+j+'-'+k+'" onclick="blockFolding();">'+moduleLink[i][j][k][0]+'</a></li>';
      }
      if (moduleLink[i][j][1]) {
        writeContent += '              </ul>';
      }
      writeContent += '            </li>';
    }
    writeContent += '          </ul>';
    writeContent += '        </div>';
    writeContent += '      </div>';
    writeContent += '    </li>';
  }
  writeContent += '    </ul>';
  //writeContent += '    <div id="buttonClose"><a href="javascript:moveMenu();"><img src="'+dropperGif+'" alt="" width="221" height="16" border="0"></a></div>';
  writeContent += '  </div>';
  writeIntoLayer('naviClipArea',writeContent);
}
var currentChildObj;
var currentChildObjIndexm;
function getLastHeight(obj,nr) {
  currentChildObj = obj;
  currentChildObjIndex = nr;
}
var modulNaviOpen = true;
function moveMenu () {
  navHeight = getDivInformation('navigation2','height');
  newSpeed = 20;
  if (typeof getDivInformation('navigation2', 'offsetTop') != 'undefined' && getDivInformation('navigation2', 'offsetTop') < 0) {
    openNaviEvent();
    modulNaviOpen = true;
  } else {
    setTimeout("closeNaviEvent()",200);
    setTimeout("modulNaviOpen = false",200);
  }
  if (navHeight >= naviSummaryHeight-19) {
    newSpeed = 40;
  }
  if (browserId != 'MSIE' && modulNaviOverImage == true) {
    newSpeed = 100;
  }
  moveObject('navigation2','',19-navHeight,newSpeed,1);
}
function closeModulNavi () {
  if (currentPositions['navigation2'][1] != 0) {
    moveMenu();
    modulNaviOpen = false;
  }
}
function openModulNavi () {
  if (currentPositions['navigation2'][1] == 0) {
    moveMenu();
    modulNaviOpen = true;
  }
}
function openNaviEvent() {
  ;
}
function closeNaviEvent() {
  ;
}
// - - - - - SCROLLER - - - - - - //
function up(setspeed,n) {
  divNum = n;
  speed = setspeed;
  eval(stop());
  if (getDivInformation('div'+n+'ScrollChild','offsetTop') < 0) {
    moveObject('div'+n+'ScrollChild','',(getDivInformation('div'+n+'ScrollChild','offsetTop') + setspeed));
  }
  goup = setTimeout("up(speed,divNum)", 1);
}
function down(setspeed,n) {
  divNum = n;
  speed = setspeed;
  eval(stop());
  if (getDivInformation('div'+n+'ScrollChild','offsetTop') > - getDivInformation('div'+n+'ScrollChild','height') + getDivInformation('div'+n+'ScrollParent','height')) {
    moveObject('div'+n+'ScrollChild','',(getDivInformation('div'+n+'ScrollChild','offsetTop') - setspeed));
  }
  godown = setTimeout("down(speed,divNum)", 1);
}
function stop() {
  if (goup) {
    clearTimeout(goup);
  }
  if (godown) {
    clearTimeout(godown);
  }
}
function initScroller() {
  for (var i=0; i < initScroller.arguments.length; i++) {
    parentLayerName = initScroller.arguments[i] + "ScrollParent";
    childLayerName  = initScroller.arguments[i] + "ScrollChild";
    if(getDivInformation(initScroller.arguments[i] + 'ScrollChild','height') > getDivInformation(initScroller.arguments[i] + 'ScrollParent','height')) {
      setVisibility(initScroller.arguments[i]+"ScrollControl",1);
      moveObject('navigation2','',1,'',1);
      moveObject('navigation2','',1,'',1);
    } else {
      setVisibility(initScroller.arguments[i]+"ScrollControl",0);
    }
  }
}
// - - - - - //SCROLLER - - - - - - //
var tempBlockNum = '';
var tempBlockSubNum = '';
var currentMatch = null;
var currentIndex;
if (typeof modulnavHeightTeasermode == 'undefined') {
  var modulnavHeightTeasermode = 274;
}
function setBlock(numb) {
  tempBlockNum = numb;
  if(oldOne != numb) {
    if (oldOne != '') {
      setVisibility('div'+oldOne+'ScrollControl',0);
      setVisibility('ulId'+oldOne,null,'none');
      moveObject('div'+oldOne+'ScrollChild',0,0);
    }
    for (i = 0; i < moduleLink.length; i++) {
      setVisibility('copyLink'+i,null,'none');
      if(document.getElementById('div'+i+'ScrollParent')){
        resizeLayer('navigation2', null,434);
        resizeLayer('div'+i+'ScrollParent', null, getDivInformation('link'+i,'height'));
      }
      if (moduleLink[numb][1]){
        if(typeof currentChildObj == 'undefined') {
          currentChildObj = document.getElementsByTagName('div')['div'+numb+'ScrollParent'];
        }
        if (typeof currentChildObjIndex == 'undefined'){
          currentChildObjIndex = numb;
        }
        var newModulheight = 0;
        var modulheightRest = 0;
        for (a = 0; a < moduleLink.length; a++) {
          if (typeof eval(document.getElementsByTagName('div')['div'+a+'ScrollParent']) == 'object' && a != currentChildObjIndex && (getDivInformation(eval(document.getElementsByTagName('div')['div'+a+'ScrollParent']),'height') < 100)) {
            if (browserId == 'MSIE') {
              modulheightRest += getDivInformation(eval(document.getElementsByTagName('div')['div'+a+'ScrollParent']),'height') + 11;
            } else {
              modulheightRest += getDivInformation(eval(document.getElementsByTagName('div')['div'+a+'ScrollParent']),'height') + 10;
            }
          }
        }
        newModulheight = getDivInformation(currentChildObj,'height');
        resizeLayer('div'+numb+'ScrollParent', null, 440 - (modulheightRest + 34));
      }
    }
    setVisibility('ulId'+numb,null,'block');
    oldOne = numb;
    initScroller('div'+numb);
  } else {
    for (i = 0; i < moduleLink.length; i++) {
      if (typeof moduleLink[currentIndex] != 'undefined' && moduleLink[currentIndex][0][0][3] == true) {
        setVisibility('copyLink'+i,null,'block');
      }
      if (browserId == 'MSIE') {
        if (i == currentIndex) {
          resizeLayer('div'+i+'ScrollParent', null, 22);
        } else {
          resizeLayer('div'+i+'ScrollParent', null, getDivInformation('div'+i+'ScrollChild','height')+0);
        }
        resizeLayer('navigation2', null, modulnavHeightTeasermode);
      } else {
        if (i == currentIndex && moduleLink[currentIndex][0][0][3]==true) {
          resizeLayer('div'+i+'ScrollParent', null, getDivInformation('div'+i+'ScrollChild','height'));
        } else if (i == currentIndex){
          resizeLayer('div'+i+'ScrollParent', null, getDivInformation('div'+i+'ScrollChild','height')+0);
        } else {
          resizeLayer('div'+i+'ScrollParent', null, getDivInformation('div'+i+'ScrollChild','height')+0);
        }
        resizeLayer('navigation2', null,modulnavHeightTeasermode -3);
      }
    }
    if(oldOne!='') {
    	setVisibility('ulId'+oldOne,null,'none');
      if (browserId == 'MSIE') {
        resizeLayer('div'+oldOne+'ScrollParent', null, getDivInformation('div'+oldOne+'ScrollChild','height')+0);
      } else {
        resizeLayer('div'+oldOne+'ScrollParent', null, getDivInformation('div'+oldOne+'ScrollChild','height')+0);
      }
      initScroller('div'+oldOne);
    }
    oldOne = '';
  }
}
function setSubBlock(subNumb) {
  tempBlockSubNum = subNumb;
  if(oldSubOne != subNumb) {
    if (oldSubOne != '') {
      if (document.getElementById('ulId'+oldSubOne)){
        setVisibility('ulId'+oldSubOne,null,'none');
      }
    }
    if (document.getElementById('ulId'+subNumb)){
      setVisibility('ulId'+subNumb,null,'block');
    }
    oldSubOne = subNumb;
    subNumb = subNumb.split('-')[0];
    initScroller('div'+subNumb);
  } else {
    setVisibility('ulId'+subNumb,null,'block');
    setVisibility('div'+oldOne+'ScrollControl',0);
    moveObject('div'+oldOne+'ScrollChild',0,0);
    setVisibility('ulId'+oldSubOne,null,'none');
    oldSubOne = oldSubOne.split('-')[0];
    initScroller('div'+oldSubOne);
    oldSubOne = '';
  }
}
var controlBlockNum = -99;
var controlSubBlockNum = -99;
function setModuleNavigationState(displayFolded){
  var contentLinkFull=getServerRelativeContentUrl();
  var navLinks = new Array();
  for (l = 0; l < moduleLink.length; l++) {
    for (m = 0; m < moduleLink[l].length; m++) {
      for (n = 0; n < moduleLink[l][m].length; n++) {
        navLinks.push(moduleLink[l][m][n][1]);
      }
    }
  }
  var evaluatedLinks = evaluateHighlighting(contentLinkFull, navLinks);
  var c=0;
  var bestmatch=999;
  var highlightedIndex = new Array(3);
  for (l = 0; l < moduleLink.length; l++) {
    for (m = 0; m < moduleLink[l].length; m++) {
      for (n = 0; n < moduleLink[l][m].length; n++) {
        if(evaluatedLinks[c] < bestmatch) {
          bestmatch=evaluatedLinks[c];
          highlightedIndex[0]=l;
          highlightedIndex[1]=m;
          highlightedIndex[2]=n;
        }
        if(bestmatch==-2) {
          break;
        }
        c++;
      }
      if(bestmatch==-2) {
        break;
      }
    }
    if(bestmatch==-2) {
      break;
    }
  }
  if(bestmatch!=999){
    currentMatch = moduleLink[highlightedIndex[0]][highlightedIndex[1]][highlightedIndex[2]][1];
    currentIndex = highlightedIndex[0];
    if (   moduleLink[highlightedIndex[0]][highlightedIndex[1]][highlightedIndex[2]] == moduleLink[highlightedIndex[0]][0][0]
      && !moduleLink[highlightedIndex[0]][1]
      && moduleLink[highlightedIndex[0]][highlightedIndex[1]][highlightedIndex[2]][3] == true) {
      setBlock('');
      setColor('link'+highlightedIndex[0],'#FFFFFF');
	  setBGColor('link'+highlightedIndex[0],'#000000');
    } else {
      setBlock(''+highlightedIndex[0]);
      setColor('link'+highlightedIndex[0],'#FFFFFF');
	  	  setBGColor('link'+highlightedIndex[0],'#808183');
      if (highlightedIndex[1] !=0) {
        setSubBlock(highlightedIndex[0]+'-'+highlightedIndex[1]);
        setColor('link'+highlightedIndex[0]+'-'+highlightedIndex[1],'#FFFFFF');
		 setBGColor('link'+highlightedIndex[0]+'-'+highlightedIndex[1],'#808183');
        if (highlightedIndex[2] !=0) {
          setColor('link'+highlightedIndex[0]+'-'+highlightedIndex[1]+'-'+highlightedIndex[2],'#FFFFFF');
		    setBGColor('link'+highlightedIndex[0]+'-'+highlightedIndex[1]+'-'+highlightedIndex[2],'#808183');
        }
      }
    }
  } else {
    setBlock('0');
  }
  if(displayFolded){
    moveMenu();
  }
}
function initialiseModuleNavigation(displayFolded){
  if(!arrayPrepared){
    for (l = 0; l < moduleLink.length; l++) {
      for (m = 0; m < moduleLink[l].length; m++) {
        for (n = 0; n < moduleLink[l][m].length; n++) {
          moduleLink[l][m][n][1]	= buildValidServerRelativeUrl(moduleLink[l][m][n][1]);
        }
      }
    }
    arrayPrepared=true;
  }
  tempDisplayFolded = displayFolded;
  setModuleHeader(false);
  if(moduleNavigationDisplayed){
    writeModuleNavigation();
    setModuleNavigationState(displayFolded);
  }
}
function getServerRelativeContentUrl(){
  var contentPage=self.location.href;
  var substract=self.location.host;
  return contentPage.substring(contentPage.indexOf(substract)+substract.length,contentPage.length);
}
var foldBack = false;
var noFolding = false;
var tempDisplayFolded, isFolding;
var isActiveFolding = true;
var arrayPrepared=false;
function blockFolding(){
  noFolding = true;
}
function moduleNaviReset(){
  if (foldBack == true && isActiveFolding == true && noFolding == false && modulNaviOpen == true) {
    if ((controlBlockNum != tempBlockNum) || (controlSubBlockNum != tempBlockSubNum)) {
      oldOne = '';
      oldSubOne = '';
      controlBlockNum = tempBlockNum;
      controlSubBlockNum = tempBlockSubNum;
      initialiseModuleNavigation(false);
    }
    clearTimeout(isFolding);
    isActiveFolding = false;
  } else {
    clearTimeout(isFolding);
    isActiveFolding = true;
  }
}

