//------------------------------------------------------------------
// AJAX-Framework
//------------------------------------------------------------------
var cxi_arr_http_request 											= new Array();

function cxi_make_request(str_index,str_url,str_method,cxi_parameter,cxi_response_function)
  {
  cxi_arr_http_request[str_index][0]								= false;
  
  
  if(!ie)
    {
	str_url															= str_url.replace('./data/','');
	}
  
  
  if(window.XMLHttpRequest)
    {// Mozilla, Safari,...
    cxi_arr_http_request[str_index][0]								= new XMLHttpRequest();
	if(cxi_arr_http_request[str_index][0].overrideMimeType)
	  {
      cxi_arr_http_request[str_index][0].overrideMimeType('text/xml');
      }
    }
  else if(window.ActiveXObject)
    {// IE
	try
	  {
      cxi_arr_http_request[str_index][0]							= new ActiveXObject("Msxml2.XMLHTTP");
      }
	catch(e)
	  {
	  try
	    {
        cxi_arr_http_request[str_index][0]							= new ActiveXObject("Microsoft.XMLHTTP");
        }
	  catch(e)
	    {}
	  }
	}
  
  if(!cxi_arr_http_request[str_index][0])
    {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
    }
  
  //----------------------------------------------------------------
  // response funktion
  //----------------------------------------------------------------
  if(cxi_response_function == '')
    {
	cxi_arr_http_request[str_index][0].onreadystatechange 			= function()
	  {
	  if(cxi_arr_http_request[str_index][0].readyState == 4)
		{
		if(cxi_arr_http_request[str_index][0].status == 200)
		  {
		  cxi_response_default(str_index);
	  	  }
		else if(cxi_arr_http_request[str_index][0].status == 404)
		  {
		  alert('not found');
		  }
		}
	  }
    }
  else
    {
	cxi_arr_http_request[str_index][0].onreadystatechange 			= eval(cxi_response_function);
    }
  str_url															= str_url.replace('index.html/data','/data');
  str_url															= str_url.replace('data/data','data');
  
  cxi_arr_http_request[str_index][0].open(str_method, str_url, true);
  if(str_method == 'POST')
    {
    cxi_arr_http_request[str_index][0].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    cxi_arr_http_request[str_index][0].setRequestHeader("Content-length", cxi_parameter.length);
    cxi_arr_http_request[str_index][0].setRequestHeader("Connection", "close");
    cxi_arr_http_request[str_index][0].send(cxi_parameter);
	}
  else
    {
    cxi_arr_http_request[str_index][0].send(null);
    }
  }
  
  
//------------------------------------------------------------------
// default response funktion
//------------------------------------------------------------------
function cxi_response_default(str_index)
  {
  // 1 ein Objekt?
  if(cxi_arr_http_request[str_index][1] != '')
    {
	cxi_arr_http_request[str_index][1].innerHTML					= cxi_arr_http_request[str_index][0].responseText;
    cxi_arr_http_request[str_index][1].style.display				= 'block';
	}
  else
    {
	//alert('nichts zu tun');
	}
  // 2 weitere funktion vorhanden?
  if(cxi_arr_http_request[str_index][2] != '')
    {
	if(typeof(cxi_arr_http_request[str_index][2]) == 'object')
	  {
	  for(i2=0;i2<cxi_arr_http_request[str_index][2].length;i2++)
	    {
		eval(cxi_arr_http_request[str_index][2][i2]);
		}
	  }
	else
	  {
	  eval(cxi_arr_http_request[str_index][2]);
	  }
	}
  else
    {
	cxi_hide_ladevorgang();
	}
  cxi_arr_http_request[str_index][0]								= false;
  }
  
  
function test_ajax(int_id)
  {
  cxi_arr_http_request['cxi_test']									= new Array();
  cxi_arr_http_request['cxi_test'][0]								= false;
  cxi_arr_http_request['cxi_test'][1]								= '';
  cxi_arr_http_request['cxi_test'][2]								= '';
  
  cxi_make_request('cxi_test',JS_rb+'gpsfile.php?id='+int_id,'GET','','cxi_test_response');
  }
  
var str_response													= null;
  
function cxi_test_response()
  {
  if(cxi_arr_http_request['cxi_test'][0].readyState == 4)
	{
	if(cxi_arr_http_request['cxi_test'][0].status == 200)
	  {
	  str_response													= cxi_arr_http_request['cxi_test'][0].responseText;
	  }
	}
  }


