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

var navRoot = document.getElementById("mainMenu");
var activeLi = false;
var updateMenuHover = function() {
    for (var i=0; i<navRoot.childNodes.length; i++) {
        node = navRoot.childNodes[i];
        if (node != activeLi && node.nodeName=="LI") {
            node.className=node.className.replace(/hover/g, "");
        }
    }
    if (activeLi) {
        activeLi.className+=" hover";
    }
};
var toActivateLi = false;
var showTimeout = false;
var hideTimeout = false;
if (document.getElementById && navRoot) {
    for (var i=0; i<navRoot.childNodes.length; i++) {
        node = navRoot.childNodes[i];
        if (node.nodeName=="LI") {
            node.onmouseover=function() {
                if (hideTimeout) clearTimeout(hideTimeout);
                if (showTimeout) clearTimeout(showTimeout);
                activeLi = this;
                showTimeout = setTimeout(function() {
                    updateMenuHover();
                }, 500);
            }
            if (hideTimeout) clearTimeout(hideTimeout);
            node.onmouseout=function() {
                var li = this;
                hideTimeout = setTimeout(function() {
                    activeLi = false;
                    updateMenuHover();
                }, 1000);
            }
        }
    }
}
var links = document.getElementsByTagName('a');
for(var i=0;i<links.length;i++) {
    if(links[i].rel.indexOf("extern") >= 0 || links[i].rel.indexOf("download") >= 0) {
        links[i].onclick = function() {
            window.open(this.href);
            return false;
        }
    }
}

activateLargeStyle(false);
if(readCookie("style")=='large') {
    activateLargeStyle(true);
}

if (document.getElementById('switchHtmlFlashFlash')) {
    if(readCookie("htmlFlash")=='html') {
        document.getElementById('switchHtmlFlashFlash').style.display = 'none';
        document.getElementById('switchHtmlFlashHtml').style.display = 'block';
    } else {
        document.getElementById('switchHtmlFlashFlash').style.display = 'block';
        document.getElementById('switchHtmlFlashHtml').style.display = 'none';
    }
}

};


currentColor = '';
function changeColor(color, title)
{
    currentColor = color;

    // Kleines Bild tauschen (mittleres Bild wird im onclick getauscht
    var baseUrl = colorBaseUrl + '/' + currentColor;
    document.getElementById('farbePreviewPic').src = baseUrl + '/frontXS.jpg';
    document.getElementById('farbePic').src = baseUrl + '/frontM.jpg';
    document.getElementById('farbeLink').href = baseUrl + '/frontL.jpg';
    document.getElementById('farbeLink').title = title;

    // Nur die passenden Stoffe einblenden
    for (i = 0; i < colorStoffe.length; i++) {
        var element = document.getElementById('stoff_' + colorStoffe[i]);
        if (colorMappings[currentColor] && colorMappings[currentColor][colorStoffe[i]]) {
            element.style.display = '';
        } else {
            if (colorStoffe[i] == currentStoff) {
			    document.getElementById('stoff').style.display = 'none';
                document.getElementById('stoffPic').src = '';
            }
            element.style.display = 'none';
        }
    }
   
    document.getElementById(color).checked = 'checked';
    
    // Stoffbilder
    changeInnen();
}

currentStoff = '';
function changeStoff(stoff, stoffcode)
{
    currentStoff = stoff;
    document.getElementById('stoff').style.display = 'block';
    document.getElementById('stoffPic').src = colorStoffBaseUrl + '/' + stoffcode + '/big.jpg';
	changeInnen();
}

function changeInnen()
{
	if ((currentColor != '' && currentStoff != '' && colorMappings[currentColor] && colorMappings[currentColor][currentStoff]) ||
		!currentColor && currentStoff
    ) {
		var baseUrl = colorBaseUrl;// + '/' + currentColor + '/' + currentStoff;
        document.getElementById('innenPreviewPic').src = baseUrl + '/innenXS.jpg?F=' + currentColor + '&P=' + currentStoff;
        document.getElementById('innenPic').src = baseUrl + '/innenM.jpg?F=' + currentColor + '&P=' + currentStoff;
        document.getElementById('innenLink').href = baseUrl + '/innenL.jpg?F=' + currentColor + '&P=' + currentStoff;
        document.getElementById('innenLink').title = colorStofftext[currentStoff];
        document.getElementById('innenPreview').style.display = 'block';
		if (document.getElementById('innen').style.display == 'none' &&
			document.getElementById('color').style.display == 'none'
		) document.getElementById('innen').style.display = 'block';
	} else {
        document.getElementById('innen').style.display = 'none';
        document.getElementById('innenPreview').style.display = 'none';
	}
}

