//===== relative URL handling code for js files ================
sWZBaseFolder = "www.arrakis.es";                          
sWZ = window.location.href;                                     
iWZ = sWZ.indexOf(sWZBaseFolder) + sWZBaseFolder.length + 1;    
sWZBase = sWZ.substring(0,iWZ);                                 
//===== Copyright © 2001 Spidersoft. All rights reserved. ======

/******************************************************************************
* ouija.js                                                                    *
*                                                                             *
* Copyright 1999 by Mike Hall.                                                *
* Web address: http://www.brainjar.com                                        *
* Last update: March 19, 2000.                                                *
*                                                                             *
* A little experiment in DHTML and the occult, two subjects which are often   *
* confused with one another.                                                  *
******************************************************************************/

var board, planchette;    // DHTML layers.

var boardY, boardY;       // Coordinates.
var nextX, nextY;
var saveX, saveY;
var posX, posY;

var offsetX = 62;         // Offset of viewport on planchette.
var offsetY = 62;

var inReply   = false;    // Status flags.
var inContact = false;

var answer = "";          // Response to spell out.
var nextChar;             // Next character to move to.

var step = 10;            // Used for movement.
var angle;

// Board positions, each letter and number has position. In addition, other
// symbols are used for other areas of the board:
// ! = 'yes'
// ~ = 'no'
// ^ = 'goodbye'
// _ = rest position
// and a space is a blank spot on the board.

var chars = "abcdefghijklmnopqrstuvwxyz1234567890!~^_ ";

// Set up character positions.

posX = new Array(105, 134, 171, 204, 240, 270, 305, 340, 372, 389, 420, 452, 484,
                 114, 151, 182, 217, 249, 274, 300, 331, 365, 399, 436, 466, 492,
                 154, 184, 217, 251, 281, 314, 346, 378, 410, 441,
                 137, 465, 300, 300, 300);
posY = new Array(182, 161, 145, 134, 126, 123, 124, 125, 130, 140, 146, 155, 175,
                 223, 202, 190, 181, 174, 171, 169, 172, 174, 185, 199, 211, 231,
                 280, 280, 280, 280, 280, 280, 280, 280, 280, 280,
                 054, 054, 350, 140, 215);

//-----------------------------------------------------------------------------
// Initialize everything.
//-----------------------------------------------------------------------------

function init() {

  var i;

  // Initialize planchette.

  board = getImage("board");
  boardX = getImagePageLeft(board);
  boardY = getImagePageTop(board);

  planchette = getLayer("planchette");
  i = chars.indexOf("_");
  moveLayerTo(planchette, boardX + posX[i] - offsetX, boardY + posY[i] - offsetY);
  showLayer(planchette);
}

//-----------------------------------------------------------------------------
// These functions handle the movement of the planchette when revealing a
// response.
//-----------------------------------------------------------------------------

function handsOn() {

  inContact = true;
}

function handsOff() {

  inContact = false;
  if (inReply) {
    saveX = getLeft(planchette);
    saveY = getTop(planchette);
    shakePlanchette();
  }
}

function startReply() {

  // Start revealing an answer.

  answer += "_";
  if (!inContact) {
    saveX = getLeft(planchette);
    saveY = getTop(planchette);
    shakePlanchette();
  }
  if (getNext()) {
    inReply = true;
    movePlanchette();
  }
  else
    inReply = false;
}

function endReply() {

  // Done revealing answer.

  nextChar = "";
  inReply = false;
  inContact  = false;
}

function getNext() {

  var i;

  // Find the next character to move to.

  if (answer == "")
    return false;

  nextChar = answer.substr(0, 1)
  answer = answer.substr(1);
  for (i = 0; i < chars.length; i++)
    if (chars.charAt(i) == nextChar) {
      nextX = posX[i];
      nextY = posY[i];
      return true;
    }

  return false;
}

