$(function() {
    setFontSizeChanger('#contents, #index_contents');
    rollover();
});

function rollover() {
    var image_cache = new Object();
    $('[src*=_roll.]').not("[src*='_on.']").each(function(i) {
        var imgsrc = this.src;
        var dot = this.src.lastIndexOf('.');
        var imgsrc_on = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
        image_cache[this.src] = new Image();
        image_cache[this.src].src = imgsrc_on;
        $(this).hover(
            function() { this.src = imgsrc_on; },
            function() { this.src = imgsrc; }
        );
    });
}

$(function(){
    if (document.URL.match(/\?print$/)) {
        $('link[media="print"]').attr({media: "all"});
    }
});


function setFontSizeChanger(target){
    $('#header dl').show();

    $("#header dl a").click(function(){
        var fontsize = $(this).parent("dd").attr("id").split("_")[1];
        $(target).attr("class","fontsize_"+fontsize);
        $.cookie('fontsize_jtbe', fontsize, { expires:30, path:'/' });
        $('#header dl').attr("class","fontselect_"+fontsize);
        return false;
    });

    var fontsize = $.cookie('fontsize_jtbe');
    if (fontsize == null) {fontsize = "small";}
    $(target).addClass("fontsize_" + fontsize);
    $('#header dl').attr("class","fontselect_"+ fontsize);

}

function htmlspecialchars(string) {
    return (string + '').replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

function rel2abs(relUrl) {
    var span = document.createElement('span');
    span.innerHTML = '<a href="' + htmlspecialchars(relUrl) + '" />';
    return span.firstChild.href;
}

$(function() {
    var flag = false;
    var href1 = location.href;
    href1 = href1.replace(location.hash, '');
    href1 = href1.replace(location.search, '');
    href1 = href1.replace('/index.html', '/');
    $('#localnavi ul a[href]').each(function() {
        var href2 = rel2abs(this.href).replace('/index.html', '/');
        if (href1 == href2) {
            $(this).parent().addClass('current');
            flag = true;
        }
    });
    if (!flag) {
        $('link[rel="up"]').each(function() {
            var href1 = rel2abs(this.href).replace('/index.html', '/');
            $('#localnavi ul a[href]').each(function() {
                var href2 = rel2abs(this.href).replace('/index.html', '/');
                if (href1 == href2) {
                    $(this).parent().addClass('current');
                }
            });
        });
    }
});



/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