function checkColor(color, title)
{
    document.getElementById(color).checked = 'checked';
    changeColor(color, title);
    return false;
}

function checkStoff(stoff, stoffcode)
{
    document.getElementById(stoff).checked = 'checked';
    changeStoff(stoff, stoffcode);
    return false;
}

// ************ Farben no CC **********************

function changeColorNoCc(bild, popup, text)
{
    document.getElementById('farbePic').src = bild;
    document.getElementById('farbeLink').href = popup;
    document.getElementById('farbeLink').title = text;
    return false;
}

function changeStoffNoCc(bild)
{
    document.getElementById('stoffPic').src = bild;
    return false;
}

// ************ Farben no CC Ende ***************

function openPf(link)
{
	var options = document.getElementById('probefahrt').options;
    if (options[options.selectedIndex].value != '') {
		link += options[options.selectedIndex].value;
	}
    window.open(link, 'probefahrtPopup', 'width=788,height=535,scrollbars=0,resizable=0');
	return false;
}

function openCc(link)
{
	var options = document.getElementById('cc_modell').options;
    if (options[options.selectedIndex].value != '') {
		link += options[options.selectedIndex].value;
	}
    window.open(link, 'ccPopup', 'width=1014,height=700,scrollbars=no,resizable=yes');
	return false;
}

function openPoi(link, type, params)
{
	var modell = document.getElementById(type + '_modell').options;
	var auv = document.getElementById(type + '_auv').options;
	var treibstoff = document.getElementById(type + '_treibstoff').options;
    if (auv.selectedIndex > 0) {
		link += '&' + auv[auv.selectedIndex].value;
	} else if (modell.selectedIndex > 0) {
		link += '&' + modell[modell.selectedIndex].value;
	}
    if (treibstoff.selectedIndex) {
		link += "&auswahl.motor=" + treibstoff[treibstoff.selectedIndex].value;
	}
	window.open(link, type + 'Popup', params);
	return false;
}

function poiChange(source, type)
{
	var modell = document.getElementById(type + '_modell').options;
	var auv = document.getElementById(type + '_auv').options;
	var tr = document.getElementById(type + '_treibstoff').options;
	
	var selectedModell = modell[modell.selectedIndex].value;
	var selectedAuv = auv[auv.selectedIndex].value;
	
	var m = poiMappings[type][selectedModell];
	while(tr.length > 1) tr[tr.length - 1] = null;
	if (source == document.getElementById(type + '_modell')) {
		while(auv.length > 1) auv[auv.length - 1] = null;
		for (var i in m) {
			auv[auv.length] = new Option(m[i].Auv, m[i].AuvCode);
		}
	} else if (source == document.getElementById(type + '_auv')) {
		var treibstoffe = m[selectedAuv]['Treibstoffe'];
		for (var i = 0; i<treibstoffe.length; i++) {
			tr[tr.length] = new Option(treibstoffe[i], treibstoffe[i]);
		}
	}
}

//tab-boxen
var switchTab = function(id, el){
    var siblings = el.parentNode.parentNode.parentNode.childNodes;
    var activeTab;
    if (siblings) {
        for (var i = 0; i < siblings.length; i++) {
            if (siblings[i].tagName == 'DIV' && siblings[i].className.match('tabContent')
                && siblings[i].className.match('active')
            ) {
                activeTab = siblings[i];
                break;
            }
        }
    }
    if (activeTab) {
        activeTab.className = activeTab.className.replace('active', '');
    }
    document.getElementById(id).className += ' active';
    
    var siblings = el.parentNode.parentNode.childNodes;
    var activeTab;
    if (siblings) {
        for (var i = 0; i < siblings.length; i++) {
            if (siblings[i].tagName == 'LI' && siblings[i].className.match('tab')
                && siblings[i].className.match('active')
            ) {
                activeTab = siblings[i];
                break;
            }
        }
    }
    if (activeTab) {
        activeTab.className = activeTab.className.replace('active', '');
    }
    el.parentNode.className += ' active';

    return false;
}

