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

$.event.add(window, "load", function () {
    $("div.slider p.desc").each(function () {
        var str = $(this).text().substr(0, 38) + '...';
        $(this).text(str);
    });
    $("img[src *= _off.]:not(#nav-global img), input[type = image]").rollover();
    $("#footer-inner").heightAdjust(3, ".catch, .needs-search, .type-search");
    $("#color-palette ul li").heightAdjust(5, "a");
    $("#nav-global > li > ul").each(function () {
        $("li > a", this).width($(this).width());
    });


    if ($("div.slider", "div.ipsa-style").length != 0) {
        $("div.slider", "div.ipsa-style").easySlider({
            prevId: 'prev',
            nextId: 'next',
            auto: false,
            controlsShow: false,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("div.slider", "div.special-contents").length != 0) {
        $("div.slider", "div.special-contents").easySlider({
            prevId: 'prev-02',
            nextId: 'next-02',
            auto: false,
            controlsShow: false,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#relation-01").length != 0) {
        $("#relation-01").easySlider({
            prevId: 'relation-prev-01',
            nextId: 'relation-next-01',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#relation-02").length != 0) {
        $("#relation-02").easySlider({
            prevId: 'relation-prev-02',
            nextId: 'relation-next-02',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#relation-03").length != 0) {
        $("#relation-03").easySlider({
            prevId: 'relation-prev-03',
            nextId: 'relation-next-03',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#relation-04").length != 0) {
        $("#relation-04").easySlider({
            prevId: 'relation-prev-04',
            nextId: 'relation-next-04',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#step-01").length != 0) {
        $("#step-01").easySlider({
            prevId: 'step-prev-01',
            nextId: 'step-next-01',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#step-02").length != 0) {
        $("#step-02").easySlider({
            prevId: 'step-prev-02',
            nextId: 'step-next-02',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#step-03").length != 0) {
        $("#step-03").easySlider({
            prevId: 'step-prev-03',
            nextId: 'step-next-03',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#step-04").length != 0) {
        $("#step-04").easySlider({
            prevId: 'step-prev-04',
            nextId: 'step-next-04',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#recommend-slider").length != 0) {
        $("#recommend-slider").easySlider({
            prevId: 'recommend-prev-01',
            nextId: 'recommend-next-01',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("#recommend-slider-02").length != 0) {
        $("#recommend-slider-02").easySlider({
            prevId: 'recommend-prev-02',
            nextId: 'recommend-next-02',
            auto: false,
            controlsShow: false,
            controlsFade: false,
            speed: 500
        });
    }
    if ($("div#qa-01").length != 0) {
        $("div#qa-01").easySlider({
            prevId: 'prev-01',
            nextId: 'next-01',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }

    if ($("div#qa-02").length != 0) {
        $("div#qa-02").easySlider({
            prevId: 'prev-02',
            nextId: 'next-02',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }
    if ($("div#qa-03").length != 0) {
        $("div#qa-03").easySlider({
            prevId: 'prev-03',
            nextId: 'next-03',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    }
    $("#qa_slide > div.slider:first").siblings("div").hide();


    $("div.slider-auto").each(function () {
        var this_id = $(this).attr("id");
        $(this).easySlider({
            prevId: this_id + '-prev',
            nextId: this_id + '-next',
            auto: false,
            controlsShow: true,
            controlsFade: false,
            speed: 500
        });
    });


    $("div.slider").heightAdjust(99, "p.name");
    $("div.slider").heightAdjust(99, "div#relation-01, div#relation-02, div#relation-03, div.item, li, span a");
    $("div#login").heightAdjust(2, "div.box_01, div.box_02");
    $("ul.slider-tab > li", "#box-relation").click(function () {
        $(this).siblings("li").removeClass("current");
        var clsnum = "#relation-" + $(this).attr("class").replace(/^.*?tab-([0-9][0-9]).*?$/, "$1");
        $(this).parents("ul").siblings("div.slider").hide();
        $(clsnum, "#box-relation").parents("div").fadeIn();
        $(this).addClass("current");
    });

    $("ul.slider-tab > li", "#box-step-relation").click(function () {
        $(this).siblings("li").removeClass("current");
        var clsnum = "#step-" + $(this).attr("class").replace(/^.*?tab-([0-9][0-9]).*?$/, "$1");
        $(this).parents("ul").siblings("div.slider").hide();
        $(clsnum, "#box-step-relation").parents("div").fadeIn();
        $(this).addClass("current");
    });

    $("div.slider", "#box-relation, #box-step-relation").each(function () {
        var elem = $(this).children("div").attr("id") + "";
        if (!elem.match(/relation-01/) && !elem.match(/step-01/)) {
            $(this).hide();
        }
    });

    $("#tab h2", "#site-top").click(function () {
        var tabId = "#" + $(this).attr("id").replace(/head-/, "");
        $(tabId).siblings("div").hide();
        $(tabId).fadeIn();
        var img = $("img", this).attr("src").replace(/_(on|off).png/, "_current.png");
        $("img", this).attr("src", img);
        var img2 = $(this).siblings("h2").children("img").attr("src").replace("_current.png", "_off.png");
        $(this).siblings("h2").children("img").attr("src", img2);
        $("img[src *= _off.]", "#tab").rollover();
    });

    setTimeout(function () {
        if ($("div.twtr-tweet", "#sns-comments")) {
            var tweet_ref = $("div.twtr-tweet", "#sns-comments").html();
            if (tweet_ref == "" || tweet_ref == null) {
                $("#sns-comments").hide();
                $("p.comments-anchor", "div.social-service").hide();
                if ($("div.voice-01", "div.box-faq").length == 0) {
                    $("div.box-faq").hide();
                }
            }
        }
    }, 300);

    if ($("a[rel^='zoom']").length != 0) {
        $("a[rel^='zoom']").prettyPhoto({
            theme: 'zoom',
            showTitle: false,
            allowresize: false
        });
    }

    $("a#live800iconlink").click(function () {
        _gaq.push(['_trackPageview', '/onlinechat/']);
    });
});

$(function () {
    if ($("ul.nav-utility02").length != 0) {
        $("ul.nav-utility02 li:last-child").addClass("last");
    }

    if ($("a[rel^='zoom']").length != 0) {
        $("a[rel^='zoom']").prettyPhoto({
            theme: 'zoom',
            showTitle: false,
            allowresize: false
        });
    }

    $("div.layout-item-list:last", "#product-list").addClass("last");
    if ($.cookie('theme')) {
        $("#container").addClass($.cookie('theme'));
    }
    $('#nav-global > li').hover(function () {
        var gnv_src = $('img', this).attr("src").replace(/_(off)\.gif/, "_on.gif");
        $('img', this).attr('src', gnv_src);
        $('ul', this).stop();
        $('ul', this).css({
            display: 'block',
            height: 'auto',
            opacity: '0.3'
        });
        $('ul', this).animate({
            opacity: '1'
        }, 250);
        $('ul', this).fadeIn('fast');
    }, function () {
        var gnv_src = $('img', this).attr("src").replace(/_(on)\.gif/, "_off.gif");
        $('img', this).attr('src', gnv_src);
        $('ul', this).fadeOut(100);
    });
    $("div.box-category").click(function () {
        window.location = $("a", this).attr("href");
    });

    $("div.layout-item-detail div.images img").hover();
    $(".listMovie .list").hover(function () {
        $(this).addClass("hover");
    }, function () {
        $(this).removeClass("hover");
    });

    $(".listMovie .list").click(function () {
        window.location = $("a", this).attr("href");
    });

    $("#products").hide();

    $("div.box-category").css("cursor", "pointer");
    $("div.box-category").hover(function () {
        $(this).css("background", "#eff5fd");
    }, function () {
        $(this).css("background", "#ffffff");
    });

    $("li.sample").css("cursor", "pointer");
    $("li.sample").hover(function () {
        $(this).css("background", "#b2dcf5");
    }, function () {
        $(this).css("background", "#ffffff");
    });

    $("div.box-webkit").not(".nolink").css("cursor", "pointer");
    $("div.box-webkit").not(".nolink").hover(function () {
        $(this).css("background", "#eff5fd");
    }, function () {
        $(this).css("background", "#ffffff");
    });

    $("li", "#select_option").click(function () {
        $("input", this).attr("checked", "checked");
    });

    if ($.browser.msie && $.browser.version == 8) {
        $('#nav-global > li a').hover(function () {
            $(this).css("background", "#eff4f8");
        }, function () {
            $(this).css("background", "#d8e3ee");
        });
    }

    //		DD_belatedPNG.fix('img');
    $('div.item-spec').each(function () {
        $("ul > li:first", this).addClass('first');
    });

    $('ul.list-01').each(function () {
        $("li:first", this).addClass('first');
    });

    $("#sitemap ul.list-sitemap li ul li:last-child").addClass("last");

    $("ul.list-thumbnail > li").click(function () {
        $(this).siblings("li").removeClass("active");
        $(this).addClass("active");
        var thumb_path = $("img", this).attr("src").replace("_s", "_b");
        var thumb_path02 = thumb_path.replace("_b", "_bb");
        $(this).parents("ul").siblings("p").children("a").children("img").attr("src", thumb_path).css({
            opacity: '0.3'
        }).animate({
            opacity: '1'
        }, 400);
        $(this).parents("ul").siblings("p").children("a").attr("href", thumb_path02);
    });


    //	$("p.color-submit").click(function(){
    //		window.location = "/ec/cmdty/+/" + $(this).siblings("select").val() + "#color";
    //	});

    $("li.number select", "div.input-color").change(function () {
        window.location = "/ec/cmdty/+/" + $(this).val() + "#color";
    });

/*
	$("body").append('<div id="cart-message"><p><img src="/shared/img/_test.png" alt="" /></p></div>');
	$("input").hover(function(){
		var windowHeight = $(window).height();
		var _h = windowHeight / 2;
		$("#cart-message").css('top', _h);
		$("#cart-message").fadeIn(1000);
		setTimeout(function(){
			$("#cart-message").fadeOut(1000);
		}, 1000);
	}, function(){});
*/

    $("input.keyword", "#search").each(function () {
        $(this).addClass("sample").val("商品検索");
    });

    $("input.keyword", "div.box-search").each(function () {
        $(this).addClass("sample").val("再検索はこちら");
    });

    $("input.keyword", "#search").focus(function () {
        var keyword = $(this).val();
        if (keyword == "商品検索") {
            $(this).val("");
            $(this).removeClass("sample");
        }
    });

    $("input.keyword", "#search").blur(function () {
        var keyword = $(this).val();
        if (keyword == "") {
            $(this).addClass("sample");
            $(this).val("商品検索");

        }
    });

    $("input.keyword", "div.box-search").focus(function () {
        var keyword = $(this).val();
        if (keyword == "再検索はこちら") {
            $(this).val("");
            $(this).removeClass("sample");
        }
    });

    $("input.keyword", "div.box-search").blur(function () {
        var keyword = $(this).val();
        if (keyword == "") {
            $(this).addClass("sample");
            $(this).val("再検索はこちら");
        }
    });
    $("th", "#select_payment").click(function () {
        $("input", this).attr("checked", "checked");
    });

    if ($("div.payment_detail", "#select_payment").length > 0) {
        $("div.payment_detail", "#select_payment").not(":first").hide();
        $("th", "#select_payment").click(function () {
            $(this).parents("tr").siblings("tr").find("div.payment_detail").hide();
            $(this).siblings("td").find("div.payment_detail").fadeIn();
        });
    }

    $("#q9").attr("checked", false);
    $("#etc", "ul.questionnaire_list").attr("disabled", "disabled").val("");;
    $("#q9").change(function () {
        if ($("#q9").attr("checked") == true) {
            $("#etc", "ul.questionnaire_list").removeAttr("disabled");
        } else {
            $("#etc", "ul.questionnaire_list").attr("disabled", "disabled").val("");
        }
    });

//	$("li input", "#cmpaign_use").attr("checked", false).val("");
//	$("li input", "#cmpaign_use").attr("checked", false);
//	$("#use_point", "#cmpaign_use").attr("disabled", "disabled").val("");
//	$("#use_point", "#cmpaign_use").attr("disabled", "disabled");

    $("input[type='radio']", "#cmpaign_use").change(function () {
        if ($(this).attr("id") == "point_check3") {
            $("#use_point", "ul#cmpaign_use").removeAttr("disabled");
        } else {
            $("#use_point", "ul#cmpaign_use").attr("disabled", "disabled").val("").removeClass("input_ng").removeClass("input_ok");
            $("div.pointsformError").remove();
        }
    });

//    $("li input", "#cmpaign_use02").attr("checked", false).val("");
//    $("#code", "#cmpaign_use02").attr("disabled", "disabled").val("");;

    $("input[type='radio']", "#cmpaign_use02").change(function () {
        if ($(this).attr("id") == "campaign_code_use2") {
            $("#campaign_code", "ul#cmpaign_use02").removeAttr("disabled");
        } else {
            $("#campaign_code", "ul#cmpaign_use02").attr("disabled", "disabled").val("").removeClass("input_ng").removeClass("input_ok");
            $("div.codeformError").remove();
        }
    });

});


$(function () {
    $("th", "#credit").click(function () {
        $("input", this).attr("checked", "checked");
    });

    $("span", "#save_credit").click(function () {
        var save_credit = $("input", "#save_credit");
        if (save_credit.attr("checked") == true) {
            save_credit.attr("checked", false);
        } else {
            save_credit.attr("checked", true);
        }
    });

    //		$("input", this).attr("checked", "checked");
    //	});

    $("span", "#approval").click(function () {
        var approval = $("input", "#approval");
        if (approval.attr("checked") == true) {
            approval.attr("checked", false);
        } else {
            approval.attr("checked", true);
        }
    });

    $("th", "#change_item").click(function () {
        $("input", this).attr("checked", "checked");
    });
    $("th", "#conveni").click(function () {
        $("input", this).attr("checked", "checked");
    });
});

function changeIframeH(h) {

    // 仮実装です。
    $("#frame-cart").height(h);
    return;
}

function getIframeH(h) {
    return $("#frame-cart").height();
}




function addCartAction() {
    return;
}

function addConsiderAction() {
    return;
}


$(function () {
    //カートに入れる画像
    //	$("body").append('<div id="minicartimg"><img src="/shared/img/minicart/minicart.png" width="302" height="302" alt="" /></div>');
    $("body").append('<div id="minicartimg"><img src="/shared/img/minicart/minicart.gif" width="302" height="302" alt="" /></div>');
    //お気に入りに追加画像
    //	$("body").append('<div id="favoriteimg"><img src="/shared/img/minicart/addfavorite.png" width="302" height="302" alt="" /></div>');
    $("body").append('<div id="favoriteimg"><img src="/shared/img/minicart/addfavorite.gif" width="302" height="302" alt="" /></div>');
    $("img", "#minicartimg, #favoriteimg").hide();
});

//画面上にカートに追加画像を表示


function minicartDisp() {
    $("img", "#minicartimg").attr("src", "/shared/img/minicart/minicart.gif");
    //	changeIframeH( 364 );
    //画面の真ん中に表示
    if (navigator.userAgent.indexOf("Firefox") != -1) {
        document.getElementById("minicartimg").style.top = window.scrollY + (window.innerHeight / 2) - 151 - 150 + "px";
    } else if (navigator.userAgent.indexOf("IE") != -1) {
        document.getElementById("minicartimg").style.top = document.documentElement.scrollTop + (document.documentElement.clientHeight / 2) - 151 - 150 + "px";
    } else {
        document.getElementById("minicartimg").style.top = document.body.scrollTop + (window.innerHeight / 2) - 151 - 150 + "px";
    }
    $("#minicartimg img:hidden:first").fadeIn("slow", function () {
        timer = setTimeout(function () {
            $("#minicartimg img").fadeOut("slow");
        }, 1000);
    });
}

//画面上にカートに追加画像を表示


function favoriteDisp() {
    $("img", "#favoriteimg").attr("src", "/shared/img/minicart/addfavorite.gif");
    //	changeIframeH( 364 );
    //画面の真ん中に表示
    if (navigator.userAgent.indexOf("Firefox") != -1) {
        document.getElementById("favoriteimg").style.top = window.scrollY + (window.innerHeight / 2) - 151 - 150 + "px";
    } else if (navigator.userAgent.indexOf("IE") != -1) {
        document.getElementById("favoriteimg").style.top = document.documentElement.scrollTop + (document.documentElement.clientHeight / 2) - 151 - 150 + "px";
    } else {
        document.getElementById("favoriteimg").style.top = document.body.scrollTop + (window.innerHeight / 2) - 151 - 150 + "px";
    }
    $("#favoriteimg img:hidden:first").fadeIn("slow", function () {
        timer = setTimeout(function () {
            $("#favoriteimg img").fadeOut("slow");
        }, 1000);
    });
}

//	color iframe adjust


function iframeAdjust(type) {
    $("body", window.parent.document).addClass(type);
}

function addEcCartAction2(elem) {
    var cmc = $(elem).attr("href").replace(/^(.*)cmc=([0-9]+)/, "$2");

    var results = Number($.ajax({
        type: "GET",
        url: "/ec/cmdtyInfo",
        data: "cmc=" + cmc + "&mode=1",
        async: false
    }).responseText);

    document.getElementById("frame-cart").src = "/ec/minicart";
    switch (results) {
    case 1:
        // 通常通り
        $("img", "#minicartimg").attr("src", "/shared/img/minicart/minicart.gif");
        break;

    case 2:
        // 品切れ中
        $("img", "#minicartimg").attr("src", "/shared/img/minicart/minicart02.gif");
        break;

    case 3:
        // 販売終了
        $("img", "#minicartimg").attr("src", "/shared/img/minicart/minicart03.gif");
        break;

    case 4:
    case 9:
        // エラー
        $("img", "#minicartimg").attr("src", "/shared/img/minicart/minicart04.gif");
        break;

    default:
        break;
    }

    if (navigator.userAgent.indexOf("Firefox") != -1) {
        document.getElementById("minicartimg").style.top = window.scrollY + (window.innerHeight / 2) - 151 - 150 + "px";
    } else if (navigator.userAgent.indexOf("IE") != -1) {
        document.getElementById("minicartimg").style.top = document.documentElement.scrollTop + (document.documentElement.clientHeight / 2) - 151 - 150 + "px";
    } else {
        document.getElementById("minicartimg").style.top = document.body.scrollTop + (window.innerHeight / 2) - 151 - 150 + "px";
    }
    $("#minicartimg img:hidden:first").fadeIn("slow", function () {
        timer = setTimeout(function () {
            $("#minicartimg img").fadeOut("slow");
        }, 1000);
    });

    return;
}

function addEcFavAction2(elem) {
    var cmc = $(elem).attr("href").replace(/^(.*)cmc=([0-9]+)/, "$2");
    var results = Number($.ajax({
        type: "GET",
        url: "/ec/cmdtyInfo",
        data: "cmc=" + cmc + "&mode=2",
        async: false
    }).responseText);

    document.getElementById("frame-cart").src = "/ec/minifavorite";
    switch (results) {
    case 1:
        // 通常通り
        $("img", "#favoriteimg").attr("src", "/shared/img/minicart/addfavorite.gif");
        break;

    case 2:
        // 登録済み
        $("img", "#favoriteimg").attr("src", "/shared/img/minicart/addfavorite02.gif");
        break;

    case 3:
        // 未ログイン
        $("img", "#favoriteimg").attr("src", "/shared/img/minicart/addfavorite03.gif");
        break;

    case 4:
    case 9:
        // エラー
        $("img", "#favoriteimg").attr("src", "/shared/img/minicart/addfavorite04.gif");
        break;

    default:
        break;
    }

    //画面の真ん中に表示
    if (navigator.userAgent.indexOf("Firefox") != -1) {
        document.getElementById("favoriteimg").style.top = window.scrollY + (window.innerHeight / 2) - 151 - 150 + "px";
    } else if (navigator.userAgent.indexOf("IE") != -1) {
        document.getElementById("favoriteimg").style.top = document.documentElement.scrollTop + (document.documentElement.clientHeight / 2) - 151 - 150 + "px";
    } else {
        document.getElementById("favoriteimg").style.top = document.body.scrollTop + (window.innerHeight / 2) - 151 - 150 + "px";
    }
    $("#favoriteimg img:hidden:first").fadeIn("slow", function () {
        timer = setTimeout(function () {
            $("#favoriteimg img").fadeOut("slow");
        }, 1000);
    });
    return;
}
