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++) {
    // SVN: Fenstergröße anpassen
    if (screen.width < 1114 || screen.height < 854) {
        var svnLinks = [
            'http://cc.porscheinformatik.com/uj_skoda_azonnal_elviheto',
            'http://cc.porscheinformatik.com/masini-noi-skoda',
            'http://cc.porscheinformatik.com/nova-vozila-skoda',
            'http://cc.skoda.at/neuwagen',
            'http://cc.skoda.at/nwapp/nws'
        ];
        for(var j=0; j<svnLinks.length; ++j) {
            var svnLink = svnLinks[j];
            if (links[i].href.substr(0, svnLink.length) == svnLink) {
                links[i].onclick = function() {
                    window.open(this.href, 'popup' + i, 'width=1014,height=700,scrollbars=1,resizable=0');
                    return false;
                }
            }
        }
    }
    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, hasInnen)
{
    currentStoff = stoff;
    if (stoffcode) {
    	document.getElementById('stoff').style.display = 'block';
    	document.getElementById('stoffPic').src = colorStoffBaseUrl + '/' + stoffcode + '/big.jpg';
    } else {
    	document.getElementById('stoff').style.display = 'none';
    }
	changeInnen(hasInnen);
}

function changeInnen(hasInnen)
{
	if (hasInnen && 
	    (
	    	(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, hasInnen)
{
    document.getElementById(stoff).checked = 'checked';
    changeStoff(stoff, stoffcode, hasInnen);
    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, width, height)
{
	var options = document.getElementById('cc_modell').options;
    if (options[options.selectedIndex].value != '') {
		link += '&' + options[options.selectedIndex].value;
	}
    window.open(link, 'ccPopup', 'width=' + width + ',height=' + height + ',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 (modell.selectedIndex > 0) {
        link += '-' + modell[modell.selectedIndex].text.replace(' ', '-');
        if (auv.selectedIndex > 0) {
            link += '/' + auv[auv.selectedIndex].text.replace(' ', '-');
        }
    }
	/*
    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 homeStageTimeout = null;
var overrideNextHomeStageImage = null;
var curHomeStageImage = 0;

function setSwitchHomeStageImage(idx)
{
    if (idx == curHomeStageImage) return;

    overrideNextHomeStageImage = idx;
    clearTimeout(homeStageTimeout);
    homeStageTimeout = null;
    homeStageChangeImage();
}

function homeStageImageOver(idx) {
    document.getElementById('switchPicsPreview'+idx).style.display = 'block';
}

function homeStageImageOut(idx) {
    document.getElementById('switchPicsPreview'+idx).style.display = 'none';
}

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]);
    document.getElementById('switchPics'+curHomeStageImage).className = '';

    if (overrideNextHomeStageImage !== null) {
        curHomeStageImage = overrideNextHomeStageImage;
        overrideNextHomeStageImage = null;
    } else {
        curHomeStageImage++;
    }
    if(curHomeStageImage+1 > homeStages.length) curHomeStageImage = 0;
    var newEl = document.getElementById(homeStages[curHomeStageImage]);
    document.getElementById('switchPics'+curHomeStageImage).className = 'active';

    newEl.style.display = 'none';
    newEl.style.zIndex = 11;
    oldEl.style.zIndex = 10;
    new Effect.Appear(newEl, {duration:2});
    homeStageTimeout = setTimeout("homeStageChangeImage()", 10000);
}

/***** HOME TEASERS *****/

// var homeStages = [];
var homeStageMoveInProgress = false;
var activePicture = 0;

var homeTeasersMoveAmount = 270;
var homeTeasersMoveAmountOverhead = 80;

function _homeTeasersInit()
{
    for (var x = 0; x < homeTeasers.length; x++) {
        var el = document.getElementById(homeTeasers[x]);
        if (x >= 2) {
            el.style.display = 'none';
        } else {
            el.style.left = (x * homeTeasersMoveAmount) + 'px';
        }
    }
}

function homeTeasersMove(direction, autoMoveAfter)
{
    if (homeStageMoveInProgress) return false;

    homeStageMoveInProgress = true;

    var currentLeftEl = document.getElementById(homeTeasers[activePicture]);

    // setup data for right picture
    var rightIndex = activePicture + 1;
    if (!homeTeasers[rightIndex]) rightIndex = 0;
    var currentRightEl = document.getElementById(homeTeasers[rightIndex]);

    // move new picture to right position
    var newIndex = rightIndex;
    if (direction == 'forward') {
        newIndex += 1;
        if (!homeTeasers[newIndex]) newIndex = 0;
    } else if (direction == 'backward') {
        newIndex = activePicture - 1;
        if (!homeTeasers[newIndex]) newIndex = homeTeasers.length - 1;
    }
    var newEl = document.getElementById(homeTeasers[newIndex]);
    if (direction == 'forward') {
        var styleLeft = parseInt(currentRightEl.style.left)+homeTeasersMoveAmount+homeTeasersMoveAmountOverhead;
    } else if (direction == 'backward') {
        var styleLeft = parseInt(currentLeftEl.style.left)-(homeTeasersMoveAmount+homeTeasersMoveAmountOverhead);
    }
    if (direction == 'backward') styleLeft = styleLeft;
    newEl.style.left = styleLeft+'px';
    newEl.style.display = '';

    // move left picture
    if (direction == 'forward') {
        var x = homeTeasersMoveAmount + homeTeasersMoveAmountOverhead;
        x = x * (-1);
    } else if (direction == 'backward') {
        var x = homeTeasersMoveAmount;
    }
    new Effect.Move(currentLeftEl, { x: x });

    // move right picture
    var x = homeTeasersMoveAmount;
    if (direction == 'backward') {
        x += homeTeasersMoveAmountOverhead;
    }
    if (direction == 'forward') x = x * (-1);
    new Effect.Move(currentRightEl, { x: x });

    // move new picture
    var x = homeTeasersMoveAmount + homeTeasersMoveAmountOverhead;
    if (direction == 'forward') x = x * (-1);
    new Effect.Move(newEl, { x: x });

    if (direction == 'forward') {
        activePicture = rightIndex;
    } else if (direction == 'backward') {
        activePicture = newIndex;
    }
    window.setTimeout('homeStageMoveInProgress = false', 1010);
    if (autoMoveAfter) {
        window.setTimeout('homeTeasersMove("'+direction+'", '+autoMoveAfter+')', autoMoveAfter);
    }
    return false;
}

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




function evalHandler(response) {
    try {
        eval(response);
    } catch(e) {
        alert('Ein Fehler beim Updaten ist aufgetrenten: '+e.toString());
    }
}

function _postback(url, data, handler)
{
  var req = loadXMLHttpRequest();
  if(!req) return(false);

  var content = '';
  for(var i in data)
  {
    content += (content.length ? '&' : '') + i + '=' + encodeURIComponent(data[i]);
  }

  function callBack()
  {
    if(req.readyState == 4)
    {
      if(req.status == 200)
      {
        if(typeof handler == 'function')
        handler(req.responseText, req);
      }
      else
      {
        alert('An error has occurred: ' + req.statusText);
      }
    }
  }

  req.onreadystatechange = callBack;

  req.open('POST', url, true);
  req.setRequestHeader
  (
      'Content-Type',
      'application/x-www-form-urlencoded; charset=UTF-8'
  );
  //alert(content);
  req.send(content);
}

function loadXMLHttpRequest() {
    var pageRequest = false;
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            pageRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                pageRequest = false;
            }
        }
    @end
    @*/

    return pageRequest;
}

