
// globale Variablen
//var ie=document.all;			// nicht mehr verwendet
var nn6 = document.getElementById && !document.all;
var isdrag = false;
var x = 0;
var y = 0;
var dobj = 0;
var candrag = false;
var picArray = new Array(36);
var ssr_heading = 0;
var imgLoader = 0;
var loadCounter = 0;
var numPics = 36;
var currentUrl = 0;
var ssrenabled = false;
var ssrTarget = 0;
var cb_mousemove = 0;
var degree = 0;
var turnStep = 1;
var mouseStep = 15;

function Close_document()
{
    window.close();
}
//
// 0000 durch heading mit 4 leading zeros ersetzen
//

//
// onLoad CB => Panorama laden
//
function doLoad_Konfig() 
{
    var url;
    var lheading;
    var Panorama;
    url = document.getElementById("dreiDURL").value;
    if (url != "") {
       lheading = parseInt(document.getElementById("Drehwinkel").value);
       ssr_setCurrentImage(setUrlHeading(url, lheading));
    Panorama = document.getElementById("Check_Panorama").className;

    if (Panorama == "check") ssr_preload_pano(url); // sorgt dafür, daß alle 36 Bilder geladen werden
    ssr_init(lheading);
    }
    contentHeight_Konfig();
//    ssrheading = lheading;
}

//
// onBlur CB => Panorama-Laden ggfs. abbrechen
//
function doBlur_Konfig() {
//  document.getElementById("Drehwinkel").value = ssrheading;
//  alert("doBlur");
  ssr_reset();
}

//function doLoad() 
//{
//    var Meldung;
//    Meldung = document.getElementById("Meldung").value;
//    if (Meldung != "")  alert(Meldung);
//}

function setUrlHeading(oldUrl, heading)
{
	var headingString = "" + heading;
  
	while(headingString.length < 4)
	{
		headingString = "0" + headingString;
	}

	return oldUrl.replace(/0000/g, headingString);
}

//
// mouse move event
//
function move(e)
{
	if (!candrag) return false;
  
	if (isdrag)
	{
		var cx = nn6 ? e.clientX : event.clientX;
		var cy = nn6 ? e.clientY : event.clientY;
		var v1 = cx-x;
		var v2 = cy-y;
	
		mouseStep = 15 + (turnStep * 6);
	
		if (v1 >  mouseStep)
		{
			turnright();
			x=cx;
		}
		
		if (v1 < -mouseStep)
		{
			turnleft();
			x=cx;
		}
		
		if (cb_mousemove)
		{
			cb_mousemove();
		}
    
		return false;
	}
}

//
// mouse down event
//
function drags(e)
{
	if (!candrag) return false;
	
	var fobj       = nn6 ? e.target : event.srcElement;
    var topelement = nn6 ? "HTML" : "BODY";
	
    while (fobj.tagName != topelement && fobj.className != "drag")
	{
		fobj = nn6 ? fobj.parentNode : fobj.parentElement;
    }
	
    if (fobj.className == "drag")
	{
      isdrag = true;
      var dobj = fobj;
	  
      var tx = parseInt(dobj.style.left+0);
      var ty = parseInt(dobj.style.top+0);
	  
      x = nn6 ? e.clientX : event.clientX;
      y = nn6 ? e.clientY : event.clientY;
	  
      return false;
    }
}

//
// mouse up event
//
function drops(e)
{
	isdrag = false;
}


//
// Panorama um eins nach links drehen
//
function turnleft() {
    
	// turnStep nur für die 3. Stufe nachkorrigieren
    if((loadCounter >= 24) && (loadCounter < 36))
    {
		// nur bei der Startposition und bei vielfachen von 3,
		// da hier eine Drehung um 2 auf ein noch nicht errechnetes Bild verweisen wuerde
		if((ssr_heading == 0) || ((ssr_heading % 3) == 0))
		{
			ssr_heading = (ssr_heading + turnStep - 1) % numPics;
		}
		else
		{
			ssr_heading = (ssr_heading + turnStep) % numPics;
		}
    }
	else
	{
		ssr_heading = (ssr_heading + turnStep) % numPics;
	} 
	
	// zum erhalten der aktuellen Position der 3D Ansicht
	document.getElementById("Drehwinkel").value = ssr_heading;
    if ( ! picArray[ssr_heading] ) return;
	ssr_setCurrentImage(picArray[ssr_heading].src);
}

