﻿var i, e;
var browserAtributeLength, browserId, platform;
var imgCountTotal, lowImageSrc, highImageSrc, currentImg, currentState, currentAct, currentPerm, checkLoad;
var slideAmount;
var divNum, documentLeftScroll, documentTopScroll, mouseX, mouseY, loopDragging;
var speedHorizontal, speedVertical, currentBack, goup, godown, speed, currentObjNo;
var setDivPosition, setBackPosition, currentObjId, currentSpeed, currenDirection, currentDelay;
var diffWidth, diffHeight, lastWidth, lastHeight, currentDiv;
var popupWindow, winUrl;
var ua = navigator.userAgent.toLowerCase();
var an = navigator.appName.toLowerCase();
var currentStep = 0;
var windowWidth = 0;
var windowHeight = 0;
var browserVersion = 0;
var loaded = 0;
var divLeft = 0;
var divTop = 0;
var looping = -1;
var slideCount = -1;
var writeBrowser = "";
var tempAct = "";
var slideNumber = "";
var slideDescription = "";
var preLoadArray = [];
var preLoadCounter = [];
var highImages = [];
var lowImages = [];
var slideText = [];
var permanentActive = [];
var slideImages = [];
var supportedOS = false;
var supportedVersion = false;
var currentLoop = false;
var dragAllowed = false;
var divIsMoving = false;
var flashversion = false;
var topFrame = null;
var contentFrame = null;
var bottomFrame = null;
var historyFrame = null;
var hiddenFrame = null;
var allowClose = true;
var modulNaviOverImage = true;
var idmodulsSpecial = "";
var minFlashVersion = 7;
var isMainnavigation = false;
var indexParameters = "";
var query = {};
var parameterArray = [];
var scrollerDefaultSize = 1024;
var scrollerCheckElements = [];
var scrollerSize = scrollerDefaultSize;
var scrollerBgImage;
var scrollerSliderImage;
var scrollerImageUp;
var scrollerImageDown;
var persoEventType = "";
var persoSeries, persoBodytype, persoModel, persoColor, persoRim;
var prmContent = "";
var divsToBeAltered = [];
var useCurtain = false;
var htmlDialogOpen = false;

if (typeof browser != 'object') {
  browser = [['Opera', 'opera ', '9.0', 'windows', 'mac os x', 'other'], ['Safari', 'safari/', '125', '', 'mac os x', ''], ['Netscape', 'netscape/', '7.1', 'windows', 'mac os x', 'other'], ['Firefox', 'firefox/', '1.0', 'windows', 'mac os x', 'other'], ['Mozilla', 'rv:', '1.7', 'windows', 'mac os x', 'other'], ['MSIE', 'msie ', '5.5', 'windows', '', ''], ['Netscape4', 'mozilla/', '4.0', '', '', '']];
}

function ssoCloseDialog(){
}

function checkClient(){
  var browserLength = browser.length;
  for (i = 0; i < browserLength; i++) {
    browserAtributeLength = browser[i].length;
    if (ua.indexOf(browser[i][1]) != -1) {
      browserId = browser[i][0];
      for (e = 3; e < browserAtributeLength; e++) {
        if (browser[i][e] != '' && (ua.indexOf(browser[i][e]) != -1 || browser[i][e] == 'other')) {
          supportedOS = true;
          platform = browser[i][e];
          break;
        } else {
          supportedOS = false;
        }
      }
      browserVersion = ua.split(browser[i][1]);
      browserVersion = parseFloat(browserVersion[1].slice(0, 3));
      if (browserVersion >= browser[i][2]) {
        supportedVersion = true;
      } else {
        supportedVersion = false;
      }
      break;
    } else {
      browserId = 'unknown';
    }
  }
  browserId = ((browserId == "MSIE") ? (((/\s+msie\s+7\.\d+/).test(navigator.appVersion.toLowerCase())) ? ("MSIE7") : (browserId)) : (browserId));
}

function checkBrowser(incompatibleBrowserUrl){
  checkClient();
  return true;
}
function checkFrameset(){
}

function removeClassName(obj, cssClass){
  if (typeof obj != 'object') {
    obj = document.getElementById(obj);
  }
  if (hasClassName(obj, cssClass)) {
    obj.className = obj.className.replace(new RegExp(cssClass), '');
  }
}

function replaceClassName(obj, oldCssClass, newCssClass){
  if (typeof obj != 'object') {
    obj = document.getElementById(obj);
  }
  if (hasClassName(obj, oldCssClass)) {
    obj.className = obj.className.replace(new RegExp(oldCssClass), newCssClass);
  }
}

