
var navOver		= false;
var actMenu		= false;

var imageLoad	= new Array();
var pictures	= new Array();

var cnWidth		= 780;
var cnPadding	= 7.5;




window.setTimeout( function () {

	if ( typeof document.getElementById('mainnav') == 'object' ) {

		addMouseEvent( document.getElementById('mainnav').getElementsByTagName('img'), true );

		document.getElementById('mainnav_sub').onmouseover = function() { mn_mouseOver(true);  };
		document.getElementById('mainnav_sub').onmouseout = function()	{ mn_mouseOver(false); };

	}

}, 300 );


window.onload = function () {

	document.getElementById('mainnav_sub').onmouseover = function() { mn_mouseOver(true);  };
	document.getElementById('mainnav_sub').onmouseout = function()	{ mn_mouseOver(false); };

	addMouseEvent( document.getElementById('mainnav').getElementsByTagName('img'), true );
	addMouseEvent( document.getElementById('subnav').getElementsByTagName('img'), false );

	addBoxLinks( document.getElementsByTagName('a') );

	
	nodes = document.getElementsByTagName('a');
	for ( var i=0; i<nodes.length; i++ ) {
		
		if ( nodes[i].className.search(/popup/) != -1 ) {
			nodes[i].onclick = function () { return openWindow (this.href,800,650,true); };
		}

	}


	if ( document.getElementById('search') ) {

		document.getElementById('reset').onclick = function () {
			document.location.href = "http://"+document.location.host+window.location.pathname;
		}

		addComboSwitch();
	}


	if ( document.getElementById('contact') ) {
		
		document.getElementById('submit').onclick = function () {
			return checkForm();
		}

	}


	var reg = /(http:\/\/[\d]*\D+)(\d+)\.jpg/;
	opt = reg.exec(document.getElementById('stage').src);

	if ( opt && opt[2] > 0 ) {

		_arr	= new Array();
		_arr.push('stage');

		_parent = document.getElementById('stage').parentNode;
		
		for ( i=--opt[2]; i>=1; i-- ) {

			_arr.push('stage'+i);

			this.element = document.createElement('img');
			element.setAttribute('id', 'stage'+i);
			element.setAttribute('src', opt[1]+i+'.jpg');
			element.setAttribute('alt', document.getElementById('stage').alt);

			_parent.appendChild(element);
			element.style.opacity = 0;
			element.style.filter = 'alpha(opacity=0)';
			element.style.visibility = 'hidden';
			element.style.position = 'absolute';
			element.style.left = 0;

		}

		var cf = new crossfader(_arr);

	}
	
	imagePreload( imageLoad );


}


function addMouseEvent (nodes, menu) {

	for ( var i=0; i<nodes.length; i++ ) {

		ftype	= nodes[i].src.substring(nodes[i].src.lastIndexOf('.'), nodes[i].src.length);
		imageLoad.push( nodes[i].src.replace(ftype, '-B'+ftype) );

		nodes[i].onmouseover = function() {
			switchImg (this,true);
			if ( menu == true ) { mn_showMenu (this,true); }
		};

		nodes[i].onmouseout = function() {
			switchImg (this,false);
			if ( menu == true ) { mn_showMenu (this,false); }
		};
	
	}

}


function imagePreload (uris) {

	var x = pictures.length;

	for ( var i=0; i<=uris.length; i++ ) {

		pictures[++x]	= new Image();
		pictures[x].src	= uris[i];

	}

}


function switchImg (img,b) {

	src		= img.src;
	ftype	= src.substring(src.lastIndexOf('.'), src.length);

	img.src = (b==true)	? src.replace(ftype, '-B'+ftype)
						: img.src = src.replace('-B','');

}


