(function($) {
    $.fn.rollover = function() {
        var elems = this;
        var nelems = elems.length;
        var off = "_off";
        var on = "_on";
        var preLoadImg = new Object();
        for (var i = 0; i < nelems; i++) {
            preLoadImg[elems[i]] = new Image();
            preLoadImg[elems[i]].src  = elems[i].src.replace(off, on);
            elems[i].onmouseover = function(){
                 this.src = this.src.replace(off, on);
            }
            elems[i].onmouseout = function(){
                 this.src = this.src.replace(on, off);
            }
        }
    }
})(jQuery);

(function($) {
    $.fn.imageCache = function() {
        var elems = this;
        var nelems = elems.length;
        var preLoadImg = new Object();
        for (var i = 0; i < nelems; i++) {
            preLoadImg[elems[i]] = new Image();
        }
    }
})(jQuery);

(function($) {
    $.fn.heightAdjust = function(row, child) {
        this.each(function(){
            var elems = $(child, this);
            var nelems = elems.length;
            var heights = new Array(nelems);
            var setHeight = function(){
                for (var i = 0; i < nelems; i++) {
                    elems[i].style.height = "";
                    heights[i] = $(elems[i]).height();
                }
                var max = 0;
                for (var i = 0; i < nelems; i++) {
                    if(0 == i % row){
                        max = Math.max.apply(Math, heights.slice(i, i + row));
                    }
                    heights[i] = max;
                }
                for (var i = 0; i < nelems; i++) {
                    elems[i].style.height = heights[i] + "px";
                }
            }
            setHeight();
            if($("#font-checker").length == 0){
                $("body").append('<div id="font-checker" style="position:absolute;left:-9999px;top:0;">&nbsp;</div>');
            }
            var baseSize = $("#font-checker").height();
            var checkSize = 0;
            setInterval(function() {
                checkSize = $("#font-checker").height();
                if(baseSize !== checkSize){
                    setHeight();
                    baseSize = checkSize;
                }
            }, 1000);
        });

    }
})(jQuery);

$.event.add(window, "load", function(){
	$("img[src *= _off.], input[type = image]").rollover();
	$('div.double').heightAdjust(2, "div.block div.block-inner");
	$('div.double-access').heightAdjust(2, "div.block div.block-inner");
	$("div.layout", "#primary").each(function(){
		var _class = $(this).attr("class");
		var margin = 5;
		var parentWidth = $(this).width();
		var imageWidth = $('> .image img', this).width();
		$(this).width(parentWidth);
		$(".image", this).width(imageWidth);
		if(!_class.match("turn")){
			if(_class.match("right")){
				$("> .content", this).css("margin-right", imageWidth + margin);
			} else {
				$("> .content", this).css("margin-left", imageWidth + margin);
			}
		}
	});
});

$(function(){
	$("table.table-01 tr:nth-child(2n+1)").addClass("odd");

	$("ul.list-route img").imageCache();

	$("ul.list-route > li").hover(function(){
		$(this).addClass("active");
		var route_img = $(this).children("dl").children("dt").children("img").attr("src");
        if(!route_img.match("active")){
			route_img = route_img.replace(/^(.*?)\./, "$1_active.");
			$(this).children("dl").children("dt").children("img").attr("src", route_img);
		}
	}, function(){
		if(!$(this).attr("class").match(/current/)){
			$(this).removeClass("active");
			var route_img = $(this).children("dl").children("dt").children("img").attr("src");
	        if(route_img.match("active")){
				route_img = route_img.replace("_active", "");
				$(this).children("dl").children("dt").children("img").attr("src", route_img);
			}
		}
	});


	$("div.double-access a").click(function(){
		var route = "." + $(this).attr("class");
		var route_img = $(route).children("dl").children("dt").children("img").attr("src");
        if(!route_img.match("active")){
			route_img = route_img.replace(/^(.*?)\./, "$1_active.");
			$(route).children("dl").children("dt").children("img").attr("src", route_img);
		}

		$(route, "ul.list-route").addClass("current");
		$(route, "ul.list-route").siblings().each(function(){
			route_img = $(this).children("dl").children("dt").children("img").attr("src").replace("_active", "");
			$(this).children("dl").children("dt").children("img").attr("src", route_img);
			$(this).removeClass("active current");
		});


	});

	var route_img = $("ul.list-route > li:first img").attr("src").replace(/^(.*?)\./, "$1_active.");
	$("ul.list-route > li:first img").attr("src", route_img);
	$("ul.list-route > li:first").addClass("current");

	$("ul.list-route > li").click(function(){
		$(this).siblings().each(function(){
			var route_img = $(this).children("dl").children("dt").children("img").attr("src");
	        if(route_img.match("active")){
				route_img = route_img.replace("_active", "");
				$(this).children("dl").children("dt").children("img").attr("src", route_img);
			}
		});
		$(this).siblings().removeClass("active current");
		$(this).addClass("current");
	});

});