function movePlanchette() {

  var i, dx, dy, last, theta;

  // If no reply to give, return.

  if (!inReply)
    return;

  // If mouse is on planchette, move it.

  if (inContact) {

    // Move planchette toward the designated character.

    dx = boardX + nextX - getLeft(planchette) - offsetX;
    dy = boardY + nextY - getTop(planchette) - offsetY;
    theta = Math.round(Math.atan2(-dy, dx) * 180 / Math.PI);
    if (theta < 0)
      theta += 360;

    // If we reached the character, pause and start on the next one.

    if (Math.abs(dx) < step && Math.abs(dy) < step) {
      moveLayerBy(planchette, dx, dy);
      last = nextChar;

      // Circle if next character is the same as the last.

      if (getNext()) {
        if (nextChar == last) {
          angle = 0;
          setTimeout('circlePlanchette()', 2000);
          return;
        }
        setTimeout('movePlanchette()', 2000);
        return
      }

      // End movement if no more characters in reply.

      else
        endReply();
    }

    // If not, move it.

    else if (theta > 23 && theta <= 68)
      moveLayerBy(planchette, step, -step);
    else if (theta > 68 && theta <= 113)
      moveLayerBy(planchette, 0, -step);
    else if (theta > 113 && theta <= 158)
      moveLayerBy(planchette, -step, -step);
    else if (theta > 158 && theta <= 203)
      moveLayerBy(planchette, -step, 0);
    else if (theta > 203 && theta <= 248)
      moveLayerBy(planchette, -step, step);
    else if (theta > 248 && theta <= 293)
      moveLayerBy(planchette, 0, step);
    else if (theta > 293 && theta <= 338)
      moveLayerBy(planchette, step, step);
    else
      moveLayerBy(planchette, step, 0);
  }

  // Set up next call.

  setTimeout('movePlanchette()', 50);
  return;
}

function circlePlanchette() {

  var x, y;

  // Mouse on planchette?

  if (!inContact) {
    setTimeout('circlePlanchette()', 50);
    return;
  }

  // Move the planchette in a small circle.

  x = getLeft(planchette) + step * Math.cos(angle);
  y = getTop(planchette) - step * Math.sin(angle);
  moveLayerTo(planchette, x, y);
  angle += Math.PI / 10;
  if (angle < 2 * Math.PI)
    setTimeout('circlePlanchette()', 50);
  else
    movePlanchette();
}

function shakePlanchette() {

  var dx, dy, x, y;

  // Mouse on planchette?

  if (inContact)
    return;

  // Randomly move planchette around it's original position.

  dx = Math.floor(Math.random() * 5) - 2;
  dy = Math.floor(Math.random() * 5) - 2;
  x = saveX + dx;
  y = saveY + dy;
  moveLayerTo(planchette, x, y);
  setTimeout('shakePlanchette()', 50);
}

//-----------------------------------------------------------------------------
// This code handles the analysis of a question and generates an answer.
//-----------------------------------------------------------------------------

// Lists of responses by category.

var none = new Array("pregunta", "tu preguntame", "yo estoy aqui", "sin tema", "habla",
                     "por que no hablas", "te escucho", "mujer gris espera", "veronica");
var days = new Array("domingo", "lunes", "martes", "miercoles", "jueves",
                     "viernes", "sabado");
var months = new Array("enero", "febrero", "marzo", "abril", "mayo", "junio",
                       "julio", "agosto", "septiembre", "octubre", "noviembre",
                       "diciembre");
var seasons = new Array("primavera", "verano", "otono", "invierno");
var times = new Array("nunca", "pronto", "muy pronto", "ahora", "semana proxima",
                      "no ahora", "tarde", "dale tiempo", "no ahora", "octubre 31 2007");
var people = new Array("amigo", "desconocido", "tu", "a nadie", "un cercano", "mujer de gris", "veronica");
var places = new Array("muy lejos", "cerca", "muy cerca", "sitio nuevo",
                       "aqui no", "tu libreria", "tu habitacion", "otra habitacion");
var other = new Array("no hablo", "mujer gris esta callada", "desconozco", "mas tarde",
                      "ahora no", "no se", "mas tarde", "futuro incierto",
                      "esta oscuro", "no veo", "oscuro");

