// functions to show/hide layers 
function show(id) {
   if (ns) document.layers[id].visibility = "show"
   if (ie) document.all[id].style.visibility = "visible"
}
function hide(id) {
   if (ns) document.layers[id].visibility = "hide"
   if (ie) document.all[id].style.visibility = "hidden"
}

// functions to preload and change images
function preload(imgObj,imgSrc) {
	if (document.images) {
		eval(imgObj+' = new Image()')
		eval(imgObj+'.src = "'+imgSrc+'"')
	}
}
function changeImage(layer,imgName,imgObj) { 
	if (document.images) {
		if (document.layers && layer!=null) eval('document.'+layer+'.document.images["'+imgName+'"].src = '+imgObj+'.src')
		else document.images[imgName].src = eval(imgObj+".src")
	}
}
function doPreload() {
	for (i=0; i < 21; i++) { 
		preload('d_plane' + i + '_left',strRoot + '/images/destinations/direct_plane_left.gif')
		preload('d_plane' + i + '_right',strRoot + '/images/destinations/direct_plane_right.gif')
		preload('i_plane' + i + '_left',strRoot + '/images/destinations/indirect_plane_left.gif')
		preload('i_plane' + i + '_right',strRoot + '/images/destinations/indirect_plane_right.gif')
	}	
}

// this is called when user clicks an airport
function fly(which,where) {  	
   if (flying==false) { 
   
   flying = true;
   
   // hide highlight images
   for (i=0; i<airportArr.length; i++) { 
        thisAirport = eval(airportArr[i] + '.name') + 'd'      
        hide(thisAirport)
        thisAirport = eval(airportArr[i] + '.name') + 'i'      
        hide(thisAirport)
        thisAirport = eval(airportArr[i] + '.name') + 'id'      
        hide(thisAirport)
   }

   // show highlight image
   clickedAirport = airportArr[which].name + 'id'      
   
   // get airport object properties
   whichAirportX = airportArr[which].x;
   whichAirportY = airportArr[which].y; 
   whichAirportDirect = airportArr[which].direct; 
   whichAirportIndirect = airportArr[which].indirect; 
    
   // create arrays containing direct and indirect airports
   directArr = new Array()
   indirectArr = new Array()
   if(whichAirportDirect!='') directArr = whichAirportDirect.split(',')
   if(whichAirportIndirect!='') indirectArr = whichAirportIndirect.split(',')

   if ((directArr.length==0) && (indirectArr.length==0)) flying = false
   else show(clickedAirport)
      
   // if no direct destinations
   if(directArr.length!=0){
   
      // for each direct destination
      for (j=0; j < directArr.length; j++) { 
   
         // which plane to use
         whichPlane = 'Dplane' + j;
                  
	 // switch image depending on direction plane will fly in
	 if (where == "airport") changeImage('Dplane' + j + 'Div','direct_plane' + j,'d_plane' + j + '_right')
	 else changeImage('Dplane' + j + 'Div','direct_plane' + j,'d_plane' + j + '_left')
	    
         // move plane to x,y and make visible
         eval(whichPlane + '.moveTo(whichAirportX,whichAirportY)');
         eval(whichPlane + '.css.visibility = "visible"');
                    
         // get airport x & y values from object properties 
         directx = eval(directArr[j] + '.x') + 2; 
         directy = eval(directArr[j] + '.y') + 4; 
        
         // fly plane to x and y of destination airport - call arrived function on last plane
         if (j == directArr.length-1) eval(whichPlane + '.glideTo("slow","slow",""+directx+"",""+directy+"",3,22,"directArrived()")');    
         else eval(whichPlane + '.glideTo("slow","slow",""+directx+"",""+directy+"",3,22)');
      } // end for loop
   // end if no direct destinations   
   } 
   
   // if no indirect destinations
   if(indirectArr.length!=0){			
   
      // for each indirect destination
      for (j=0; j < indirectArr.length; j++) { 
   
         // which plane to use
         whichPlane = 'plane' + j;

	 // switch image depending on direction plane will fly in
	 if (where == "airport") changeImage('plane' + j + 'Div','indirect_plane' + j,'i_plane' + j + '_right')
	 else changeImage('plane' + j + 'Div','indirect_plane' + j,'i_plane' + j + '_left')
	 
         // move plane to x,y and make visible
         eval(whichPlane + '.moveTo(whichAirportX,whichAirportY)');
         eval(whichPlane + '.css.visibility = "visible"');
                    
         // get airport x & y values from object properties 
         indirectx = eval(indirectArr[j] + '.x') + 2; 
         indirecty = eval(indirectArr[j] + '.y') + 4; 
            
         // fly plane to x and y of destination airport - call arrived function on last plane
         if (j == indirectArr.length-1) eval(whichPlane + '.glideTo("slow","slow",""+indirectx+"",""+indirecty+"",3,22,"indirectArrived()")');    
         else eval(whichPlane + '.glideTo("slow","slow",""+indirectx+"",""+indirecty+"",3,22)');
      } // end for loop

   // end if no indirect destinations
   }

   } // end if 
}

function directArrived() {  
  // hide direct planes
  for (k=j+1; k>=0; k--) { 	
     kPlane = 'Dplane' + k;   
     eval(kPlane + '.css.visibility = "hidden"');
  }
  
  // show highlight images for destinations
  for (i=0; i<directArr.length; i++) { 
     thisAirport = eval(directArr[i] + '.name') + 'd'      
     show(thisAirport)
  }
  flying=false;
}

function indirectArrived() {  
  // hide indirect planes
  for (k=j; k>=0; k--) { 		
     kPlane = 'plane' + k; 	
     eval(kPlane + '.css.visibility = "hidden"');
  }
  
  // show highlight images for destinations
  for (i=0; i<indirectArr.length; i++) { 
     thisAirport = eval(indirectArr[i] + '.name') + 'i'  
     show(thisAirport)
  }
  flying=false;
}