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



function buildCustomSearch(){
  /* custom google search using esqsoft template */

  var html = ''
  +'<form style="display:inline;" action="http://www.esqsoft.com/javascript/search-results.htm" id="cse-search-box">'
  +'<div>'
  +'  <input type="hidden" name="cx" value="partner-pub-1157892973816149:hfy1pm-l4em" />'
  +'  <input type="hidden" name="cof" value="FORID:11" />'
  +'  <input type="hidden" name="ie" value="ISO-8859-1" />'
  +'  <input type="text" name="q" size="33" />'
  +'  <input type="submit" name="sa" value="Search" />'
  +'</div>'
  +'</form>'
  +'<sc'+'ript type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></sc'+'ript>'

  return html
}



function buildDomainsWidget(){
  var html = ''
    +'<div id="domain_search" style="margin:0; padding:0;">'
    +'<form style="margin:0; padding:0;" method="post" action="https://www.securepaynet.net/gdshop/registrar/search.asp?se=%2B&prog%5Fid=hosting411&from=rhp" id="form1" name="form1" target="_blank"> '
    +'<table width="500" cellspacing="0" cellpadding="5" style="border:1px #eee solid; background:#cdd; white-space:nowrap;">'
    +'  <tr>'
    +'    <td style="color:#000; border-right:1px #000 dotted; font-weight:bold;">'
    +'      DOMAIN NAME&nbsp;'
    +'    </td><td width="100%" style="color:#000; padding-left:10px;">'
    +'      <input type="button" value="Price/Availability &raquo;" onclick="this.form.action=\'https://www.securepaynet.net/gdshop/registrar/search.asp?se=%2B&prog%5Fid=hosting411&from=rhp\'; this.form.submit();"> '
    +'    </td>'
    +'    <td style="color:#000; border-right:1px #000 dotted; font-weight:bold;">'
    +'      HOSTING&nbsp;'
    +'    </td><td width="100%" style="color:#000; padding-left:10px;">'
    +'      <input type="button" value="Check &raquo;" onclick="this.form.action=\'http://www.securepaynet.net/hosting/web-hosting2.aspx?ci=1782&prog_id=hosting411\'; this.form.submit();"> '
    +'    </td>'
    +'</tr></table>'
    +'</form>'
    +'</div>'
  
  return (html)
}


function buildHeaderHTML(){
  var html = ''
  +'<div class="search">'
  +'  <div style="width:50%;position:absolute;left:10px;" id="slot2">'
  +   buildDomainsWidget()
  +'  </div>'
  +'  <div style="width:300px;position:absolute;right:10px;top:10px;" id="slot1">'
  +   buildCustomSearch()
  +'  </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

}



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" */
  }

}



window.header = function(){
  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();
}