function hasClassName(obj, cssClass){
  if (typeof obj != 'object') {
    obj = document.getElementById(obj);
  }
  return (obj.className.indexOf(cssClass) != -1);
}

function addClassName(obj, cssClass){
  if (typeof obj != 'object') {
    obj = document.getElementById(obj);
  }
  if (!hasClassName(obj, cssClass)) {
    obj.className += ' ' + cssClass;
  }
}


var isFolding, resetModulNaviTimer, moduleHeader;
function moduleNaviReset(){}

function highlightNavigations(moduleNaviFolded, initModuleNavigation, startCounter){
  if (document.getElementById('moduleNavigation') != undefined) {
    moduleNavigation = new ModuleNavigation('moduleNavigation', moduleNaviFolded);
    moduleNavigation.init();
    clearTimeout(isFolding);
  }
  // There is a bug in Firefox on Macintosh. If a flash movie is overlayed by a
  // semi-transparent HTML layer set with -moz-opacity the flash movie is not visible
  // Therefor we remove this setting on firefox mac, if the page contains an embed tag
  if (browserId == "Firefox" && platform == "mac os x" && document.getElementsByTagName('embed').length > 0) {
    document.getElementById('moduleNavigation').style.MozOpacity = "1";
    document.getElementById('moduleNavigation').style.opacity = "1";
  }
}

/** Module Navigation Class. This class handles all functionality of the module navigation
 * @projectDescription Module Navigation Class. This class handles all functionality of the module navigation
 * @author Marcello di Simone
 * @namespace ModuleNavigation
 * @version 1.0
 */
var modulenaviAnimation = true;
var hiddenModuleNavigation = false;
var moduleNavigation;
/** This class handles all functionality of the module navigation
 * @classDescription This class handles all functionality of the module navigation
 * @constructor
 * @return {ModuleNavigation}
 */
