
//  <!----------------------------------------------------------------------------------->
//  <!--                                                                               -->
//  <!-- This code was created by Mark West for the mixedcasesspaces web site 2003-05. -->
//  <!-- Feel free to read it and find out how I did stuff, but if you knick anything  -->
//  <!-- substantial, then please credit me in your code, and let me know that my code -->
//  <!-- has been useful!  Thanks.  Mark.                                              -->
//  <!--                                                                               -->
//  <!----------------------------------------------------------------------------------->


// saves processing time fetching the document object every time.
var doc = window.document;

// global preferences:
var fadeEffects = true;    // fade in results etc on or off
var scrollEffects = true;  // scroll open windows etc on or off
var debugMode = false;     // debug mode on or off (where implemented)
var fadeInSpeed = 100;      // speed of fade effects
var fadeInOffset = 100;    // pause multiplier before object fades in
var fadeInStep = 1;        // step size for fade in procedure
var expandInSpeed = 5;     // speed of expand/contract scroll
var expandInStep = 10;     // step size for expand/contract
var expanding = false;     // only allow one process to expand at at time

// disable the normal context menu:
document.oncontextmenu=new Function("return false;");

// D'Silva specific code:

function init()
{
  resizer();
  doc.getElementById("pic1").style.Zindex=10;
  doc.getElementById("pic2").style.Zindex=9;
  doc.getElementById("pic3").style.Zindex=9;
  fadeIn("pic1",0); // this will call cycle when done
}

function resizer()
{
  if (isIE)
  {
    window_width=doc.body.offsetWidth;
    window_height=doc.body.offsetHeight;
  }
  else
  {
    window_width=window.innerWidth;
    window_height=window.innerHeight;
  }

  // resize screen elements:
  if (window_width>40)
  {
    document.getElementById("content").style.width=window_width-40;
  }
  else
  {
    document.getElementById("content").style.width=0;
  }

  // resize all screen elements
  if (window_width>180)
  {
    document.getElementById("mcs_div").style.left=window_width-180;
  }
  else
  {
    document.getElementById("mcs_div").style.left=0;
  }
}

// cycle between three picture that are overlaid, fading between them.
// pics are called pic1, pic2, pic3.  pic1 will already be displayed.
var current_pic=2;
var next_pic=1;

function cycle()
{
  // choose which pic to fade in next:
  if (Math.random()<0.5)
  {
    next_pic=current_pic+1;
  }
  else
  {
    next_pic=current_pic+2;
  }
  if (next_pic==4)
  {
    next_pic=1;
  }
  if (next_pic>4)
  {
    next_pic=2;
  }

  // set it to foreground (it's filter is 0 anyway):
  doc.getElementById("pic"+next_pic).style.zIndex=10;
  // set the current pic to background:
  doc.getElementById("pic"+current_pic).style.zIndex=9;

  // fade it in.  When done, this will call fadeIn_done()
  fadeIn("pic"+next_pic,1);
}

function fadeIn_done()
{
  // make it invisible again for next time:
  doc.getElementById("pic"+current_pic).style.zIndex=9;
  if (isIE)
  {
    doc.getElementById("pic"+current_pic).style.filter="alpha(opacity:0)";
  }
  else
  {
    doc.getElementById("pic"+current_pic).style.MozOpacity=0;
  }

  current_pic = next_pic;

  // cycle again.  If current is 1, wait a bit longer on average.
  if (current_pic==1)
  {
    setTimeout("cycle();",(5000+(10000*Math.random())));
  }
  else
  {
    setTimeout("cycle();",(1000*Math.random()));
  }
}

function add_email()
{
  if ((doc.getElementById('email').value!="") && (doc.getElementById('email').value!=" your email"))
  {
    window.open("addemail.php?email="+doc.getElementById('email').value,"email","width=200,height=150,screenX=20,screenY=20,modal=no,resizable=no,scrollbars=no");
  }
}

////////////////////////////////////////////////////////////////////////////

// Browser detect javascript:

// Used by the js code wherever a code branch is required due to browser conflicts:
var isIE = false;
if (navigator.appName.indexOf("Microsoft")!=-1)
{
  isIE = true;
}

//////////////////////////////////////////////////////////////////////////////

// Utility functions:

// returns 'true' if the number provided in even
function isEven(theNumber)
{
  return (theNumber%2) ? false:true;
}