//------------------------------------------------------------------
// screenbreite
//------------------------------------------------------------------
map_width															= 398;
function setScreenType(screentype)
  {
  bclass															= document.body.className;
  /*
  if(bclass.indexOf(' ') > 0)
    {
	bclass															= bclass.replace(/^\w+/,screentype);
	}
  else
    {
	bclass															= screentype + ' ' + bclass;
	}
  document.body.className											= bclass;
  */
  document.body.className											= screentype;
  createCookie("ScreenType", screentype, 365);
  
  map_width															= 398;
  if(screentype == 'wider')
    {
	map_width														= 603;
	}
  
  try
    {
    map.checkResize();
    map39.checkResize();
    map40.checkResize();
    map41.checkResize();
    map49.checkResize();
	}
  catch(e)
    {}
  }
function createCookie(name,value,days)
  {
  if(days) 
    {
    var date														= new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires														= "; expires="+date.toGMTString();
    }
  else
    {
	expires															= "";
	}
  document.cookie													= name+"="+value+expires+"; path=/";
  }


function toggle_hoehenprofil(nr)
  {
  if(document.getElementById('hoehenprofil'+nr).style.display != 'block')
    {
	document.getElementById('hoehenprofil'+nr).style.display		= 'block';
	}
  else
    {
	document.getElementById('hoehenprofil'+nr).style.display		= 'none';
	}
  }
  
  
function get_entfernung(b1,l1,b2,l2)
  {
  breite1															= b1 / 180 * Math.PI ;
  laenge1															= l1 / 180 * Math.PI ;
  breite2															= b2 / 180 * Math.PI ;
  laenge2														 	= l2 / 180 * Math.PI ;
  
  erg																= (Math.acos( Math.sin(breite1)*Math.sin(breite2) + Math.cos(breite1)*Math.cos(breite2)*Math.cos(laenge2-laenge1) )*6378.137);
  return erg;
  }


function get_min_entfernung(lat,lon)
  {
  merke_erg															= 10000000000000;
  for(i=0;i<points2.length;i++)
    {
	merke_erg														= Math.min(merke_erg,get_entfernung(lat,lon,points2[i][0],points2[i][1]));
	}
  return merke_erg;
  }


arr_caches															= new Array();
arr_caches_html														= new Array();
function show_geocaches()
  {
  document.location.href											= '#etappe49';
  document.getElementById('map_loading49').style.display			= 'block';
  
  if(arr_caches.length == 0)
    {
    cxi_arr_http_request['geocaches']								= new Array();
    cxi_arr_http_request['geocaches'][0]								= false;
    cxi_arr_http_request['geocaches'][1]								= '';
    cxi_arr_http_request['geocaches'][2]								= '';
    
    cxi_make_request('geocaches',JS_rb+'media/routen/donauberglandweg/geocaching.xml','GET','','show_geocaches_response');
    }
  }

function show_geocaches_response()
  {
  if(cxi_arr_http_request['geocaches'][0].readyState == 4)
	{
	if(cxi_arr_http_request['geocaches'][0].status == 200)
	  {
	  var icon = new GIcon();
	  icon.image = "http://www.lippachmuehle.de/bilder/ico_map.png";
	  //icon.shadow = "http://www.lippachmuehle.de/media/bilder/marker/panoramio-shadow.png";
	  icon.iconSize = new GSize(12, 20);
	  //icon.shadowSize = new GSize(22, 22);
	  icon.iconAnchor = new GPoint(6, 10);
	  icon.infoWindowAnchor = new GPoint(0, 0);
	  
	  var xml_object												= cxi_arr_http_request['geocaches'][0].responseXML;
	  var cx_modulbaum												= xml_object.getElementsByTagName('loc').item(0);
	  var arr_items													= cx_modulbaum.getElementsByTagName('waypoint');
	  
	  for(i_xml=0;i_xml<arr_items.length;i_xml++)
	    {
		var geocache												= arr_items[i_xml];
		bez															= geocache.getElementsByTagName('name')[0].firstChild.nodeValue;
		nr															= geocache.getElementsByTagName('name')[0].getAttribute('id');
		lat															= geocache.getElementsByTagName('coord')[0].getAttribute('lat');
		lon															= geocache.getElementsByTagName('coord')[0].getAttribute('lon');
		str_link													= geocache.getElementsByTagName('link')[0].firstChild.nodeValue;
		entfernung													= Math.round((get_min_entfernung(lat,lon)*1000),0)+' m';
		
		str															= '<h4>'+nr+'</h4><p>'+bez+'<br />Entfernung: '+entfernung+'</p><p><a href="'+str_link+'" target="_blank">weitere Infos</a></p>';
		map49.addOverlay(createMarker49(new GLatLng(lat, lon),str,icon));
		arr_caches[i_xml]											= nr;
		}
	  document.getElementById('map_loading49').style.display		= 'none';
	  }
	}
  }
  

function createMarker49(point, html, icon)
  { 
  var marker														= new GMarker(point,icon,false);
  GEvent.addListener(marker, "click", function() { 
			marker.openInfoWindowHtml(html); 
			}); 
  return marker;
  }