function toggleZubehoerDescription($id)
{
	var desc = document.getElementById('descriptionText[' + $id + ']');
	var linkOpen = document.getElementById('descriptionLinkOpen[' + $id + ']');
	var linkClose = document.getElementById('descriptionLinkClose[' + $id + ']');
	if (desc.style.display == '') {
		desc.style.display = 'none';
		linkOpen.style.display = '';
		linkClose.style.display = 'none';
	} else {
		desc.style.display = '';
		linkOpen.style.display = 'none';
		linkClose.style.display = '';
	}
	return false;
}


var hovered = new Array();
function hoverPic(a, src, setClassName, restoreOthers) {
    if(typeof setClassName!='undefined' && setClassName && !a.className.match(/ hover$/)) {
        a.className+=" hover";
    }
    var foundImg = false;
    for(var i=0;i<a.childNodes.length;i++) {
        if(a.childNodes[i].nodeName.toLowerCase()=="img") {
            if(!a.childNodes[i].oldSrc) a.childNodes[i].oldSrc = a.childNodes[i].src;
            a.childNodes[i].src = src;
            foundImg = true;
            break;
        }
    }
    if(!foundImg) {
        for(var i=0;i<a.childNodes.length;i++) hoverPic(a.childNodes[i], src, false, false);
    }

    if(typeof restoreOthers!='undefined' && restoreOthers) {
        var obj;
        while(obj = hovered.pop()) {
            restoreHoverPic(a, setClassName);
        }
        hovered.push(a);
    }
}

function restoreHoverPics() {
    var obj;
    return;
    while(obj = hoverImgs.pop()) {
        obj.src = obj.oldSrc;
    }
    while(obj = hoverTags.pop()) {
        obj.className = obj.className.replace(" hover", "");
    }
}
function restoreHoverPic(a, setClassName) {
    if(typeof setClassName!='undefined' && setClassName) {
        a.className = a.className.replace(" hover", "");
    }
    for(var i=0;i<a.childNodes.length;i++) {
        if(a.childNodes[i].nodeName.toLowerCase()=="img") {
            a.childNodes[i].src = a.childNodes[i].oldSrc;
            return;
        }
    }
    for(var i=0;i<a.childNodes.length;i++) restoreHoverPic(a.childNodes[i], false);
}


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;
}

function activateLargeStyle(activate) {
    if(document.getElementById) {
        if (typeof activate == 'undefined') {
            activate = document.getElementById('largeStyleSheet').disabled;
        }
        document.getElementById('largeStyleSheet').disabled = !activate;
    }
}

window.onunload = function(e) {
    var style = ''
    if(!document.getElementById('largeStyleSheet').disabled) {
        style = 'large';
    }
    if(readCookie("style")!=style){
		createCookie("style", style, 365);
	}
}


function switchHtmlFlash() {
    if(readCookie("htmlFlash")=='flash') {
        createCookie("htmlFlash", 'html', 365);
    } else {
        createCookie("htmlFlash", 'flash', 365);
    }
    location.href = location.href;
}

var curHomeStageImage = 0;
function homeStageChangeImage()
{
	for (var x = 0; x < homeStages.length; x++) {
		var s = document.getElementById(homeStages[x]);
		s.style.zIndex = '';
		s.style.display = '';
	}
    var oldEl = document.getElementById(homeStages[curHomeStageImage]);
    curHomeStageImage++;	 
    if(curHomeStageImage+1 > homeStages.length) curHomeStageImage = 0;
    var newEl = document.getElementById(homeStages[curHomeStageImage]);
    
    newEl.style.display = 'none';
    newEl.style.zIndex = 11;
    oldEl.style.zIndex = 10;
    new Effect.Appear(newEl, {duration:8});
    setTimeout("homeStageChangeImage()", 10000);
}

/* Template Modelle/Ausstattungsvergleich */
function avglCheckMax(checkbox, numberOfVariants){
    var obj;
    var max = 4;
    var count = 0;

    if(checkbox.checked==true){
        for (var x=0;x<numberOfVariants;x++){
            obj = document.getElementById('cat'+x);
            if (obj.checked==true){
                count++;
            }
        }
        if (count>max){
            checkbox.checked = false;
        }
    }
}
