if ((typeof refresh_detect) == 'undefined') {
  var refresh_detect = true;
}
if ((typeof scrollreset) == 'undefined') {
  var scrollreset = true;
}
if ((typeof mp3player_show) == 'undefined') {
  var mp3player_show = true;
}

function frameset(loc) {
  if (top.frames.length <= 0) {
    var s = top.location.href;
    var p = s.indexOf("#");
    if (p > 0) {
      loc = loc + "&a=" + s.substr(p + 1);
    }
    if (window.location.href.replace) {
      top.location.replace(loc);
    } else {
      top.location.href = loc;
    }
  }
}

function refresh_prepare() {
  if (refresh_detect) {
    var today = new Date();
    var now = today.getUTCSeconds();
    document.cookie = "SHTST=" + now + ";";
    document.cookie = "SHTSP=" + escape(location.href) + ";";
    document.cookie = "SHTSF=" + escape(top.frames[1].location.href) + ";";
  } else {
    document.cookie = "SHTST=;";
    document.cookie = "SHTSP=;";
    document.cookie = "SHTSF=;";
  }
}

function refresh_disable() {
  refresh_detect = false;
  return true;
}

function refresh_check() {
  var cookie = document.cookie;
  var cookieArray = cookie.split("; ");

  for(var i = 0; i < cookieArray.length; i++) {
    var nameValue = cookieArray[i].split("=");
    if (nameValue[0].toString() == "SHTST") {
      var cookieTime = parseInt(nameValue[1]);
    } else if (nameValue[0].toString() == "SHTSP") {
      var cookieName = nameValue[1];
    } else if (nameValue[0].toString() == "SHTSF") {
      var cookieFrame = nameValue[1];
    }
  }

  var today = new Date();
  var now = today.getUTCSeconds();
  var url = unescape(cookieFrame);
  url = linktoframe(url);

  if (cookieName && cookieTime && cookieFrame &&
      cookieName == escape(top.location.href) &&
      url != top.location.href && Math.abs(now - cookieTime) < 5) {
    if (top.location.href.replace) {
      top.location.replace(url);
    } else {
      top.location.href = url;
    }
  } else {
    if((typeof top.attachEvent) != 'undefined') {
      top.attachEvent('onunload', refresh_prepare);
    } else {
      top.onunload = refresh_prepare;
    }
  }
}

function linktoframe(link) {
  var host = "";
  var path = link;

  var i = link.lastIndexOf("/");
  if (i > 0) {
    host = link.substr(0, i + 1);
    path = link.substr(i + 1);
  }

  var p = path.split("?");
  if (p.length > 0 && ((typeof p[0]) != 'undefined') && p[0] != "") {
     var r = "?frame=" + p[0].replace(/\.php/, "");
     if (p.length > 1 && ((typeof p[1]) != 'undefined') && p[1] != "") {
       var a = p[1].split("#");
       if (a.length > 1) {
         r = r + "&" + a[0] + "&a=" + a[1];
       } else {
         r = r + "&" + p[1];
       }
     }
     return host + r;
  }

  return host + path;
}

function savescroll() {
  if (scrollreset) {
    var sx, sy;
    if (document.all) {
      if (!document.documentElement.scrollLeft) {
        sx = document.body.scrollLeft;
      }else{
        sx = document.documentElement.scrollLeft;
      }
      if (!document.documentElement.scrollTop) {
        sy = document.body.scrollTop;
      } else {
        sy = document.documentElement.scrollTop;
      }
    } else {
      sx = window.pageXOffset;
      sy = window.pageYOffset;
    }
    window.name = String(sx) + "|" + String(sy);
  }
}

function resetscroll() {
  if (scrollreset) {
    var p = window.name.split("|");
    var sx = Number(p[0]);
    var sy = Number(p[1]);
    var loc = String(document.location);
    if (!isNaN(sx) && !isNaN(sy) && loc.indexOf("#") < 0) {
      window.scrollTo(sx, sy);
    }
  }
}

function scrollreset_disable() {
  scrollreset = false;
  return true;
}

function mainheight() {
  var menu = getById('menucell');
  var main = getById('maindiv3');
  if ((menu) && (main)) {
    var menuh = menu.offsetHeight - 15;
    var mainh = main.offsetHeight;
    if (menuh > mainh) {
      main.style.height = menuh + "px";
    }
  }
}

function getbgmframe() {
  if (top.bgm) {
    return top.bgm;
  } else {
    return window.frames['bgm'];
  }
}

function getmp3player() {
  var bgmframe = getbgmframe();
  if (bgmframe) {
    if (document.getElementById)
      return bgmframe.document.getElementById('mp3');
    else {
      return bgmframe.document.all['mp3'];
    }
  }
  return null;
}

function stopmp3player() {
  var mp3player = getmp3player();
  if (mp3player) {
    mp3player_show = false;
    mp3player.innerHTML = "<img src='images/playerpale.png' style='margin-top:1px;margin-left:1px;' alt=''>";
  }
}

function reloadmp3player() {
  var mp3player = getmp3player();
  var bgmframe = getbgmframe();
  if (bgmframe && mp3player && mp3player_show) {
    var s = mp3player.innerHTML;
    if (s.substr(0, 4).toLowerCase() == '<img') {
      bgmframe.location.reload(true);
    }
  }
}

window.onload = function() {
  mainheight();
  resetscroll();
  reloadmp3player();
}

window.onscroll = savescroll;
window.onkeypress = savescroll;
window.onclick = savescroll;

