
var line1 = null, line2 = null, lines = null;
var decorationImage = null, decorationBox = null, decorationImageBackground = null;
var line1Color = null, line2Color = null;
var bodyColor = null, decorationBoxColor = null, decorationImageBackgroundColor = null;
var step = 0;

function intro (delay)
{
  ++step;
  if (step <= 50) {
    var t = step / 50;
    var tc = 1 - t;
    var tcs1 = Math.pow(tc, 0.67);
    var tcs2 = Math.pow(tc, 3);
    line1.style.left = (1000 * tcs1) + "px";
    line1.style.letterSpacing = (1000 * tcs2) + "px";
    var p1 = Math.pow(t, 2);
    var p2 = Math.sqrt(t);
    var p3 = 1.0;
    line1.style.color = mixColor([0, 0, 0], line1Color, [p1, p2, p3]);
    makeVisible(line1);
    if (step == 50) {
      line1.style.fontStyle = "normal";
    }
  } else if (step == 58) {
      lines.style.marginLeft = "0px";
      lines.style.borderWidth = "1px";
  } else if (step <= 60) {
    ;
  } else if (step <= 110) {
    var t = (step - 60) / 50;
    var tc = 1 - t;
    var tcs = Math.pow(tc, 5);
    line2.style.left = (1000 * tcs) + "px";
    line2.style.letterSpacing = (1000 * tcs) + "px";
    var p1 = Math.pow(t, 0.2);
    var p2 = Math.pow((1 - Math.cos(t * Math.PI)) / 2, 2);
    var exp = 1000;
    var p3 = (1 - Math.pow(exp, t)) / (1 - exp);
    line2.style.color = mixColor([0, 0, 0], line2Color, [p1, p2, p3]);
    makeVisible(line2);
    if (step == 110) {
      line2.style.fontStyle = "normal";
    }
  } else if (step <= 120) {
    ;
  } else if (step <= 170) {
    if (step % 1 == 0) {
      var t = (step - 120) / 50;
      t = t*t*t;
      var tc = 1 - t;
      decorationImage.style.left = (20 * tc) + "%";
      decorationImage.style.top = (-10 * Math.pow(tc, 2)) + "%";
      decorationImage.style.width = (100 * Math.pow(t, 1)) + "%";
      decorationImage.style.height = (200 * t) + "px";
      makeVisible(decorationImage);
      var ts = Math.pow(Math.max(2*(t - 1) + 1, 0), 2);
      for (var i = 0; i < 3; ++i) {
        decorationImageBackground[i].style.backgroundColor = mixColor(bodyColor, decorationImageBackgroundColor[i], ts);
        makeVisible(decorationImageBackground[i]);
      }
    }
  } else if (step <= 180) {
    ;
  } else if (step <= 190) {
    if (step % 2 == 0) {
      var t = (step - 180) / 10;
      for (var i = 0; i < 3; ++i) {
        decorationBox[i].style.backgroundColor = mixColor(bodyColor, decorationBoxColor[i], t);
        makeVisible(decorationBox[i]);
      }
    }
  } else if (step <= 200) {
    ;
  } else {
    document.location = "start";
    return;
  }
  setTimeout("intro(" + delay + ");", delay);
}

function initIntro () {
  line1 = getAndHide("line1");
  line2 = getAndHide("line2");
  decorationImage = getAndHide("decorationImage");
  decorationBox = [
    getAndHide("decorationBox1"),
    getAndHide("decorationBox2"),
    getAndHide("decorationBox3")
  ];
  decorationImageBackground = [
    getAndHide("decorationImageBackground1"),
    getAndHide("decorationImageBackground2"),
    getAndHide("decorationImageBackground3")
  ];
  lines = line1.parentNode;
  lines.style.borderWidth = "0px";
  lines.style.marginLeft = "1px";
  lines.style.fontStyle = "italic";
  line1Color = rgb("f7efff");
  line2Color = rgb("f7efff");
  bodyColor = rgb("003068");
  decorationBoxColor = [ rgb("00468c"), rgb("003f84"), rgb("003d7b") ];
  decorationImageBackgroundColor = [ rgb("80a6cf"), rgb("548eb3"), rgb("548eb3") ];
  intro(20);
}

function getAndHide (id) {
  var element = document.getElementById(id);
  element.style.visibility = "hidden";
  return element;
}

function rgb ()
{
  var result = [ ];
  if (arguments.length == 3) {
    var i = 0;
    result.push(hex(arguments[i++]) / 255);
    result.push(hex(arguments[i++]) / 255);
    result.push(hex(arguments[i++]) / 255);
  } else if (typeof arguments[0] == "string" && arguments[0].length == 6) {
    var pos = -2;
    result.push(hex(arguments[0].substr(pos += 2, 2)));
    result.push(hex(arguments[0].substr(pos += 2, 2)));
    result.push(hex(arguments[0].substr(pos += 2, 2)));
  }
  return result;
}

function hex (value)
{
  if (typeof value == "number") {
    return value;
  }
  var string = new String(value), result = 0;
  for (var i = 0; i < string.length; ++i) {
    var digit = "0123456789abcdef".indexOf(string.charAt(i).toLowerCase());
    if (digit < 0) {
      break;
    }
    result = result * 0x10 + digit;
  }
  return result;
}

function mixColor (color1, color2, mix)
{
  var result = "rgb(", separator = "";
  for (var i = 0; i < 3; ++i) {
    result = result + separator + Math.round(color1[i] + (color2[i] - color1[i]) * (mix[i] || mix));
    separator = ", ";
  }
  result += ")";
  return result;
}

function makeVisible (element)
{
  if (element.style.visibility != "visible") {
    element.style.visibility = "visible";
  }
}