//
// Panorama um eins nach rechts drehen
//
function turnright() {

	// turnStep nur für die 3. Stufe nachkorrigieren
    if((loadCounter >= 24) && (loadCounter < 36))
    {
		// nur bei der Startposition und bei vielfachen von 3,
		// da hier eine Drehung um 2 auf ein noch nicht errechnetes Bild verweisen wuerde
		if((ssr_heading == 0) || ((ssr_heading % 3) == 0))
		{
			ssr_heading = (ssr_heading + (numPics - turnStep - 1)) % numPics;
		}
		else
		{
			ssr_heading = (ssr_heading + (numPics - turnStep)) % numPics;
		}
    }
	else
	{
		ssr_heading = (ssr_heading + (numPics - turnStep)) % numPics;
	}
  
	// zum erhalten der aktuellen Position der 3D Ansicht
	document.getElementById("Drehwinkel").value = ssr_heading;
    if ( ! picArray[ssr_heading] ) return;
	ssr_setCurrentImage(picArray[ssr_heading].src);
}

//
// Event, wenn ein Bild geladen wurde
//
function sleep(){
}

function panLoaderDone()
{
    
	loadCounter++;
	
	if (loadCounter >= numPics)
	{
		// Schrittbreite zum Drehen des Models an die 4. Stufe (maximale Feinheit = 36 Schritte)  anpassen
		turnStep = 1;
	    ssrTarget.style.cursor=nn6 ? 'pointer' : 'hand';
	    return;
	}
	
	else if (ssrenabled == true)
	{
		// in 4 immer feiner werdenden Stufen die Einzelbilder des Panoramas laden
		
		if(loadCounter < 6)
		{
			// die Zählvariable in eine Position auf dem Kreis umrechnen
			degree = 6 * loadCounter;
			
			// mit geändertem Zeiger signalisieren, das das Objekt noch nicht gedreht werden kann
			ssrTarget.style.cursor="wait";
		}
		
		else if(loadCounter < 12)
		{
			degree = 3 + (6 * (loadCounter - 6));
				
			// da die 1. Stufe nun fertig ist, darf das Objekt ab jetzt gedreht werden
			if(!candrag)
			{
				candrag = true;
				
				// mit geändertem Zeiger signalisieren, das das Objekt nun gedreht werden darf
				ssrTarget.style.cursor="pointer";
			}
			
			// Schrittbreite zum Drehen des Models an die 1. Stufe (6 Schritte) anpassen
			turnStep = 6;
			
			// für jede Stufe Text des Fortschrittsbalkens anpassen
			//ssrProgressBarText.innerHTML="Drehbare 3D Ansicht wird geladen (2. Stufe)";
		}
		
		else if(loadCounter < 24)
		{
			degree = 1 + (3 * (loadCounter - 12));
			
			// Schrittbreite zum Drehen des Models an die 2. Stufe (12 Schritte) anpassen
			turnStep = 3;
			
			// für jede Stufe Text des Fortschrittsbalkens anpassen
		}
		
		else
		{
			degree = 2 + (3 * (loadCounter - 24));
			
			// Schrittbreite zum Drehen des Models an die 3. Stufe (24 Schritte) anpassen
			// wird beim eigentlichen Drehen später nachkorrigiert
			turnStep = 2;
		}
	
	   // picArray[degree] = new Image();
	    
	    //picArray[degree].onload = panLoaderDone;
	    //picArray[degree].onerror = panLoaderDone;
	    
   	    //picArray[degree].onload = new Function("panLoaderDone()");
	    //picArray[degree].onerror = new Function("panLoaderDone()");

	    
	    //var url_temp=setUrlHeading(currentUrl, degree);
		//picArray[degree].src = url_temp+' ';


	    setTimeout("delay_load("+degree+")",10);
	    
		
	    //window.status = picArray[degree].src;
	    
	   // alert(picArray[degree].src);
	    //Object.dpDump(picArray[degree].src);
	}
}
function delay_load(degree)
{
	    picArray[degree] = new Image();	    
	    picArray[degree].onload = panLoaderDone;
	    picArray[degree].onerror = panLoaderDone;	    
	    picArray[degree].src = setUrlHeading(currentUrl, degree);
}