function consult() {

  var question, words, r;
  var i, today;

  // Make up and answer based on keywords in the question.

  inContact = false;
  question = document.forms[0].elements[0].value;
  question = clean(question);
  words = question.split(" ");
  r = Math.random();

  // No question?

  if (question == "") {
    answer = pickFromList(none);
    startReply();
    return false;
  }

  // Look for two words surrounding 'or' and pick one at random.

  if (words.length >= 3)
    for (i = 0; i < words.length - 3; i++)
      if (words[i + 1] == "o") {
        if (r < .5)
          answer = words[i];
        else
          answer = words[i + 2];
        startReply();
        return false;
      }

  // At random, give a ambiguous answer.

  if (r < .1)
    answer = pickFromList(other);

  // Time related question?

  else if (inList(words, "dia")) {         // Pick a random day.
    if (r < .7)
      answer = pickFromList(days);
    else
      answer = pickFromList(times);
  }
  else if (inList(words, "mes")) {    // Pick a random month.
    if (r < .7)
      answer = pickFromList(months);
    else
      answer = pickFromList(times);
  }
  else if (inList(words, "tiempo"))            // Pick random time.
    answer = Math.floor(Math.random() * 12) + 1;
  else if (inList(words, "cuando")) {          // Pick a random time.
    if (r < .3)
      answer = pickFromList(days);
    if (r < .4)
      answer = pickFromList(months);
    if (r < .5)
      answer = pickFromList(seasons);
    else
      answer = pickFromList(times);
  }

  // Asking for a number?

  else if (inList(words, "como") &&
      (inList(words, "pocos") ||
       inList(words, "a menudo") ||
       inList(words, "muchos") ||
       inList(words, "mucho"))) {
    answer = Math.round(Math.random() * 10) + 1;    // Give a one or two digit
    if (answer != 10 && r < .5)                     // random number.
      answer += Math.round(Math.random() * 10);
  }

  // Asking about a person?

  else if (inList(words, "quien"))
    answer = pickFromList(people);

  // Asking about a place?

  else if (inList(words, "donde"))
    answer = pickFromList(places);

  // Asking about a thing?

  else if (inList(words, "que"))
    answer = pickFromList(other);

  // Yes or no question?

  else if (inList(words, "soy") ||
           inList(words, "eres") ||
           inList(words, "es") ||
           inList(words, "hago") ||
           inList(words, "haces") ||
           inList(words, "hace") ||
           inList(words, "quiero") ||
           inList(words, "puedo") ||
           inList(words, "pude") ||
           inList(words, "habria") ||
           inList(words, "debo")) {
    if (r < .5)
      answer = "!";
    else
      answer = "~";
  }

  // All else failed, give a nice, ambiguous answer.

  if (answer == "")
    answer = pickFromList(other);
  startReply();
  return false;
}

function clean(s) {

  var i, c, t;
  var letters = "abcdefghijklmnopqrstuvwxyz";

  // Convert string to lower case.

  t = s.toLowerCase();

  // Expand any contractions.

  s = "";
  for (i = 0; i < t.length; i++) {
    if (t.substr(i, 2) == "'s") {
      s += " es";
      i++;
    }
    else if (t.substr(i, 2) == "'t") {
      s += " no";
      i++;
    }
    else if (t.substr(i, 3) == "'ll") {
      s += " quiero";
      i += 2;
    }
    else
      s += t.substr(i, 1);
  }

  // Replace any non-letters with spaces.

  t = "";
  for (i = 0; i < s.length; i++) {
    c = s.substr(i, 1);
    if (letters.indexOf(c) >= 0)
      t += c;
    else
      t += " ";
  }
  return t;
}

function inList(list, word) {

  var i;

  // Return true if the given word is in the list.

  for (i = 0; i < list.length; i++)
    if (list[i] == word)
      return true;
  return false;
}

function pickFromList(list) {

  var r;

  // Return a random word from a list.

  r = Math.floor(Math.random() * list.length);
  return list[r];
}

