window.imgXxlPopUpUrl = '';
window.onload = function ()
{
	/**
	 *  Öffnet alle Links in neuem Fenster, obwohl das absolut scheiße ist!
	 */
	aLinks = document.getElementsByTagName( 'A' );
	for ( i = aLinks.length; i--;  )
	{
		if ( aLinks[i].href.substr( 0, 10 ) == 'javascript' ) { continue; }
		if ( aLinks[i].id == 'googlepopup' ) { continue; }
		if ( aLinks[i].id == 'kennzeichenpopup' ) { continue; }
		if ( aLinks[i].hostname != window.location.hostname ) { aLinks[i].target = '_blank'; }
	}

	/**
	 *  Schnellsuche -- Ausblenden von Formularfeldern
	 */
	window.searchForm = document.getElementById( 'js-schnellsuche' );
	if ( window.searchForm )
	{
		window.searchForm.elementsToHide = [];
		for ( var n, i = 0; i < window.searchForm.childNodes.length; ++i )
		{
			n = window.searchForm.childNodes[i];
			if ( n.nodeType != 1 ) continue;
			if ( !n.className || n.className.indexOf( 'schnellsuche' ) < 0 )
			{
				n.oldDisplay = n.style.display;
				n.style.display = 'none';
				window.searchForm.elementsToHide[window.searchForm.elementsToHide.length] = n;
			}
		}
		
		var ctrl = document.getElementById( 'js-toggle-schnellsuche' );
		if ( ctrl )
		{
			var toggler = document.createElement( 'A' );
			toggler.href = '';
			toggler.isCollapsed = true;
			toggler.textMore = 'mehr Suchoptionen';
			toggler.textLess = 'weniger Suchoptionen';
			toggler.appendChild( document.createTextNode( toggler.textMore ) );
			var br1 = document.createElement( 'BR' );
			var br2 = document.createElement( 'BR' );
			ctrl.insertBefore( br2, ctrl.firstChild );
			ctrl.insertBefore( br1, ctrl.firstChild );
			ctrl.insertBefore( toggler, ctrl.firstChild );
			toggler.onclick = function ()
			{
				if ( this.isCollapsed )
				{
					for ( var i = 0; i < window.searchForm.elementsToHide.length; ++i )
					{
						window.searchForm.elementsToHide[i].style.display =
							window.searchForm.elementsToHide[i].oldDisplay;
					}
					this.isCollapsed = false;
					this.firstChild.nodeValue = this.textLess;
				}
				else
				{
					for ( var i = 0; i < window.searchForm.elementsToHide.length; ++i )
					{
						window.searchForm.elementsToHide[i].style.display = 'none';
					}
					this.isCollapsed = true;
					this.firstChild.nodeValue = this.textMore;
				}
				return false;
			}
		}
	}

	var carlink, carlist = document.getElementById( 'carlist' );
	
	if ( carlist )
	{
		aTbody = carlist.getElementsByTagName( 'TBODY' );
		for ( t = 0; t < aTbody.length; t++ )
		{
			carlist = aTbody[t];
			if ( carlist && carlist.childNodes )
			{
				for ( var i = carlist.childNodes.length; i--; )
				{
					if ( !carlist.childNodes[i].style ) { continue; }
					carlink = carlist.childNodes[i].getElementsByTagName( 'A' );
					if ( carlink[1] && carlink[1].href )
					{
						carlist.childNodes[i].className += ' pointer';
						carlist.childNodes[i].link = carlink[0].href;
						carlist.childNodes[i].target = carlink[0].target;
						carlist.childNodes[i].onclick = function ()
						{
							if ( this.link ) 
							{ 
								if ( !this.target ) 
								{ 
									window.location = this.link; 
								}
								else
								{
									window.open( this.link, '_blank' );
									return false;
								}						
							}
						};
					}
				}		
			}
		}
	}
	// Detailansicht - Bilder
	
	var eImgages;
		
	if ( eImages = document.getElementById( 'images' ) )
	{
		var tmp = eImages.getElementsByTagName( 'UL' )[0];
		window.eImage = eImages.getElementsByTagName( 'IMG' )[0]; // Big image
		var aLinks = tmp.getElementsByTagName( 'A' ); // Thumbnail links
		
		window.iCid = window.location.search.substr( 5 );
		
		// Path to big image
		window.sImgSrcPath = eImage.src.substr( 0, eImage.src.lastIndexOf( '/' ) + 1 );
		
		// Check if "no photo"
		tmp = eImage.src.substr( eImage.src.lastIndexOf( '/' ) + 1, 1 );
		if ( tmp < '0' || tmp > '1' ) { return; }
		
		if ( aLinks.length < 1 ) { return; } // If not at least one Thumbnail
		// Loop through thumbs
		for ( var i = 0; i < aLinks.length; i++ )
		{
			aLinks[i].iImgNr = i;
			aLinks[i].onclick = rotateImage;
			aLinks[i].bigImage = new Image();
			aLinks[i].bigImage.src = sImgSrcPath + i + '.jpg';
		}
	}
	
	// JavaScript XXL-PopUp
	if ( ( eNode1 = document.getElementById( 'xxlpopup' ) ) && eNode1.href )
	{
		eNode1.href = 'javascript:popup(\'' + eNode1.href + '\')';
	}
	
	// JavaScript Googlemaps-Popup
	if ( ( eNode2 = document.getElementById( 'googlepopup' ) ) && eNode2.href )
	{
		eNode2.onclick = function ()
		{
			popup( eNode2.href, 700, 600, 'Anfahrt' );
			return false;
		}
	}
	
	// JavaScript Wunschkennzeichen-Popup
	if ( ( eNode3 = document.getElementById( 'kennzeichenpopup' ) ) && eNode3.href )
	{
		eNode3.onclick = function ()
		{
			popup( eNode3.href, 780, 580, 'Wunschkennzeichen' );
			return false;
		}
	}
}

function rotateImage ()
{
	eImage.src = this.bigImage.src;
	window.imgXxlPopUpUrl = 'xxl.php?cid=' + iCid + '&pid=' + this.iImgNr; 
	return false;
}

function popup ( sUrl, iWidth, iHeight, sName )
{
	if ( window.imgXxlPopUpUrl ) { sUrl = window.imgXxlPopUpUrl; }
	
	if ( !iWidth ) { iWidth = 640; }
	if ( !iHeight ) { iHeight = 620; }
	if ( !sName ) { sName = '_POPUP'; }

	winPopUp = window.open(
		sUrl,
		sName,
		"width=" + iWidth + 
		",height=" + iHeight + 
		",scrollbars=0,resizable=0,status=0,location=0,toolbar=0,directories=0,menubar=0" )
		winPopUp.focus();
}

function clearForm ( f ) 
{
	if ( !f.elements ) { return false; }
	for( var i = 0; i < f.elements.length; i++ ) {
		f.elements[i].checked = false;
		f.elements[i].selectedIndex = 0;
	}
	f['car[Mode]'].value = '';
	return false;
}
