// @@ GLOBAL VARIABLES

var mainDomain="christiansciencewatford.org.uk";
var siteName="First Church of Christ, Scientist, Watford.";
var postcode="WD17+3AD";
var styleSheet="css/default.css";
var qCount=9;
var now = new Date();
var quoteInterval = 15000;
var fadeDuration = 4;
var steps = 30;
var thisQuote = 1;
var lastQuote = qCount;

function generate(x, y) { // creates a random number between two given integers

  var range = y - x + 1;
  return (Math.floor(Math.random() * range) + x);

}; // END generate()

function daysFromNow(days) {
  delete now;
  now = new Date();
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    now.setTime(now.getTime() - skew);
  // above 'fix' from DocJS.com, for older browsers I think :S
  now.setTime(now.getTime() + days * 24 * 60 * 60 * 1000);
  return now;
}

window.onload = function() {
  if (!xGetCookie("quoteSetting")) {
    thisQuote = generate(1,qCount);
    fadeIn(xGetElementById('q'+thisQuote),fadeDuration)
    setTimeout("xSetCookie('quoteSetting', "+thisQuote+", daysFromNow(364));",fadeDuration*1000);
  } else {
      thisQuote = xGetCookie("quoteSetting");
      if (document.referrer.indexOf(mainDomain)==-1) fadeIn(xGetElementById('q'+thisQuote),fadeDuration)
      else xShow('q'+thisQuote);
    }
  setTimeout("cycleQuote("+thisQuote+",1);",quoteInterval/1.5);
}

function fadeIn(obj,dur) {
  xHide(obj);
  if (xIE4Up) {
    obj.style.filter="blendTrans(duration="+dur+")";
    obj.filters.blendTrans.Apply();
    xShow(obj);
  } else {
    obj.style.opacity=0;
    xShow(obj);
    for (i=1,opa=obj.style.opacity; (opa=(1/steps)*i)<=1; i++) setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='"+opa+"';",Math.round(opa*(dur*1000)));
  }
  if (xIE4Up) obj.filters.blendTrans.Play();
}

function fadeOut(obj,dur) {
  xShow(obj);
  if (xIE4Up) {
    obj.style.filter="blendTrans(duration="+dur+")";
    obj.filters.blendTrans.Apply();
    xHide(obj);
  } else {
    for (i=1,opa=0; (opa=(1/steps)*i)<=1; i++) setTimeout("xGetElementById('q"+thisQuote+"').style.opacity='"+(1-opa)+"';",Math.round(opa*(dur*1000)));
    setTimeout("xHide('q"+thisQuote+"')",dur*1000);
  }
  if (xIE4Up) obj.filters.blendTrans.Play();
}

function cycleQuote(lastQuote,incr) {
  fadeOut(xGetElementById('q'+lastQuote),fadeDuration);
  thisQuote = eval(lastQuote)+incr;
  if (thisQuote>qCount) thisQuote=1;
  if (thisQuote<1) thisQuote=qCount;
  fadeIn(xGetElementById('q'+thisQuote),fadeDuration);
  setTimeout("xSetCookie('quoteSetting', "+thisQuote+", daysFromNow(364));",fadeDuration*1000);
  setTimeout("cycleQuote("+thisQuote+",1);",quoteInterval);
}


// @@@@@ generating a spam-safe e-mail address @@@@@

function drawEmail(alias,text,dn,title) {
  var at = '&#064;'; // at symbol encoded
  var p1 = '&#109;&#097;&#105;&#108;'; // protocol name encoded part 1
  var p2 = '&#116;&#111;&#58;'; // protocol name encoded part 2
  if (!dn) dn=mainDomain;
  if (title) title=' title="'+title+'"'
    else title='';
  if (!text) text=alias+at+dn;
  document.write('<a href="'+p1+p2+alias+at+dn+'"'+title+'>'+text+'</a>');
}


