var isIE4 = false; isNN4 = false;

// codes for puzzle onsite
// --- begin ---
function defineImages() {
  picPool[0] = new picPara("laputa1.jpg",540,420,5,4,10,"image from Laputa, 540x420, 5x4, 24kb");
  picPool[1] = new picPara("laputa2.jpg",360,450,4,5,10,"image from Laputa, 360x450, 4x5, 26kb");
  picPool[2] = new picPara("laputa3.jpg",640,480,5,4,10,"image from Laputa, 640x480, 5x4, 45kb");
  picPool[3] = new picPara("laputa4.jpg",450,350,6,5,10,"image from Laputa, 450x350, 6x5, 20kb");
}

var picPool = new Array();
var pic_image_source = "";
var pic_image_width = 0;
var pic_image_height = 0;
var pic_grip_x = 1;
var pic_grip_y = 1;
var pic_table_gap = 0;

var puzzle_window = null;

function mouseHover() { window.status = picPool[this.index].info; return true; }
function mouseGone() { window.status = window.defaultStatus; return true; }
function mouseClick() { goPuzzleX(this.index); return false; }

function picPara(image_source,image_width,image_height,grip_x,grip_y,table_gap,image_info) {
  this.source = image_source;
  this.width = image_width;
  this.height = image_height;
  this.grip_x = grip_x;
  this.grip_y = grip_y;
  this.table_gap = table_gap;
  this.info = image_info;
}

function setupImages() {
  defineImages();

  var linksCount = document.links.length, hashIndex = -1, puzzleIndex = -1, hashTag = "puzzlex";
  for (var i = 0; i < linksCount; i++) {
    hashIndex = document.links[i].hash.indexOf(hashTag);
    if (hashIndex != -1) {
      puzzleIndex = parseInt(document.links[i].hash.substring(hashIndex+7));
      document.links[i].index = puzzleIndex;
      document.links[i].onmouseover = mouseHover;
      document.links[i].onmouseout = mouseGone;
      document.links[i].onclick = mouseClick;
    }
  }
}

function Build_Puzzle() {
  var window_para = "width=" + (pic_image_width + pic_table_gap * 6) +",height=" + (pic_image_height + pic_table_gap * 6) + ",titlebar=1";

  if (isIE4 || isNN4) {
    if (puzzle_window != null) {
      if (!puzzle_window.closed) { puzzle_window.close(); }
    }

    puzzle_window = window.open("puzzlex/puzzlex.html", "Puzzle_Window", window_para);
  }
  else {
    versionAlert();
  }
}
// --- end ---

function versionAlert() { alert("Sorry !\nYou need a version 4 or above browser."); }

function goPuzzleX(pic_index) {
  pic_image_source = picPool[pic_index].source;
  pic_image_width = picPool[pic_index].width;
  pic_image_height = picPool[pic_index].height;
  pic_grip_x = picPool[pic_index].grip_x;
  pic_grip_y = picPool[pic_index].grip_y;
  pic_table_gap = picPool[pic_index].table_gap;

  Build_Puzzle();
}

function goPuzzle() {
  if (isIE4) {
    window.open("puzzle/puzzleIE.html", "Puzzle", "width=720,height=560,resizable,titlebar=1");
  }
  else if (isNN4) {
    window.location.href = "puzzle/puzzle.zip";
  }
  else {
    versionAlert();
  }
}

function goTetris2() {
  if (isNN4) {
    window.open("tetris2/tetrisNN2.html", "Tetris3", "width=564,height=564,resizable,status,titlebar=1");
  }
  else if (isIE4) {
    window.open("tetris2/tetrisIE2.html", "Tetris3", "width=564,height=564,resizable,status,titlebar=1");
  }
  else {
    versionAlert();
  }
}

function goSnake() {
  if (isNN4) {
    window.open("snake/game.htm", "Snake", "width=220,height=300,resizable,titlebar=1");
  }
  else if (isIE4) {
    window.open("snake/game.htm", "Snake", "width=220,height=300,resizable,titlebar=1");
  }
  else {
    versionAlert();
  }
}

function goTetris() {
  if (isNN4) {
    window.open("tetris/tetrisNN.html", "Tetris", "width=360,height=400,resizable,titlebar=1");
  }
  else if (isIE4) {
    window.open("tetris/tetrisIE.html", "Tetris", "width=360,height=400,resizable,titlebar=1");
  }
  else {
    versionAlert();
  }
}

function goWarship() {
  window.open("warshipx/yx_ship.html", "", "WIDTH=580,HEIGHT=500,TITLEBAR=1");
}

function goBubble() {
  if (isNN4 || isIE4) {
    window.open("bubble/bubble.html", "Bubble", "width=300,height=400,titlebar=1");
  }
  else {
    versionAlert();
  }
}

function goMessageRoll() {
  if (isNN4 || isIE4) {
    window.open("message/sample_roll.html", "Message", "width=560,height=480,resizable,titlebar=1");
  }
  else {
    versionAlert();
  }
}

function goMessageBox() {
  if (isNN4 || isIE4) {
    window.open("message/sample_box.html", "Message", "width=560,height=480,resizable,titlebar=1");
  }
  else {
    versionAlert();
  }
}

function goMenuG2x() {
  if (isNN4 || isIE4) {
    window.open("menuG2/indexx.html", "MenuG2x", "scrollbars,toolbar,resizable,status,titlebar=1");
  }
  else {
    versionAlert();
  }
}

function goMenuG2() {
  if (isNN4 || isIE4) {
    window.open("menuG2/index.html", "MenuG2", "scrollbars,toolbar,resizable,status,titlebar=1");
  }
  else {
    versionAlert();
  }
}

function goMenu() {
  if (isNN4 || isIE4) {
    window.open("menu/index.html", "Menu", "scrollbars,toolbar,resizable,status,titlebar=1");
  }
  else {
    versionAlert();
  }
}

function goFolder() {
  window.open("folder/folder_sample-v3.html", "Folder", "scrollbars,toolbar,resizable,status,titlebar=1");
}

function initDoc() {
  isIE4 = (document.all)?true:false; isNN4 = (document.layers)?true:false;
  setupImages();
}