function mn_showMenu (menu,on) {
	var node	= document.getElementById( menu.name.replace('mn_', 'mns_') );
	var cont	= document.getElementById('mainnav_sub');
		actMenu	= menu;

	if ( !node ) {
		return false;
	}

	node.style.display = on == true ? "block"
									: navOver == true	? "block"
														: "none";
	
	if ( on == true ) {

		cont.style.width = "600px";

		mnLeft	= cnWidth-cnPadding-absleft(menu);
		mnWidth = node.offsetWidth;

		node.style.right = (mnLeft-mnWidth <= 0 )	? "0px"
													: parseInt(mnLeft-mnWidth)+"px";

	}else{
		cont.style.width = 0;
	}

}


function mn_mouseOver (on) {

	navOver = on;
	mn_showMenu (actMenu,on);
	switchImg (actMenu,on);

}


function absleft(i) {

	return (i.offsetParent) ? parseInt(i.offsetLeft)+absleft(i.offsetParent)
							: parseInt(i.offsetLeft);

}


function openWindow (url,w,h,oneWindow) {

	if ( !oneWindow || !newWindow || newWindow.closed ) {
		var newWindow = window.open (url,name,"width="+w+",height="+h+",dependent=yes,location=no,menubar=no,toolbar=no");
	}else{
		newWindow.location.href = url;
	}

	newWindow.focus();
	return false;

}

