// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function is_authenticated() {
  return document.cookie.match(/(^|;\s*)idcore=[^;\s]+/);
}

// ログイン済みボタン

function setup_head_buttons() {
  if (is_authenticated()) {
    $("logOut").style.display = "";
/*
    if (document.cookie.match(/(^|;\s*)nm=([^;]+)/)) {
      var s = escape_html(decodeURIComponent(RegExp.$2));
      $("login_status").innerHTML = "ようこそ、" + s + "さん";
    } else {
      $("login_status").innerHTML = "ログイン中";
    }
*/
    do_request("/mypage/status", "GET", null, true, function(http) {
      $("login_status").innerHTML = http.responseText;
    });
  } else {
    $("logIn").style.display = "";
  }
}

// サブメニュー検索BOX
function setup_head_navi_search() {
  $("navi_search_btn").onclick = function() { $("navi_search_text").onfocus(); $("navi_search_form").submit(); return false; }
  $("navi_search_text").onblur = function() { if (this.value == "") { this.value = "キーワードを入力"; this.style.color = "#808080"; } }
  $("navi_search_text").onfocus = function() { if (this.value == "キーワードを入力") { this.value = ""; this.style.color = "#000000"; } }
  $("navi_search_text").onblur();
}

// お気に入り追加

function setup_favorite() {
  var ua = navigator.userAgent.toLowerCase();
  if (/msie/.test(ua) && !/opera/.test(ua)) {
    var e = $("movie_count");
    e.innerHTML = '<' + 'a href="javascript:add_favorite()">お気に入りに追加<' + '/a>' + e.innerHTML;
  }
}

function add_favorite() {
  window.external.addFavorite("http://eiga.com/", "映画のことならeiga.com");
}

// TOPICS

var topics_scroll_timer = null;
var topics_scroll_max_width = 0;

function scroll_topics_text() {
  var x = parseInt($("topicsTextScroll").style.left, 10);
  if (topics_scroll_max_width + x > 0) {
    x -= 5;
    $("topicsTextScroll").style.left = x + "px";
    topics_scroll_timer = setTimeout("scroll_topics_text()", 100);
  }
}

function setup_head_topics() {
  topics_scroll_max_width = $("topicsTextLink").offsetWidth - 298;
  if (topics_scroll_max_width > 0) {
    $("topicsTextScroll").onmouseover = function() {
      if (!topics_scroll_timer) {
        topics_scroll_timer = setTimeout("scroll_topics_text()", 100);
      }
    };
    $("topicsTextScroll").onmouseout = function() {
      if (topics_scroll_timer) {
        clearTimeout(topics_scroll_timer);
        topics_scroll_timer = null;
      }
      $("topicsTextScroll").style.left = "0px";
    };
  }
}

// 検索ボックス

function do_search_article() {
  search_keyword_onfocus();
  var keyword = trim_both($("search_keyword").value);
  location.href = keyword.length == 0 ? "/search/" : "/search/list?t=" + encodeURIComponent(keyword);
  return false;
}

function do_search_theater() {
  search_keyword_onfocus();
  var keyword = trim_both($("search_keyword").value);
  location.href = keyword.length == 0 ? "/theater/" : "/theater/list/1/-/" + encodeURIComponent(keyword);
  return false;
}

function search_keyword_onfocus() {
  var e = $("search_keyword");
  if (e.value == e.defaultValue) e.value = "";
  e.style.color = "#000000";
}

function search_keyword_onblur() {
  var e = $("search_keyword");
  if (e.value == "") e.value = e.defaultValue;
  e.style.color = "#808080";
}

// 上映スケジュール
var last_sche_sel = 0;

function select_schedule(n) {
  if (last_sche_sel > 0) {
    $("sche_link" + last_sche_sel).className = "";
    $("sche_pane" + last_sche_sel).className = "innerSearch noSelect";
  }
  $("sche_link" + n).className = "noSelect";
  $("sche_pane" + n).className = "innerSearch";
  last_sche_sel = n;
  return false;
}

var no_pref_opt = new Option("都道府県を選択", "");
var pref_opts = [];
pref_opts[0] = [ new Option("茨城県", "8"), new Option("栃木県", "9"), new Option("群馬県", "10"), new Option("埼玉県", "11"), new Option("千葉県", "12"), new Option("東京都", "13"), new Option("神奈川県", "14") ];
pref_opts[1] = [ new Option("滋賀県", "25"), new Option("京都府", "26"), new Option("大阪府", "27"), new Option("兵庫県", "28"), new Option("奈良県", "29"), new Option("和歌山県", "30") ];
pref_opts[2] = [ new Option("岐阜県", "21"), new Option("静岡県", "22"), new Option("愛知県", "23"), new Option("三重県", "24") ];
pref_opts[3] = [ new Option("北海道", "1"), new Option("青森県", "2"), new Option("岩手県", "3"), new Option("宮城県", "4"), new Option("秋田県", "5"), new Option("山形県", "6"), new Option("福島県", "7") ];
pref_opts[4] = [ new Option("新潟県", "15"), new Option("山梨県", "19"), new Option("長野県", "20") ];
pref_opts[5] = [ new Option("富山県", "16"), new Option("石川県", "17"), new Option("福井県", "18") ];
pref_opts[6] = [ new Option("鳥取県", "31"), new Option("島根県", "32"), new Option("岡山県", "33"), new Option("広島県", "34"), new Option("山口県", "35"), new Option("徳島県", "36"), new Option("香川県", "37"), new Option("愛媛県", "38"), new Option("高知県", "39") ];
pref_opts[7] = [ new Option("福岡県", "40"), new Option("佐賀県", "41"), new Option("長崎県", "42"), new Option("熊本県", "43"), new Option("大分県", "44"), new Option("宮崎県", "45"), new Option("鹿児島県", "46"), new Option("沖縄県", "47") ];

