/* function used to pop up contact details */
function contactPopup(x,show) {
  var popup=document.getElementById('contact_popup');

  if(show==undefined) {
    if(popup.style.display!='block') popup.style.display='block';
    else popup.style.display='none';
  } else {
    if(show) popup.style.display='block';
    else popup.style.display='none';
  }
  
  //if(popup.style.display=='block') brochurePopup(false);
}

function doContactPop(){
	contactPopup(); 
	pageTracker._trackPageview('/contact.html'); 
}

/*function brochurePopup(show) {
  var popup=document.getElementById('brochure_popup');
  
  if(show==undefined) {
    if(popup.style.display!='block') popup.style.display='block';
    else popup.style.display='none';
  } else {
    if(show) popup.style.display='block';
    else popup.style.display='none';
  }
    
  if(popup.style.display=='block') contactPopup(false,false);
}*/


/*
* ajax call to retrieve and possible execute code on response from server
*
*/
function doTask(params) {
  if (document.implementation && window.document.implementation.createDocument)
  {
    xmlDoc = document.implementation.createDocument("", "", null);
    xmlDoc.onload = doResponse;
  }
  else if (window.ActiveXObject)
  {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.onreadystatechange = function () {
      if (xmlDoc.readyState == 4) doResponse()
    };
  }
  else
  {
    return false;
  }
  d = new Date; // Generic - use to create unique url requests
  var rand = d.getTime();
  var url=document.location.protocol+'//'+document.location.host+'/alt/doTask.xml?rand='+rand+'&'+params;
  try{
    xmlDoc.load(url); // this is what we like to do
  } catch(e) { // if failed try using httprequest as a fallback - (e.g. Safari)
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
    if(req) {
      req.onreadystatechange = processHttpRequest;
      req.open("GET", url, true);
      req.send("");
    }

  }

  return false;
}

/*
* some browsers require this extra step to catch the http reponse
*
*/
function processHttpRequest() {
  if (req.readyState == 4) {
        if (req.status == 200) {
      xmlDoc=req.responseXML;
      doResponse();
        }
    }
}

