
//Time in milliseconds between rotating facts
rotate_interval = 20000;

//id of the div containing this whole section
fact_container_id = 'homepage-title-intro'

//id of the element containing the fact
fact_id = 'homepage-title-intro-fact';

//Ending text
fact_closer = '';

//Hide the default link from Javascript enabled browsers only
document.write("<style type='text/css'> #" + fact_id + " {visibility:hidden; zoom:1;} <\/style>");

//Globally scoped reducing array of facts
fact_list = new Array(); 
fact_list[0]="The Church of England plays a vital role in the life of the nation, proclaiming the Christian Gospel in words and actions <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";
fact_list[1]="A network of parishes covers the country, bringing a Christian presence to the nation as well as strengthening community life  <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";
fact_list[2]="Around one million people participate in worship each Sunday <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";
fact_list[3]="In 2007, 43 per cent of adults attended a church or place of worship for a memorial service and 20 per cent seeking a quiet space <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";
fact_list[4]="Seven in 10 agree that Church of England schools have a positive role in educating the nation’s children <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";
fact_list[5]="552 new clergy were ordained in 2007, the highest number since the year 2000: 262 women and 290 men <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";
fact_list[6]="A quarter of regular churchgoers are involved in voluntary community service outside the church <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";
fact_list[7]="The CofE provides activities in the local community for more than half a million under 16s and 38,000 young people aged 16 to 25 <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";
fact_list[8]="People value their local church and 72% consider it an important part of their local community <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";
fact_list[9]="63% would be concerned if the local church was not there <a id=\"homepage-title-intro-morefacts\" href=\"/about/thechurchofenglandtoday/\">More facts<\/a>";

//Globally scoped tracker for facts that have been shown
facts_shown = new Array(); 

function getRandomFact() {
     var working_facts = fact_list;
     if (working_facts.length < 1) {
         working_facts = facts_shown;
         }
     var rnd_fact_no = Math.round((working_facts.length-1)*Math.random());
     var returning_fact = working_facts[rnd_fact_no];
     if (fact_list.length > 0) {
         facts_shown.push(returning_fact);
         fact_list.splice(rnd_fact_no, 1);
     }
     return returning_fact;
}

function setOpacity(obj, opacity) {
  var opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function fadeIn(obj_id, opacity) {
  if (document.getElementById) {
    var obj = document.getElementById(obj_id);
    if (opacity < 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+obj_id+"',"+opacity+")", 200);
    }
  }
  return true;
}

function fadeOut(obj_id, opacity) {
  if (document.getElementById) {
    var obj = document.getElementById(obj_id);
    if (opacity > 0) {
      setOpacity(obj, opacity);
      opacity -= 10;
      window.setTimeout("fadeOut('"+obj_id+"',"+opacity+")", 200);
    }
  }
  return true;
}

function rotateFact() {
    var fact = document.getElementById(fact_id);
    //fadeOut(fact_id,100); This would need executing asynchronously
    fact.innerHTML = getRandomFact();
    fadeIn(fact_id,0);
    }

function initFacts() {
  if (document.getElementById) {
      var fact = document.getElementById(fact_id);
      if (fact_list.length > 0) {
          setOpacity(fact, 0);
          fact.style.visibility = 'visible';
          fact.innerHTML = getRandomFact();
          fadeIn(fact_id,0); //Our initial fact
          fact_container = document.getElementById(fact_container_id);
          fact_closer_element = document.createElement('span');
          fact_closer_element.innerHTML = fact_closer;
          fact_container.appendChild(fact_closer_element);
          window.setInterval("rotateFact()", rotate_interval);
          }
      }
}

if (window.addEventListener) window.addEventListener("load",initFacts,false);
else if (window.attachEvent) window.attachEvent("onload",initFacts);

