// look if our required varaibles were defined!
//if(!WMS_URL) alert("ERROR: required parameter 'WMS_URL' was missing");
if(!Sarissa.IS_ENABLED_XMLHTTP) alert("ERROR: required file 'sarissa.js' was not included!");
if(!Sarissa.IS_ENABLED_SELECT_NODES) alert("ERROR: required file 'sarissa_ieemu_xpath.js' was not included!");


// global parameters
//var WMS_PARAMETER_ADDITIONS = "VERSION=1.3.0&SERVICE=WMS&";
// use png24 one when we use images for background,..
//var WMS_FORMAT = "image/png; mode=24bit"; 
//var WMS_FORMAT = "image/jpeg";

function getObj(name)
{
	if (document.getElementById)
	{
		return document.getElementById(name);
	}
	else if (document.all)
	{
		return document.all[name];
	}
	else if (document.layers)
	{
		return document.layers[name];
	}
}

function showMapImage() {
	var url = getMapUrl("GetMap");
	// do the actual load of the page:
	// alert(url);

	var mapimage = document.getElementById("map-image");

	// now retrieve our image of the map
	var image = new Image();
	image.src = url;
	
	// image was loaded, now use this one as background!
	mapimage.style.backgroundImage = "url('" + url + "')";
}
function testfun()
{
	alert("asfas");
}
function showFeatureInfo(coordinate) {
	//alert("now inside featureinfo");
	// determine based upon the capabilities xml, what the queryable layers are,...
	//var url = getMapUrl("GetFeatureInfo");
	var url = "http://vijn.hawarit.com/~schollenkamp/map/webpage/Info.php?coordinate="+ coordinate;
	// next format can also be retrieved from the capabilities file!
	//url = url + "x=" + xpos + "&";
	//url = url + "y=" + ypos + "&";

	//alert(url);

	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	var result = xmlhttp.responseText;
	

	//DETERMINE GID and do a call to details.asp?gid=%gid%
	
	//alert(result);
	//alert(event.clientX);
	//alert(url);
	// for text, otherone doesnt work? :S

	if(result == "") 
	{
	    return "nothing";
	}
	else
	{
	// determine the gid and use the gid to query or details screen
	//var gid = 1234;
	//gid = result.substr((result.search("gid") + 7),2);
	//if(gid.search('\'') != -1)
	//{
	//	gid = gid.substr(0,1)	
	//}
		info = result;
	
	//url = "details.php?gid=" + gid;
	//xmlhttp = new XMLHttpRequest();
	//xmlhttp.open("GET", url, false);
	//xmlhttp.send(null);
		showInfo(result); 
	}
}



function showInfo( messagehtml) 
{
	var getfeatureinfo = document.getElementById("getfeatureinfo");
	if(!getfeatureinfo) {
	    alert("ERROR: element with name: 'getfeatureinfo' was not found!");
	    return;
	}
	
	if(messagehtml.search("does not exist") == -1)
	{
		getfeatureinfo.innerHTML = messagehtml;
		// TODO: we need to know the offset!
		//getfeatureinfo.style.left = x + "px";
		//getfeatureinfo.style.top = y + "px";	
		getfeatureinfo.style.visibility='visible';
	}
	info = messagehtml;
}

function toggleVisibility_sf(div) 
{
	var toggleDiv = document.getElementById(div);
	if(!toggleDiv) {
	    alert("ERROR: element with name: " + div + " was not found!");
	    return;
	}
	
	if(toggleDiv.style.visibility =='hidden')
	{
		toggleDiv.style.visibility ='visible'
	}
	else
	{
		toggleDiv.style.visibility ='hidden'
	}
	
}

function hideInfo()
{
	var getfeatureinfo = document.getElementById("getfeatureinfo");
	if(!getfeatureinfo) {
	    alert("ERROR: element with name: 'getfeatureinfo' was not found!");
	    return;
	}
    //getfeatureinfo.style.visibility='hidden';
	getfeatureinfo.style.display='none';

	var divframe = document.getElementById("divframe");
	if(!divframe) {
	    alert("ERROR: element with name: 'divframe' was not found!");
	    return;
	}
   // divframe.style.visibility='hidden';
    divframe.style.display='none';
}

function getPagePositionCoord(o,e,coord)
{
	var val = -1;
	
	if(coord == "x")
	{
		if(document.all)
			val = e.clientX;
		else
			val = e.pageX;
	}
	else if(coord == "y")
	{
		if(document.all)
			val = e.clientY;
		else
			val = e.pageY;
	}

	return val;
}

function getCoord(caller, e, coord)
{
	var val = -1;
	
	if(coord == "x")
	{
		if(document.all)
			val = e.offsetX;
		else
			val = e.pageX - getPosition(caller, "Left");
	}
	else if(coord == "y")
	{
		if(document.all)
			val = e.offsetY;
		else
			val = e.pageY - getPosition(caller, "Top");
	}

	return val;
}

function mapclick(object,e) 
{   
    // hide possible info box
	alert("x coordinate :");
    hideInfo();

	// fetch coordinates for the action
	var xClick = getCoord(object, e, "x");
	var yClick = getCoord(object, e, "y");
	//alert("x coordinate :" + xClick + "y coordinate :" + yClick);
}
