$(function(){

// グローバルナビのサブナビはIE6ではhover時にjsでクラスhoverを付けて表示
if(typeof document.body.style.maxHeight === "undefined"){
  $("#globalNavi > li")
    .hover(
      function(){
        $(this).addClass("hover");
      },
      function(){
        $(this).removeClass("hover");
      }
    )
  .end();
}

// ヘッダーの鳥メッセージをajaxで表示
$.ajax({
  url: "/header_message/message.txt",
  async: true,
  cache: false,
  dataType: "text",
  success: function(text){
    $("#headerMessage").html(text);
  },
  error: function(){
    $("#headerMessage").html("ようこそ！<br />人形劇団ひとみ座のホームページへ");
  }
});

// 画像のタイトルをキャプションとして表示
$("#second img[title!='']")
  .each(function(){
    var caption = $(this).attr("title");
    var width =   $(this).width();
    $(this)
      .wrap("<span class='imgWrap' style='width:" + width + "px;'></span>")
      .after("<span class='imgCaption'>" + caption + "</span>")
    .end();
  })
.end();

$("a")
  // ページ内リンクはスムーズなスクロール（#topは一番上へ）
  .filter("[href^='#']")
    .click(function(){
      var hash = $(this).attr("href");
      var posiotion = (hash=="#top")?0 : $(hash).offset().top;
      $("html, body")
        .animate(
          {scrollTop:posiotion},
          {easing:"easeOutCirc",duration:400,complete:function(){location.hash = hash;}}
        )
      .end();
      return false;
    })
  .end()
.end();

});

// フォームの値のチェック
function formCheck(form){
  var errorMessage = [];

  //空チェック
  $(form).find("input, textarea, select").each(function(){
    if($(this).attr("requiredjs") === "requiredjs" && $(this).val() === ""){
      var label = $(this).attr("label") || $(this).closest("td").prev("th").text();
      errorMessage.push("【" + label + "】の項目を入力してください");
    }
  });

  //メールアドレスチェック

  //メールアドレス確認チェック
  
  var email1 = $("#mail_email").val();
  var email2 = $("#mail_email_confirm").val();
  if(email1 && email2 && email1 !== "" && email2 !== "" && email1 !== email2){
    errorMessage.push("メールアドレスと確認用メールアドレスが異なります");
  }
 
  if(errorMessage.length > 0){
    alert(errorMessage.join("\n"));
  }else{
    $(form).submit();
  }
}