var ModuleNavigation = (function(){
  var _normalHeight = 460;
  var _teaserHeight = 460;
  var _closedHeight = 42;
  var _hiddenHeight = 22;
  /** maximum height of the entire module navigation
   * @type {Number}
   */
  var _maxHeight;
  /** minimum height of the entire module navigation
   * @type {Number}
   */
  var _minHeight = 42;
  /** height of the subtopic element
   * @type {Number}
   */
  var _subtopicHeight = 0;
  /** Number of steps the folding animation should make
   * @type {Number}
   */
  var _foldingSteps = 10;
  /** Number of milliseconds to wait between the folding animation steps
   * @type {Number}
   */
  var _foldInterval = 20;
  /** Number of steps the scroll animation should make
   * @type {Number}
   */
  var _scrollSteps = 100;
  /** Number of milliseconds to wait between the scroll animation steps
   * @type {Number}
   */
  var _scrollInterval = 20;
  /** Number of the scroll animation intervall
   * @type {Number}
   */
  var scrollIntId;
  /** Number of the fold animation intervall
   * @type {Number}
   */
  var foldIntId;
  /** current active node element test bla @see init
   * @type {DOMElement}
   * @see init
   */
  var activeNode;
  /** current active parent node element
   * @type {DOMElement}
   */
  var activeTopNode;
  /** the last node which has been activated
   * @type {DOMElement}
   */
  var oldNode;
  /** the current active list element with scrollable content
   * @type {DOMElement}
   */
  var currentScrollContent;
  /** the container holding the scroll controll elements
   * @type {DOMElement}
   */
  var scrollControll;
  /** the list element containing the module navigation list
   * @type {DOMElement}
   */
  var moduleNavigationList;
  /** HTML element to write the final code into
   * @type {DOMElement}
   */
  var outputObject;
  var scrollState = false;
  return (function(outputObjectId, startFolded){
    outputObject = document.getElementById(outputObjectId);
    this.folded = false;
    this.hidden = false;
    var self = this;
    /** initialize the current module navigation
     * @alias moduleNavigation.init
     */
    this.init = function(){
      scrollControll = document.getElementById('scrollControll');
      moduleNavigationList = document.getElementById('moduleNavigationList');
      _maxHeight = _normalHeight;
      if ( moduleNavigationList != undefined ) {
        // style attribute needs to be set for later use, cause it's not initialized
        // with the css class value by default
        _subtopicHeight = moduleNavigationList.offsetHeight + moduleNavigationList.offsetTop;
        if (startFolded) {
          addClassName(outputObject, 'closed');
          this.folded = true;
        }
        if ( modulenaviAnimation == false ) {
          this.configureFoldingAnimation( 1, 1000 );
        }
        if ( hiddenModuleNavigation ) {
          outputObject.style.height = 'auto';
          this.hideModuleNavigation( hiddenModuleNavigation )
        } else {
          outputObject.style.height = (startFolded) ? _minHeight + 'px' : _maxHeight + 'px';
        }
      } else {
        if ( hasClassName( outputObject, 'hidden' ) ) {
          this.hidden = true;
        }
      }
    };
    /** Switchs the module navigation to teaser mode and back to normal
     * @param {Boolean} setActive defines if the Teaser Mode should be activated or not
     */
    var setTeaserMode = function( setActive ){
      if( setActive ){
        _maxHeight = _teaserHeight;
        outputObject.style.height = _maxHeight;
        addClassName(outputObject, 'teaserMode');
      }else{
        _maxHeight = _normalHeight;
        outputObject.style.height = _maxHeight;
        removeClassName(outputObject, 'teaserMode');
      }
    };
    /** This method adds the hightlighting class to the link element passed by currentLink
     * @alias moduleNavigation.highlightCurrentLink
     * @param {DOMElement} currentLink is a reference to the link element to be highlighted
     */
    this.highlightCurrentLink = function(currentLink){
      addClassName(currentLink, 'moduleNaviHighlight');
      var currentNavParentID = currentLink.getAttribute('navParent');
      if (currentNavParentID != undefined) {
        var currentNavParent = document.getElementById(currentNavParentID);
        this.nodeAction(currentLink, false);
        this.highlightCurrentLink(currentNavParent);
      } else {
        var nextNode = getNextSibling(currentLink);
        if (nextNode !== null) {
          activeNode = currentLink.parentNode;
          initScrollControll(true);
        } else {
          initScrollControll(false);
        }
      }
    };
    /** Firefox walkaround to determin the next sibbling DOM node. Because Firefox defines formating whitespaces between tags as a textnode
     * @param {DOMElement} currentLink is a reference to an object to start with
     */
    var getNextSibling = function(currentLink){
      if( currentLink.nextSibling != undefined ){
        var nextNode = currentLink.nextSibling;
        while (nextNode.nodeType != 1) {
          if (nextNode.nextSibling != null) {
            nextNode = nextNode.nextSibling;
          } else {
            return null;
          }
        }
        return nextNode;
      } else {
        return null;
      }
    };
    /** initialize the folding animation of the menu
     * @param {Number} step is the value to alter the height of the navigation on each repeat
     */
    var foldNavigation = function(step){
      var si;
      var sj;
      var currentHeight = outputObject.offsetHeight;
      if ((!self.folded && currentHeight > _minHeight) || (self.folded && currentHeight < _maxHeight)) {
        if (currentHeight + step < _minHeight) {
          for(var i = 0 ; i < 100; i++)
          {
            if( i < 10 )
            {
                si = '0' + i                
            }
            else
            {                
                si = i
            }
            if( document.getElementById('ctl24_ctl' + si + '_link0') )
            {
              document.getElementById('ctl24_ctl' + si + '_link0').style.display = 'none';
              document.getElementById('ctl24_ctl' + si + '_li').style.display = 'none';
              for(var j = 0 ; j < 100; j++)
              {
                if( j < 10 )
                {
                    sj = '0' + j
                }
                else
                {
                    sj = j
                }
                if( document.getElementById('ctl24_ctl' + si + '_ctl' + sj + '_link0') )
                {
                    document.getElementById('ctl24_ctl' + si + '_ctl' + sj + '_li').style.display = 'none';
                    document.getElementById('ctl24_ctl' + si + '_ctl' + sj + '_link0').style.display = 'none';
                }
              }    
            }   
          }    
          outputObject.style.height = _minHeight + 'px';
        } else if (currentHeight + step > _maxHeight) {
          for(var i = 0 ; i < 100; i++)
          {
            if( i < 10 )
            {
                si = '0' + i                
            }
            else
            {                
                si = i
            }       
            if( document.getElementById('ctl24_ctl' + si + '_link0') )
            {
              document.getElementById('ctl24_ctl' + si + '_link0').style.display = 'block';
              document.getElementById('ctl24_ctl' + si + '_li').style.display = 'block';
              for(var j = 0 ; j < 100; j++)
              {  
                if( j < 10 )
                {
                    sj = '0' + j
                }
                else
                {
                    sj = j
                }                  
                if( document.getElementById('ctl24_ctl' + si + '_ctl' + sj + '_link0') )
                {             
                   document.getElementById('ctl24_ctl' + si + '_ctl' + sj + '_li').style.display = 'block';
                   document.getElementById('ctl24_ctl' + si + '_ctl' + sj + '_link0').style.display = 'block';
                }
              }    
            }      
          }    
          outputObject.style.height = _maxHeight + 'px';
        } else {
          outputObject.style.height = (currentHeight + step) + 'px';
          return true;
        }
      }
      stopFold();
    };
    /** This function initialize the folding animation of the module navigation
     * @alias moduleNavigation.startFold
     * @param {Boolean} forceAction is a boolean Value if the module navigation should be explizitly closed or opend
     */
    this.startFold = function( forceAction ){
      window.clearInterval(foldIntId);
      var step;
      if ( !this.folded && ( forceAction == undefined || forceAction === true ) ) {
        step = Math.round((_minHeight - outputObject.offsetHeight) / _foldingSteps);
      } else if( this.folded && ( forceAction == undefined || forceAction === false ) ) {
        step = Math.round((_maxHeight - outputObject.offsetHeight) / _foldingSteps);
      }
      if( step != null && step != 0 ){
        foldIntId = window.setInterval(function(){
          foldNavigation(step);
        }, _foldInterval);
      }
    };
    /** This function stops the folding animation of the module navigation */
    var stopFold = function(){
      window.clearTimeout(foldIntId);
      if (self.folded) {
        removeClassName(outputObject, 'closed');
      } else {
        addClassName(outputObject, 'closed');
      }
      self.folded = !self.folded;
    };
    /** with this method you're able to set the speed of the folding animation
     * @alias moduleNavigation.configureFoldingAnimation
     * @param {Number} foldingSteps defines how many steps the animation takes to finish
     * @param {Number} foldingIntervall defines the time to wait until the next step
     */
    this.configureFoldingAnimation = function(foldingSteps, foldingIntervall){
      _foldingSteps = foldingSteps;
      _foldInterval = foldingIntervall;
    };
    /** Shows and positions or hides the scollControll layer
     * @todo same like in nodeAction
     * @see BMW.ModuleNavigation.nodeAction
     * @param {Boolean} scrollState is a boolean value if the subtopic scoller will be displayed or hidden
     */
    var initScrollControll = function(scrollState){
      if (scrollState) {
        activeTopNode = activeNode;
        scrollControll.style.top = activeTopNode.offsetTop + 'px';
        scrollControll.style.height = activeTopNode.offsetHeight - 10 + 'px';
        currentScrollContent = activeTopNode.getElementsByTagName('ul')[0];
        showScrollIfNeeded();
      } else {
        activeTopNode = null;
        scrollControll.style.display = 'none';
      }
    };
    /** checks the height of the child elements of scrollable container and shows/hides the scoll controll if needed */
    var showScrollIfNeeded = function(){
      if ( activeTopNode != undefined ) {
        currentScrollContent.scrollTop = 0;
        var currentContent = activeTopNode.getElementsByTagName('ul')[0].firstChild;
        var currentHeight = 0;
        while( currentContent != null ){
          currentHeight += ( currentContent.offsetHeight != undefined )? currentContent.offsetHeight + 5:0;
          currentContent = currentContent.nextSibling;
        }
        if ( currentHeight > activeTopNode.offsetHeight ) {
          setTimeout( function(){ scrollControll.style.display = 'block'; scrollState = true; }, 50 );
        }else{
          setTimeout( function(){ scrollControll.style.display = 'none'; scrollState = false; }, 50 );
        }
      }
    };
    /** scrolls the content of the currentScrollContent
     * @param {Number} step defines a value to alter the scroll position of the element
     */
    var scrollContent = function(step){
      if ((step > 0 && activeTopNode.scrollTop < currentScrollContent.offsetHeight ) || (step < 0 && currentScrollContent.scrollTop > 0)) {
        currentScrollContent.scrollTop += step;
      } else {
        self.stopScroll();
      }
    };
    /** Sets an interval to scroll the current subtopic content up or down
     * @alias moduleNavigation.startScroll
     * @param {Number} scrollDirection is a positive or negative value to define the direction of the animation
     */
    this.startScroll = function(scrollDirection){
      this.stopScroll();
      var step = (activeTopNode.offsetHeight / _scrollSteps) * scrollDirection;
      scrollIntId = window.setInterval(function(){ scrollContent(step); }, _scrollInterval);
    };
    /** Stops the current scroll animation interval
     * @alias moduleNavigation.stopScroll
     */
    this.stopScroll = function(){
      window.clearInterval(scrollIntId);
    };
    /** with this method you're able to set the speed of the scroll animation
     * @alias moduleNavigation.configureScrollAnimation
     * @param {Number} scrollSteps defines how many steps the animation takes to finish
     * @param {Number} scrollIntervall defines the time to wait until the next step
     */
    this.configureScrollAnimation = function(scrollSteps, scrollIntervall){
      _scrollSteps = scrollSteps;
      _scrollInterval = scrollIntervall;
    };
    /** Switches the module navigation to hidden or back to normal
     * @param {Boolean} hideNavi defines if the module navigation should be hidden or not
     */
    this.hideModuleNavigation = function( hideNavi ){
      if( hideNavi ){
        addClassName( outputObject, 'hidden' );
        this.hidden = true;
      }else{
        removeClassName( outputObject, 'hidden' );
        this.hidden = false;
      }
    };
    /** loops through the DOM upwards to find all parent nodes in the list
     * @param {DOMElement} childObject is the object to start with
     * @param {Boolean} setActive defines if the parent element should be set to active or not
     * @return {DOMElement} returns the last parent element that has been found
     */
    var switchParentsActive = function( childObject, setActive ){
      var tmpNodeObj = childObject;
      while (tmpNodeObj.parentNode.id != 'moduleNavigationList') {
        tmpNodeObj = tmpNodeObj.parentNode.parentNode;
        if (setActive) {
          addClassName(tmpNodeObj, 'active');
        } else {
          removeClassName(tmpNodeObj, 'active');
        }
      }
      return tmpNodeObj;
    };
    /** This function will be called by the topic links in the navigation to handle opening and closing of subtopics
     * @alias moduleNavigation.nodeAction
     * @todo find a better way to determin the current parent node element
     * @param {DOMElement} nodeObject is a object reference to the clicked link element
     * @param {Boolean} scrollState is a boolean value if the subtopic scoller should be initialized
     * @return {Boolean} returns false to stop the link to fire
     */
    this.nodeAction = function(nodeObject, scrollState){
      var hasBeenActive = hasClassName( nodeObject.parentNode, 'active' );
      if (typeof oldNode == 'object' && oldNode !== null) {
        var oldNodeObj = switchParentsActive( oldNode, false );
        oldNodeObj.style.height = "auto";
        removeClassName(oldNode, 'active');
      }
      activeNode = nodeObject.parentNode;
      // if scrollState is true, this means it's a 1st level element.
      // and if oldNode and activeNode pointing on the same object,
      // close the current opened element and remove the scrollControll
      if (oldNode == activeNode && scrollState) {
        setTeaserMode( true );
        if (scrollState) {
          initScrollControll(false);
        }
        oldNode = null;
      } else if(oldNode != activeNode && scrollState && hasBeenActive ){
        setTeaserMode( false );
        switchParentsActive( oldNode, false );
        if (scrollState) {
          initScrollControll(false);
        }
        oldNode = null;
      } else if(oldNode == activeNode && !scrollState && hasBeenActive ) {
        setTeaserMode( false );
        var newNodeObj = switchParentsActive( activeNode, true );
        removeClassName( oldNode, 'active');
        oldNode = newNodeObj;
        if (scrollState) {
          initScrollControll(false);
        }else{
          showScrollIfNeeded();
        }
        if( currentScrollContent != undefined ){
          currentScrollContent.style.position = 'static';
          currentScrollContent.style.position = 'relative';
        }
      } else {
        setTeaserMode( false );
        var newNodeObj = switchParentsActive( activeNode, true );
        //var newHeight = (_maxHeight - _subtopicHeight);
        var newHeight = 202;
        //alert(newHeight);
        newNodeObj.style.height = newHeight + "px";
        newNodeObj.getElementsByTagName('ul')[0].style.height = newHeight - 27 + 'px';
        addClassName(activeNode, 'active');
        if (scrollState) {
          initScrollControll(true);
        }else{
          showScrollIfNeeded();
        }
        oldNode = activeNode;
      }
      return false;
    };
  });
})();

  
  var columns = 5;
  checkClient();
  teaser = false;

  function onLoadFunctions(){
    var moduleNavigation = false;
    moduleNavigation=true;
    highlightNavigations(true, moduleNavigation);
  }

  
  

