var efhList = new Array();

function efhcounter(listing,result)
{
  if (typeof result == 'object' || typeof result == "function")
  {
    var listnum = "";
    try
    {
      listnum=result[0].getElementsByTagName('listnum')[0].firstChild.data;
    }
    catch(err)
    {
    }
    //var mls = result[0].getElementsByTagName('mls')[0].firstChild.data;
    //var mls = result[0].getElementsByTagName('listagent')[0].firstChild.data;
    //var vtid = result[0].getElementsByTagName('vtid')[0].firstChild.data;
    //var error = result[0].getElementsByTagName('error')[0].firstChild.data;
  }
  else
  {
    if (listing)
    {
      url = '/efhcounter.asw?listnum=' + listing['listnum'] + '&mls=' + listing['mls'] + '&listagent=' + listing['listagent'];
      //alert(url);
      counter = new AjaxLoader(url);
      counter.load();
    }
  }
}

function initFeaturedHome()
{
  // rearrange feathomes to random order
  var numHomes = feathomes.length - 1;
  var found = 0;
  var cnt = 0;
  feathomes[0] = '';
  
  var temp = ""
  
  feathomes.sort(function() {return 0.5 - Math.random()}) //Array elements now scrambled
  
  for (var i = 0; i < feathomes.length; i++)
  {
    if (isNaN(feathomes[i]['index']))
    {
      feathomes.splice(i,1)
    }
    else if (feathomes[i]['index'] == 0)
    {
      var tmp = new Array();
      tmp = feathomes[i]
      feathomes[i] = feathomes[0]
      feathomes[0] = tmp
    }
  }

  for (var i = 1; i < feathomes.length - 1; i++)
  {
    if (feathomes[i]['index'] == 0)
      continue;
    if (isNaN(feathomes[i]['index']))
      continue;
    efhList[i] = i;  
    id = 'fhInfoDivInner_' + efhList[i];
  }
  
  populateFeaturedHomes();
}

function populateFeaturedHomes()
{
  // the ids for our elements follow the form efh + x (efh1, efh2, etc)
  if ((start + fhDisplayNum) > efhList.length)
    start = efhList.length - fhDisplayNum + 1;
  
  if (start < 1)
    start = 1;

  var pos = start;
  
  for (var i = 1;i <= fhDisplayNum; i++)
  {
    var id = "efh" + i;
    var elem = document.getElementById(id)
    elem.innerHTML = makeEFHHtml(pos)
    pos++;
  }

  // I need to figure out if the 1st image displayed
  // is the 1st image in the list
  var fhlast = document.getElementById('fhlast');
  if (start ==1 )
  {
    // turn off the left arrow
    fhlast.style.display = 'none'
  }
  else
  {
    // make sure it's on
    fhlast.style.display = 'inline'
  }
  
  // I need to figure out if the last image displayed
  // is the last image in the list
  var fhnext = document.getElementById('fhnext');
  if (start == (efhList.length - fhDisplayNum + 1))
  {
    // turn off the right arrow
    fhnext.style.display = 'none'
  }
  else
  {
    // make sure it's on
    fhnext.style.display = 'inline'
  }
  
  start += fhDisplayNum
  
}

function changeFeaturedHomes(dir)
{
  if (dir == "prev")
    start = start - (fhDisplayNum * 2);

  populateFeaturedHomes();
}