//
// Einstieg zum Vorladen des Panoramas
//
function preLoadPano(url)
{
	currentUrl = url;
	candrag = false;
	loadCounter = 0;
	cb_mousemove = 0;
	degree = 0;
	turnStep = 1;
	mouseStep = 15;

	picArray[0] = new Image();
	picArray[0].onload = panLoaderDone;
	picArray[0].onerror = panLoaderDone;
	picArray[0].src = setUrlHeading(url, 0);
	window.status = picArray[0].src;
	
}

//
// Neues Bild setzen
//
function ssr_setCurrentImage(url)
{
	ssrTarget.src = url;
}

//
// Html Element für Bühne (Bild) definieren
//
function ssr_setTarget(target)
{
 //   if (! target) return();
 //   if (!target == Null)
 //   {
	    ssrTarget = target;
	    ssrTarget.onmousedown = drags;
	    ssrTarget.onmousemove = move;
	    ssrTarget.onmouseup = drops;
 //   }
}

//
//
// (Öffentlicher) Einstieg zum Vorladen des Panoramas
//
function ssr_preload_pano(url)
{
	ssrenabled = true;
	if (ssrTarget)
	{
	    ssrTarget.style.cursor='auto';
	    //alert(url);
	    preLoadPano(url);
	}
}

//
// Initialisieren ssr beim Seiteneinstieg
//
function ssr_init(h)
{
	ssrenabled = true;
	ssr_heading = h;
}

//
// Zurücksetzen ssr beim Seiteneinstieg
//
function ssr_reset()
{
	ssrenabled = false;
}

function get( ID ) {
	return document.getElementById( ID );
}

function contentHeight() {
	// BERECHNUNGEN FÜR CONTENTDARSTELLUNG
	var headerHeight = 252;
	var cnttopHeight = get('CONTENTTOP').offsetHeight;
	var btnHeight = 80;
	var contentHeight = headerHeight + cnttopHeight + btnHeight;
	var mainHeight = get('SIZE').offsetHeight;
	var cnttopHeight = get('CONTENTTOP').offsetHeight;
	var paramHeight = 0;
	var counter = 1;
	var Meldung;
	var Gesamtbreite = document.body.offsetWidth;
//	
	if (get('HiddenBreite')) {
	     get('HiddenBreite').value = Gesamtbreite;
	}

	while (h2 = get('STEP' + counter)) {
		paramHeight += parseInt(h2.offsetHeight);
		counter++;
	}
	if (get('BOUNDREG')) {
		get('BOUNDREG').style.height = mainHeight - contentHeight -50 + 'px';
		get('BOUNDREG').style.display = 'block';
	}
	if (get('BOUND')) {
		get('BOUND').style.height = mainHeight - contentHeight + 38 + 'px';
		get('BOUND').style.display = 'block';
	}
	var cnttblHeight = get('MAIN').offsetHeight;
	if (get('PARAMETER')) {
		get('PARAMETER').style.height = mainHeight - paramHeight - headerHeight -30 + 'px';
		get('PARAMETER').style.display = 'block';		
	}
	if (get('OPTIONS')) {
		var optionsHeight = 28;
		get('OPTIONS').style.height = mainHeight - headerHeight - optionsHeight + 'px';
		get('OPTIONS').style.display = 'block';		
	}	
	// BERECHNUNGEN FÜR AUSGEGRAUTES DIV	
	if (get('LAYER')) {
		get('LAYER').style.height = mainHeight - 225 + 'px';
	}	
	// BERECHNUNGEN FÜR EINGEBLENDETE INFO	
	if (get('INFOTBL')) {	
		get('TXT').style.height = mainHeight - headerHeight - btnHeight + 'px';
		get('TXTBOUND').style.height = mainHeight - headerHeight - btnHeight + 'px';
		var txtHeight = get('TXTBOUND').offsetHeight;
		if (get('SHADOWRI')) {get('SHADOWRI').style.height = txtHeight - 9 + 'px';}
		get('INFOTBL').style.width = '80%';		
		get('INFOTBL').style.marginRight = 'auto';		
		get('INFOTBL').style.marginLeft = 'auto';		
		var txtWidth = get('TXT').offsetWidth;		
		if (get('SHADOWBO')) {get('SHADOWBO').style.width = txtWidth - 8 + 'px';}
	}
//	Meldung = document.getElementById("Meldung").value;
//	if (Meldung != "")  alert(Meldung);
}

