/*

Parts Based on Dynamic Heading Generator By Stewart Rosenberger www.stewartspeak.com



Parts based on code by Dean Edwards/Matthias Miller/John Resig dean.edwards.name



Parts Based on Lightbox by Lokesh Dhakar www.huddletogether.com

Creative Commons Attribution 2.5 License



*/



function OdaberiMotor(item,url)

{

itemId = item.name;

id123 = item.options[item.selectedIndex].value;

document.location.href = url +'&id='+id123;

}





var imageLoaded = false;

var initDone = false;



document.write('<link id="hide-flicker" rel="stylesheet" media="screen" href=""http://www.volkswagen.com.hr/css/replacement-screen.css" />');

window.flickerCheck = function()

{

    if(!imageLoaded)

        setStyleSheetState('hide-flicker',false);

};

setTimeout('window.flickerCheck();', 1500)



var replaceItems;

replaceSelector("div#mainMenu li.notActive a.level1","http://www.volkswagen.ba/cms/dtr/heading.php",true);

replaceSelector("div#mainMenu li.active a.level1","http://www.volkswagen.ba/cms/dtr/heading.php",true);

replaceSelector("div#topHeadline h1","http://www.volkswagen.ba/cms/dtr/heading.php",false);

replaceSelector("div#topHeadlineHaendler h1","http://images.volkswagen.at/heading.php",false);

replaceSelector("div#topHeadlineHaendler2 h1","http://images.volkswagen.at/heading.php",false);

replaceSelector("div#homeSubMenue a.level1","http://images.volkswagen.at/heading.php",true);

replaceSelector("div.homeTeaser h1 a","http://images.volkswagen.at/heading.php",true);

replaceSelector("div.homeTextTeaser h1","http://images.volkswagen.at/heading.php",false);

replaceSelector("div.homeTextTeaserHaendler h1","http://images.volkswagen.at/heading.php",false);

replaceSelector("div#subMenueTop h2 a.active","http://images.volkswagen.at/heading.php",false);

replaceSelector("div#subMenueTop h2 a.inactive","http://images.volkswagen.at/heading.php",true);

replaceSelector("div#subMenueTop li.notActive a.level1","http://www.volkswagen.ba/cms/dtr/heading.php",true);

replaceSelector("div#subMenueTop li.active a.level1","http://images.volkswagen.at/heading.php",true);

replaceSelector("div.modellGruppeStage a span.button","http://images.volkswagen.at/heading.php",true);

replaceSelector("h1.vwhead","http://www.volkswagen.ba/cms/dtr/heading.php",false);

replaceSelector("button","http://images.volkswagen.at/heading.php",false);



var test = new Image();

test.onload = function() {

    imageLoaded = true;

    if(initDone) replacement();

}

test.src = "http://volkswagen.com.hr/files/test.png?date=" + (new Date()).getTime();



function init()

{

    // quit if this function has already been called

    if (initDone) return;



    // flag this function so we don't do the same thing twice

    initDone = true;



    // kill the timer

    if (_timer) clearInterval(_timer);



    if(imageLoaded) {

        replacement();

    }



    addHoverClass(getElementsBySelector("div#subMenueTop li.level1"));

    addHoverClass(getElementsBySelector("div#mainMenu li"));

    addHoverClass(getElementsBySelector("div.modellGruppeStage a"));



    //download-links und externe links in neuem fenster öffnen

    //links auf fremde domains per ajax zählen

    var links = document.getElementsByTagName('a');

    for(var i=0;i<links.length;i++) {

        if(links[i].href.substr(location.protocol.length+2, location.hostname.length)!=location.hostname

        && links[i].href.substr(0, 11)!='javascript:') {

            links[i].oldOnClick = (links[i].onclick) ? links[i].onclick : new Function;

            links[i].onclick = function() {

                if(this.rel.indexOf("extern") >= 0 || this.rel.indexOf("download") >= 0) {

                    window.open(this.href);

                    return false;

                } else {

                    return this.oldOnClick();

                }

            }

        } else if (links[i].rel.indexOf("extern") >= 0 || links[i].rel.indexOf("download") >= 0) {

            links[i].oldOnClick = (links[i].onclick) ? links[i].onclick : new Function;

            links[i].onclick = function() {

                window.open(this.href);

                this.oldOnClick();

                return false;

            }

        }

    }





};



/* for Mozilla/Opera9 */

if (document.addEventListener) {

        document.addEventListener("DOMContentLoaded", init, false);

}



/* for Internet Explorer */

