var cat = 'USACensusOrte';
var catName = 'Incorporated Places (2008)';
var google_ad_client = "pub-7251478393613561";
var google_ad_channel = "2764227782";
var google_ad_searchchannel = "9414679865";
var years = 10;

function StartPos (name, x,y, zoom) {
  this.name = name;
  this.x = x;
  this.y = y;
  this.zoom = zoom;
}

var starts = new Array (
  new StartPos('USA', -96,40, 4),
  new StartPos('Alaska', -152,65, 5),
  new StartPos('California/Nevada', -117.5,37.5, 6),
  new StartPos('Florida', -82.5,28, 7),
  new StartPos('Georgia/Alabama', -85,33, 7),
  new StartPos('Great Lakes', -87,44, 7),
  new StartPos('Hawaii', -156.5,20.4, 8),
  new StartPos('Lower Mississippi', -91,32.5, 7),
  new StartPos('Midwest (Northwest)', -98,46, 6),
  new StartPos('Midwest (Southeast)', -85,40, 7),
  new StartPos('Midwest (Southwest)', -96.5,37, 6),
  new StartPos('New England', -71,44, 7),
  new StartPos('New York', -76,43, 7),
  new StartPos('Pennsylvania/New Jersey', -76.5,40.7, 7),
  new StartPos('Rocky Mountains (North)', -110.5,45, 6),
  new StartPos('Rocky Mountains (South)', -108.5,37.6, 6),
  new StartPos('Texas/Oklahoma', -100,32, 6),
  new StartPos('Tennessee Valley', -85.5,36.5, 7),
  new StartPos('The Carolinas', -79.7,34.5, 7),
  new StartPos('Virginia/Maryland', -79,38, 7),
  new StartPos('Washington/Oregon', -120,45.5, 7)
)

document.writeln('<script src="genbymap.js" type="text/javascript"></script>');

function setStartCenter() {
  map.setCenter(new GLatLng(40,-96), 4);
}

function getSizeImgName (pop) {
  var img = 'http://www.bymap.org/';
  if (pop >= 5000000)
    img += 'G5000.gif';
  else if (pop >= 1000000)
    img += 'G1000.gif';
  else if (pop >= 500000)
    img += 'G500.gif';
  else if (pop >= 250000)
    img += 'G250.gif';
  else if (pop >= 100000)
    img += 'G100.gif';
  else if (pop >= 50000)
    img += 'G50.gif';
  else if (pop >= 20000)
    img += 'G20.gif';
  else if (pop >= 10000)
    img += 'G10.gif';
  else if (pop >= 5000)
    img += 'G5.gif';
  else if (pop >= 1000)
    img += 'G1.gif';
  else
    img += 'G0.gif';
  return img;
}

function getDensityImgName (density) {
  var img = 'http://www.bymap.org/';
  if (density == 0)
    img += 'D0.gif';
  else if (density >= 8000)
    img += 'D1.gif';
  else if (density >= 4000)
    img += 'D2.gif';
  else if (density >= 2000)
    img += 'D3.gif';
  else if (density >= 1000)
    img += 'D4.gif';
  else if (density >= 500)
    img += 'D5.gif';
  else if (density >= 250)
    img += 'D6.gif';
  else if (density >= 125)
    img += 'D7.gif';
  else 
    img += 'D8.gif';
  return img;
}

function getChangeImgName (incr) {
  var img = 'http://www.bymap.org/';
  if (incr == null)
    img += 'C0.gif';
  else if (incr > 1.5)
    img += 'C1.gif';
  else if (incr > 0.4)
    img += 'C2.gif';
  else if (incr > -0.25)
    img += 'C3.gif';
  else if (incr > -0.75)
    img += 'C4.gif';
  else
    img += 'C5.gif';
  return img;
}

function createCityMarker(point, name, admin, status, pop, date, oldpop, olddate, area, wiki) {
  wiki = decodeURI(wiki);
  var text = '<strong>' + name + '</strong>, ' + status + ', ' + admin;
  text += '<br/><br/>Population: <img src="' + getSizeImgName(pop) + '" width="13px" height="13px" alt=""> ' + pop + ' inhabitants <span style="font-size: 2.3mm;">(' + date + ')</span>';
  var incr = null;
  if (oldpop > 0) {
    text += '<br/>Comparison: <img src="' + getSizeImgName(oldpop) + '" width="13px" height="13px" alt=""> ' + oldpop + ' inhabitants <span style="font-size: 2.3mm;">(' + olddate + ')</span>';
    incr = Math.round(10000.0*(Math.exp(Math.log(pop/oldpop)/years)-1.0)) / 100;
    if (incr >= 0)
      text += '<br/>Population Increase: <img src="' + getChangeImgName(incr) + '" width="13px" height="13px" alt=""> +' + incr + '% per year';
    else
      text += '<br/>Population Decrease: <img src="' + getChangeImgName(incr) + '" width="13px" height="13px" alt=""> ' + incr + '% per year';
  }
  var density = 0;
  if (area > 0) {
    density = Math.round(1000*pop/area)/10;
    text += '<br/>Density: <img src="' + getDensityImgName(density) + '" width="13px" height="13px" alt=""> ' + density + ' pers./sq.km = ' + Math.round(2590*pop/area)/10 + ' pers./sq.mile';
  }
  var searchtext = name;
  if (cat == 'USACensusAdmin2')
    searchtext += ' '+status+' '+admin;
  text += '<br/><br/>Infos: <a href="javascript:showwiki(\'' + wiki + '\')">Wikipedia</a><br/>';
  text += 'Google: <a href="javascript:showgoogle(\'' + searchtext + '\',\'hotels\')">Hotels</a> ';
  text += ' <a href="javascript:showgoogle(\'' + searchtext + '\',\'sights\')">Sights</a> ';
  text += ' <a href="javascript:showgoogle(\'' + searchtext + '\',\'maps\')">Maps</a> ';
  var img = 'http://www.bymap.org/';
  if (display == 'change')
    img = getChangeImgName (incr)
  else if (display == 'density')
    img = getDensityImgName (density);
  else
    img = getSizeImgName (pop);
  return new WMarker(point,name,admin,status,text,img,20,20);
}

function computeWikiCall (m) {
  if (cat == 'USACensusAdmin2')
    return 'http://en.wikipedia.org/wiki/Special:Search?go=Article&search='+m.getAttribute("name")+' '+m.getAttribute("status")+', '+m.getAttribute("adm");
  else
    return 'http://en.wikipedia.org/wiki/Special:Search?go=Article&search='+m.getAttribute("name")+', '+m.getAttribute("adm");
}

function computeZoom (pop) {
  if (cat == 'USACensusAdmin2')
    return 10;
  if (pop > 200000)
    return 10;
  else if (pop > 50000)
    return 11;
  else if (pop > 10000)
    return 12;
  else
    return 13;
}
