$(document).ready(initialize);

  var geocoder;
  var map;
  var marker;
  var satLine = new Array();
  var satList = new Array();
  var satNameList = new Array();
  function buildURL() {
      $('#urlbox').val(document.location.hostname + location.pathname + "?ll=" + marker.getPosition().toUrlValue(12) + "&sl=" + satList.join(",") + "&z=" + map.getZoom());
  }
  function drawDirection() {
      if(satList.length == 0) return;
      for(i = 0; i < satLine.length; i++) satLine[i].setMap(null);
      for(i = 0; i < satList.length; i++)
      {
        satLine[i] = new google.maps.Polyline({
          path: [marker.getPosition(), new google.maps.LatLng(0, satList[i])],
          geodesic: true,
          strokeColor: "#FF0000",
          strokeOpacity: 1.0,
          strokeWeight: 2,
//          title: "test"
        });
        satLine[i].setMap(map);
//        var str = satNameList[i];
//        google.maps.event.addListener(satLine[i], 'mouseover', function() {alert(str);});
      }
      buildURL();
    }
  function initialize() {
    var v = getUrlVars();
    geocoder = new google.maps.Geocoder();
    var SatCoord = new google.maps.LatLng(50.4495, 30.5253);
    var markerCoord;
    var ll;
    var zooml = 11;
    if(v['ll'] !== undefined) ll = v['ll'].split(',');
    if(ll !== undefined && ll.length >= 2) markerCoord = new google.maps.LatLng(ll[0], ll[1]);
    else markerCoord = new google.maps.LatLng(50.4495, 30.5253);
    var zl = v['z'];
    if(zl !== undefined) zooml = parseInt(zl);
    var mapOptions = {
      zoom: zooml,
      center: markerCoord,
      mapTypeId: google.maps.MapTypeId.HYBRID
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    var image = '..img/logo_sat.png';
    var SatMarker = new google.maps.Marker({
      map:map,
      icon: image,
      position: SatCoord,
      title: "satelit.com.ua"
    });

    marker = new google.maps.Marker({
      map:map,
      draggable:true,
      position: markerCoord,

    });

    if(v['sl'] !== undefined)
    {
      satList = new Array();
      sl = v['sl'].split(',');
      for(i = 0; i < sl.length; i++)
      {
        satList.push(sl[i]);
      }
      if(satList.length > 0) drawDirection();
    }

/*    var circle = new google.maps.Circle({
      map: map,
      radius: 10
    });
    circle.bindTo('center', marker, 'position');*/
    google.maps.event.addListener(marker, "dragend", function() {
      marker.title = marker.getPosition().lat() + "\n" + marker.getPosition().lng();
      drawDirection();
      });

    google.maps.event.addListener(map, 'click', function(event) {
      marker.setPosition(event.latLng);
      drawDirection();
    });
    google.maps.event.addListener(map, 'zoom_changed', function(event) {
      buildURL();
    });

    $("#satlist").change(satlistUpdated);
    buildURL();
  }
 
  function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        marker.setPosition(results[0].geometry.location);
        drawDirection();
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
  function satlistUpdated() {
    satList = new Array();
    satNameList = new Array();
    $("#satlist option:selected").each(function () {
        satList.push($(this).val());
        satNameList.push($(this).text());
        });
    drawDirection();
    };

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
 
    return vars;
}
