//Global array for the home page banner images.
var hpbimgs = new Array();
var hpbcurrentimg = 0;
var hpbcurrenttxt = 0;
var hpnewsvisible = 2;
var hpeventsvisible = 2;
var hptimgs = new Array();

function setEventsVisible()
{
  var item = document.getElementById('hpeventsitem2');
  if(!item)
  {
    item = document.getElementById('hpeventsitem1');
    if(!item)
      hpeventsvisible = 0;
    else
      hpeventsvisible = 1;
  }
}

function setNewsVisible()
{
  var item = document.getElementById('hpnewsitem2');
  if(!item)
  {
    item = document.getElementById('hpnewsitem1');
    if(!item)
      hpnewsvisible = 0;
    else
      hpnewsvisible = 1;
  }
}

function addCNitem(idroot)
{
  if(hpnewsvisible==5)
    return false;
  else
    hpnewsvisible++;
    
  var check = addhpitem(idroot+hpnewsvisible);
  
  if(check == false)
    hpnewsvisible--;
}

function addEVitem(idroot)
{
  if(hpeventsvisible==5)
    return false;
  else
    hpeventsvisible++;
    
  var check = addhpitem(idroot+hpeventsvisible);
  
  if(check == false)
    hpeventsvisible--;
}

function addhpitem(id)
{
  var item = document.getElementById(id);
  if(item != null)
  {
    item.style.display='block';
    return true;
  }
  return false;
}

function removeCNitem(idroot)
{
  if(hpnewsvisible==0)
    return false;
  
  var check = removehpitem(idroot+hpnewsvisible);
  
  if(check == true)
    hpnewsvisible--;
}

function removeEVitem(idroot)
{  
  if(hpeventsvisible==0)
    return false;
  
  var check = removehpitem(idroot+hpeventsvisible);
  
  if(check == true)
    hpeventsvisible--;
}

function removehpitem(id)
{
  var item = document.getElementById(id);
  if(item != null)
  {
    item.style.display='none';
    return true;
  }
  return false;
}

function restore(id)
{
  var e = document.getElementById(id);
  if(e)
  {
    e.style.display='';
  }
}

function showtestim(e, id, total)
{
  var n = document.getElementById('hptestnext');
  var p = document.getElementById('hptestprev');

  if(id==total)
    n.src = 'tpls/images/tEC/big_R_gray_arrow.gif';
  else
  {
    n.src = 'tpls/images/tEC/big_R_blue_arrow.gif';
    n.parentNode.href='?testimonial='+(id+1);
  }
    
  if(id==1)
    p.src = 'tpls/images/tEC/big_L_gray_arrow.gif';
  else
  {
    p.src = 'tpls/images/tEC/big_L_blue_arrow.gif';
    p.parentNode.href='?testimonial='+(id-1);
  }
  
  for(var i=1;i<=total;i++)
  {
    if(i==id)
    {
      document.getElementById('hptestimonial'+id).style.display = 'block';
      document.getElementById('hptest'+id).src = 'tpls/images/tEC/big_blue_circle.gif';
      document.getElementById('hptest'+id).alt = 'testimonial '+id+' selected';
    }
    else
    {
      document.getElementById('hptestimonial'+i).style.display = 'none';
      document.getElementById('hptest'+i).src = 'tpls/images/tEC/small_gray_circle.gif';
      document.getElementById('hptest'+i).alt = 'testimonial '+i+' unselected';
    }
  }
}

function addHPBImage(img)
{
  hpbimgs[hpbimgs.length]=img;
}

function populateImageNodeFromStr(e, imgstr)
{
  if( imgstr != null && e != null )
  {
    var t = imgstr.match(/ src="([^"]+)"/);
    if( t != null )
    {
      e.src = t[1];
      var w = imgstr.match(/ width="([^"]+)"/);
      if( w != null && w[1] != null )
        e.style.width = w[1]+'px';
      var h = imgstr.match(/ height="([^"]+)"/);
      if( h != null && h[1] != null )
        e.style.height = h[1]+'px';
      var alt = imgstr.match(/ alt="([^"]+)"/);
      if( alt != null && alt[1] != null )
        e.alt = alt[1];
    }
  }
  
  return e;
}

function getCurrentHPBImageNode()
{
  var tr = document.getElementById('hpb0');
  return tr.getElementsByTagName('IMG')[0];
}

