function TBF_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.TBF_p) d.TBF_p=new Array();
    var i,j=d.TBF_p.length,a=TBF_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.TBF_p[j]=new Image; d.TBF_p[j++].src=a[i];}}
}

function TBF_swapImgRestore() { //v3.0
  var i,x,a=document.TBF_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function TBF_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=TBF_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function TBF_swapImage() { //v3.0
  var i,j=0,x,a=TBF_swapImage.arguments; document.TBF_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=TBF_findObj(a[i]))!=null){document.TBF_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function toggleProvinceBox(land) {
  var el = $('provincie-info');
  var img = $('province_flag');
  if (!land) {
    TBF_swapImgRestore();
    el.className = 'hidden';
  } else {
    TBF_swapImage('trans','','img/kaarten/'+land.toLowerCase().replace('-','')+'.gif',0);
    img.alt = land;
    img.src = 'img/vlaggen/'+ land.toLowerCase() +'.jpg';
    $('province_name').innerHTML = land;
    $('S_count').innerHTML = (typeof provinciesData[land] != 'undefined') ? provinciesData[land].S : 0;
    $('L_count').innerHTML = (typeof provinciesData[land] != 'undefined') ? provinciesData[land].L : 0;
    el.className = 'visible';
  }
}

var shifted = false;

function calcPos(form) {
  for (var i=0; i<form.elements.length; i++) {
    if (form.elements[i].type!='hidden')
      continue;
    form.elements[i].value = i+1;
    //console.log(form.elements[i]);
  }
  return true;
}

function loadGmap(lat, lng) {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("googlemap"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    var placer = new GLatLng(lat,lng);
    map.setCenter(placer, 16);
    var arrowIcon = new GIcon();
    arrowIcon.image = "img/gm_arrow.png";
    arrowIcon.shadow = "img/gm_arrow_shadow.png";
    arrowIcon.iconSize = new GSize(39,34);
    arrowIcon.shadowSize = new GSize(39,34);
    arrowIcon.iconAnchor = new GPoint(8,32);
    markerOptions = { icon:arrowIcon };
    map.addOverlay(new GMarker(placer, markerOptions));
  }
}

function moveRow(el, dir) {
  var nn, tbl, nnode, cls;
  while (el && el.tagName != 'TR')
    el = el.parentNode;
  if (el.tagName != 'TR')
    return;
  tbl = el.parentNode;
  cls = el.className;
  nnode = (dir == 'up') ? 'previousSibling' : 'nextSibling';
  nn = el[nnode];
  while (nn && nn.tagName != 'TR') {
    if (nn.nodeType == 8)
      return;
    nn = nn[nnode];
  }
  el.className = nn.className;
  nn.className = cls;
  if (dir!='up')
    nn = nn.nextSibling;
  tbl.insertBefore(el, nn);
  if (!shifted) {
    document.getElementById('saveButton').disabled = false;
    shifted = true;
  }
}

if (window.addEvent) {
  window.addEvent('domready', function() {
    if (typeof initGmap == 'function')
      initGmap();
  });
}