/*
* process the response from server to the doTask function,
*
*/
function doResponse() {
  var tasks = xmlDoc.getElementsByTagName('task');
  var cmd=null;

  for(var i=0;i<tasks.length;i++) {
    cmd=tasks[i].getElementsByTagName('cmd')[0];

    if (cmd.childNodes[0]) {
      switch(cmd.childNodes[0].nodeValue) {
        case 'replace': // replace the innerHTML of an element
          var id=tasks[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
          if (tasks[i].getElementsByTagName('content')[0].textContent) var content=tasks[i].getElementsByTagName('content')[0].textContent; //avoid FF 4096 char limit
          else var content=tasks[i].getElementsByTagName('content')[0].childNodes[0].nodeValue;
          if (id.length>0 && document.getElementById(id)) document.getElementById(id).innerHTML=content;
          break;
        case 'execute': // run some js
          var script=tasks[i].getElementsByTagName('script')[0].childNodes[0].nodeValue;
          eval(script);
          break;

      }
    }
  }
}

/*
* use JS to hide controls which are only required when JS is not enabled
* - do this by adding hideJS as a class to the css
*/
function hideElements() {
  if (document.styleSheets) {
    if (document.styleSheets[0].addRule) {    // IE
      document.styleSheets[0].addRule('.hideJS','display: none',0);
    } else {    // !IE
      document.styleSheets[0].insertRule('.hideJS {display: none}', 0);
    }
  }

}

/*
* Add the onload function to any other onload function
*/
function addLoadEvent(func) {
  var oldonload = window.onload;

  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(hideElements);

// show the form wait box
function showWait(id) {
  if (document.getElementById(id)) {
    var el=document.getElementById(id);

    el.style.position='absolute';
    el.style.left='50%';
    el.style.top='50%';
    el.style.display='block';
    el.style.marginLeft="-" + parseInt(el.offsetWidth  / 2)+'px';
    el.style.marginTop ="-" + parseInt(el.offsetHeight / 2)+'px';
    // IE has a quirk which means we need to reload the animated gif to make it animate
    if (document.getElementById(id+'wImg')) setTimeout("document.getElementById('"+id+"wImg').src=document.getElementById('"+id+"wImg').src",20);
  }
}

/**
* toggle the more info box
*/
function moreinfo() {
  var el=document.getElementById("more_info");
  var ltext='';

  if(el.style.display!="block") {
    el.style.width="0px";
    el.style.display="block";
    ltext="close information";
    func='miOpen()';
  } else {
    ltext="more information";
    func='miHide()';
  }

  document.getElementById('moreinfolink').innerHTML=ltext;

  setTimeout(func,15);

  return false;
}

/**
* open the more info box
*/
function miOpen() {
  var el=document.getElementById("more_info");
  var el_c=document.getElementById('more_info_content');
  var cwidth=parseInt(el.style.width.replace("px",""));
  var nwidth=0;

  el_c.style.display="none";

  if(cwidth<(342-10)) nwidth=cwidth+10;
  else nwidth=342;

  if(cwidth<342) {
    el.style.width=nwidth+"px";
    setTimeout('miOpen()',15);
  } else {
    miReveal();
  }
}

/**
* close the more info box
*/
function miClose() {
  var el=document.getElementById("more_info");
  var el_c=document.getElementById('more_info_content');
  var cwidth=parseInt(el.style.width.replace("px",""));
  var nwidth=0;

  el_c.style.display="none";

  if(cwidth>10) nwidth=cwidth-10;
  else nwidth=0;

  if(cwidth>0) {
    el.style.width=nwidth+"px";
    setTimeout('miClose()',15);
  } else {
    el.style.display="none";
  }
}

/**
* reveal the more info content
*/
function miReveal() {
  var el=document.getElementById('more_info_content');
  var opacity=parseFloat(el.style.opacity);
  if(el.style.filter) {
    var ie_opacity=parseInt(el.style.filter.replace("alpha(opacity=","").replace(")",""));
  }

  el.style.display="block";

  if(opacity<1) {
    el.style.opacity=opacity+0.1;
    if(el.style.filter) el.style.filter="alpha(opacity="+(ie_opacity+10)+")";
    setTimeout('miReveal()',15);
  }
}

function miHide() {
  var el=document.getElementById('more_info_content');
  var opacity=parseFloat(el.style.opacity);
  if(el.style.filter) {
    var ie_opacity=parseInt(el.style.filter.replace("alpha(opacity=","").replace(")",""));
  }

  if(opacity>0) {
    el.style.opacity=opacity-0.1;
    if(el.style.filter) el.style.filter="alpha(opacity="+(ie_opacity-10)+")";
    setTimeout('miHide()',15);
  } else {
    miClose();
  }
}

var slideRate=40;

function slidePageIn() {
  var el=document.getElementById('bulk');
  var lft=parseInt(el.style.marginLeft.replace("px",""));
  var width=960;

  // slow the rate down as we get near to the end of the slide
  if(lft>-180) slideRate=25;

  if(lft<(0-slideRate)) nlft=lft+slideRate;
  else nlft=0;

  if(nlft<0) {
    el.style.marginLeft=nlft+"px";
    setTimeout('slidePageIn()',5);
  } else {
    el.style.marginLeft="0px";
  }
}

function slidePageOut(url) {
  var el=document.getElementById('bulk');
  var lft=parseInt(el.style.marginLeft.replace("px",""));
  var width=960;

  // speed up as we move out
  slideRate=40;

  if(lft>(-960+slideRate)) nlft=lft-slideRate;
  else nlft=-960;

  if(nlft>-960) {
    el.style.marginLeft=nlft+"px";
    setTimeout('slidePageOut("'+url+'")',5);
  } else {
    el.style.marginLeft="-960px";
    document.location=url;
  }
}

function setupSlides() {
  var el_b=document.getElementById('bulk');

  if(el_b) {
    el_b.style.marginLeft="-960px";

    var el=document.getElementById('more_info_content');
    el.style.opacity="0";
    el.style.filter="alpha(opacity=0)";
    el.style.zoom="1";

    setTimeout('slidePageIn()',5);
  }
}


//addLoadEvent(setupSlides);
window.onload=setupSlides;