
// Create <div> layer with ID, Class optional
function makeLayer(name, left, top, width, height, visible, content, theClass) {

	if (theClass)
	{
		document.writeln('<div id="' + name + '" class="' + theClass +'"');
		document.writeln(content);
		document.writeln('</div>');
	}
	else
	{
		document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px;' + ' visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
		document.writeln(content);
		document.writeln('</div>');
	}

	clipLayer(name, 0, 0, width, height);

}

// get the layer object called "name"
function getLayer(name) {
	return(document.getElementById(name).style);
}

// move layer to x,y
function moveLayer(name, x, y) {		
	var layer = document.getElementById(name);		
	if (layer != null) {
    		layer.style.left = x + "px";
   			layer.style.top  = y + "px";
 	}
}


// make layer invisible
function hideLayer(name) {		
 	var theLayer = document.getElementById(name);		
	if (theLayer != null) {
		  theLayer.style.visibility = "hidden";
	}
}

// make layer visible
function showLayer(name) {		
	var theLayer = document.getElementById(name);		
	if (theLayer != null) 
	{
  	 theLayer.style.visibility = "visible";
	}
}


// toggle layer to invisible
function hideLayer1(name) {

 	var layer = document.getElementById(name).style;		

	if (layer != null) {
   		 layer.display = "none";
	}
	else
	{alert("!!!");}
	
	//calculate space for Layercontrol
	calcSpace();

}

// toggle layer to visible
function showLayer1(name) {		
 	var layer = document.getElementById(name).style;		
	if (layer != null) {
   		 layer.display = "block";
	}
	else
		{alert("!!!");}

	//calculate space for Layercontrol
	calcSpace();
}


function button_to_over(theId)
{
	if (document.getElementsByName(theId)[0].src.indexOf("disabled") > 0)
	{ return; }
	document.getElementsByName(theId)[0].src = document.getElementsByName(theId)[0].src.replace("-default","-over");
}

function button_from_over(theId)
{

	if (document.getElementsByName(theId)[0].src.indexOf("disabled") > 0)
	{ return; }
	document.getElementsByName(theId)[0].src = document.getElementsByName(theId)[0].src.replace("-over","-default");

}


function setMapCursorStyle(stil)
{
	document.getElementById('theMap').style.cursor=stil;
	document.getElementById('theTop').style.cursor=stil;
}

function Quicklegend_click()
{
	
  if (document.getElementById('theQuickLegTable').style.visibility == "hidden")
  {
    document.legendbutton.src = "graphics/legend_1.gif"
    showLayer('theQuickLegTable');
  }
  else
  {
    document.legendbutton.src = "graphics/legend_0.gif"
    hideLayer('theQuickLegTable');
  }
	
}

function legendbutton_click(elem_id, ein_aus_buttonName) // (<was wird ein-/ausgeblendet>, <von welchen Ein/Aus Button>)
{
    if (getLayer(elem_id).display == "none")
    {
		document.getElementById(ein_aus_buttonName).src = "pix/legend_1.gif";
		showLayer1(elem_id);
    }
    else
    {
	document.getElementById(ein_aus_buttonName).src = "pix/legend_0.gif";
	hideLayer1(elem_id);
    }
	
}


// clip layer display to clipleft, cliptip, clipright, clipbottom
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		

    var layer = document.getElementById(name);		
    layer.style.clip = 'rect(' + cliptop + 'px ' +  clipright + 'px ' + clipbottom + 'px ' + clipleft +'px)'; 
}


