$(document).ready(function() {
    $("#dragdrop").draggable();

    $("#kairu").bind("click", function(e) {
        $('#menubox').show();
    });

    $("#text").bind("keypress", function(e) {
        if (e.keyCode == 13) {
            kairu();
        }
    });

    $("#search").bind("click", kairu);

    if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
        $('#menubox_head').css('width', '226px');
        $('#menubox_title').css('width', '226px');
    }

    function kairu() {
        var text = $("#text").val();

        $("#result").html("<img src=\"img/ajax-loader_ball.gif\" />");

        $.get("kairu.php",{text: text},function(response) {
            setTimeout(function() {
                $("#result").html(response);
                $("#text").select();
            }, 0500);
        });
    }

    function getWindowEvent() {
        if (window.event) {
            return window.event;
        }

        var caller = arguments.callee.caller;
        while (caller) {
            var ob = caller.arguments[0];
            if (ob) {
                if ((ob.constructor == KeyboardEvent) || (ob.constructor == MouseEvent)) {
                    return ob;
                }
            }
            caller = caller.caller;
        }
        return null;
    }

    function help() {
        if (getWindowEvent().keyCode == 112) {
            if (getWindowEvent().type != "keydown") {
                return true;
            }
            if (jQuery.browser.msie) {
                getWindowEvent().keyCode = 0;
            }
            $('#kairu').toggle();
            if ($('#kairu').css("display") == "block") {
                $('#menubox').show();
            } else {
                $('#menubox').hide();
            }

            return false;
        }
        return true;
    }

    window.document.onkeydown  = help;
    window.document.onkeypress = help;
    window.document.onkeyup    = help;
});