$(function() {
    // menu
    $('#global_navigator').droppy();

    $('.hover_on_off_switch').hover(
        function () {
            $(this).attr('src', $(this).attr('src').replace('_off\.', '_on.'));
        },
        function () {
            $(this).attr('src', $(this).attr('src').replace('_on\.', '_off.'));
        }
    );

    // top page he
    $('.to_page_top_button').click(
        function (ev) {
            window.scrollTo(0,0);
            return false;
        }
    );

    // side menu
    $('#site_global_side_menu').accordion({
        // event: "click hoverintent",
        autoHeight: false,
        navigation: true,
        collapsible: true
    });

    // external site links / pdf links
    $('a').each(function () {
        var href = $(this).attr('href');
        if (typeof(href) !== 'string') {
            return;
        }
        if (href.match(/\.pdf$/i)) {
	    $(this).attr('target', '_blank');
	}
        if (href.match(/support\.sohei\.co\.jp/i)) {
	    $(this).attr('target', '_blank');
	}
        if (href.match(/^http[s]?:\/\//i)
	    && ! href.match(/[a-z0-9]+\.sohei\.co\.jp/i)
            && ! href.match(/sohei\.jp/i)) {
	    $(this).attr('target', '_blank');
	}
    });

    // glossary
    $('body').append('<div style="display: none" id="dialog-content"></div>');
    $('a').click(
        function (ev) {
            var result = true;
            var href = $(this).attr('href');
            if (href.match(/sohei\.jp\/glossary\//i)
                || href.match(/^\/glossary\//i)) {
                var title = $(this).text();
                if (title.match(/^[ ]*$/)) {
                    title = $(this).children('img:first-child').attr('alt');
                }
                $('#dialog-content')
                    .load(href)
                    .dialog({modal: true,
                             title: title,
                             width: 480,
                             height: 300,
                             buttons: { "閉じる": function() { $(this).dialog("close"); } },
                             close: function (event, ui) {
                                 $('#dialog-content').text('');
                             }
                            });
                result = false;
            }
            return result;
        }
    );

});

var cfg = ($.hoverintent = {
    sensitivity: 7,
    interval: 500
});

$.event.special.hoverintent = {
    setup: function() {
	$(this).bind("mouseover", jQuery.event.special.hoverintent.handler);
    },
    teardown: function() {
	$(this).unbind("mouseover", jQuery.event.special.hoverintent.handler);
    },
    handler: function(event) {
	event.type = "hoverintent";
	var self = this,
	args = arguments,
	target = $(event.target),
	cX, cY, pX, pY;

	function track(event) {
	    cX = event.pageX;
	    cY = event.pageY;
	};
	pX = event.pageX;
	pY = event.pageY;
	function clear() {
	    target
		.unbind("mousemove", track)
		.unbind("mouseout", arguments.callee);
	    clearTimeout(timeout);
	}
	function handler() {
	    if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) {
		clear();
		jQuery.event.handle.apply(self, args);
	    } else {
		pX = cX;
		pY = cY;
		timeout = setTimeout(handler, cfg.interval);
	    }
	}
	var timeout = setTimeout(handler, cfg.interval);
	target.mousemove(track).mouseout(clear);
	return true;
    }
};

