// Global variables
var isCSS;
var isW3C;
var isIE4;
var isNN4;
var isIE6CSS;

// set initial window Height and Width to dont call drawMap() on load
var windowHeight = getInsideWindowHeight();
var windowWidth  = getInsideWindowWidth();

// Initialize upon load to let all browsers establish content objects
function initDHTMLAPI( ) 
{
    if (document.images) 
	{
        isCSS = (document.body && document.body.style) ? true : false;
        isW3C = (isCSS && document.getElementById) ? true : false;
        isIE4 = (isCSS && document.all) ? true : false;
        isNN4 = (document.layers) ? true : false;
        isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
    }
}

// Set event handler to initialize API
window.onload = initDHTMLAPI;

// Return the available content width space in browser window
function getInsideWindowWidth( ) 
{
    if (window.innerWidth) 
	{
        return window.innerWidth;
    } 
	else if (isIE6CSS) 
	{
        // measure the html element's clientWidth
        return document.body.parentElement.clientWidth;
    } 
	else if (document.body && document.body.clientWidth) 
	{
        return document.body.clientWidth;
    }
    return 0;
}
   
// Return the available content height space in browser window
function getInsideWindowHeight( ) 
{
    if (window.innerHeight) 
	{
        return window.innerHeight;
    } 
	else if (isIE6CSS) 
	{
        // measure the html element's clientHeight
        return document.body.parentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) 
	{
        return document.body.clientHeight;
    }
    return 0;
}


// draw the placeholder
function drawMap(mt,lg)
{	
	// use the provided paramter to detect the correct maptype
	lang = 'de';
	if ( mt ) maptype = mt;
	if ( lg ) lang = lg;
	if ( maptype == 'JAVA' || maptype == 'java' )
	{
		 
		appletTag = '<applet code="'+applet_code+'" codebase="'+applet_codebase+'" archive="'+applet_archive+'" id="map24_placeholder" name="map24_placeholder" width="'+appletWidth+'" height="'+appletHeight+'">';
		appletTag += '<param name="appid" value="'+applet_appid+'">';
		appletTag += '</applet>';
		// prompt("",applet_appid);
		// prompt('AppletTag',appletTag);
		//nWh = (nWh < 340) ? 340 : nWh;
		//document.getElementById('basemapswitch').style.top = nWh + 'px';
		document.getElementById('copyright').style.top = (windowHeight-17) + 'px';
		nWh = windowHeight-61;
		if (windowHeight>0) {
			document.getElementById('contentscroller').style.height = (windowHeight-145) + 'px';
		}
		document.getElementById('map').innerHTML = "";	
		document.getElementById('map').innerHTML = appletTag;			
		//alert(appletTag);
		document.getElementById('javaloader').style.display = 'none';			
	}
	else
	{
		// the MGI won't produce images bigger than 1000px
		// so we set this as the max value for our width and height attributes
		if ( mapWidth >= 1000 ) mapWidth = 1000;
		if ( mapHeight >= 1000 ) mapHeight = 1000;
		var now = new Date();
		document.getElementById('copyright').style.top = (windowHeight-17) + 'px';
		nWh = windowHeight-61;
		if (windowHeight>0) {
			document.getElementById('contentscroller').style.height = (windowHeight-145) + 'px';
		}
		appletHeight = appletHeight-2;
		appletWidth = appletWidth+2;
		//document.getElementById('cgimg_').innerHTML = '';
		//document.getElementById('cgimg_').innerHTML = '<input type="image" name="mapcgi" id="mapcgi" src="http://maptp15.map24.com/map24/webservices1.5?cgi=Map24RenderEngine&action=renderMapImage&mid='+mid+'&sid='+sid+'&iw='+appletWidth+'&ih='+appletHeight+'&debug=CGI&t='+now.getTime()+'" width='+appletWidth+' height='+appletHeight+' border=0>';
		//alert(document.getElementById('map').innerHTML);
		map24_controller.location.href = 'map24.hidden.mrc.php?do=resize&maptype=CGI&map24_sid='+map24_sid+'&sid='+sid+'&cgi_width='+appletWidth+'&cgi_height='+appletHeight;
		//map24_map.location.href = 'map24.hidden.map.php?map24_sid='+map24_sid+'&sid='+sid;
	}
}	


function doResize()
{
	window.setTimeout("checkPageResize()",500);
}


// check if we need to resize the page/frame
function checkPageResize()
{
	// alert('checkPageResize');
	var currentWidth  = getInsideWindowWidth();
	var currentHeight = getInsideWindowHeight();
  	if ( (currentWidth != windowWidth) || (currentHeight != windowHeight) )
  	{
		// resize the div holding the routing description
		resizeLeftCol();

		// resize the map
		resizeMap();
	}
	window.setTimeout("checkPageResize()",500);
}

// resize the map placeholder
function resizeMap()
{
	//alert('resizeMap');
	if ( document.getElementById("map") )
	{
		// get the current size(s)
		windowHeight = getInsideWindowHeight();
		windowWidth  = getInsideWindowWidth();
		
		mapWidth  = windowWidth - reservedWidth;
		mapHeight = windowHeight - reservedHeight - 22;
		
		//207 is width of the left navigation DIV, 5 - border at right
		appletWidth = windowWidth - 207 - 5;
		// 60 is height of the banner at the top, 22 - space at the bottom for "Bedingungen" message
		appletHeight = windowHeight - 60 - 22;
		
		drawMap();
	}
	else
	{
		// alert('COULDNT FIND DA MAP');
	}
}

// resize the div holding the left column
function resizeLeftCol()
{
	//  alert("Width: " + getWindowWidth() + "\n" + "Height: " + getWindowHeight() + "\n" + "resizeRD()");
	if ( document.getElementById('leftcol') )
	{
		// get the current size(s)
		windowHeight = getInsideWindowHeight();
		windowWidth  = getInsideWindowWidth();
		
		// height of navbar with buttons
		// navbarHeight = document.getElementById('navbar').offsetHeight;
		// height of routing details (start loc, dest loc overall time anfd length)
		// routingDetailsHeight = document.getElementById('routingDetails').offsetHeight;
		// some space reserved
		leftColReservedHeight = 0;
		
		// calculate the new height for the routing description container
		// routingDescriptionHeight = windowHeight - navbarHeight - routingDetailsHeight - rdReservedHeight;
		leftColHeight = windowHeight - leftColReservedHeight;

		// we want to have a minimum height for the description
		if ( leftColHeight  < 100 ) leftColHeight  = 100;
		document.getElementById('leftcol').style.height = leftColHeight+"px";
	}
}
// begin onResize poll
doResize();
