(function ($) {

    $.fn.rollOver = function () {
        $(this).each(function () {
            var self = $(this);
            var src = self.attr('src');
            var body = src.substr(0, src.lastIndexOf('.'));

            if (!body.match(/\_on$/)) {
                // Preload
                var over = src.replace(/(\.gif|\.jpg|\.png)/, '_on$1');
                var img = new Image();
                img.src = over;

                img.onload = function () {
                    self.hover(function () {
                        self.attr('src', over);
                    }, function () {
                        self.attr('src', src);
                    });
                }
            }
        });
    };

    $.fn.selection = function () {
        $(this).prev().find('a').click(function () {
            $('body').unbind().click(function () {
                $('.selector').hide();
            });
            $(this).parent().next().slideDown();
        });

        $(this).find('a').click(function () {
            $('body').unbind().click(function () {
                $('.selector').hide();
            });
            $(this).parent().next().slideDown();
        });

        $(this).find('li').each(function () {
            $(this).css('cursor', 'pointer').css('display', 'block').hover(function () {
                $(this).addClass('hover');
            }, function () {
                $(this).removeClass('hover');
            });
            $(this).click(function (e) {
                //e.stopPropagation();
                $(this).parent().prev().find('span').html($(this).html());
                $(this).removeClass('hover');
                $(this).parent().prev().prev().val($(this).attr('class'));
                $(this).parent().prev().prev().attr('value', $(this).attr('class'));
                $(this).parent().slideUp('fast');
            });
        });
    };

    $(document).ready(function () {
        $('.rollOver').rollOver();

        if ($('.column_half').length > 0) {
            $('.box > .column_half:odd').css('margin-right', '0px');
            var min_height = 0;
            $('.column_half').each(function () {
                if ($(this).height() > min_height) min_height = $(this).height();
            });
            $('.column_half').css('height', min_height);
        }

        $('h4:not([class*=no-wrap])')
			.wrapInner('<span/>')
			.prepend('<img src="/content/images/h4_before.png" width="3" height="22" alt="" />')
			.append('<img src="/content/images/h4_after.png" width="3" height="22" alt="" />');


        if ($('#main_contents table').length > 0) {
            $('#main_contents table th').wrapInner('<span/>');
            $('#main_contents table th span').each(function () {
                $(this).css('height', $(this).parent().height() + 'px');
            });
        }

        if ($('#floating').length > 0) {
            if ($('#side_contents').outerHeight() < $('#main_contents').outerHeight()) {
                $('#side_contents').css('height', $('#main_contents').height());
            }
            var scroll_timer = null;
            var onResize = function () {
                $('#right_menu').css('left', $(window).width() - $('#right_menu').width());
            };
            var offset = $('#side_contents').offset();
            var onScroll = function () {
                if ($(window).scrollTop() < offset.top) {
                    if (parseInt($('#floating').css('top').replace(/px$/, '')) == 0) {
                        return;
                    }
                    target = 0;
                } else {
                    target = ($(window).scrollTop() - offset.top + 20);
                    if ($('#floating').outerHeight() + target > $('#side_contents').innerHeight()) {
                        target = $('#side_contents').innerHeight() - $('#floating').outerHeight();
                    }
                    target += 'px';
                }
                if ($('#side_contents #floating').css('top') == target) {
                    return;
                }

                $('#side_contents #floating').animate(
					{ top: target },
					{
					    duration: 'normal',
					    easing: 'easeOutCubic'
					    /*,
					    complete:function(){
					    clearInterval(scroll_timer);
					    scroll_timer = null;
					    }
					    */
					}
				);
            };

            //onResize();
            //onScroll();

            if ($('#side_contents #floating').length > 0) {
                $(window).resize(onResize);
                /*
                $(window).scroll(function() {
                if (scroll_timer == null) {
                scroll_timer = setInterval(onScroll, 500);
                }
                });
                */
                scroll_timer = setInterval(onScroll, 500);
            }
        }


        // 特徴部分で選択されているタブ画像を切り替える処理
        var switchSelectedTab = function (selectedObj) {

            var tabFileNamePrefix = "/content/images/index/tab/tab-";
            var tabFileExtension = ".jpg";

            $("#switch_tab img[rel][rel!='']").each(function () {

                var tabFileName = "";

                if (selectedObj.attr("rel") != $(this).attr("rel")) {
                    tabFileName = tabFileNamePrefix + $(this).attr("rel") + tabFileExtension;
                } else {
                    tabFileName = tabFileNamePrefix + $(this).attr("rel") + "_on" + tabFileExtension;
                }

                $(this).attr("src", tabFileName);

            });
        };

        // クリックされたタブのコンテンツを表示する処理
        $("#switch_tab img[rel][rel!='']").click(function () {

            var clickedTab = $(this);

            // コンテンツ内容を切り替え
            $("#tab_content").children().each(function () {

                if (clickedTab.attr("rel") != $(this).attr("id")) {
                    $(this).fadeOut("normal", function () {
                        $("#" + clickedTab.attr("rel")).fadeIn(1200);
                    });
                }

            });

            // タブの選択状態を変更
            switchSelectedTab($(this));

        });

        // デフォルトのコンテンツ以外を非表示にする処理
        var defaultContentID = "e_vision";
        $("#tab_content").children().each(function () {

            if (defaultContentID != $(this).attr("id")) {
                $(this).hide();
            }

        });
        switchSelectedTab($("#switch_tab ul li img[rel][rel=" + defaultContentID + "]"));

    });
})(jQuery);