/*@cc_on @*/

/*@if (@_win32)

        document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");

        var script = document.getElementById("__ie_onload");

        script.onreadystatechange = function() {

                if (this.readyState == "complete") {

                        init(); // call the onload handler

                }

        };

/*@end @*/



/* for Safari */

if (/KHTML|WebKit/i.test(navigator.userAgent)) { // sniff

        var _timer = setInterval(function() {

                if (/loaded|complete/.test(document.readyState)) {

                        init(); // call the onload handler

                }

        }, 10);

}



/* for other browsers */

window.onload = init;









function openSubMenue(a) {

    var childs = a.parentNode.parentNode.childNodes;

    for(var i=0;i<childs.length;i++) {

        childs[i].className = childs[i].className.replace(" open", "");

        childs[i].className = childs[i].className.replace("open", "");

    }

    a.parentNode.className += " open";

}



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 addHoverClass(elements) {

    for(var i=0;i<elements.length;i++) {

        elements[i].onmouseover = function() {

            if(document.all && !this.className.match(/ hover$/)) this.className+=" hover";

            /*

            if(!this.className.match(/ hover$/)) this.className+=" hover";

            this.hideIt = false;

            */

            recursiveOnMouseOver(this);

        }

        elements[i].onmouseout = function() {

            if(document.all) this.className = this.className.replace(" hover", "");

            /*

            //nach timeout ausblenden; auskommentiert weils ned benötigt wird aber später vielleicht doch

            var a = this;

            a.hideIt = true;

            setTimeout(function() {

                if(a.hideIt) {

                    a.className = a.className.replace(" hover", "");

                }

            }, 400);

            */

            recursiveOnMouseOut(this);

        }

    }

}

function recursiveOnMouseOver(element) {

    for(var i=0;i<element.childNodes.length;i++) {

        if (element.childNodes[i].onmouseover) element.childNodes[i].onmouseover();

        recursiveOnMouseOver(element.childNodes[i]);

    }

}

function recursiveOnMouseOut(element) {

    for(var i=0;i<element.childNodes.length;i++) {

        if (element.childNodes[i].onmouseout) element.childNodes[i].onmouseout();

        recursiveOnMouseOut(element.childNodes[i]);

    }

}





function replaceSelector(selector,url,hover)

{

    if(typeof replaceItems == "undefined")

        replaceItems = new Array();



    replaceItems[replaceItems.length] = {selector: selector, url: url, hover: hover};

}





function replacement()

{

    for(var i=0;i<replaceItems.length;i++)

    {

        var elements = getElementsBySelector(replaceItems[i].selector);

        if(elements.length > 0) for(var j=0;j<elements.length;j++)

        {

            if(!elements[j]) continue;



            var element = elements[j];



            var text = extractText(elements[j]);

            text = text.replace(/^\s*|\s*$/g,""); //trim



            while(elements[j].hasChildNodes())

                elements[j].removeChild(elements[j].firstChild);



            var url = replaceItems[i].url + "?text="+encodeURIComponent(text)+"&selector="+escape(replaceItems[i].selector);



            var image = document.createElement("img");

            image.className = "replacement";

            image.alt = text.replace("[v]","").replace("[>]","").replace("<br/>", " ").replace("<br />", " "); //die pfeile+umbrüche aus dem alttext rausnehmen

            if(elements[j].tagName.toLowerCase()=='button') {

                //wenn parent ein button ist die breite händisch setzen

                //weil der ie macht ihn um ein paar pixel zu breit

                //+1 weil er sonst beim tollen onmousedown-effekt vom ie abgeschnitten wird

                image.onload = function() {

                    if(this.width > 0) {

                        this.parentNode.style.width = (this.width+1)+'px';

                    }

                }

            }

            elements[j].appendChild(image); //bevor src gesetzt wird!

            image.src = url;



            if(replaceItems[i].hover) {

                elements[j].hoverSrc = url+"&hover";

                var preload = new Image();

                preload.src = elements[j].hoverSrc;

                elements[j].originalSrc = url;

                elements[j].onmouseover = function() {

                    if(this.firstChild.src!=this.hoverSrc)

                        this.firstChild.src = this.hoverSrc;

                }

                elements[j].onmouseout = function() {

                    if(this.firstChild.src!=this.originalSrc)

                        this.firstChild.src = this.originalSrc;

                }

            }

        }

    }



    setStyleSheetState('hide-flicker',false);

}



function setStyleSheetState(id,enabled)

{

    var sheet = document.getElementById(id);

    if(sheet)

        sheet.disabled = (!enabled);

}