// replace layer's content with new content
function alter_innerHTML(name, content) {
			content = content.replace(/\'/g,"\'");
			document.getElementById(name).innerHTML = content;
}

var popUpWin = "";


// open popup window
function openPopup(wurl, wname){
		        
	wname = window.open(wurl, wname, "resizable=yes,width=400,height=200,status=no,location=no,scrollbars=yes,toolbar=no");
	popUpWin = wname;
	popUpWin.focus();
	
	return popUpWin;
}

// open popup window
function openQueryPopup(wurl, wname){

	wname = window.open(wurl, wname, "resizable=no,width=500,height=400,status=no,location=no,toolbar=no, dependent");
	popUpWin = wname;
	popUpWin.focus();

	return popUpWin;
}

function addHistoryEntry(theImgage, theImgExt, theScale)
{

	if (historyPos == maxHistoryEntries)
	{
		if (currentPos > 0)
		{
			ShiftHistoryEntries(currentPos);
		}
	}
	else
	{
		currentPos++;
	}

	mapHistory[currentPos].image  = theImgage;
	mapHistory[currentPos].imgext = theImgExt;
	mapHistory[currentPos].scale  = theScale;

	if(historyEntries < maxHistoryEntries && currentPos == historyPos)
	{
		historyPos += 1;			
		historyEntries += 1;
	}
	
	if (currentPos > 0)
	{
		enable_Button("previous");	
	}
	else
	{
		disable_Button("previous");	
	}
	if (currentPos >= historyPos -1)
	{ disable_Button("next"); }

}

function ShiftHistoryEntries(stopPos)
{
	for (i=0; i<stopPos;i++)
	{
		
		mapHistory[i].image = mapHistory[i+1].image;
		mapHistory[i].imgext = mapHistory[i+1].imgext;
		mapHistory[i].scale = mapHistory[i+1].scale;
	}
}

function HistoryGoBack()
{
	if(!historyEntries || currentPos<1){
		return;	
	}
		
	currentPos--;
	
	imgextent = mapHistory[currentPos].imgext;

	var split_imgext = imgextent.split("+");
	left   = parseInt(split_imgext[0]);
	bottom = parseInt(split_imgext[1]);
	right  = parseInt(split_imgext[2]);
	top    = parseInt(split_imgext[3]);
	theScale = mapHistory[currentPos].scale;

	window.document.theImage.src = mapHistory[currentPos].image;
	
	// If reference map exists insert
	if(document.refmap ){
		//document.refmap.src   = mapHistory[currentPos].refmap;
		if (theOverviews.length > 1)
		{
			check_overview(theScale, (right+left)/2, (top+bottom)/2);
		}	
		drawRefMapMarker(left, right, bottom, top)
	}
	
	// If scale exists insert
	if(document.scale){
		//document.scale.src   = mapHistory[currentPos].scaleImg;
		drawScalebar(theScale);
		updateScaleNumber(theScale);	

	}

	if(currentPos < 1)
	{
		disable_Button("previous");
	}
	else
	{
		enable_Button("previous");	
	}

	enable_Button("next");		
}

function HistoryGoNext()
{
	if(!historyEntries || currentPos >= historyPos-1){
		return;	
	}

	currentPos++;
	
	imgextent = mapHistory[currentPos].imgext;

	var split_imgext = imgextent.split("+");
	left   = parseInt(split_imgext[0]);
	bottom = parseInt(split_imgext[1]);
	right  = parseInt(split_imgext[2]);
	top    = parseInt(split_imgext[3]);	
	theScale = mapHistory[currentPos].scale;

	window.document.theImage.src = mapHistory[currentPos].image;
	
		// If reference map exists insert
	if(document.refmap ){
		//document.refmap.src   = mapHistory[currentPos].refmap;
		if (theOverviews.length > 1)
		{
			check_overview(theScale, (right+left)/2, (top+bottom)/2);
		}	
		drawRefMapMarker(left, right, bottom, top)
	}
	
	
	// If scale exists insert
	if(document.scale){
		drawScalebar(theScale);
		updateScaleNumber(theScale);	
	}
	scale = mapHistory[currentPos].scale;

	if(currentPos >= historyPos-1){
		disable_Button("next");
	}else{
		enable_Button("next");
	}

	enable_Button("previous");

	return;
}

function calcSpace()
{
    var right_layer_height = 570 -2;
    try {       
                
        if (document.getElementById("treediv")) {            
            document.getElementById("treediv").style.height= (right_layer_height - document.getElementById("theLayerControl").offsetTop)+"px";
        }
    }
    catch(e) {                
    } 
    
    return (right_layer_height - document.getElementById("theLayerControl").offsetTop);
        
}      

function setLanguage(newLang) {
    if(sprache!=newLang) {
        sprache = newLang;
        window.location.href="go?FrameLoaderActionSprache="+newLang;        
    }
}

