//alert(location.pathname+'\n'+location.host+'\n'+location.href);
var relpath = (location.host?'/':location.pathname.match(/.+websites\/[^\/]+\//))


function buildHeaderHTML(){
  var html = ''
  +'<div class="search">'
  +'<div style="width:50%;position:absolute;left:10px;" id="slot1">'
  +'</div>'
  +'<div style="width:50%;position:absolute;right:10px;" id="slot2">'
  +'</div>'
  +'</div>'

  +'<div style="width:160px;height:600px;float:right;margin:20px;">'
  +'<iframe id="ab8ac9ef" name="ab8ac9ef" src="http://hostagnostic.com/openads/adframe.php?n=ab8ac9ef&amp;clientid=7" framespacing="0" frameborder="no" scrolling="no" width="160" height="600" allowtransparency="true"><a href="http://hostagnostic.com/openads/adclick.php?n=ab8ac9ef" target="_blank"><img src="http://hostagnostic.com/openads/adview.php?clientid=7&amp;n=ab8ac9ef" border="0" alt=""></a></iframe>'
  +'</div>'

  +'<div style="width:300px; height:250px; float:right; margin:20px; ">'
  +'<iframe id="a55f33a4" name="a55f33a4" src="http://hostagnostic.com/openads/adframe.php?n=a55f33a4&amp;clientid=8" framespacing="0" frameborder="no" scrolling="no" width="300" height="250" allowtransparency="true"><a href="http://hostagnostic.com/openads/adclick.php?n=a55f33a4" target="_blank"><img src="http://hostagnostic.com/openads/adview.php?clientid=8&amp;n=a55f33a4" border="0" alt=""></a></iframe>'
  +'</div>'

  return html

}


window.header = function(){
  /* start by including libraries inline */
  document.write('<sc'+'ript src="'+relpath+'lib/buildCustomSearch.js"></sc'+'ript>')
  document.write('<sc'+'ript src="'+relpath+'lib/buildDomainsWidget.js"></sc'+'ript>')

  document.write(buildHeaderHTML());
}


window.onload = function(){
  /* push remote loaded functionality into the corresponding containers */
  var obj = document.getElementById('slot1');
  var span = document.createElement('span');
  span.innerHTML = buildCustomSearch();
  obj.appendChild(span);

  var obj = document.getElementById('slot2');
  var span = document.createElement('span');
  span.innerHTML = buildDomainsWidget();
  obj.appendChild(span);

  if(!location.host) fixDocroot();
}


function fixDocroot(){
  /*  try to normalize links when running in dev mode w/o a web server 
      1. we need to prefix links with their filepath on the file system
      2. we can't assume "index.htm" will be auto-derived from "/"
  */

  if(location.host) return; /* don't do this when we're running with a web server, it'll resolve docroot correctly */

  var mypath = location.href.match(/.*\.(info|com|ws|net)\//)[0].replace(/\/$/,'');

  var arr = document.getElementsByTagName('a');
  for(var i=0; i<arr.length; i++){
    arr[i].href = arr[i].href.replace(/^file:\/\//,mypath);   /* change link hrefs from file:///x.htm to file://localpath/x.htm */
    if(arr[i].href == mypath+'/') arr[i].href += 'index.htm'; /* change link hrefs from "/" to "/index.htm" */
  }

}