function extractText(element)

{

    if(typeof element == "string")

        return element;

    else if(typeof element == "undefined")

        return element;

    //else if(element.innerText) doesn't return brs

    //    return element.innerText;



    var text = "";

    var kids = element.childNodes;

    for(var i=0;i<kids.length;i++)

    {

        if (kids[i].nodeName.toLowerCase()=='br') {

            text += '<br />';

        }

        if(kids[i].nodeType == 1) {

            text += extractText(kids[i]);

        } else if(kids[i].nodeType == 3) {

            text += kids[i].nodeValue;

        }

    }

    return text;

}



/*

    Finds elements on page that match a given CSS selector rule. Some

    complicated rules are not compatible.

    Based on Simon Willison's excellent "getElementsBySelector" function.

    Original code (with comments and description):

        http://simon.incutio.com/archive/2003/03/25/getElementsBySelector

*/

function getElementsBySelector(selector)

{

    var tokens = selector.split(' ');

    var currentContext = new Array(document);

    for(var i=0;i<tokens.length;i++)

    {

        token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');

        if(token.indexOf('#') > -1)

        {

            var bits = token.split('#');

            var tagName = bits[0];

            var id = bits[1];

            var element = document.getElementById(id);

            if(!element || (tagName && element.nodeName.toLowerCase() != tagName))

                return new Array();

            currentContext = new Array(element);

            continue;

        }



        if(token.indexOf('.') > -1)

        {

            var bits = token.split('.');

            var tagName = bits[0];

            var className = bits[1];

            if(!tagName)

                tagName = '*';



            var found = new Array;

            var foundCount = 0;

            for(var h=0;h<currentContext.length;h++)

            {

                var elements;

                if(tagName == '*')

                    elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');

                else

                    elements = currentContext[h].getElementsByTagName(tagName);



                for(var j=0;j<elements.length;j++)

                    found[foundCount++] = elements[j];

            }



            currentContext = new Array;

            var currentContextIndex = 0;

            for(var k=0;k<found.length;k++)

            {

                if(found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b')))

                    currentContext[currentContextIndex++] = found[k];

            }



            continue;

        }



        if(token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/))

        {

            var tagName = RegExp.$1;

            var attrName = RegExp.$2;

            var attrOperator = RegExp.$3;

            var attrValue = RegExp.$4;

            if(!tagName)

                tagName = '*';



            var found = new Array;

            var foundCount = 0;

            for(var h=0;h<currentContext.length;h++)

            {

                var elements;

                if(tagName == '*')

                    elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');

                else

                    elements = currentContext[h].getElementsByTagName(tagName);



                for(var j=0;j<elements.length;j++)

                    found[foundCount++] = elements[j];

            }



            currentContext = new Array;

            var currentContextIndex = 0;

            var checkFunction;

            switch(attrOperator)

            {

                case '=':

                    checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };

                    break;

                case '~':

                    checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };

                    break;

                case '|':

                    checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };

                    break;

                case '^':

                    checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };

                    break;

                case '$':

                    checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };

                    break;

                case '*':

                    checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };

                    break;

                default :

                    checkFunction = function(e) { return e.getAttribute(attrName); };

            }



            currentContext = new Array;

            var currentContextIndex = 0;

            for(var k=0;k<found.length;k++)

            {

                if(checkFunction(found[k]))

                    currentContext[currentContextIndex++] = found[k];

            }



            continue;

        }



        tagName = token;

        var found = new Array;

        var foundCount = 0;

        for(var h=0;h<currentContext.length;h++)

        {

            var elements = currentContext[h].getElementsByTagName(tagName);

            for(var j=0;j<elements.length; j++)

                found[foundCount++] = elements[j];

        }



        currentContext = found;

    }



    return currentContext;

}



function showHideElement(elementID, type){

	var obj = document.getElementById(elementID);

	obj.style.visibility = type;

}



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;

}



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) {

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

	}



	var flashmode = curMode;

    if(readCookie("flashmode")!=flashmode && initModeChange==true){

		createCookie("flashmode", flashmode, 365);

	}

}



activateLargeStyle(false);

if(readCookie("style")=='large') {

    activateLargeStyle(true);

}











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

        }

    }

}



/* Template DownloadTabs */

function showDownloadTab(a) {

    var tab = a.parentNode.parentNode;

    var tabs = tab.parentNode.childNodes;

    for(var i=0;i<tabs.length;i++) {

        if(tabs[i].className)

            tabs[i].className = tabs[i].className.replace(" tabActive", "");

    }

    tab.className += " tabActive";

}