function contentHeight_Konfig() {
	// BERECHNUNGEN FÜR CONTENTDARSTELLUNG
	var headerHeight = 252;
	var cnttopHeight = get('CONTENTTOP').offsetHeight;
	var btnHeight = 75;
	var contentHeight = headerHeight + cnttopHeight + btnHeight;
	var mainHeight = get('SIZE').offsetHeight;
	var cnttopHeight = get('CONTENTTOP').offsetHeight;
	var paramHeight = 0;
	var counter = 1;
	var Meldung;
	var links;
	var Gesamtbreite = document.body.offsetWidth;
//	
	if (get('HiddenBreite')) {
	     get('HiddenBreite').value = Gesamtbreite;
	}
	while (h2 = get('STEP' + counter)) {
		paramHeight += parseInt(h2.offsetHeight);
		counter++;
	}
//	if (get('BOUND') && mainHeight > contentHeight) {
//		get('BOUND').style.height = mainHeight - contentHeight  + 'px';
//		get('BOUND').style.display = 'block';
//	}
	if (get('BOUND')) {
		get('BOUND').style.height = mainHeight - contentHeight + 'px';
		get('BOUND').style.display = 'block';
	}
	var cnttblHeight = get('MAIN').offsetHeight;
	if (get('PARAMETER')) {
//		get('PARAMETER').style.height = mainHeight - paramHeight - headerHeight  + 'px';
		get('PARAMETER').style.height = mainHeight - paramHeight - headerHeight -75 + 'px';
		get('PARAMETER').style.display = 'block';		
	}
	if (get('OPTIONS')) {
		var optionsHeight = 28;
		get('OPTIONS').style.height = mainHeight - headerHeight - optionsHeight + 'px';
		get('OPTIONS').style.display = 'block';		
	}	
	// BERECHNUNGEN FÜR AUSGEGRAUTES DIV	
	if (get('LAYERZOOM')) {
		get('LAYERZOOM').style.height = mainHeight - 125 + 'px';
//		get('LAYERZOOM').style.top = 10 + 'px';
	}	
	if (get('LAYER')) {
		get('LAYER').style.height = mainHeight - 225 + 'px';
	}	
	// BERECHNUNGEN FÜR EINGEBLENDETE INFO	
	if (get('INFOTBL')) {	
		get('TXT').style.height = mainHeight - headerHeight - btnHeight + 'px';
		get('TXTBOUND').style.height = mainHeight - headerHeight - btnHeight + 'px';
		var txtHeight = get('TXTBOUND').offsetHeight;
		if (get('SHADOWRI') ) get('SHADOWRI').style.height = txtHeight - 9 + 'px';
		if (get('INFOTBL')) {
		    get('INFOTBL').style.width = '80%';		
		    get('INFOTBL').style.marginRight = 'auto';		
		    get('INFOTBL').style.marginLeft = 'auto';		
		}
		var txtWidth = get('TXT').offsetWidth;		
		if (get('SHADOWBO')) get('SHADOWBO').style.width = txtWidth - 8 + 'px';
	}

	if (get('INFOTBLZOOM')) {	
//		get('TXT').style.height = mainHeight - headerHeight - btnHeight + 'px';
//		get('TXTBOUND').style.height = mainHeight - headerHeight - btnHeight + 'px';
//		var txtHeight = get('TXTBOUND').offsetHeight;
//		get('SHADOWRI').style.height = txtHeight - 9 + 'px';
		get('INFOTBLZOOM').style.width = '90%';	
//		var txtWidth = get('TXT').offsetWidth;		
//		get('SHADOWBO').style.width = txtWidth - 8 + 'px';
	}

	if (get('INFOLAYERZOOM')) {	
  		get('INFOLAYERZOOM').style.top = 1 + 'px';
  		get('Schliessen').style.height = 30 + 'px';
		if (Gesamtbreite > 1020)
    		get('INFOLAYERZOOM').style.left = (Gesamtbreite - 1020)/2 + 15 + 'px';
		else
    		get('INFOLAYERZOOM').style.left = 1 + 'px';
	}
    if (Gesamtbreite < 1010)
    {
       if (get('I_Logo')) {
          get('I_Logo').style.width = 120 + 'px';
          get('I_Logo').style.height = 43 + 'px';
       //alert (Gesamtbreite);
       }
    }	
}