function addBoxLinks (nodes) {
	var box = "";

	for ( var i=0; i<nodes.length; i++ ) {

		if ( nodes[i].href.search(/.*\#/) != -1 ) {

			nodes[i].onclick = function() {
				return magicBox (this);
			};

			checkBox( nodes[i] );
		}
	}
}

function magicBox (link) {
	var el = document.getElementById( link.href.replace(/.*\#/,"") );

	if ( !el )
		return false;

	if ( el.style.display == "block" ) {
		link.style.backgroundImage = 'url(/images/icn_arrow.gif)';
		el.style.display = "none";

	}else{
		link.style.backgroundImage = 'url(/images/icn_arrow-d.gif)';
		el.style.display = "block";
		
	}
	
	return false;

}

function checkBox ( link ) {

	if ( !document.getElementById( link.href.replace(/.*\#/,"") ) )
		return false;

	var el = document.getElementById( link.href.replace(/.*\#/,"") ).getElementsByTagName('input');
	var counter = 0;

	for ( var i=0; i<el.length; i++ ) {
		var select = null;

		eval('reg = /'+el[i].name+'=([^&]*)/;');
		select = reg.exec(window.location.href);

		if ( select != null && select[1] == 'on' ) {
			el[i].checked = true;
			counter++;
		}
	}

	if ( counter > 0 ) {
		magicBox(link);
	}

}


function addComboSwitch () {
	var form = document.getElementById('search');

	form.marke1.onchange = function () {
		comboSwitch(form.marke1,form.modell1);
	}

	form.marke2.onchange = function () {
		comboSwitch(form.marke2,form.modell2);
	}

	form.marke3.onchange = function () {
		comboSwitch(form.marke3,form.modell3);
	}

	form.kategorie.onchange = function () {

		var newLoc;

		if ( window.location.href.search(/kategorie/) != -1 ) {
			newLoc = window.location.href.replace(/kategorie=[^&]*/, "kategorie="+form.kategorie.value);
		}else{
			newLoc = window.location.href+(window.location.href.indexOf('?')!=-1?'&':'?')+"kategorie="+form.kategorie.value;
		}

		window.location.href = newLoc;

	}

	form.bereich.onchange = function () {

		var newLoc;

		if ( window.location.href.search(/bereich/) != -1 ) {
			newLoc = window.location.href.replace(/bereich=./, "bereich="+form.bereich.value);
		}else{
			newLoc = window.location.href+(window.location.href.indexOf('?')!=-1?'&':'?')+"bereich="+form.bereich.value;
		}

		window.location.href = newLoc;

	}


	comboSwitch(form.marke1,form.modell1);
	comboSwitch(form.marke2,form.modell2);
	comboSwitch(form.marke3,form.modell3);

}

function comboSwitch ( marke, model ) {

	if ( marke.value == -1 ) {
		return false;
	}

	var option = m[marke.value].split(';');
	var val;

	comboClear(model);

	eval('reg = /'+model.name+'=([^&]*)/;');
	reg.exec(window.location.href);
	var select = RegExp.$1;

	for ( var i=0; i<option.length; i++ ) {

		val = option[i].split(',');

		if ( val[1] ) {

			var selected = (val[0] == select ? true : false);
			next = new Option( val[1], val[0], selected, selected );
			model.options[model.length] = next;

		}

	}

}

function comboClear ( model ) {

	for ( var i=model.options.length; i>0; i-- ) {
		model.options[i] = null;
	}

}


function addFavorit () {

	var uri = 'http://'+window.location.host;


	if (document.all ) {
		window.external.AddFavorite(uri, uri);
	}

	else if (typeof window.external.addPanel == 'function' ) {
		window.external.addPanel(uri, uri, '');
	}

	else {
		alert('Bitte drücken sie STRG+D');
	}

//	return false;

}


function getParent ( node, type ) {

	var ret = node.parentNode;

	if ( ret.tagName != type ) {
		return getParent(ret,type);
	}

	return ret;

}


function checkForm () {
	var err = 0;

	if ( document.getElementById('name').value == '' ) {
		document.getElementById('err_name').style.display = 'block';
		err++;
	}else{
		document.getElementById('err_name').style.display = 'none';
	}

	if ( document.getElementById('telefon').value == '' ) {
		document.getElementById('err_tel').style.display = 'block';
		err++;
	}else{
		document.getElementById('err_tel').style.display = 'none';
	}

	if ( document.getElementById('email').value == '' ) {
		document.getElementById('err_email').style.display = 'block';
		err++;
	}else{
		document.getElementById('err_email').style.display = 'none';
	}

	if ( document.getElementById('nachricht').value == '' ) {
		document.getElementById('err_msg').style.display = 'block';
		err++;
	}else{
		document.getElementById('err_msg').style.display = 'none';
	}

	if ( err ) {
		return false;
	}

	return true;
}






/**
 *	author:		Timothy Groves - http://www.brandspankingnew.net
**/



var useBSNns;

if (useBSNns) {
	if (typeof(bsn) == "undefined")
		bsn = {}
	var _bsn = bsn;
}

else{
	var _bsn = this;
}



_bsn.crossfader = function(arr) {

	this.aDivs	= arr;
	this.nAct	= 0;
	this.nDur	= 1200;	//fadetime
	this.nDelay = 6000;	//delay
	
	var p=this;
	this.nID1 = setInterval(function() { p.newfade() }, this.nDelay);
}


_bsn.crossfader.prototype.newfade = function() {

	if (this.nID1)
		clearInterval(this.nID1);
	
	this.nOldAct = this.nAct;
	this.nAct++;

	if (!this.aDivs[this.nAct])
		this.nAct = 0;
	
	if (this.nAct == this.nOldAct)
		return false;
	
	document.getElementById( this.aDivs[this.nAct] ).style.visibility = "visible";
	
	this.nInt = 50;
	this.nTime = 0;
	
	var p = this;
	this.nID2 = setInterval(function() { p.fade() }, this.nInt);
}


_bsn.crossfader.prototype.fade = function() {
	this.nTime += this.nInt;
	
	var ieop = Math.round( (0.5*(1 - Math.cos(Math.PI*this.nTime/this.nDur))) * 100 );
	var op = ieop / 100;

	document.getElementById( this.aDivs[this.nAct] ).style.opacity = op;
	document.getElementById( this.aDivs[this.nAct] ).style.filter = "alpha(opacity="+ieop+")";
	
	if (this.nOldAct > -1) {
		document.getElementById( this.aDivs[this.nOldAct] ).style.opacity = 1 - op;
		document.getElementById( this.aDivs[this.nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
	}
	
	if (this.nTime == this.nDur) {

		clearInterval( this.nID2 );
		
		if (this.nOldAct > -1)
			document.getElementById( this.aDivs[this.nOldAct] ).style.visibility = "hidden";	
		
		var p=this;
		this.nID1 = setInterval(function() { p.newfade() }, this.nDelay);
	}
}