(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);

(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($) {
    // $("div.layout-auto").layout(".image", ".content", "left", 10);
    $.fn.layout = function(imgclass, contentclass, imgposition, margin) {
        this.each(function () {
            var this_width = $(this).width();
            var image_width = $(imgclass + ' img', this).width();
            if ($.browser.msie && $.browser.version == 6) {
                var content_width = this_width - image_width - (margin * 1.5);
            } else {
                var content_width = this_width - image_width - margin;
            }
            var image_class = $(imgclass, this).attr('class');
            if(image_class.match('left') != null){
                $(contentclass, this).addClass('right').width(content_width);
            } else {
                $(contentclass, this).addClass('left').width(content_width);
            }
            $(imgclass, this).width(image_width);
        });
    }
})(jQuery);

$.event.add(window, "load", function () {
    $("#footer-inner").heightAdjust(3, ".catch, .needs-search, .type-search");
    if ($.browser.msie && $.browser.version == 6) {
        DD_belatedPNG.fix('img');
    }
    $("div.layout-index").each(function(){
        $(this).heightAdjust(2, "div.block");
    });
    $('div.layout-auto').layout('div.image', 'div.content', 'left', 12);
});

$(function () {
    if($.cookie('textsize')){
        $("#container").addClass($.cookie('textsize'));
        if($.cookie('textsize') == 'normal' && $('li.normal', '#size-control').length != 0){
            var currentsrc = $("li.normal > img", "#size-control").attr('src').replace(/^(.*?)\.gif/, "$1_on.gif");
            $("li.normal > img", "#size-control").attr('src', currentsrc);
        } else if($.cookie('textsize') == 'small' && $('li.small', '#size-control').length != 0) {
            var currentsrc = $("li.small > img", "#size-control").attr('src').replace(/^(.*?)\.gif/, "$1_on.gif");
            $("li.small > img", "#size-control").attr('src', currentsrc);
        } else if($.cookie('textsize') == 'large' && $('li.large', '#size-control').length != 0) {
            var currentsrc = $("li.large > img", "#size-control").attr('src').replace(/^(.*?)\.gif/, "$1_on.gif");
            $("li.large > img", "#size-control").attr('src', currentsrc);
        }
    }

    $("li", "#size-control").click(function(){
        $("li", "#size-control").each(function(){
            var imgsrc = $("img", this).attr("src").replace(/_on/, "");
            $("img", this).attr("src", imgsrc);
        });
        var this_src = $("img", this).attr("src").replace(/^(.*?)\.gif/, "$1_on.gif");
        $("img", this).attr("src", this_src);
    });

    $("img[src *= _off.]:not(.current img), input[type = image]").rollover();

    $("div.block", "div.layout-index, div.layout-index-02, div.layout-index-03").not(".nolink").hover(function(){
        $(this).addClass("hover");
    }, function(){
        $(this).removeClass("hover");
    });

    $("div.block", "div.layout-index, div.layout-index-02, div.layout-index-03").not(".nolink, #everywhere, #virtual").click(function(){
        location.href = $("a", this).attr("href");
    });

    $("li", "#size-control").click(function(){
        $.cookie("textsize", null);
        var sizeClass = $(this).attr("class");
        $("#container").removeClass().attr("class", sizeClass);
        $.cookie("textsize", sizeClass, {expires: 7, path: "/"});

    });

    $("div.double").each(function(){
        $("div.block:nth-child(2n)", this).addClass("last");
    });

    $("div.triple").each(function(){
        $("div.block:nth-child(3n)", this).addClass("last");
    });

    $("div.quadruple").each(function(){
        $("div.block:nth-child(4n)", this).addClass("last");
    });

    $("ul.double", "div.layout-index").each(function(){
        $("li:nth-child(2n)", this).addClass("last");
    });

    if($("#main > *:first-child").attr("class") == "head-02"){
        $("#main").addClass("adjust");
    }

    $("table tr:nth-child(2n+1)", "#rel-products").addClass("row-odd");
    $("table tr:last-child", "#rel-products").addClass("last");

    $("a[rel='popup-01']").click(function(){
        window.open(this.href, 'popup', 'width=722, height=600, scrollbars=yes');
        return false;
    });

    $("a[rel='popup-02']").click(function(){
        window.open(this.href, 'popup', 'width=702, height=500, scrollbars=yes');
        return false;
    });

    $("a[rel='popup-03']").click(function(){
        window.open(this.href, 'popup', 'width=900, height=700, scrollbars=yes');
        return false;
    });

    $("div#everywhere").click(function(){
        var _href = $("a", this).attr("href");
        window.open(_href, 'popup', 'width=860, height=600, scrollbars=yes');
        return false;
    });

    $("div#virtual").click(function(){
        var _href = $("a", this).attr("href");
        window.open(_href, 'popup', 'width=702, height=500, scrollbars=yes');
        return false;
    });

    $('#close').click(function(){
        window.close();
    });
    $("tbody tr:nth-child(2n+2)", "table.table-01").addClass("row-even");
    $("tbody tr:nth-child(2n+2) th[rowspan], tbody tr:nth-child(2n+2) td[rowspan]", "table.table-01").css("background-color", "#fff");

    $("ul > li:nth-child(2n)", "div.list-news, div.list-news-02").addClass("even");

    $("a[href$=.pdf]").not("div.layout-pdf a").addClass("link-pdf").attr("target", "_blank").append('<img src="/assets/shared/images/icon_pdf02.gif" alt="PDFダウンロード" />');

    if (jQuery.browser.msie && jQuery.browser.version == 6) {
        $("h2 img", "div.layout-index, div.layout-index-02, div.layout-index-03").each(function(){
            var alt = $(this).attr("alt");
            $(this).attr("alt", "");
            $(this).after('<span class="text">' + alt + '</span>');
        });
    }
});

