var body    = $('body');
var body_id = body.attr('id');

/**
 * Debug
 */
var debug_area;
var debug_mode = false;
$(function() {
    if (body.hasClass('debug')) {
        debug_mode = true;
    }
    if (debug_mode == true) {
        body.append('<div id="debug_area" style="width: 100%;height:150px;left:0;top:0;position:absolute;background-color:#666;color:#000;overflow:scroll;"></div>');
        debug_area = $('#debug_area');
        debug_area.fadeTo(0, 0.5);
    }
});
function trace(text)
{
    if (debug_mode == true) {
        debug_area = $('#debug_area');
        var original_html = debug_area.html();
        var html = original_html + text + '<br />';
        debug_area.html(html);
    }
}

function uniformHeight(obj) {
    var maxheight = 0;
    obj.each(function(){
        if ($(this).height() > maxheight) {
            maxheight = $(this).height();
        }
    });
    obj.each(function(){
        $(this).css('min-height', maxheight);
    });
}

function setTargetBlank(obj) {
    obj.attr('target', '_blank');
}

function init()
{
    $('#global_navigation > ul > li').each(function() {
        var img_obj = $('> a > img', this);
        if ($(this).hasClass(body_id)) {
            img_obj.attr('src', img_obj.attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
        } else {
            img_obj.hover(function() {
                $(this).attr('src', $(this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
            }, function() {
                $(this).attr('src', $(this).attr('src').replace(/^(.+)_on(\.[a-z]+)$/, '$1$2'));
            });
            $('<img>').attr('src', img_obj.attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
        }
    });
    
    $('#sub_navigation > ul > li').each(function() {
        var img_obj = $('> a > img', this);
        if (body.hasClass($(this).attr('class'))) {
            img_obj.attr('src', img_obj.attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
        } else {
            img_obj.hover(function() {
                $(this).attr('src', $(this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
            }, function() {
                $(this).attr('src', $(this).attr('src').replace(/^(.+)_on(\.[a-z]+)$/, '$1$2'));
            });
            $('<img>').attr('src', img_obj.attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
        }
    });
    
    
    // 画像ロールオーバー
    $('img.rollover').hover(function() {
        $(this).attr('src', $(this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
    }, function() {
        $(this).attr('src', $(this).attr('src').replace(/^(.+)_on(\.[a-z]+)$/, '$1$2'));
    }).each(function() {
        $('<img>').attr('src', $(this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
    });
    
    setTargetBlank($('a.blank'));
}

$(document).ready(init);