// fades in a screen object
function fadeIn(objectId,offset)
{
  if (doc.getElementById(objectId) == null) {logError("fadeIn: null object passed");}
  if (debugMode) {logMessage("fadeIn: called. objectId="+objectId);}

  // set object fade to start value:
  if (isIE)
  {
    doc.getElementById(objectId).style.filter="alpha(opacity:0)";
  }
  else
  {
    doc.getElementById(objectId).style.MozOpacity=0;
  }

  // fade in:
  setTimeout("doFadeIn('"+objectId+"',10)",offset*fadeInOffset);
}

// fade up the object gradually until it reaches 100%
function doFadeIn(objectId,step)
{
  if (doc.getElementById(objectId) == null) 
  {
    // possibly the page has changed/reloaded while fadein is still running, this is no big problem.
    return false;
  }

  if (isIE)
  {
    if (step<100)
    {
      doc.getElementById(objectId).style.filter="alpha(opacity:"+step+")";
      // call next step:
      setTimeout("doFadeIn('"+objectId+"',"+(step+fadeInStep)+")",fadeInSpeed);
    }
    else
    {
      doc.getElementById(objectId).style.filter="alpha(opacity:100)";
      fadeIn_done();
    }
  }
  else
  {
    if (step<100)
    {
      doc.getElementById(objectId).style.MozOpacity=step/100;
      // call next step:
      setTimeout("doFadeIn('"+objectId+"',"+(step+fadeInStep)+")",fadeInSpeed);
    }
    else
    {
      doc.getElementById(objectId).style.MozOpacity=1;
      fadeIn_done();
    }
  }
}

// returns the height the element needs to be to fit all the contents in it.
function getFullHeight(element)
{
  // n.b. in IE we could just read the 'scrollHeight' value, but this does not work in Mozilla.
  var target = doc.getElementById(element);
  target.style.display="block";
  var start_height = target.style.height;
  // set height to "" which makes it go to it's required 'full' height in both browsers:
  target.style.height='';
  var result = target.scrollHeight;
  target.style.height = start_height;
  return result+5;
}

// this sets the height to an integer height rather than '' (blank), so we can use the expandTo() function. 
function setToFullHeight(element)
{
  var target = doc.getElementById(element);
  target.style.display="block";
  target.style.height='';
  var result = target.scrollHeight;
  target.style.height = result;
}

function expandTo(objectId,size)
{
  if (doc.getElementById(objectId) == null) {logError("expandIn: null object passed");}
  if (debugMode) {logMessage("expandIn: called. objectId="+objectId);}

  if (expanding) { return false; }

  if (!scrollEffects)
  {
    doc.getElementById(objectId).style.height=size;
    if (size==0) {doc.getElementById(objectId).style.display="block";} 
  }
  else
  {
    expanding = true;
    // set display to visible
    doc.getElementById(objectId).style.display="block";
    // expand to chosen height
    doExpand(objectId,size);
  }
}

// expand the object gradually until it reaches full size
function doExpand(objectId,size)
{
  if (doc.getElementById(objectId) == null) 
  {
    // possibly the page has changed/reloaded while doexpand is still running, this is no big problem.
    return false;
  }

  var currentSize=parseInt(doc.getElementById(objectId).style.height);
  if (currentSize<(size-expandInStep))
  {
    // expand
    doc.getElementById(objectId).style.height=currentSize+expandInStep+"px";
    setTimeout("doExpand('"+objectId+"',"+size+")",expandInSpeed);
  }
  else if (currentSize>(size+expandInStep))
  {
    // collapse
    doc.getElementById(objectId).style.height=currentSize-expandInStep+"px";
    setTimeout("doExpand('"+objectId+"',"+size+")",expandInSpeed);
  }
  else
  {
    // finishing height
    doc.getElementById(objectId).style.height=size;
 doc.getElementById(objectId).style.width=200;
    if (size<=1)
    {
      doc.getElementById(objectId).style.display="none";
    }
    expanding=false;
  }
}

// Debugging utilities:

// deal with error messages in the JS
function logError(message)
{
  if (debugMode)
  {
    debugWindow = window.open('','debugwin');
    // convert message special html characters to display on screen OK:
    message = message.replace(/</g,"&lt;");
    debugWindow.document.write("<br><b>&gt;&nbsp;ERROR:&nbsp;"+message+"</b>");
  }
}

// deal with debug messages in the JS
function logMessage(message)
{
  debugWindow = window.open('','debugwin');
  // convert message special html characters to display on screen OK:
  message = message.replace(/</g,"&lt;");
  debugWindow.document.write("<br>&gt;&nbsp;"+message);
}
