var points = new Array( 14 );
var dists = new Array( 64000, 32000, 16000, 8000, 4000, 2000, 1000, 400, 200, 100, 50, 20, 10, 4 );
var cid;
var SID;
var dist;

function init() {
  for ( var i = 0; i < points.length; ++i ) points[i] = new Array();
  
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  map.addControl(new GOverviewMapControl()); 

  icon = new GIcon();
  icon.image = "http://geourl.divnull.net/style/mm_20_red.png";
  icon.iconSize = new GSize(12, 20);
  icon.shadowSize = new GSize(22, 20);
  icon.iconAnchor = new GPoint(6, 20);
  iconblue = new GIcon(icon,"http://geourl.divnull.net/style/mm_20_blue.png");
}

function webSiteData( number ) {
  var res = map.getZoom()-4;
  if ( res < 0 ) res = 0;
  var rootNode = document.getElementById("geourlList");
  while( rootNode.childNodes.length > 0 ) {
    rootNode.removeChild( rootNode.firstChild );
  }
  var loadingDiv = document.createElement( "div" );
  var loadingStyle =document.createAttribute( "style" );
  loadingStyle.nodeValue="text-align: center";
  loadingDiv.setAttributeNode( loadingStyle );
  var loadingImg = document.createElement( "img" );
  loadingImg.src = "http://geourl.divnull.net/style/loading.gif";
  loadingImg.alt = "";
  loadingDiv.appendChild( loadingImg );  
  document.getElementById("geourlList").appendChild( loadingDiv );  
  GDownloadUrl("website.php?id="+number+"&d="+dists[res]/1000 +"&cid="+cid+"&dist="+dist+"&"+SID, function(data, responseCode) {
    var xmlRoot = GXml.parse(data).documentElement;
    rootNode.removeChild( rootNode.firstChild );
    rootNode.appendChild( parseNode( xmlRoot ) );
  });
}

function parseNode( xmlNode ) {
  var node;
  var aNode;
  if ( xmlNode.nodeType == 1 ) {
    node = document.createElement( xmlNode.nodeName );
    for ( var i=0; i < xmlNode.attributes.length; ++i ) {
      aNode = document.createAttribute( xmlNode.attributes[i].nodeName );
      aNode.nodeValue = xmlNode.attributes[i].nodeValue;
      node.setAttributeNode( aNode );
    }
    for ( var i=0; i < xmlNode.childNodes.length; ++i ) {
      node.appendChild( parseNode( xmlNode.childNodes[i]) );
    }
  } else if ( xmlNode.nodeType == 3 ) {
    node = document.createTextNode( xmlNode.nodeValue );
  }
  
  return node;
}

function createMarker( point, number, manager ) {
  var min = checkPoint( point );
  if ( min < 18 ) {
    var marker = new GMarker( point, icon );
    GEvent.addListener(marker, "click", function() {
      webSiteData( number );
    });
    manager.addMarker(marker,min,17);
  }
}

function checkPoint( point ) {
  var cont = false;
  var i=0;
  var j=0;
  var k=0;
  for ( i = 0; i < points.length; ++i ) {
    if ( cont ) {
      points[i-1].push( point );
      if ( i == 1 ) return 0;
      else return i+3;
    }
    cont = true;
    for ( k = 0; k < i+1; ++k ) {
      for ( j = 0; j < points[k].length && cont; ++j ) {
        if ( points[k][j].distanceFrom(point) < dists[i] ) {
          cont = false;
        }
      }
    }
  }
  return 18;
}

function clear() {
  delete points;
  delete dists;
}

function Go (select) {
  var val = select.options[select.options.selectedIndex].value;
  window.location = "http://geourl.divnull.net?uid=" + cid + "&dist=" + val;
}

function sh( id ) {
  if ( document.getElementById( ""+id ).style.display == "none" )
    document.getElementById( ""+id ).style.display = "";
  else
    document.getElementById( ""+id ).style.display = "none";
}
