// AT Desktop Misc START

var MIDI_disabled, MIDI_url, MIDI_stopped_special

function ATLoadMIDI(p, src) {
  if (MIDI_disabled)
    return true

  if (!wmp7)
    return false

  p = p.replace(/[\#\?].*$/, "").replace(/\/\w+\.\w+$/, "/")
  if (!/\/$/.test(p))
    p += "/"
  p = p.replace(/^.+(Anime\%20Theme|animetheme.com)\//i, "")

  var r = /\.\.\//
  while (r.test(src)) {
    p = p.replace(/(^|\/)[^\/]+\/$/, "/")
    src = src.replace(r, "")
  }
  p = p.replace(/^\//, "")

  src = p + src
  try {
    if (MIDI_url == src) {
      if (MIDI_stopped_special && midi.controls.isAvailable("play"))
        midi.controls.play()
    }
    else {
      MIDI_url = src
      theme_txt = ""

      midi.URL = src
      Seq.item("Check_MIDI").Play()
    }
  }
  catch (err) {}
  MIDI_stopped_special = false

  return true
}

function direct_js() {
  if (http_online && (!member_name || !/^(AnimeTheme)$/.test(member_name))) {
    alert("Only authorized persons are allowed to use JavaScript here for security reasons.")
    return
  }

  var js = prompt("Please enter a single-line JavaScript command. Press CANCEL to return.","")
  if (!js)
    return

  execScript(js, "JavaScript")
}

// Random Title BG

function checkDirectSelection() {
  if (!http_online || ATPM)
    return 1

  if (confirm("Direct selection of Desktop Lady/Guy and Title Background is available to Anime Theme Premier members ONLY.\n\nPress OK for membership details, or CANCEL to load a random selection instead.")) {
    setTimeout('autoRunMain("atp_members/atp_signup.html")', 0)
    return 0
  }

  return -1
}

function randomTitleBG() {
  var num
  var max = title_bg_max

  if (confirm("Current Title Background #: " + title_bg_num + "/" + max + "\n\nPress OK for random, or CANCEL for direct selection."))
    num = random(title_bg_max)
  else {
    var result = checkDirectSelection()
    if (result == 0)
      return
    if (result == -1)
      num = random(title_bg_max)
    else {
      num = parseInt(prompt("Enter a number(0 - " + (max-1) + ").", title_bg_num))
      if ((isNaN(num)) || (num < 0) || (num >= max))
        return false
    }
  }

  title_bg_num = num

  var img_src = "temp/title_bg/" + addZero(num+1, 4) + ".png"
  if (use_AIL)
    Ltitle_bg.filters[0].src = img_src
  else
    Ltitle_bg.style.backgroundImage = 'url(' + img_src + ')'
}

function TcontextMenu(ev) {
  if (!loaded)
    return false

  var e
  if (ie_dom) {
    ev = event
    e = ev.srcElement
  }
  else
    e = ev.target

  hideContext()

  var x = ev.clientX
  var y = ev.clientY
  var m_w = LTcontext_menu.offsetWidth
  var m_h = LTcontext_menu.offsetHeight
  var m_x = (x > sw - m_w) ? x - m_w : x
  var m_y = (y > sh - m_h) ? y - m_h : y

  var ms = LTcontext_menu.style
  ms.posLeft = m_x
  ms.posTop = m_y
  ms.visibility = "inherit"

  ev.cancelBubble = true
  return false
}

// Error handling

var first_error = true

function error(msg, filename, line) {
  if (/unspecified|permission|syntax|error loading script/i.test(msg) || /addthis\.com/i.test(filename))
    return true

  if (first_error) {
    if (confirm("An error has occurred in the script on this page.\n\nLine: " + line + "\nError: " + msg + "\n\n" + "Press OK to send me an automatic bug report, or press CANCEL to continue."))
      window.open("mailto:webmaster@animetheme.com?body=" + filename + "%0A" + line + "%0A" + escape(msg) + "%0A" + navigator.appName + " " + navigator.appVersion)
    first_error = false
  }

  return true
}

onerror = error

// Show busy status

function AT_show_busy(busy) {
  var vis = (busy) ? "inherit" : "hidden"

  document.getElementById("Ibusy01").style.visibility = vis
  document.getElementById("Ibusy02").style.visibility = vis
}

// Desktop Lady messages

var guide_init_msg = "Welcome to Anime Theme~!"
var guide_new_g_msg = "Hello! Are you waiting for me? ^_^"
var guide_new_gs_msg = "Hi... oh please don't look at me like this ^_^;"

// Desktop Lady

var frame = -1
var current_action, msgs, guide_img, guide_x, guide_y, guide_width, guide_height, dummy_guide
var g_prefix = "g"

var PI = Math.PI
var circle = []
for (i = 0; i < 40; i++) {
  var rad = (i+1) / 40 * 2 * PI
  circle[2 * i] = -(Math.round(Math.cos(rad) * 25) - 25)
  circle[2 * i + 1] = -Math.round(Math.sin(rad) * 10)
}

var sp_circle = []
for (i = 1; i <= 20; i++) {
  var rad = (i/20 * 2 + 1.5) * PI
  sp_circle[2 * i] = Math.cos(rad)
  sp_circle[2 * i + 1] = Math.sin(rad)
}

var gf_prefix, gf_num

function clickGuide(e) {
  hideContext()
  if (!/(gs?)(-?\d+)\.gif/i.test(Iguide.src))
    return

  gf_prefix = RegExp.$1
  gf_num = parseInt(RegExp.$2)

  var flags = eval(gf_prefix + ((gf_num < 0) ? "m" : "") + "_fav_flags")

  if (flags[gf_num]) {
    Lguide_mark.style.display = "none"
    Lguide_unmark.style.display = "block"
    Sunmark.innerText = gf_prefix + gf_num
  }
  else {
    Lguide_mark.style.display = "block"
    Smark.innerText = gf_prefix + gf_num
    Lguide_unmark.style.display = "none"
  }
  Lguide_mark_group.style.display = (guide_string && !/Favorites|^Best Collection/.test(guide_string)) ? "block" : "none"
  Lguide_clear.style.display = (g_favorites.length || gs_favorites.length) ? "block" : "none"

  var guide_type, guides_type
  if (gf_num < 0) {
    guide_type = "Guy"
    guides_type = "Guys"
  }
  else {
    guide_type = "Lady"
    guides_type = "Ladies"
  }
  for (var i = 0; i < 3; i++)
    document.getElementById("Sguide_type" + i).innerText = guide_type
  document.getElementById("Sguides_type").innerText = guides_type

  var ds = Lguide_menu.style
  ds.posLeft = e.clientX
  ds.posTop = e.clientY
  ds.visibility = "inherit"
}

function confirmGS() {
  var msg =
  'WARNING: Some images in this section may contain scenes of mild nudity and adult nature. This section is for ADULTS ONLY!\n\n'
+ 'NOTE: There is NO erotic or Hentai stuff in this section.\n\n'
+ 'Press OK to return, or CANCEL to continue (only for adults).'

  return !confirm(msg)
}

function next_guide(sexy_guide, cancel_if_frame) {
  if (frame && cancel_if_frame) {
    alert("Please wait until the guide is idle.")
    return false
  }

  var num, max, new_prefix
  var count = 0

  if (sexy_guide) {
    if (g_prefix == "gs")
      count = guide_img
    else
      new_prefix = "gs"
    max = guide_sexy_max
  }
  else {
    if (g_prefix == "g")
      count = guide_img
    else
      new_prefix = "g"
    max = guide_max
  }

  if (guide_string) {
    guideArray(guide_string)
    max = guide_arrays[guide_string].length
    count = 0
  }

  if (count < 0)
    count = 0

  if (max == 1)
    num = 0
  else if (confirm("There are " + max + " Desktop Guides in this category.\n\nPress OK for random, or CANCEL for direct selection."))
    num = null
  else {
    var result = checkDirectSelection()
    if (result == 0) {
      guide_string = ""
      return false
    }
    if (result == -1)
      num = null
    else {
      num = parseInt(prompt("Enter a number(0 - " + (max-1) + ").", count))
      if ((isNaN(num)) || (num < 0) || (num >= max)) {
        guide_string = ""
        return false
      }
    }
  }

  if (new_prefix)
    g_prefix = new_prefix
  dummy_guide = new Image()
  dummy_guide.onload = function () { setTimeout("dummy_loaded()", 100) }
  var g_target = (num == null) ? random_guide() : random_guide(num)
  var g_event = 'dummy_guide.src = "' + g_target + '"'
  if (frame)
    ending_action = g_event + '; Ikiss.style.visibility="hidden"'
  else
    setTimeout(g_event, 0)

  AT_show_busy(true)
  return true
}

function dummy_loaded() {
  AT_show_busy()

  guide_width = parseInt(dummy_guide.width)
  guide_height = parseInt(dummy_guide.height)

  Iguide.src = dummy_guide.src
  Iguide.style.pixelWidth = guide_width
  Iguide.style.pixelHeight = guide_height
  if (use_AIL)
    Iguide.filters(0).src = Iguide.src

  if (!frame) {
    var msg = (g_prefix == "g") ? guide_new_g_msg : guide_new_gs_msg
    activate_guide('init_guide(["'+msg+'"], report)')
  }
}

function random_guide(num) {
// num = 0
  if (guide_string)
    var g_a = guide_arrays[guide_string]

  if (num == null) {
    var max
    if (guide_string)
      max = g_a.length
    else
      max = (g_prefix == "g") ? guide_max : guide_sexy_max
    guide_img = random(max)
  }
  else
    guide_img = num

  if (guide_string)
    guide_img = g_a[guide_img]

  var s = (guide_img < 0) ? "-" : ""
  var gi = Math.abs(guide_img)
  s += ((gi < 10) ? "0" : "") + gi

  return "_guides/" + g_prefix + s + ".gif"
}

function random(num) {
  return Math.floor(Math.random() * num)
}

var guide_string = ""
var guide_arrays = []

function guideArray(str) {
  if (guide_arrays[str])
    return guide_arrays[str]

  var a = []
  var c = g_class[str]
  for (var i = 0; i < c.length; i++) {
    var v = c[i]
    if (/^-?\d+$/.test(v)) {
      a[a.length] = v
      continue
    }

    if (/^\D/.test(v)) {
      guideArray(v)
      a = a.concat(guide_arrays[v])
      continue
    }

    var sign = 1
    if (/!$/.test(v)) {
      sign = -1
      v = v.substring(0, v.length-1)
    }
    var min = parseInt(v)
    var max = parseInt(v.substring(v.indexOf("-")+1, v.length))
    for (var k = min; k <= max; k++)
      a[a.length] = k * sign
  }

  guide_arrays[str] = a
}

function move_guide(g, w, frame) {
  if (g.posLeft < -guide_width) {
    Seq.item("Guide").Stop()
    return
  }
  g.posLeft -= w / 20
  g.posTop += frame * 2 - 21
}

function spiral_guide(g, frame) {
  var f = frame / 20
  var r = ((screen.width-guide_width)/2 - guide_x) * f
  g.posTop = g_top - (guide_height*f)/2 + sp_circle[2*frame] * r
  g.posLeft = g_left - (guide_width*f)/2 + sp_circle[2*frame+1] * r
}

function show_guide(g, w, frame) {
  var f = frame / 20
  g.pixelWidth = guide_width * f
  g.pixelHeight = guide_height * f

  if (guide_path == 0)
    move_guide(g, w, frame)
  else
    spiral_guide(g, frame)
}

function place_cloud(c, g) {
  if (frame == 21) {
    guide_x = g.posLeft
    guide_y = g.posTop
  }
  c.posLeft = guide_x + guide_width + 10
  c.posTop = g.posTop + (guide_height - 151) / 2
}

var frame_offset

function report() {
  var return_value = (frame_offset != 21)
  if (report_core(50, frame_offset, return_value)) {
    current_action = show_list
    show_main_menu()
  }
}

function show_main_menu() {
  menu_state = 1
  msgs = msgs_chat
  frame_offset = frame - 50
  Lmenu_list.style.visibility = "inherit"

  if (w3c_dom && delayed_HTML)
    setTimeout('if (delayed_HTML) { Lmenu_content.innerHTML = delayed_HTML; delayed_HTML = "" }', 500)

  hide_window()
}

function leave_main_menu(msgs_leave) {
  menu_state = 2
  msgs = (msgs_leave) ? msgs_leave : msgs_full[2]
  frame_offset = frame + 1
  Lmenu_list.style.visibility = "hidden"
  hide_window()
}

var dialogue_frame = -1

function report_core(msg_delay, f_offset, endless_loop) {
  frame++
  var g = Iguide.style
  var c = Lcloud.style

  if (frame < 21) {
    show_guide(g, (screen.width - guide_x) / 2, frame)
    return false
  }

if (w3c_dom) g.visibility = "inherit"

  var f = frame - f_offset
  var ref_frame = f % msg_delay
  var msg_length = msgs.length * msg_delay - 10
  var within_first_loop = (f <= msg_length)
  if (endless_loop || within_first_loop) {
    if (f == 0)
      place_cloud(c, g)

    if (ref_frame == 0) {
      dialogue_frame = ((endless_loop) && (menu_state == 1) && (current_action != report)) ? Math.random() * msgs.length : f / msg_delay
      dialogue_frame = parseInt(dialogue_frame)
      Lcloud_msg.innerText = msgs[dialogue_frame]
      c.visibility = "inherit"
    }
    else {
      var hidden_frame = (msg_delay >= 50) ? 40 : msg_delay - 10
      if (ref_frame == hidden_frame)
        c.visibility = "hidden"
    }

    circling(g, (frame - 20) % 40)

    if (!within_first_loop && endless_loop)
      return true
  }
  else {
    c.visibility = "hidden"

    var mov_f = f-msg_length
    if (mov_f == 1) {
      if (/(gs?\d+)\.gif/i.test(Iguide.src)) {
        var ks = Ikiss.style
        ks.posLeft = g.posLeft + (random(guide_width/2)+guide_width/4) - 45
        ks.posTop = g.posTop + random(guide_height/4) - 27
        ks.visibility = "inherit"
      }
    }

    move_guide(g, guide_x+guide_width, mov_f)
  }
  return false
}

function goodbyeKiss() {
  if (Ikiss.style.visibility == "hidden")
    return

  onerror = function () { setTimeout('Ikiss.style.visibility="hidden"; onerror=error', 100); return true }
  if (ie_win) {
    Ikiss.filters.alpha.opacity = 100
    Ikiss.filters.alpha.enabled = 1
  }
  else if (w3c_dom)
    Ikiss.style.opacity = 1

  onerror = error
  Seq.item("GoodbyeKiss").Play()
}

function fadeAwayKiss() {
  if (ie_win)
    Ikiss.filters.alpha.opacity -= 10
  else if (w3c_dom)
    Ikiss.style.opacity -= 0.1
}

var menu_state, ending_action, hide_frame_num, msgs_full, msgs_chat, guide_func, guide_path, g_top, g_left
menu_state = -1
ending_action = ""

/*
menu_state
-1: Hidden
 0: Ready to show at the right timing
 1: Visible
 2: Hidding (guide leaving)
*/

function activate_guide(func) {
  if (!loaded)
    return

  guide_func = func + ((func.charAt(func.length-1) == ";") ? "" : ";") + ' Ikiss.style.visibility="hidden";'
  execScript(func, "JavaScript")
}

function init_guide(content, action, init_x, e_a) {
  var g = Iguide.style
  var c = Lcloud.style

  if (frame) {
    var leaving_msg = "OK I will be back with the service you requested. Please wait for a while~!"
    if (menu_state == -1 || menu_state == 2) {
      if (dialogue_frame < msgs.length - 1) {
        msgs = msgs.slice(0, dialogue_frame + ((c.visibility=="inherit") ? 1:2))
        msgs[msgs.length-1] = leaving_msg
        if (frame > 20)
          guide_says(leaving_msg)
      }
      else if (c.visibility=="inherit")
        guide_says(leaving_msg)
      ending_action = guide_func + ending_action
      return false
    }

    if (menu_state == 0) {
      msgs = msgs.slice(0, dialogue_frame).concat(content)
      if (c.visibility=="inherit")
        guide_says(msgs[0])
      if (e_a)
        ending_action = e_a + ((e_a.charAt(e_a.length-1) == ";") ? "" : ";") + ending_action
      return true
    }

    if (menu_state == 1) {
      if (action == report) {
        leave_main_menu([leaving_msg])
        ending_action = guide_func + ending_action
        return false
      }

      hide_window()
      eval(action_for_already_shown_menu)

      if (e_a)
        ending_action = e_a + ((e_a.charAt(e_a.length-1) == ";") ? "" : ";") + ending_action
      return true
    }
  }

  msgs = content

  guide_path = (Math.random() > 0.5) ? 1 : 0
  if (!init_x)
    init_x = 10 + 234
  guide_x = (screen.width - (guide_width + init_x)) / 2

  g_top = screen.height - (guide_height + 60)
  if (g_top > 100)
    g_top -= (g_top - 100) / 2

  if (guide_path == 0)
    g_left = guide_x + (screen.width - guide_x) / 2
  else {
    g_top += guide_height / 2
    g_left = screen.width / 2
  }

  g.posLeft = g_left
  g.posTop = g_top

  g.pixelHeight = 0
  g.pixelWidth = 0
  g.visibility = "inherit"

  frame_offset = 21
  current_action = action
  if (e_a)
    ending_action = e_a

  Seq.item("Guide").Play()
  return true
}

function build_list(id, content, list, e_a) {
  action_for_already_shown_menu = 'Lmenu_list.style.visibility = "inherit"'

  if (!init_guide(content[0], show_list, 50+250, e_a))
    return

  draw_list(id, list)

  msgs_full = content
  msgs_chat = content[1]

  if (menu_state == -1) {
    menu_state = 0
    hide_frame_num = -1
  }
}

var delayed_HTML

function draw_list(id, list) {
  if (Lmenu_content.menuID == id)
    return

  if (!/\[(B|C)/.test(list[list.length-2])) {
    if (/Main Menu|Guides List/.test(id)) {
      list[list.length] = "[Close]"
      list[list.length] = 'leave_main_menu()'
    }
    else {
      list[list.length] = "[Back]"
      list[list.length] = 'draw_list("Main Menu",dynamic_desktop_list)'
    }
  }

  var span = '<SPAN style="color:#CCFFFF; cursor:' + cursor + '" onMouseOut="this.style.color=\'#CCFFFF\'"  onMouseOver="this.style.color=\'#FFCCFF\'"  '
  var innerHTML = '<ul>'
  for (var i = 0; i < list.length/2; i++)
    innerHTML += '<li>' + span + 'onClick=\'' + list[2*i+1].replace(/'/g, "&apos;") + '\'>' + list[2*i] + '</SPAN></li>'
  innerHTML += '</ul>'

  Lmenu_content.menuID = id

  if ((w3c_dom) && (Lmenu_list.style.visibility == "hidden")) {
    Lmenu_content.innerHTML = "<p>Now Loading...</p>"
    delayed_HTML = innerHTML
  }
  else {
    Lmenu_content.innerHTML = innerHTML
    delayed_HTML = ""
  }
}

function guide_says(msg) {
  var g = Iguide.style
  var c = Lcloud.style
  hide_frame_num = frame + 30
  place_cloud(c, g)
  Lcloud_msg.innerText = msg
  c.visibility = "inherit"
}

function place_main_menu() {
  var w = guide_x + guide_width + 50
  Lmenu_list.style.posLeft = (w > sw-250) ? sw - 250 : w
}

var action_for_already_shown_menu

function show_list() {
  guide_shows_something(show_list_core)
}

function show_list_core() {
  if (Lmenu_list.style.visibility != "hidden")
    return

  place_main_menu()
  show_main_menu()
}

function guide_shows_something(func) {
  var count
  var g = Iguide.style
  var c = Lcloud.style

  if (frame < 20) {
    report_core()
    return
  }

  if (frame == hide_frame_num)
    c.visibility = "hidden"

  if (menu_state == 0) {
    if (report_core(40,21,true))
      func()
  }
  else if (menu_state == 1)
    report_core(200,frame_offset,true)
  else if (menu_state == 2)
    report_core(30,frame_offset)
}

function circling(g, count) {
  g.posLeft = guide_x + circle[2 * count]
  g.posTop = guide_y + circle[2 * count + 1]
}

var screen_num
if (screen.width == 640)
  screen_num = 1
else if (screen.width == 800)
  screen_num = 2
else
  screen_num = 3
var wall_timerID = null

function check_midi() {
  if (midi.controls.isAvailable("play"))
    Seq.item("Check_MIDI").Stop()
}

// Web Window

var windows_max = 0

function draw_window(id, txt) {
  windows_max++

  var close_b = (ie_dom) ? '12px;font-family:Webdings">r' : '14px;font-family:Comic Sans MS;font-weight:bold">x'
document.write(
  '<DIV id=Lwindow'+id+' class=Taskbar style="position:absolute;visibility:hidden">\n'
+ '<DIV id=Lcaption'+id+' style="position:absolute;top:2;left:2;background-color:ACTIVECAPTION;height:18;overflow:hidden">\n'
+ '<img src="rei_ico.gif" style="position:absolute;top:1;left:1;cursor:'+cursor+'" onClick="if (Lmenu_content.menuID) { show_main_menu(); guide_says(\'Please choose your desired service~!\') } else { leave_main_menu() }" onMouseOver="InfoMini_Over(\'Return to menu\', event)" onMouseOut="InfoMini_Out()">\n'
+ ((txt) ? '<span style="position:absolute;left:20;top:1;color:white;cursor:default; font-family:Arial;font-size:12px">' + txt + '</span>\n' : '')
+ '<div id=LcloseB'+id+' class=Taskbar style="position:absolute;top:2;width:' + ((w3c_dom) ? 12 : 16) + 'px;height:' + ((w3c_dom) ? 10 : 14) + 'px;padding:0;overflow:hidden;cursor:'+cursor+'" onClick="InfoMini_Out(); leave_main_menu()" onMouseOver="InfoMini_Over(\'Close\', event)" onMouseOut="InfoMini_Out()"><span style="position:relative;top:' + ((w3c_dom) ? -7 : -5) + 'px;left:' + ((w3c_dom) ? 1 : 0) + 'px;font-size:'+close_b+'</span></div>\n'
+ '</DIV>\n'
+ '<DIV id=Lwindow_content'+id+' style="position:absolute;top:22;left:2" onClick="var rv = autoRun(event); if (rv != null) return rv">'
)
}

function fix_windows() {
  for (var id = 0; id < windows_max; id++) {
    var c = self["Lwindow_content" + id]
    var w = c.offsetWidth
    var h = c.offsetHeight
    fix_window(id, w,h)
  }
}

function fix_window(id, w,h) {
  if (!w)
    w = 310
  if (!h)
    h = 400
  var lw = self["Lwindow" + id].style
  lw.pixelWidth = w + 8
  lw.pixelHeight = h + 28
  self["Lcaption" + id].style.pixelWidth = w
  self["LcloseB" + id].style.posLeft = w - 18
}

var active_window = null

function show_window(id, w_msgs) {
  hide_window()

  active_window = id
  place_window(id)

  if (windows_src[id]) {
    var c = self["Lwindow_content_iframe" + id]
    c.style.pixelWidth = 100
    c.style.pixelHeight = 100
    c.innerHTML = '<IFRAME SCROLLING=NO src="' + windows_src[id] + '" style="width:100%;height:100%"></IFRAME>'
    windows_src[id] = null
  }

  Lmenu_list.style.visibility = "hidden"
  visChange(self["Lwindow" + id], true)

  if (w_msgs) {
    msgs = w_msgs
    guide_says(w_msgs[0])
  }
}

function place_window(id) {
  var lw = self["Lwindow" + id].style
  var w = lw.pixelWidth
  var h = lw.pixelHeight
  var top = Lmenu_list.style.posTop
  var left = Lmenu_list.style.posLeft
  lw.posTop = (h > 320) ? top - (h - 320) / 2 : top
  lw.posLeft = (left + w > sw) ? sw - w : left
}

function hide_window() {
  if (active_window != null) {
    visChange(self["Lwindow" + active_window], false)
    active_window = null
  }
}

// normal Theme

function Theme(dir, bg, midi, txt) {
  if (dir)
    dir += "/"
  this.bg = dir + bg + ".jpg"
  this.midi = dir + midi + ((/\./.test(midi)) ? "" : ".mid" )
  this.txt = txt
}

var theme_num, theme_txt

var themes = [
  new Theme("","bg4","open",'Yanni\'s "Aria"'),
  new Theme("arrest","(bg|gallery/bg)","arrest",'You\'re Under Arrest OVA opening theme'),
  new Theme("","bastard/bg7","wallpaper/bastard8",'Contradanza'),
  new Theme("cc_sakura","bg","catch",'Card Captor Sakura OP "Catch You Catch Me"'),
  new Theme("chrono","bg","another",'Chrono Cross "Another World"'),

  new Theme("chrono","bg","intro",'Chrono Cross main theme'),
  new Theme("dbz","(bg2|bg25|gallery/bg)","dbz",'Dragon Ball Z opening theme'),
  new Theme("esca","bg39","esca1a",'Vision of Escaflowne opening theme'),
  new Theme("eva","(gallery/bg|bg24)","soul",'Neon Genesis Evangelion movie "Death and Rebirth" ED "Soul Refrain"'),
  new Theme("eva","bg_mana","yokan",'Neon Genesis Evangelion PC game "Girl Friend of Steel" ED "Presentiment"'),

  new Theme("","(eva/bg_mana|gallery/misc/bg5)","beautiful_life",'Japanese TV drama "Beautiful Life" OP "Tonight on the moon hills"'),
  new Theme("eva","bg","eva",'Neon Genesis Evangelion opening theme'),
  new Theme("eva","bg19","asuka",'Neon Genesis Evangelion "Asuka Strikes"'),
  new Theme("eva","bg17","rei",'Neon Genesis Evangelion - Rei theme'),
  new Theme("eva","bg18","moon",'"Fly Me To The Moon"'),

  new Theme("ff10","bg","ff10_main",'Final Fantasy X introduction theme'),
  new Theme("ff10","bg_fmv","ff10_sutekidane",'Final Fantasy X theme song "Suteki da Ne"'),
  new Theme("ffx2","bg","ffx2_yuna",'Final Fantasy X-2 - Yuna theme'),
  new Theme("ffx2","bg_fmv","ffx2_yuna_ballad",'Final Fantasy X-2 "Yuna\'s Ballad"'),
  new Theme("","(ff12/bg|ff12/bg_fmv)","ff7/ff",'"Final Fantasy"'),

  new Theme("ff7","bg1","main",'Final Fantasy VII main theme'),
  new Theme("ff7","bg2","tifa",'Final Fantasy VII - Tifa theme'),
  new Theme("ff7","bg3","aerith",'Final Fantasy VII - Aerith theme'),
  new Theme("ff7","bg9","ninja",'Final Fantasy VII - Yuffie theme'),
  new Theme("ff7","(bg8|bg_ff7_dc)","jenova",'Final Fantasy VII "J-E-N-O-V-A"'),

  new Theme("ff7","gallery/bg","gallery/ff7_last",'Final Fantasy VII "Last Day"'),
  new Theme("ff7","bg_ccff7","opening",'Final Fantasy VII opening theme'),
  new Theme("ff8","bg","ff8_battle",'Final Fantasy VIII battle theme'),
  new Theme("ff8/gallery","bg","ff8_chase",'Final Fantasy VIII "Never Look Back"'),
  new Theme("ff8","bg_fmv","ff8_eyes",'Final Fantasy VIII theme song "Eyes On Me"'),

  new Theme("ff8","bg_files","ff8_waltz",'Final Fantasy VIII "Waltz for the Moon"'),
  new Theme("ff9","bg","ff9_main",'Final Fantasy IX main theme'),
  new Theme("ff9","bg_fmv","melodies",'Final Fantasy IX ED "Melodies Of Life", piano solo version'),
  new Theme("ff9","bg_files","not_alone",'Final Fantasy IX "Not Alone"'),
  new Theme("goddess","bg10","goddess",'Oh My Goddess OP'),

  new Theme("haruhi","(bg|gallery/bg)","haruhi_hare_hare_yukai",'Haruhi Suzumiya ED "Hare Hare Yukai"'),
  new Theme("inuyasha","bg","change_the_world",'Inuyasha OP "Change The World"'),
  new Theme("kenshin","bg","kenshin_end",'Rurouni Kenshin ED'),
  new Theme("love_hina","(bg|bg_files)","sakura_saku",'Love Hina OP "Sakura Saku"'),
  new Theme("macross","bg1","m7_7moon",'Macross 7 OP "Seventh Moon"'),

  new Theme("macross","(bg0|gallery/bg_mf|gallery/bg_all)","macross",'Macross "Do You Remember Love?"'),
  new Theme("","macross/gallery/(bg|bg_all)","wallpaper/m10",'Macross 7 "Try Again"'),
  new Theme("maison_ikkoku","bg","mi_love",'Maison Ikkoku "A Bell Rings at Dawn"'),
  new Theme("mononoke","(bg34|gallery/bg)","mononoke",'The Princess Mononoke main theme'),
  new Theme("mononoke","bg_on_your_mark","on_your_mark",'Chage & Aska\'s "On Your Mark"'),

  new Theme("sailor","bg16","love",'Sailor Moon "Warrior of Love"'),
  new Theme("sakura","bg1","sakura",'Sakura Wars OP'),
  new Theme("slayers","bg22","reason",'Slayers Next main theme'),
  new Theme("","(tenchi/bg5|tenchi/gallery/bg)","wallpaper/tenchi12",'Tenchi Muyo OVA main theme'),
  new Theme("tenchi","bg11","ten_remi",'Tenchi Muyo OVA'),

  new Theme("","(cool/bg_cool|chage_aska/bg)","chage_aska/midi/say_yes",'Chage & Aska\'s "SAY YES"'),
  new Theme("","video/bg_misc","chage_aska/midi/n_darlin",'Chage & Aska\'s "no no darlin"'),
  new Theme("","gallery/misc/bg[12678]","chage_aska/midi/tyottosa",'Chage & Aska\'s "Tonight, a little embrace"'),
  new Theme("gallery/misc/","bg[12678]","lovesong",'Chage & Aska\'s "Love Song"'),
  new Theme("gallery","(bg|misc/bg4|misc/bg5)","epilogue",'Chage & Aska\'s "Epilogue"'),

  new Theme("dhtml","bg","raspberry",'Chage & Aska\'s "Wild Raspberry"'),
  new Theme("","bg_icocur","never",'"The Neverending Story"'),
  new Theme("","(dhtml/bg|bg_icocur)","tenchi/once",'Yanni\'s "Once Upon a Time"'),
  new Theme("","bg_sp","vktheme1",'"VK Theme"')

// MJ special
,new Theme("temp","_mj_bg_off_the_wall","_mj_off_the_wall",'Michael Jackson\'s "Off the Wall"')
]

function changeTheme() {
  if (!wmp7) {
    alert("This function requires Windows Media Player 7+.")
    return
  }

  var num
  var max = themes.length
  if (confirm("Press OK for random, or CANCEL for direct selection."))
    num = random(max)
  else {
    num = parseInt(prompt("Enter a number(0 - " + (max-1) + ").", theme_num))
    if ((isNaN(num)) || (num < 0) || (num >= max))
      return false
  }

  if (season_special)
    Ispecial_bg.style.visibility = "hidden"

  theme_num = num
  var t = themes[num]
  theme_txt = t.txt
  changeThemeBG(t)

  midi.URL = t.midi
  MIDI_url = t.midi
  Seq.item("Check_MIDI").Play()
}

function changeThemeBG(t) {
  var bg = t.bg

  var r = /\[(.+)\]/
  if (r.test(bg)) {
    var para = RegExp.$1
    bg = bg.replace(r, para.charAt(random(para.length)))
  }

  var r = /\((.+)\)/
  if (r.test(bg)) {
    var para = RegExp.$1.split("|")
    bg = bg.replace(r, para[random(para.length)])
  }

  document.body.style.backgroundImage = "url(" + bg + ")"
}

function showThemeText() {
  showMarqueeMsg('This random MIDI is from ' + themes[theme_num].txt + '.')
}

function showMarqueeMsg(txt) {
  if (ie_dom)
    Ltheme_txt.innerHTML = '<MARQUEE width=' + (sw-370) + ' LOOP=1 onFinish="this.style.display=\'none\'">' + txt + '</MARQUEE>'
  else {
    Ltheme_txt.style.pixelWidth = sw-370
    Ltheme_txt.innerHTML = '<span id=Sbar_msg style="position:relative; left:150px">' + txt + '</span>'
    setTimeout('document.getElementById("Sbar_msg").style.display="none"', 10*1000)
  }
}

// Random Wallpaper

var member_pass

var random_wallpaper_loading

var random_wallpaper_wait_time = 0

var screen_res = screen_num
if (screen_res >= 3) {
  if (screen.height > 1050)
    screen_res = 5
  else if (screen.height > 900)
    screen_res = 4
}

var random_bg_result_list = [
  "Login", 'alert("In order to use this <Random Wallpaper> feature, you need to login once (Anime Theme Premier Membership required) and let the computer remember your password (browser cookie function required)."); autoRunMain("http://www.animetheme.com/cgi-bin/ikonboard/loginout.cgi",event,this)',
  "Sign Up!<br>(Premier Membership)", 'autoRunMain("atp_members/atp_signup.html",event,this)',
  "Random Wallpaper (Demo)", 'alert("This is a demo of <Random Wallpaper>. A fixed wallpaper is loaded in this demo, but in the actual case, a RANDOM wallpaper from our huge and high-quality wallpaper gallery (over 8000 wallpapers in total) will be loaded!"); RandomWallpaper("temp/samples/misc_wall_027054_56122_" + screen_res + ".jpg")',
  "Wallpaper Gallery", 'autoRunMain("gallery/",event,this)'
]

function RandomWallpaper(src, dir, re) {
  if (random_wallpaper_loading) {
    alert("Please wait until the current wallpaper has been loaded.")
    return
  }
  if (bg_dummy || (loaded > 1)) { // Special wallpaper loading
    alert("Another wallpaper is loading.")
    return
  }
  if (screen_res < 2) {
    if (loaded)
      alert("Your screen resolution must be at least 800x600 to use this feature.")
    return
  }

  random_wallpaper_loading = true

  if (src) {
    document.cookie = "random_bg_result=OK|||"
    Ispecial_bg.style.visibility = "hidden"
    Ispecial_bg.src = src
  }
  else {
    IRW_thumbnail.style.visibility = "hidden"
    IRW_busy.style.visibility = "inherit"

    dir = (dir) ? "&dir=" + encodeURIComponent(dir) : ""
    re = (re) ? "&re=" + encodeURIComponent(re) : ""

    var command_url
    if (http_online)
      command_url = 'http://www.animetheme.com/cgi-bin/ikonboard/random_bg.cgi?res=' + screen_res + dir + re + '&dummy=' + Math.random()
    else {
      command_url = "empty.gif"
      document.cookie = "random_bg_result=offline|macross/gallery/|1|m" + addZero(random(50)) + ".jpg"
    }

    if (!loaded)
      return command_url

    setTimeout('IRW_dummy.src="' + command_url + '";', random_wallpaper_wait_time)
  }

  AT_show_busy(true)
}

function RandomWallpaper_Finished(first_one) {
  if (!loaded && !first_one)
    return
  if (!random_wallpaper_loading)
    return

  random_wallpaper_loading = false

  if (loaded)
    AT_show_busy()

  random_wallpaper_wait_time = 5 * 1000
  setTimeout('random_wallpaper_wait_time=0', random_wallpaper_wait_time)

  var random_bg_result, random_bg_dir, random_bg_page, random_bg_file

  var random_bg_cookie = cookie_get("random_bg_result", true)
  if (random_bg_cookie) {
    var paras = random_bg_cookie.split("|")

    random_bg_result = paras[0]
    random_bg_dir = paras[1]
    random_bg_page = paras[2]
    random_bg_file = paras[3]
  }

  if (random_bg_result == "OK") {
    setTimeout('place_bg(); Ispecial_bg.style.visibility = "inherit"', 100)

    Lmessage_custom_content.innerHTML =
  '<ul>\n'
+ '<li><a href="' + ((random_bg_dir) ? "http://www.animetheme.com/cgi-bin/ikonboard/download.cgi?src=" + random_bg_dir + random_bg_file : Ispecial_bg.src) + '">Download this wallpaper!</a></li>\n'
+ '<li><a href="' + ((random_bg_dir) ? random_bg_dir : "gallery/misc/") + '">Visit this gallery!</a></li>\n'
+ '</ul>\n'

    if (ie_win)
      LBG_Filter.filters.alpha.opacity = 50
    else if (w3c_dom)
      LBG_Filter.style.opacity = 0.50
  }
  else if (random_bg_result) {
    IRW_thumbnail.src = random_bg_dir + random_bg_file
    LRW_link01.href = random_bg_dir + 'index' + ((random_bg_page == 1) ? '' : random_bg_page) + '.html'
    LRW_link02.href = random_bg_dir
//+ '<p>NOTE: <a href="http://www.animetheme.com/cgi-bin/ikonboard/loginout.cgi">Login</a> as <a href="atp_members/atp_signup.html">Anime Theme Premier Member</a> to unlock the FULL features of "Random Wallpaper"!</p>\n'

    if (loaded)
      LRW.style.visibility = "inherit"
  }
  else {
    if (loaded)
      activate_guide('init_guide(["You need to enable the browser cookies function."], report)')
    return
  }

/*
  else {
    var msg
    if (random_bg_result == "EXPIRED")
      msg = "Your Premier membership has expired... :P"
    else if (random_bg_result == "ATPM_NEEDED")
      msg = "Only Anime Theme Premier members can use this feature... :P"
    else
      msg = "Invalid login... :P"

    activate_guide('build_list("Random BG Result List", [["' + msg + '"],["You need to login to use this feature~!"],["See ya~!"]], random_bg_result_list)')
  }
*/
}

function AdjustBGFilter() {
//    str += 'Lmessage_parent.style.pixelHeight = Lmessage_parent.offsetHeight - ((w3c_dom)?8:0)'
  setTimeout('LBG_Filter.style.pixelWidth = Lmessage_parent.offsetWidth - ((w3c_dom)?8:0); LBG_Filter.style.pixelHeight = Lmessage_parent.offsetHeight - ((w3c_dom)?8:0); LBG_Filter.style.display = "block";', 0)
}

var RW_thumbnail_mod_y = random(2)+1
var RW_thumbnail_mod_x = 3 - RW_thumbnail_mod_y
RW_thumbnail_mod_y *= (Math.random() < 0.5) ? -1 : 1
RW_thumbnail_mod_x *= (Math.random() < 0.5) ? -1 : 1

function RW_thumbnail_animate() {
  var iw = IRW_thumbnail.width
  var ih = IRW_thumbnail.height
  if (!iw)
    iw = 320
  if (!ih)
    ih = 240

  var w_mod
  if (iw < 300)
    w_mod = -1
  else if (ih < 200)
    w_mod = 1
  else
    w_mod = 0

  var mw = 200 - iw
  var mh = 150 - ih

  var s = IRW_thumbnail.style
  var top  = s.posTop
  var left = s.posLeft

  top  += parseInt(RW_thumbnail_mod_y)
  left += parseInt(RW_thumbnail_mod_x)

  var mod_x, mod_y

  if ((top > 0) || (top < mh)) {
    mod_y = (RW_thumbnail_mod_y > 0) ? -1 : 1
    top = (top < 0) ? mh : 0
  }
  if ((left > 0) || (left < mw)) {
    mod_x = (RW_thumbnail_mod_x > 0) ? -1 : 1
    left = (left < 0) ? mw : 0
  }

  if (mod_y || mod_x) {
    if (!mod_y)
      mod_y = (RW_thumbnail_mod_y < 0) ? -1 : 1
    if (!mod_x)
      mod_x = (RW_thumbnail_mod_x < 0) ? -1 : 1

    RW_thumbnail_mod_y = (random(((mod_y && mod_x) ? 2 : 3)) + ((mod_y) ? 1 : 0) + w_mod) + 0.1
    if (RW_thumbnail_mod_y > 3)
      RW_thumbnail_mod_y = 3
    else if (RW_thumbnail_mod_y < 0)
      RW_thumbnail_mod_y = 0
    RW_thumbnail_mod_x = (3 - RW_thumbnail_mod_y) + 0.1

    RW_thumbnail_mod_y *= mod_y
    RW_thumbnail_mod_x *= mod_x
  }

  s.posTop  = top
  s.posLeft = left
}

// Special Theme

var active_midi

function draw_specialBG() {
  if (season_special) {
    theme_num = 0
    bg_id = random(bg_wall_max)
    active_midi = "xmas.mid"

    document.write('<img id=Ispecial_bg style="position:absolute;top:0;left:0;filter:revealTrans;visibility:hidden" src="wallpaper/'+bg_string+addZero(bg_id)+'_'+screen_num+'.jpg" onLoad="RandomWallpaper_Finished()">')
  }
  else {
    theme_num = random(themes.length)

// MJ special
// theme_num=themes.length-1

    var t = themes[theme_num]
    active_midi = t.midi
    changeThemeBG(t)

    document.write('<img id=Ispecial_bg style="position:absolute;top:0;left:0;visibility:hidden" src="empty.gif" onLoad="RandomWallpaper_Finished()">')
  }
}

var season_special// = true

if (w3c_dom && season_special)
  document.write('<script language="JavaScript" src="../n6_filters.js"></sc'+'ript>')

var bg_string
if (season_special)
  bg_string = "bg_xmas"

var bg_id, bg_dummy

var bg_loading = [
  'The image is now loading. Please wait for a while. You can press "Close" to stop loading.',
  'Please wait... this may take some time ^_^',
  'Still loading... ^_^;'
]

var choose_xmas_bg = [
  "Asuka", 'load_newBG(1)',
  "Asuka & Rei", 'load_newBG(2)',
  "Comic Party (I)", 'load_newBG(3)',
  "Comic Party (II)", 'load_newBG(4)',
  "Digi Charat", 'load_newBG(36)',
  "Fate/stay night", 'load_newBG(52)',
  "Final Fantasy VII", 'load_newBG(0)',
  "Galaxy Angel", 'load_newBG(46)',
  "Gundam SEED", 'load_newBG(33)',
  "Hana Q", 'load_newBG(40)',
  "Haruhi Suzumiya (I)", 'load_newBG(45)',
  "Haruhi Suzumiya (II)", 'load_newBG(49)',
  "Kango Shicyauzo", 'load_newBG(9)',
  "Like Life", 'load_newBG(39)',
  "Love Hina", 'load_newBG(8)',
  "Lucky Star (I)", 'load_newBG(50)',
  "Lucky Star (II)", 'load_newBG(51)',
  "Lucky Star (III)", 'load_newBG(57)',
  "Mahou Sensei Negima!", 'load_newBG(62)',
  "Nadesico", 'load_newBG(5)',
  "Noir", 'load_newBG(6)',
  "Shakugan No Shana (I)", 'load_newBG(48)',
  "Shakugan No Shana (II)", 'load_newBG(54)',
  "Snow (I)", 'load_newBG(21)',
  "Snow (II)", 'load_newBG(22)',
  "Snow (III)", 'load_newBG(23)',
  "Snow (IV)", 'load_newBG(26)',
  "Snow (Misc)", 'load_newBG(24)',
  "Studio Mebius (I)", 'load_newBG(7)',
  "Studio Mebius (II)", 'load_newBG(10)',
  "Studio Mebius (III)", 'load_newBG(11)',
  "Studio Mebius (IV)", 'load_newBG(12)',
  "Studio Mebius (V)", 'load_newBG(20)',
  "T2 Art Works", 'load_newBG(42)',
  "Toaru Majutsu no Index (I)", 'load_newBG(66)',
  "Toaru Majutsu no Index (II)", 'load_newBG(67)',
  "Misc CG (I)", 'load_newBG(13)',
  "Misc CG (II)", 'load_newBG(14)',
  "Misc CG (III)", 'load_newBG(15)',
  "Misc CG (IV)", 'load_newBG(16)',
  "Misc CG (V)", 'load_newBG(17)',
  "Misc CG (VI)", 'load_newBG(18)',
  "Misc CG (VII)", 'load_newBG(19)',
  "Misc CG (VIII)", 'load_newBG(25)',
  "Misc CG (IX)", 'load_newBG(27)',
  "Misc CG (X)", 'load_newBG(28)',
  "Misc CG (XI)", 'load_newBG(29)',
  "Misc CG (XII)", 'load_newBG(30)',
  "Misc CG (XIII)", 'load_newBG(31)',
  "Misc CG (XIV)", 'load_newBG(32)',
  "Misc CG (XV)", 'load_newBG(34)',
  "Misc CG (XVI)", 'load_newBG(35)',
  "Misc CG (XVII)", 'load_newBG(37)',
  "Misc CG (XVIII)", 'load_newBG(38)',
  "Misc CG (XIX)", 'load_newBG(41)',
  "Misc CG (XX)", 'load_newBG(43)',
  "Misc CG (XXI)", 'load_newBG(44)',
  "Misc CG (XXII)", 'load_newBG(47)',
  "Misc CG (XXIII)", 'load_newBG(53)',
  "Misc CG (XXIV)", 'load_newBG(55)',
  "Misc CG (XXV)", 'load_newBG(56)',
  "Misc CG (XXVI)", 'load_newBG(58)',
  "Misc CG (XXVII)", 'load_newBG(59)',
  "Misc CG (XXVIII)", 'load_newBG(60)',
  "Misc CG (XXIX)", 'load_newBG(61)',
  "Misc CG (XXX)", 'load_newBG(63)',
  "Misc CG (XXXI)", 'load_newBG(64)',
  "Misc CG (XXXII)", 'load_newBG(65)'
]

var bg_wall_max = choose_xmas_bg.length / 2

function load_newBG(num) {
  if (random_wallpaper_loading) {
    guide_says("Random wallpaper is still loading.")
    return
  }
  if (bg_id == num) {
    guide_says("This is the current background.")
    return
  }

  draw_list("Special BG Close", ["[Close]",'bg_dummy.onload=null;ending_action=""; leave_main_menu()'])

  msgs = bg_loading
  guide_says(msgs[0])

  bg_id = num

  if (bg_dummy)
    bg_dummy.onload = null

  bg_dummy = new Image()
  bg_dummy.onload = specialBG_loaded
  bg_dummy.src = "wallpaper/"+bg_string+addZero(num)+"_"+screen_num+".jpg"
}

function specialBG_loaded() {
  if ((Lmenu_list.style.visibility == "hidden") || (Lmenu_content.menuID != "Special BG Close"))
    return

  ending_action = 'document.body.style.backgroundColor="black"; Ispecial_bg.style.visibility="hidden"; Ispecial_bg.src=bg_dummy.src; showMainContent(false); setTimeout("place_bg()",500); setTimeout("show_specialBG()",1100)'
  leave_main_menu(["Enjoy~!"])
}

function place_bg() {
  var scr_dim = [sw,sh]
  var img_dim = [Ispecial_bg.width,Ispecial_bg.height]
  var img_pos = [0,0]
  for (var i = 0; i < 2; i++) {
    var iv = img_dim[i]
    if (!iv)
      continue

    var sv = scr_dim[i]
    img_pos[i] = (sv - iv) / 2
  }
  Ispecial_bg.style.posLeft = img_pos[0]
  Ispecial_bg.style.posTop = img_pos[1]
}

function showMainContent(vis) {
  if (vis) {
    Lmain.style.visibility = "inherit"
    LRW.style.visibility = "inherit"
    Seq.item("RW_Thumbnail_Animate").Play()
  }
  else {
    Lmain.style.visibility = "hidden"
    LRW.style.visibility = "hidden"
    Seq.item("RW_Thumbnail_Animate").Pause()
  }
}

function special_start() {
  if (!season_special)
    return

  if (w3c_dom)
    initFilters(["Ispecial_bg"])

  if (isAutoLoad) {
    Ispecial_bg.style.visibility = "inherit"
    return
  }

  loaded = 99

  showMainContent(false)

  place_bg()
  setTimeout('show_specialBG()', 1000)
}

function show_specialBG() {
  try {
    var f = Ispecial_bg.filters.revealTrans
    f.transition = 23
    f.duration = 4
    f.apply()
    Ispecial_bg.style.visibility = "inherit"
    f.play()

    setTimeout("finish_specialBG()", 4200)
  }
  catch (ex) {
    setTimeout("finish_specialBG()",100)
  }
}

function finish_specialBG() {
  bg_dummy = null

  showMainContent(true)

  Ispecial_bg.style.visibility = "inherit"
  document.body.style.backgroundColor = "white"

  if (loaded > 1) {
    loaded = true
    activate_guide('init_guide(["Welcome to Anime Theme~! Merry Christmas~! ^_^"], report)')
  }
}

// AT Desktop Misc END