function initHomepage()
{
  setEventsVisible();
  setNewsVisible();
  hpbtimeout = window.setTimeout('showHPB()',8000);
  if(typeof(initHptNavigation) == 'function')
  {
    initHptNavigation('work');
    initHptNavigation('test');
  }
}

function showHPB()
{
  var num = (hpbcurrentimg+1)%3;
  
  var e1 = getCurrentHPBImageNode()
      e1.id = 'hpbimg1';
      e1.style.zIndex = 3;
  var imgloc = e1.parentNode;
      imgloc.style.position = 'relative';
  var e2 = document.getElementById('hpbimg2');
  if( e2 == null )
  {
    // Create element
    e2 = document.createElement('IMG');
    e2.style.display = 'none';
    e2.id = 'hpbimg2';
    e2.border="0";
    imgloc.insertBefore(e2,e1);
  }
  
  populateImageNodeFromStr(e2, hpbimgs[num]);
    
  swapActiveHPBText(hpbcurrenttxt, num);
  hpbcurrenttxt = num;
  
  fadetimeout = window.setTimeout('fade(0)',100);
}

function swapActiveHPBText(curr, next)
{
  var arr = document.getElementById('hpb'+curr).getElementsByTagName('TD')[0].getElementsByTagName('DIV')[0];
  var tar = document.getElementById('hpb'+next).getElementsByTagName('TD')[0].getElementsByTagName('DIV')[0];
  
  var arr_arrow = arr.getElementsByTagName('DIV')[0];
  var tar_arrow = tar.getElementsByTagName('DIV')[0];
  
  arr.className = 'hpbheadnosel';
  arr_arrow.style.display = 'none';
  
  tar.className = 'hpbheadsel';
  tar_arrow.style.display = 'inline';
}

function fade(n)
{
  var faded = false;
  // Looks at slogan toggle. This will determine which slogan to use depending on which is currently visible.
  // If image has loaded, starts fading down imgsloganimg and fading up imgsloganimg2
  // Same for the slogans
  
  // Once image fully loaded then start fading
  var e1 = null;
  var e2 = null;
  
  e1 = document.getElementById('hpbimg1');
  e2 = document.getElementById('hpbimg2');
  
  // Fade between e1 and e2. Check that e2 image has completed loading
  if( e2.complete == true )
  {
    // Whilst fading, use absolute positioning for the item being faded up
    e2.style.position = 'absolute';
    e2.style.left = '0px';
    e2.style.top  = '0px';
    e2.style.zIndex = 4;
    e2.style.display = 'block';
    e2.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity='+n+');';
    e2.style.opacity = n/100;
    
    if( n < 100 )
      n += 4;
    else
      faded = true;
  }
  
  if( faded == true )
  {
    // Set to be static
    e1.parentNode.removeChild(e1);
    e2.style.position = 'static';

    hpbcurrentimg = (hpbcurrentimg+1)%3;
    hpbtimeout = window.setTimeout('showHPB()',5500);
  }
  else
  {
    fadetimeout = window.setTimeout('fade('+n+')',100);
  }	
}

function stopHPBChange(num)
{
  if(typeof hpbtimeout == "number")
    window.clearTimeout(hpbtimeout);
  if(typeof fadetimeout == "number")
    window.clearTimeout(fadetimeout);
  
  var e1 = null;
  var e2 = null;
  
  swapActiveHPBText(hpbcurrenttxt, num);
  hpbcurrenttxt = num;
  
  e1 = document.getElementById('hpbimg1');
  e2 = document.getElementById('hpbimg2');
  if(e2 == null)
    e2 = getCurrentHPBImageNode();
    
  e2 = populateImageNodeFromStr(e2, hpbimgs[num]);
  e2.style.opacity = 1;
  e2.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=100);';
  if(e1 != null)
    e1.parentNode.removeChild(e1);
  e2.style.position = 'static';
  
  hpbcurrentimg = num;
}

function startHPBChange()
{
  hpbtimeout = window.setTimeout('showHPB()',5500);
}


function getTotalTestim()
{
  var n;
  for(var i=1;i<=10;i++)
  {
    var e = document.getElementById('hptest'+i);
    if(e!=null && typeof(e) != 'undefined')
    {
      if(e.firstChild != null && typeof(e.firstChild) != 'undefined')
        n = i;
    }
  }
  return n;
}

function getTotalWork()
{
  var n;
  var i = 1;
  while(i<1000)
  {
    var e = document.getElementById('hpwork'+i);
    if(e!=null && typeof(e) != 'undefined')
      n = i;
    else
      return n;
    i++;
  }
  return n;
}