/* *** FLASH/HTML Switchen *** */



var flPlayerDownloadPage = 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash';



var flSwitchText = new Object();

flSwitchText['html'] = 'HTML verzija';

flSwitchText['flash'] = 'Flash verzija';



//Flash-Version

var flVers = 0;

var flMovie = '';

var flBgcolor = '';

var flSize = '';

var flWmode = '';

var flParameter = '';

var flExtCode = '';



//Flash- oder Html-Variante

var initModeChange = false;

var curMode = '';

var switchMode = '';



function getCurMode(flRequired){

	var curMode = '';

	if (flRequired==true) //Wenn auf der Seite eine Flash-Stage ist

	{

		if ((getSwfVer() >= flVers && readCookie("flashmode")!='html') || readCookie("flashmode")=='flash'){

			curMode = 'flash';

		} else {

			curMode = 'html';

		}

	} else {

		if (readCookie("flashmode")!='html'){

			curMode = 'flash';

		} else {

			curMode = 'html';

		}

	}



	return curMode;

}



function changeFlashMode(){



	//Init setzen, damit Cookie gespeichert wird

	initModeChange = true;



	//Modi vertauschen

	var newSwitchM = curMode;

	curMode = switchMode;

	switchMode = newSwitchM;



	//Cookie wird in "onunload" gesetzt

	location.reload();

}





function writeStageFlashDownload(){

	document.write(getStageFlashDownload());

}



function writeStageFlash(movie,bgcolor,size,parameters,nocache,extCode,wmode){

    var oeTags = getStageFlash(movie,bgcolor,size,parameters,nocache,extCode,wmode);

    document.write(oeTags);

}



function getStageFlashDownload(){

	var flashstage = document.getElementById('stage');

	flashstage.className = 'flashStageNoFlash';

	var html = '';

	html += '<div>';

	html += '<h3><a href="'+flPlayerDownloadPage+'" onclick="window.open(this.href);return(false);">Šteta, ili nemate instaliran Flash-Plugin, ili imate instaliranu zastarjelu verziju!</a></h3>';

	html += '<p><a href="'+flPlayerDownloadPage+'" onclick="window.open(this.href);return(false);">...jer stvarno smo se potrudili dočarati Vam posebnosti animacije i zvuka. Stoga Vas molimo da downloadate besplatni Flash-Plugin.</a></p>';

	html += '<p><a href="'+flPlayerDownloadPage+'" onclick="window.open(this.href);return(false);"><img src="http://volkswagen.com.hr/files/get_flash_player.gif" /></a></p>';

	html += '</div>';

	return html;

}



function getStageFlash(movie,bgcolor,size,parameters,nocache,extCode,wmode){

    if (extCode==''){

 	    var postfix = '';

		if (nocache=='true'){

			postfix = '&nc='+Math.round(Math.random()*10000);

	    }

		var oeTags = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" '+ size +' id="flash" align="middle" wmode="opaque">'

	    +'<param name="allowScriptAccess" value="sameDomain" /> '

	    +'<param name="movie" value="'+movie+postfix+'" /> '

	    + (parameters ? '<PARAM NAME="FLASHVARS" VALUE="' + parameters + '">' : '')

	    +'<param name="menu" value="false" /> '

	    +'<param name="quality" value="high" /> '

	    +'<param name="scale" value="noscale" /> '

	    +'<param name="salign" value="lt" /> '

	    +'<param name="bgcolor" value="'+bgcolor+'" /> ';

	    

	    if (wmode && wmode!=''){

	    	oeTags += '<param name="wmode" value="'+wmode+'" /> ';

	    }

	    

	    oeTags += '<param name="FlashVars" value="" /> '

	    +'<embed src="'+movie+postfix+'" menu="false" quality="high" ';

	    

	    if (wmode && wmode!=''){

	    	oeTags += 'wmode="'+wmode+'" '

	    }

	    

	    oeTags += ' salign="lt" scale="noscale" bgcolor="'+bgcolor+'" '+ size +' '

	    + (parameters ? 'FLASHVARS="' + parameters + '" ' : '')

	    + 'name="flash" align="middle" allowScriptAccess="sameDomain" '

	    + 'type="application/x-shockwave-flash" '

	    + 'pluginspage="http://www.macromedia.com/go/getflashplayer" />'

	    +'</object>';

	} else {

		var oeTags = unescape(extCode);

	}

	return oeTags;

}



