//Redundant module - Replaced with 'webcraft.com.au/scripts/website.js. 


var map = null;
var geocoder = null;

//GMapType.   G_SATELLITE_MAP   G_NORMAL_MAP   G_HYBRID_MAP

var MType = "N";
var ZoomSize = 13;

var StreetDefault = "--Optional--";
var WebPageThis = "map_street_au_full.htm";
var ZoomDistant = 13;
var ZoomMedium  = 15;
var ZoomClose   = 17;

var State  = "";
var Town   = "";  
var Street = "";
var Lat = 0;
var Lon = 0;

javascript:window.defaultStatus = "Map Search in Australia";


function load(){


  document.FormEnterLocation.TextStreet.value = StreetDefault;




  var URL = "" + window.location;
  
  //alert( "load() - " + URL );    
  
  
  var Zoom = 0; 
  
  var index = URL.indexOf( "?" );    
  var Args = "";
  
  
  if( index > -1 ){           
    
    var Name = "";
    var Value = "";
    
    //alert( "load() ~ " + Args  );
    
    index++;  //Get passed the'?'.    
    Args = URL.substring( index, URL.length );   //1 based.    
    
    var IsDone = false;   

    while( IsDone == false ){
    
      var indexNameStart = 0;
      var indexNameEnd = Args.indexOf( "=" );    
      
      var indexValueStart = indexNameEnd + 1;   //Get passed the '='.
      var indexValueEnd = Args.indexOf( "&" );
     
      if( indexValueEnd == -1 ){
        indexValueEnd = Args.length;
      //  alert( "load() #1 ~ " + Args  );
      }
      
      Name = Args.substring( indexNameStart, indexNameEnd );
      Value = Args.substring( indexValueStart, indexValueEnd );
      
     
      if( Name == "State" ){
        
        //State = Value;
        
        //alert( "'" + Value + "'" );

        State = StripTextFormat( Value );
        
        //alert( "'" + State + "'" );
      }
      else
      if( Name == "Town" ){
        //Town = Value;
        
        Town = StripTextFormat( Value );
        
      }
      else
      if( Name == "Street" ){
        //Street = Value;
        
        Street = StripTextFormat( Value );
        
        if( Street == StreetDefault ) Street = "";

      }
      else
      if( Name == "Lat" ){
        Lat = Value;
      }
      else
      if( Name == "Lon" ){
        Lon = Value;
      }
      else
      if( Name == "Zoom" ){
        Zoom = Value;
        
        //alert( Zoom );
        
      }      
      
      //alert( "load() #2 ~ Name = '" + Name + "' ~ Value = '" + Value + "'" );      

      Args = Args.substring( indexValueEnd, Args.length );
      
      if( Args.length == 0 ){      
        IsDone = true;
      }
      else{
        Args = Args.substring( 1, Args.length )
      }  
    }    
  }
  
  
  
  
  
  if( Zoom != 0 ){
    ZoomSize = parseInt( Zoom );    //Fixed
  }
  else{  

  }  
  
  
  //alert( "load() - Zoom : " + Zoom );  
  //alert( "load() - '" + URL + "'" );
  //alert( "load() ~ " + index + " ~ " + URL.length );
  //alert( "load() - Lat: " + Lat + "   Lon: " + Lon );
  
  if( Lat == 0 || Lon == 0 ){
  
    //Lat = -37.8100;         //Melbourne Aust.
    //Lon = 144.9600;         //    
    //Street = StreetDefault;
    //State = "Victoria";
    //Town = "Melbourne";
    
    Lat = -35.30826;           //Canberra Aust. 'Centre' of parliament house( not same as Address: 'Canberra, ACT').
    Lon = 149.12433;           //    
    Street = StreetDefault;
    State = "ACT";
    Town = "Canberra";
    
    ZoomSize = ZoomMedium;

  } 
  
  if( Street == "" ){
  
    //Street = StreetDefault;
  }
  
  //document.FormEnterLocation.TextStreet.value = Street; 
 
  document.FormEnterLocation.TextTown.value = Town; 
  
    
  for( i=0; i<document.FormEnterLocation.SelectState.options.length; i++ ){  
  
    var TestState = document.FormEnterLocation.SelectState.options[ i ].value;
    if( TestState == State ){
      document.FormEnterLocation.SelectState.selectedIndex = i;
      
      //alert( "load() - State pos : " + i );  //OK!      
    }
  } 


  if( GBrowserIsCompatible() ){
    map = new GMap2(document.getElementById("map"));
    
    map.setCenter(new GLatLng( Lat, Lon ), ZoomSize );   
    //map.setCenter(new GLatLng( -37.8100, 144.9600 ), ZoomSize );  //Melbourne Aust.  
    
    
    GEvent.addListener( map, "dragend", function() {
      //alert("You draged the map.");
      OnDragEnd();
    });

       
    geocoder = new GClientGeocoder();
  }  
  
  
  DisplayLatLon(); 
}