function hpNavigate(id, e, d)
{
  var reg;
  var total;
  if(id=='work')
  {
    reg = new RegExp(/(wtype=(?:\w{0,2})?&work=)(\d+)/);
    total = getTotalWork();
  }
  else if(id=='test')
  {
    reg = new RegExp(/(testimonial=)(\d+)/);
    total = getTotalTestim();
  }
  
  var t = e.href.match(reg);
  
  var toshow = parseInt(t[2]);
  var tohide = toshow+(d=='n'?-1:1);
  
  var e1 = document.getElementById('hp'+id+toshow);
  var e2 = document.getElementById('hp'+id+tohide);
  
  e1.style.display='block';
  e2.style.display='none';
    
  var pli = document.getElementById('hp'+id+'prevli');
  var nli = document.getElementById('hp'+id+'nextli');
  
  if(pli != null)
    incNavHref(pli, d, total, reg);
  if(nli != null)
    incNavHref(nli, d, total, reg);
  
  if(toshow==1)
    configureLink(id, 'r','p', pli, '?'+t[1], total);
  else if(toshow==2 && d=='n')
    configureLink(id, 'a','p', pli, '?'+t[1], total);
  else if(toshow==(total-1) && d=='p')
    configureLink(id, 'a','n', nli, '?'+t[1], total);
  else if(toshow==total)
    configureLink(id, 'r','n', nli, '?'+t[1], total);
}

function configureLink(id, addrem, d, li, href, total)
{
  var arrow = null;
  var func = function(){ hpNavigate(id, this, d); return false;};
  
  if(d=='n')
  {
    arrow = document.getElementById('hp'+id+'next');
    href += total;
  }
  else
  {
    arrow = document.getElementById('hp'+id+'prev');
    href += '1';
  }
  
  if(addrem == 'a')  
    addNavLink(id, arrow, li, href, func);
  else
    remNavLink(id, arrow, li);
}

function addNavLink(id, arrow, li, href, func)
{
  if(li == null || arrow == null)
    return false;
 
  var tags = li.getElementsByTagName('a');
  if(tags.length>0)
    return false;
  
  var anch = document.createElement('a');
  anch.href = href;
  anch.onclick = func;
  
  var rl = arrow.src.match(/big_([RL])_gray_arrow/)[1];
  
  anch.appendChild(hptimgs[id+rl+'A']);
  li.appendChild(anch);
  li.removeChild(arrow);
}

function remNavLink(id, arrow, li)
{
  if(li == null || arrow == null)
    return false;
 
  var tags = li.getElementsByTagName('a');
  if(tags.length==0)
    return false;
    
  var rl = arrow.src.match(/big_([RL])_blue_arrow/)[1];
  
  var anch = tags[0];
  li.appendChild(hptimgs[id+rl+'I']);
  li.removeChild(anch);
}

function incNavHref(e, d, total, reg)
{
  var tags = e.getElementsByTagName('a');
  if(tags.length!=0)
  {
    var t = tags[0];
    n = parseInt(t.href.match(reg)[2]);
    
    if(d=='n' && n != total)
      n++;
    else if(d=='p' && n != 1)
      n--;
    
    t.href = t.href.replace(reg,"$1"+n);
  }
}

function initHptNavigation(id)
{
  var leftactive = document.createElement('img');
  leftactive.alt = 'move to previous';
  leftactive.src = 'tpls/images/tEC/big_L_blue_arrow.gif';
  leftactive.id = 'hp'+id+'prev';
  hptimgs[id+'LA']=leftactive;
  
  var leftinactive = document.createElement('img');
  leftinactive.alt = 'move to previous';
  leftinactive.src = 'tpls/images/tEC/big_L_gray_arrow.gif';
  leftinactive.id = 'hp'+id+'prev';
  hptimgs[id+'LI']=leftinactive;
  
  var rightactive = document.createElement('img');
  rightactive.alt = 'move to previous';
  rightactive.src = 'tpls/images/tEC/big_R_blue_arrow.gif';
  rightactive.id = 'hp'+id+'next';
  hptimgs[id+'RA']=rightactive;
  
  var rightinactive = document.createElement('img');
  rightinactive.alt = 'move to previous';
  rightinactive.src = 'tpls/images/tEC/big_R_gray_arrow.gif';
  rightinactive.id = 'hp'+id+'next';
  hptimgs[id+'RI']=rightinactive;
}