function region_change() {
  var pref = $("sche_pref");
  pref.selectedIndex = 0;
  pref.options.length = 0;
  pref.options[0] = no_pref_opt;
  var e = $("sche_region");
  var region = parseInt(e.options[e.selectedIndex].value, 10) - 1;
  if (0 <= region && region < pref_opts.length) {
    var opts = pref_opts[region];
    for (var i = 0; i < opts.length; i++) {
      pref.options[i + 1] = opts[i];
    }
  }
  return true;
}

// AJAXウィンドウ
var ajax_win_height = 0;
var ajax_win_height_inc = 0;

function is_ajax_win_closed() {
  return $("ajax_win").style.display == "none";
}

function open_ajax_win(height) {
    $("ajax_win").style.height = height + "px";
    $("ajax_win").style.display = "";
/*
  if (height) {
    ajax_win_height = height;
    ajax_win_height_inc = 0;
    $("ajax_win").style.height = "0px";
    $("ajax_win").style.display = "";
  }
  var h = parseInt($("ajax_win").style.height, 10);
  if (ajax_win_height_inc < 50) {
    ajax_win_height_inc += 10;
  }
  h += ajax_win_height_inc;
  if (h < ajax_win_height) {
    $("ajax_win").style.height = h + "px";
    setTimeout("open_ajax_win()", 100);
  } else {
    $("ajax_win").style.height = ajax_win_height + "px";
  }
*/
  return false;
}

function close_ajax_win(next) {
    $("ajax_win").style.display = "none";
    $("ajax_win").style.height = "0px";
/*
  if (!next) {
    ajax_win_height_inc = 0;
  }
  var h = parseInt($("ajax_win").style.height, 10);
  if (ajax_win_height_inc < 50) {
    ajax_win_height_inc += 10;
  }
  h -= ajax_win_height_inc;
  if (h > 0) {
    $("ajax_win").style.height = h + "px";
    setTimeout("close_ajax_win(true)", 100);
  } else {
    $("ajax_win").style.height = "0px";
    $("ajax_win").style.display = "none";
  }
*/
  return false;
}

function post_ajax_form(form) {
  do_request(form.action, "POST", get_form_data(form), true, function(http) {
    $("ajax_win").innerHTML = http.responseText;
  });
  return false;
}

// 映画館検索
function setup_right_theater() {
  select_schedule(1);
  $("sche_link1").onclick = function() { return select_schedule(1); }
  $("sche_link1").href = "javascript:void(0)";
  $("sche_link2").onclick = function() { return select_schedule(2); }
  $("sche_link2").href = "javascript:void(0)";
  $("sche_region").onchange = function() { return region_change(); }
  $("sche_search_btn1").onclick = function() { $("sche_area_form").submit(); return false; }
  $("sche_search_btn1").href = "javascript:void(0)";
  $("sche_search_btn2").onclick = function() { $("sche_title_form").submit(); return false; }
  $("sche_search_btn2").href = "javascript:void(0)";
}

// 作品ナビ
function setup_head_navi(movie_id) {
  $("sche_pref_btn").onclick = function() { $("sche_pref_form").submit(); return false; };
  $("sche_pref_btn").href = "javascript:void(0)";
  $("vote_btn").onclick = function() {
    if (is_ajax_win_closed()) {
      do_request("/movie/" + movie_id + "/vote", "GET", null, true, function(http) {
        $("ajax_win").innerHTML = http.responseText;
        open_ajax_win(170);
      });
    } else {
      close_ajax_win();
    }
    return false;
  };
  $("vote_btn").href = "javascript:void(0)";

//  $("clip_btn").onclick = function() {
//    if (is_ajax_win_closed()) {
//      do_request("/movie/" + movie_id + "/clip/inline", "GET", null, true, function(http) {
//        $("ajax_win").innerHTML = http.responseText;
//        open_ajax_win(400);
//      });
//    } else {
//      close_ajax_win();
//    }
//    return false;
//  };
//  $("clip_btn").href = "javascript:void(0)";
}

// 右サイド作品ナビ
function setup_right_navi(movie_id) {
  $("right_pref").onchange = function() { $("right_navi_form").submit(); };
}