function DisplayStateTown( Street, State, Town ){

  //Street = document.FormEnterLocation.TextStreet.value; 
  //Town = document.FormEnterLocation.TextTown.value; 
 
 
     if( Town == "" ){
     
       if( State == "Victoria" ) Town = "Melbourne";
       else
       if( State == "New South Wales" ) Town = "Sydney";
       else
       if( State == "South Australia" ) Town = "Adelaide";
       else
       if( State == "Queensland" ) Town = "Brisbane";
       else
       if( State == "Northern Territory" ) Town = "Darwin";
       else
       if( State == "Western Australia" ) Town = "Perth";
       else
       if( State == "Tasmania" ) Town = "Hobart";
       else
       if( State == "ACT" ) Town = "Canberra";
       else{
         //State = "Victoria";
         //Town = "Melbourne";
       }  
       document.FormEnterLocation.TextTown.value = Town;  
     } 
     
     ZoomSize = ZoomMedium;
     
     TempStreet = "";
     
     Street = Street;  
     Town = Town; 
     
     //Street = WebPageThis.Street;  
     //Town = WebPageThis.Town; 

     
     if( Street != "" && Street  != StreetDefault ){
       TempStreet = Street + ", ";   
       ZoomSize = ZoomMedium;
     }
     else{     
       ZoomSize = ZoomDistant;
     }
 
     address = TempStreet + Town + ", " + State + " Australia"
 
     if( geocoder ){
        geocoder.getLatLng( address, function(point) {
            if (!point) {
            
              alert( "Address: '" + address + "' Not found");
              
            } 
            else {
              map.setCenter( point, ZoomSize );
              var marker = new GMarker(point);
              map.addOverlay(marker);
              //marker.openInfoWindowHtml(address);
            }
          }
        );        
     }
     
     DisplayLatLon();

 }
 
 
 
 function DisplayZoom( ZoomFactor ){
 
   //alert( ZoomFactor )   
   
   ZoomSize = ZoomFactor; 
   
   map.setZoom( ZoomFactor )   
 }
 
 
 function SetMapType( Type ){
 
   if( Type == "N" ) map.setMapType( G_NORMAL_MAP );
   else
   if( Type == "S" ) map.setMapType( G_SATELLITE_MAP );
   else
   if( Type == "H" ) map.setMapType( G_HYBRID_MAP );
   else   
     map.setMapType( G_NORMAL_MAP );
   
 }
 
 function ToggleMapType(){
 
   if( MType == "N" ){
     map.setMapType( G_SATELLITE_MAP );
     MType = "S"
   }  
   else{
     map.setMapType( G_NORMAL_MAP ); 
     MType = "N"   
   }
   
   //javascript:window.status = 'Toggle \'Map\' and \'Image\' views';
 }
 
 
 
 
 function OpenPlace(){
 
   State  = document.FormEnterLocation.SelectState.options[ document.FormEnterLocation.SelectState.selectedIndex ].value;
   Town   = document.FormEnterLocation.TextTown.value; 
   Street = document.FormEnterLocation.TextStreet.value; 
   
   //var Lat = "123";
   //var Lon = "456";   
   
   var LatLon = map.getCenter();

   Lat = LatLon.lat();
   Lon = LatLon.lng();   
 
   var URL = "map_street_au_full.htm" + "?State=" + State + "&Town=" + Town + "&Street=" + Street + "&Lat=" + Lat + "&Lon=" + Lon + "&Zoom=" + ZoomSize;
 
   //alert( "OpenPlace() - " + URL );
   
   window.location = URL;
 
 }
 
 
 
  function AddMapToFavorites(){

    State  = document.FormEnterLocation.SelectState.options[ document.FormEnterLocation.SelectState.selectedIndex ].value;
    Town   = document.FormEnterLocation.TextTown.value; 
    Street = document.FormEnterLocation.TextStreet.value; 
    var StreetComma = ", ";
    
    var LatLon = map.getCenter();

    Lat = LatLon.lat();
    Lon = LatLon.lng();
    
    Lat = RoundOffNumberPoint( Lat, 4 );
    Lon = RoundOffNumberPoint( Lon, 4 );
    
    if( Street == StreetDefault || Street == "" ){
      Street = "";
      StreetComma = "";
    }  

    var URL = "http://download.com.au/maps/map_street_au_full.htm" + "?State=" + State + "&Town=" + Town + "&Street=" + Street + "&Lat=" + Lat + "&Lon=" + Lon + "&Zoom=" + ZoomSize;
    var PageTitle = "My Map Search - '" + Street + StreetComma + Town + ", " + State + "' Lat " + Lat + " Lon " + Lon; 
       
    javascript:window.external.AddFavorite( URL, PageTitle );

  }
  
  
  function RoundOffNumberPoint( Number, Places ){    
  
    //alert( "Number " + Number );    
    
    var Parts = Math.pow( 10, Places );                    //1=10, 2=100, 3=1000 etc.    
    
    //alert( "Parts " + Parts );    //Never displays !
    
    Number = Math.round( Number * Parts ) / Parts;  
    
    //alert( "Number " + Number );
    
    return Number;  
  }
  
  
  
  
  function DisplayLatLon(){
  
    var LatLon = map.getCenter();

    Lat = LatLon.lat();
    Lon = LatLon.lng();    
    
    Lat = RoundOffNumberPoint( Lat, 4 );
    Lon = RoundOffNumberPoint( Lon, 4 );    
    
    if( document.FormEnterLocation.TextLat != null ){
      document.FormEnterLocation.TextLat.value = Lat;
    }
    if( document.FormEnterLocation.TextLon != null ){  
      document.FormEnterLocation.TextLon.value = Lon;  
    }
  }
  
  
  function OnDragEnd(){
  
    DisplayLatLon();
  
  }
  
  
  function StripTextFormat( String ){
  
    var Pos = 0;
  
    while( String.indexOf( "%20" ) > 0 ){
    
      Pos = String.indexOf( "%20" );        //We don't want this format. 
      var String_1 = String.substring( 0, Pos );
      var String_2 = String.substring( Pos + 3, String.length );
      String = String_1 + " " + String_2;   //We want a space instead !
    }
  
    return String;
  }
  
  
  function SendKML(){
  
  
    var LatLon = map.getCenter();

    Lat = LatLon.lat();
    Lon = LatLon.lng();
    
    Lat = RoundOffNumberPoint( Lat, 4 );
    Lon = RoundOffNumberPoint( Lon, 4 );
    
    var Alt = "500";     //Metres.
    
    var StreetComma = ", ";
    
    if( Street == StreetDefault || Street == "" ){
      Street = "";
      StreetComma = "";
    }  

    
    PlaceMark = Street + StreetComma + Town + ", " + State;
  
  
    var URL = "http://winesales.com.au/test/send_kml.asp" + "?PlaceMark=" + PlaceMark + "&Lat=" + Lat + "&Lon=" + Lon + "&Alt=" + Alt;
    
    alert( URL );
    
    document.URL = URL;
  
  }
  
  
  
  function CreateMarker( point, Caption ){
    
    var marker = new GMarker( point );
    GEvent.addListener( marker, "click", function() {
      marker.openInfoWindowHtml( Caption );
    });
    return marker;
  }

  
  
  function AddMarker( Lat, Lon, Caption ){       
    
    var point = new GLatLng( Lat, Lon );
    
    var Marker = CreateMarker( point, Caption );
    
    map.addOverlay( Marker );
    
    Marker.openInfoWindowHtml( Caption );

  
  }
  
  
  function MoveTo( Lat, Lon ){  
    
    window.setTimeout( function(){
      map.panTo( new GLatLng( Lat, Lon ));
    }, 1000);
    
  }
  
  function AddMarkerMove( Lat, Lon, Caption, Image, URL ){
  
    var ImageTag = "";
  
    if( Image != '' ){
      ImageTag = "<IMAGE src='" + Image + "' WIDTH='100' HEIGHT='100'>";
    }
    
    if( URL != '' ){
    
      var URLString = URL;      
      var Pos = 0;      
      
      Pos = URLString.indexOf( "//" );    //Clear off the 'http://', 'https://', 'ftp://'.
      if( Pos > 0 ){
        Pos += 2;
      }
      else{                               //Not a page or file as above, now test if an E-mail address. 
        if( URL.indexOf( "@" ) > 0 ){
          URL = "mailto:" + URL;
        }      
      }      
      
      if( Pos > 0 ){      
        URLString = URLString.substring( Pos, URLString.length );
      }      
    
      URLTag = "<A TARGET='_blank' HREF='" + URL + "'>"
    }
    
    var HTMLString = "<B>" + Caption +"</B><BR>" + ImageTag + "<BR>" + URLTag + URLString + "</A>";

    AddMarker( Lat, Lon, HTMLString );
    MoveTo( Lat, Lon );
    
    window.setTimeout( function(){  //Need to set timeout as the 'MoveTo' method has one and has not got there yet ! 
      DisplayLatLon();
    }, 1000);
    

  }