// ===================================
// General functions in JavaScript
// ===================================


  // Toggles a layer (hide/show)
function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function write_loginpwd_md5() 
{
  document.loginform.password.value = MD5(document.loginform.password.value);
  return true;
}

  // Confirm a redirekt, f.e. Logout
function confirmLink(_message,_link)  
{
  if (confirm(_message))
  {
    window.location.href = _link;
  }
}

  // initialise the tinyMCE editor
function loadTinyMCE()
{
  tinyMCE.init({
	mode : "exact",
	elements : "text_german, text_englisch, text_german_mail, text_englisch_mail, text_german_pdf, text_englisch_pdf",
	theme : "advanced",
	plugins : "safari,table,emotions,advimage,preview,media,searchreplace,contextmenu,paste,fullscreen",
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,sub,sup,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,removeformat,visualaid",
	theme_advanced_buttons4 : "link,unlink,anchor,image,cleanup,|,charmap,emotions,iespell,media,|,fullscreen,code,preview,|,",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "none",
	height : "480",
	width : "385",
	theme_advanced_resizing : true,
	extended_valid_elements : "a[name|href|target|title|onclick|rel],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|style],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
  });  
}

function popupwindow(url,name, height, width)
{
  var newwindow;
	newwindow=window.open(url,'','height='+height+',width='+width+',toolbar=no,menubar=no,scrollbar=no,resizable=no,directories=no,minimize=no,maximize=no');
	if (window.focus == true) 
  {
    newwindow.focus();
  }
}

function beginrefresh()
{
   // if the user is typing a message the limit to the next reload is always 10
  
  if (!document.images) // keine Bilder vorhanden - nur Google Map
    return;
    
  if (secondstogo == 0)
  {
      var imageURL = document.pushtvimage.src + "0";
      if (imageURL.indexOf("000000000000000000000000000000") != -1)
        imageURL = imageURL.substring(0,imageURL.indexOf("000000000000000000000000000000"));
      document.pushtvimage.src = imageURL;
      secondstogo = refreshrate;
  }
  else
  { 
    secondstogo -= 1;
    window.status = secondstogo+" seconds left until image refresh!";
  }  
}

function loadGoogleMap(lat,lon, callsign, username) 
{
  if (GBrowserIsCompatible()) 
  {
    var map = new GMap2(document.getElementById("map"));
    var marker = new GMarker(new GLatLng(lat,lon)); 
    var html= "<b>"+username+"</b><br/>" + callsign;
    map.setCenter(new GLatLng(lat, lon), 5);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(html);    
    map.addControl(new GLargeMapControl());
    map.addControl(new GScaleControl());
    map.addControl(new GMapTypeControl());
  }
}

function loadFlightDetailMap(lat,lon,dep_lat,dep_lon,dest_lat,dest_lon, groundspeed, callsign)
{
	if (GBrowserIsCompatible()) 
	{
	  var map = new GMap2(document.getElementById("map"));
	  var marker = new GLatLng(lat, lon);
	  var dep_marker = new GLatLng(dep_lat, dep_lon);
	  var dest_marker = new GLatLng(dest_lat, dest_lon);
	  
	  var distToDest = marker.distanceFrom(dest_marker);
	  var distFromDep = marker.distanceFrom(dep_marker);
	  
	  var bounds = new GLatLngBounds;
	  bounds.extend(dep_marker);
	  bounds.extend(dest_marker);
	  bounds.extend(marker);
	  
	  map.setCenter(new GLatLng(lat,lon), map.getBoundsZoomLevel(bounds));
	  map.setMapType(G_HYBRID_MAP)
    map.addControl(new GMapTypeControl());
    map.addControl(new GSmallMapControl());
    
    var depIcon = new GIcon(G_DEFAULT_ICON);
    depIcon.image = "design/img/marker/departure.png";
    markerOptions = { icon:depIcon };
	  map.addOverlay(new GMarker(dep_marker, markerOptions));
	   
    var posIcon = new GIcon(G_DEFAULT_ICON);
    posIcon.image = "design/img/marker/airport.png";
    markerOptions = { icon:posIcon }; 
      
    var acmarker = new GMarker(marker, markerOptions);
	  map.addOverlay(acmarker);
	      GEvent.addListener(acmarker, 'click',
		      function()
		      {
		         var htmlString = '';
		         distToDest_  = distToDest/1000/1.852;
		         distFromDep_ = distFromDep/1000/1.852;
		         timeToDest_hr   = Math.floor(distToDest_ / groundspeed);
		         timeToDest_min  = ((distToDest_ / groundspeed)-timeToDest_hr)*60;
		         htmlString += '<b>' + callsign + '</b><br><br>';
		         htmlString += 'Distance to Destination: ' + (distToDest_.toFixed(0)) + ' Nm<br>';
 	           htmlString += 'Distance from Departure: ' + (distFromDep_.toFixed(0)) + ' Nm<br><br>';
 	           htmlString += 'Estimated Time Enroute:  ' + (timeToDest_hr.toFixed(0)) + ':' + (timeToDest_min.toFixed(0)) + ' hrs'; 	           
		         this.openInfoWindowHtml(htmlString);
		      }
	      );

    var destIcon = new GIcon(G_DEFAULT_ICON);
    destIcon.image = "design/img/marker/destination.png";
    markerOptions = { icon:destIcon };
	  map.addOverlay(new GMarker(dest_marker, markerOptions));
    
    var polyOptions = {geodesic:true};
    var desttoplane = new GPolyline([
      new GLatLng(dest_lat, dest_lon),
      new GLatLng(lat, lon)
      ], "#ff0000", 2, 1, polyOptions);
    map.addOverlay(desttoplane);
    
    var deptoplane = new GPolyline([
      new GLatLng(dep_lat, dep_lon),
      new GLatLng(lat, lon)
      ], "#00ff00", 2, 1, polyOptions);
    map.addOverlay(deptoplane);
    
    var tmpPolygon = new GPolyline([
      new GLatLng(dep_lat, dep_lon),
      new GLatLng(dest_lat, dest_lon)
      ], "#000000", 0, 1, polyOptions);
     
   map.setCenter(tmpPolygon.getBounds().getCenter(), map.getBoundsZoomLevel(bounds));
	}
}






 
