$(document).ready(function() {
    // ロールオーバー
    var postfix = '_on';
    $('#g_navi img, #header .all img, #column #form img, #writer #form img, .ro img').not('[src*="'+ postfix +'."]').each(function() {
        var img = $(this);
        var src = img.attr('src');
        var src_on = src.replace('_off', '_on');
        $('<img>').attr('src', src_on);
        img.hover(
            function() {
                img.attr('src', src_on);
            },
            function() {
                img.attr('src', src);
            }
        );
    });

    $("#header #searchform input.text").focus(function() {
        if (this.value == "チーム名、大会名、選手名で検索！") {
            $(this).val("");
        }
    });

    $("#header #searchform input.text").blur(function() {
        if (this.value == "") {
            $(this).val("チーム名、大会名、選手名で検索！");
        }
    });

    // タブパネル
    $(".tabpanel").each(function() {
        var tabpanelId = $(this).attr('id');
        $("#"+tabpanelId+" ul.panel li.content:not("+$("#"+tabpanelId+" ul.tab li a.selected").attr("href")+")").hide();
        $("#"+tabpanelId+" ul.tab li a").click(function() {
            $("#"+tabpanelId+" ul.tab li a").removeClass("selected");
            $(this).addClass("selected");
            $("#"+tabpanelId+" ul.panel li.content").hide();
            $($(this).attr("href")).show();
            return false;
        });
    });
    
    // バナー
    $.ajaxSetup({ cache: false });
    $("#ad_header").load(('https:' == document.location.protocol ? '/hb/' : '/') + "banner/ad-top");
    $("#ad_middle").load(('https:' == document.location.protocol ? '/hb/' : '/') + "banner/ad-middle");
    $("#ad_side").load(('https:' == document.location.protocol ? '/hb/' : '/') + "banner/ad-side");
    $("#ad_side2").load(('https:' == document.location.protocol ? '/hb/' : '/') + "banner/ad-side2");
    if ($("#ad_bottom").length) {
        $.get(('https:' == document.location.protocol ? '/hb/' : '/') + "banner/ad-bottom", function(data) {
            $("#ad_bottom").append(data);
        });
    }
    $("#ad_news").load(('https:' == document.location.protocol ? '/hb/' : '/') + "banner/ad-news");
    $("#ad_column").load(('https:' == document.location.protocol ? '/hb/' : '/') + "banner/ad-column");
    $("#ad_report").load(('https:' == document.location.protocol ? '/hb/' : '/') + "banner/ad-report");

// コメント & 応援メッセージ
    $('#message-form, #comment-form').html('<div class="loading"><img src="/common/images/base/loading.gif" alt="Now Loading..." /></div>');
    $('#message-form form, #comment-form form').live('submit', function() {
        var id = $(this).parent().attr('id');
        var data = $(this).serializeArray();
        $(this).html('<div class="loading"><img src="/common/images/base/loading.gif" alt="Now Loading..." /></div>');
        $.post($(this).attr('action'), data, function(d) {
            $("#" + id).html(d);
        });

        return false;
    });
});

$.event.add(window, "load", function() {
    $("ul.list li").each(function() {
        $left = $(this).children("dl.left");
        $right = $(this).children("dl.right");
        if ($left.outerHeight() > $right.outerHeight()) {
            $right.css("height", $left.outerHeight()-28);
        } else {
            $left.css("height", $right.outerHeight()-28);
        }
    });
});

// フォームクリア
$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input', this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea') {
      this.value = '';
    } else if (type == 'checkbox' || type == 'radio') {
      this.checked = false;
      if (this.value == '') {
          this.checked = true;
      }
    } else if (tag == 'select') {
      this.selectedIndex = 0;
    }
  });
};

// 新規ウインドウ
function openWindow(url, title) {
    var w = screen.width <= 1440 ? screen.width : 1440;
    var h = screen.height <= 900 ? screen.height : 900;
    window.open(url, title, 'width=' + w + ',height=' + h + ',toolbar=no,menubar=no,location=no,scrollbars=yes');
}