function contentHeight_reg() {
	// BERECHNUNGEN FÜR CONTENTDARSTELLUNG
	var headerHeight = 252;
	var cnttopHeight = get('CONTENTTOP').offsetHeight;
	var btnHeight = 80;
	var contentHeight = headerHeight + cnttopHeight + btnHeight;
	var mainHeight = get('SIZE').offsetHeight;
	var cnttopHeight = get('CONTENTTOP').offsetHeight;
	var paramHeight = 0;
	var counter = 1;
	var Meldung;
	var Gesamtbreite = document.body.offsetWidth;
//	
	if (get('HiddenBreite')) {
	     get('HiddenBreite').value = Gesamtbreite;
	}

	while (h2 = get('STEP' + counter)) {
		paramHeight += parseInt(h2.offsetHeight);
		counter++;
	}
	if (get('BOUND')) {
		get('BOUND').style.height = mainHeight - contentHeight + 10 + 'px';
		get('BOUND').style.display = 'block';
	}
	var cnttblHeight = get('MAIN').offsetHeight;
	if (get('PARAMETER')) {
		get('PARAMETER').style.height = mainHeight - paramHeight - headerHeight -50 + 'px';
		get('PARAMETER').style.display = 'block';		
	}
	if (get('OPTIONS')) {
		var optionsHeight = 28;
		get('OPTIONS').style.height = mainHeight - headerHeight - optionsHeight + 'px';
		get('OPTIONS').style.display = 'block';		
	}	
	// BERECHNUNGEN FÜR AUSGEGRAUTES DIV	
	if (get('LAYER')) {
		get('LAYER').style.height = mainHeight - 225 + 'px';
	}	
	// BERECHNUNGEN FÜR EINGEBLENDETE INFO	
	if (get('INFOTBL')) {	
		get('TXT').style.height = '150px'; //mainHeight - headerHeight - btnHeight + 'px';
		get('TXTBOUND').style.height = '150px'; //mainHeight - headerHeight - btnHeight  + 'px';
		var txtHeight = get('TXTBOUND').offsetHeight;
		get('SHADOWRI').style.height = txtHeight - 9 + 'px';
		get('INFOTBL').style.width = '80%';		
		get('INFOTBL').style.marginRight = 'auto';		
		get('INFOTBL').style.marginLeft = 'auto';		
		var txtWidth = get('TXT').offsetWidth;		
		get('SHADOWBO').style.width = txtWidth - 8 + 'px';
	}
    if (Gesamtbreite < 1010)
    {
       if (get('I_Logo')) {
          get('I_Logo').style.width = 120 + 'px';
          get('I_Logo').style.height = 43 + 'px';
       //alert (Gesamtbreite);
       }
    }	
}


function Konfig_Radio_onclick(Nr) {
    //alert(Nr);
    document.getElementsByTagName('body')[0].style.cursor='wait';    
	if (get('HiddenCounter'))
	{	
       open('konfiguration.aspx?Option=' + Nr + '&Count=' + get('HiddenCounter').value,'_self');
    }
    else
	{	
       open('konfiguration.aspx?Option=' + Nr,'_self');
    }
}

function vorKonfig_Radio_onclick(weg,Nr) {
    //alert(Nr);
	if (get('HiddenCounter'))
	{	
       open('vorkonfiguration.aspx?weg=' + weg + '&Nr=' + Nr + '&Count=' + get('HiddenCounter').value,'_self');
    }
    else
	{	
       open('vorkonfiguration.aspx?weg=' + weg + '&Nr=' + Nr,'_self');
    }
}

function FzgAuswahl_Radio_onclick(Nr,Marke) {
    //alert(Nr);
	if (get('HiddenCounter'))
	{	
       open('Fahrzeugauswahl.aspx?MaterialNr=' + Nr + '&Marke=' + Marke + '&Count=' + get('HiddenCounter').value,'_self');
    }
    else
	{	
       open('Fahrzeugauswahl.aspx?MaterialNr=' + Nr + '&Marke=' + Marke,'_self');
    }
}

function btn_allg_onclick(Option) {
    //alert(Option);
    open('konfiguration.aspx?'+Option ,'_self');
}

function vorkonfig_onclick(Option) {
    //alert(Option);
    //document.getElementsByTagName('body')[0].style.cursor='wait';    
    open('vorkonfiguration.aspx?'+Option ,'_self');
}

function closeInfo() {
	get('LAYER').style.display = 'none';
	get('INFOLAYER').style.display = 'none';	
}

function closeZoom() {
	get('LAYERZOOM').style.display = 'none';
	get('INFOLAYERZOOM').style.display = 'none';	
}
