// +------------------------------------------------------------------+
// | Puzzle Message Board           Version 1                         |
// | Copyright 1999  Xin Yang        yangxin@iname.com                |
// | Created 09/20/1999              Last Modified 11/24/1999         |
// | Web Site:                       http://yx.webprovider.com        |
// +------------------------------------------------------------------+
// | Copyright 1999  Xin Yang        All Rights Reserved.             |
// |                                                                  |
// | yx_messageBox.js may be used and hosted free of charge by anyone |
// | for personal purpose so long as this copyright notice remain     |
// | intact.                                                          |
// |                                                                  |
// | Obtain permission before selling the code for this program or    |
// | hosting this program on a commercial web or redistributing this  |
// | program over the Internet or in any other medium. In all cases   |
// | copyright must remain intact.                                    |
// +------------------------------------------------------------------+

// File       yx_messageBox.js
// Function   A puzzle message board
// Browser    IE4, IE5 & NN4x
// Usage      1. place following line in the <head></head> section:
//               <script language="javascript" src="yx_messageBox.js"></script>

//            2. Insert following statement in where the message board to be shown
//               <div id="myMessageBoard" style="position:relative; left:0; top:0; visibility:visible; z-index:1;"></div>

//            3. place the function call in the <body> tag:
//               onload='messageBoard(msgHolder, msgPool, msgWidth, msgHeight, msgTileX, msgTileY, msgMethod, msgInterval)'
//               where:
//               # msgHolder - should be "myMessageBoard", or you must change the style sheet line accordingly
//               # msgPool - the user-defined message array, each message should be a html statement string
//               # msgWidth - the width of the message board
//               # msgHeight - the height of the message board
//               # msgTileX - how many tiles a message is to be cut into, vertically
//               # msgTileY - how many tiles a message is to be cut into, horizontally
//               # msgMethod - how the tiles flip, where
//                 - 0 : randomly
//                 - 1 : from left to right
//                 - 2 : from left to right, then right to left
//                 - 3 : from top to bottom
//                 - 4 : from top to bottom, then bottom to top
//                 - 5 : from left to right, separately
//                 - 6 : from top to bottom, separately
//                 - 7 : one of 0-6, randomly chosen for a message
//               # msgInterval - how long in secondss a message stays

// Script begins

var is_NN4 = false;
var is_IE4 = false;

var messageBox = 0;
var messageBoxCount = 0;
var messageBoxIndex = 0;
var messageBoxFlag = new Array();
var messageHolder = "";
var messageID = "box";
var messagePool = null;
var messagePoolLength = 0;
var messagePoolIndex = 0;
var messageWidth = 0;
var messageHeight = 0;
var messageTileX = 0;
var messageTileY = 0;
var messageTileIndex = 0;
var messageTileUnitX = 0;
var messageTileUnitY = 0;
var messageInterval = 0;
var messageMethod = 0;
var messageMethods = 7;
var messageMethodIndex = 0;
var messageMethodID = new Array();
    messageMethodID[0] = getBoxID0;
    messageMethodID[1] = getBoxID1;
    messageMethodID[2] = getBoxID2;
    messageMethodID[3] = getBoxID3;
    messageMethodID[4] = getBoxID4;
    messageMethodID[5] = getBoxID5;
    messageMethodID[6] = getBoxID6;

function messageBoxInit(x,y) {
  var layerID = "";
  var layerClip = "";
  var layerString = "";
  var thisLayer = null;

  layerID = messageID + x + "x" + y +"";

  if (messageBoxCount < messageBox) {
    if (is_IE4) {
      layerClip = "clip:rect(" + (y * messageTileYUnit) + "px " + ((x + 1) * messageTileXUnit) + "px " + ((y + 1) * messageTileYUnit) + "px " + (x * messageTileXUnit) + "px); "
      layerString = '<div id="' + layerID + '" style="position:absolute; left:0; top:0; width:' + messageWidth + '; height:' + messageHeight + '; ' + layerClip + 'visibility:visible; z-index:1;"><\/div>';
      document.all[messageHolder].insertAdjacentHTML("BeforeEnd", layerString);
    }
    else {
      document.layers[layerID] = new Layer(messageWidth, document.layers[messageHolder]);
      thisLayer = document.layers[layerID];
      thisLayer.height = messageHeight;
      thisLayer.clip.top = y * messageTileYUnit;
      thisLayer.clip.right = (x + 1) * messageTileXUnit;
      thisLayer.clip.bottom = (y + 1) * messageTileYUnit;
      thisLayer.clip.left = x * messageTileXUnit;
      thisLayer.zIndex = 1;
      thisLayer.visibility = "show";
    }

    messageBoxCount++;
  }

  return layerID;
}

function messageBoxFlagInit() {
  for (var i = 0; i < messageBox; i++)
    messageBoxFlag[i] = true;
}

function messageHolderInit() {
  var holderString = "";

  if (is_IE4) {
    document.all[messageHolder].style.width = messageWidth;
    document.all[messageHolder].style.height = messageHeight;
  }
  else {
    holderString = "<table width=" + messageWidth + " height=" + messageHeight + " border=0 cellpadding=0 cellspacing=0><tr><td><font size=1>&nbsp;</font></td></tr></table>";
    document.layers[messageHolder].document.open();
    document.layers[messageHolder].document.writeln(holderString);
    document.layers[messageHolder].document.close();
  }
}

function getBoxID6(boxIndex) {
  var boxMidBig = Math.ceil(messageBox / 2);
  var boxY = 0;
  var boxX = 0;

  if (messageTileY % 2 != 0) {
    if (boxIndex < boxMidBig) {
      boxY = (boxIndex * 2) % messageTileY;
      boxX = Math.floor(boxIndex * 2 / messageTileY);
    }
    else {
      boxY = ((boxIndex - boxMidBig) * 2 + 1) % messageTileY;
      boxX = Math.floor(((boxIndex - boxMidBig) * 2 + 1) / messageTileY);
    }

    return messageTileX * boxY + boxX
  }
  else {
    if (boxIndex < boxMidBig) {
      boxY = (boxIndex * 2 + Math.floor(boxIndex * 2 / messageTileY) % 2) % messageTileY;
      boxX = Math.floor((boxIndex * 2 + Math.floor(boxIndex * 2 / messageTileY) % 2) / messageTileY);
    }
    else {
      boxY = ((boxIndex - boxMidBig) * 2 + 1 - Math.floor((boxIndex - boxMidBig) * 2 / messageTileY) % 2) % messageTileY;
      boxX = Math.floor(((boxIndex - boxMidBig) * 2 + 1 - Math.floor((boxIndex - boxMidBig) * 2 / messageTileY) % 2) / messageTileY);
    }

    return messageTileX * boxY + boxX
  }
}

function getBoxID5(boxIndex) {
  var boxMidBig = Math.ceil(messageBox / 2);

  if (messageTileX % 2 != 0) {
    if (boxIndex < boxMidBig)
      return boxIndex * 2
    else
      return (boxIndex - boxMidBig) * 2 + 1;
  }
  else {
    if (boxIndex < boxMidBig)
      return boxIndex * 2 + Math.floor(boxIndex * 2 / messageTileX) % 2
    else
      return (boxIndex - boxMidBig) * 2 + 1 - Math.floor((boxIndex - boxMidBig) * 2 / messageTileX) % 2;
  }
}

function getBoxID4(boxIndex) {
  var boxY = boxIndex % messageTileY;
  var boxX = Math.floor(boxIndex / messageTileY);

  if (boxX % 2 == 0)
    return messageTileX * boxY + boxX
  else
    return messageTileX * (messageTileY - boxY - 1) + boxX;
}

function getBoxID3(boxIndex) {
  var boxY = boxIndex % messageTileY;
  var boxX = Math.floor(boxIndex / messageTileY);

  return messageTileX * boxY + boxX;
}

function getBoxID2(boxIndex) {
  var boxX = boxIndex % messageTileX;
  var boxY = Math.floor(boxIndex / messageTileX);

  if (boxY % 2 == 0)
    return boxIndex
  else
    return messageTileX * boxY + (messageTileX - 1 - boxX);
}

function getBoxID1(boxIndex) {
  return boxIndex
}

function getBoxID0(boxIndex) {
  var randomID = Math.floor(Math.random() * (messageBox - boxIndex));
  var boxID = 0;

  for (var i = 0; i < messageBox; i++)
    if (messageBoxFlag[i]) {
      if (randomID == 0) {
        messageBoxFlag[i] = false;
        boxID = i;
        break;
      }
      else
        randomID--;
    }

  return boxID;
}

function getMethod() {
  return Math.floor(Math.random() * messageMethods);
}

function messageShow() {
  var layerID = "";
  var thisID = 0;
  var thisX = 0;
  var thisY = 0;
  var thisLayer = null;

  if (messageBoxIndex < messageBox) {
    thisID = messageMethodID[messageMethodIndex](messageBoxIndex);
    thisX = thisID%messageTileX;
    thisY = Math.floor(thisID/messageTileX);
    layerID = messageBoxInit(thisX,thisY);

    if (is_IE4)
      document.all[layerID].innerHTML = messagePool[messagePoolIndex];
    else {
      thisLayer = document.layers[layerID];
      thisLayer.document.open();
      thisLayer.document.writeln(messagePool[messagePoolIndex]);
      thisLayer.document.close();
    }

    messageBoxIndex++;
    setTimeout("messageShow()",0);
  }
  else {
    messageBoxIndex = 0;
    messagePoolIndex = (++messagePoolIndex < messagePoolLength)?messagePoolIndex:0;
    if (messageMethod == 7)
      messageMethodIndex = getMethod();
    if (messageMethodIndex == 0)
      messageBoxFlagInit();
    setTimeout("messageShow()",messageInterval);
  }
}

function messageBoard(msgHolder, msgPool, msgWidth, msgHeight, msgTileX, msgTileY, msgMethod, msgInterval) {
  is_NN4 = document.layers;
  is_IE4 = document.all;

  if (is_NN4 || is_IE4) {
    messageHolder = msgHolder;
    messagePool = msgPool;
    messagePoolLength = messagePool.length;
    messageWidth = msgWidth;
    messageHeight = msgHeight;
    messageTileX = msgTileX;
    messageTileY = msgTileY;
    messageBox = messageTileX * messageTileY;
    messageTileXUnit = Math.floor(msgWidth/msgTileX)
    messageTileYUnit = Math.floor(msgHeight/msgTileY);
    messageMethod = msgMethod;
    if (messageMethod != 7)
      messageMethodIndex = messageMethod
    else
      messageMethodIndex = getMethod();
    messageInterval = msgInterval * 1000;

    messageHolderInit();
    if (messageMethodIndex == 0)
      messageBoxFlagInit();
    messageShow();
  }
}

// Script Ends

