$( function() {
    $(document).ready(function(){
        $("div#footer").after("<div id='WikiPreviewOverray_Container'/>")
        $("div#WikiPreviewOverray_Container")
            .css( "max-width", 380)
            .css( "max-height", 480)
            .css( "top" ,  0)
            .css( "left",  0);
        $("#WikiPreviewOverray_Container").append("<span id='WikiPreviewOverray_Button'></span>")
        $("span#WikiPreviewOverray_Button").click( function(){
            $("#WikiPreviewOverray_Container").hide("slow");
        });
        $("#WikiPreviewOverray_Container").hide();
        $("#WikiPreviewOverray_Container").append("<div id='WikiPreviewOverray_Html'/>")
        $("div#WikiPreviewOverray_Html")
            .css( "overflow", "auto")
            .css( "max-width", 370)
            .css( "max-height", 470)
    });

    $("#text").bind("contextmenu",function(event){ 
        if(document.selection) {
            // IE
            var range = document.selection.createRange();
            var selected_value = range.text;
        } else {
            // IE 以外
            var org = document.getElementById("text");
            var start = org.selectionStart;
            var end = org.selectionEnd;
            var selected_value = org.value.substring(start, end);
        }

        if(selected_value.length > 0) {
            var url = location.pathname.split("/");
            var req_url = "";
            for(var i = 1; i < url.length; i++) {
                if(url[i] == "wiki") {
                    break;
                }
                req_url += "/" + url[i];
            }
            req_url += "/WikiPreviewOverray/post";
            
            $.post(
                req_url,
                {"tohtml" : selected_value, "__FORM_TOKEN" : $('[name=__FORM_TOKEN]').attr('value')},
                function(data, status) {
                    $("div#WikiPreviewOverray_Html").html(data);
                    $("div#WikiPreviewOverray_Container")
                        .css( "top" ,  event.pageY)
                        .css( "left",  event.pageX);
                    $("#WikiPreviewOverray_Container").hide();
                    $("#WikiPreviewOverray_Container").show("slow");
                },
                "html"
            );
            selected_value = "";
        }
        
        // 右クリックメニュー（コンテキストメニュー）を非表示にする。
        return false;
    });

    $("#comment").bind("contextmenu",function(event){ 
        if(document.selection) {
            // IE
            var range = document.selection.createRange();
            var selected_value = range.text;
        } else {
            // IE 以外
            var org = document.getElementById("comment");
            var start = org.selectionStart;
            var end = org.selectionEnd;
            var selected_value = org.value.substring(start, end);
        }

        if(selected_value.length > 0) {
            var url = location.pathname.split("/");
            var req_url = "";
            for(var i = 1; i < url.length; i++) {
                if(url[i] == "ticket") {
                    break;
                }
                req_url += "/" + url[i];
            }
            req_url += "/WikiPreviewOverray/post";
            
            $.post(
                req_url,
                {"tohtml" : selected_value, "__FORM_TOKEN" : $('[name=__FORM_TOKEN]').attr('value')},
                function(data, status) {
                    $("div#WikiPreviewOverray_Html").html(data);
                    $("div#WikiPreviewOverray_Container")
                        .css( "top" ,  event.pageY)
                        .css( "left",  event.pageX);
                    $("#WikiPreviewOverray_Container").hide();
                    $("#WikiPreviewOverray_Container").show("slow");
                },
                "html"
            );
            selected_value = "";
        }
        
        // 右クリックメニュー（コンテキストメニュー）を非表示にする。
        return false;
    });

    $("#field-description").bind("contextmenu",function(event){ 
        if(document.selection) {
            // IE
            var range = document.selection.createRange();
            var selected_value = range.text;
        } else {
            // IE 以外
            var org = document.getElementById("field-description");
            var start = org.selectionStart;
            var end = org.selectionEnd;
            var selected_value = org.value.substring(start, end);
        }

        if(selected_value.length > 0) {
            var url = location.pathname.split("/");
            var req_url = "";
            for(var i = 1; i < url.length; i++) {
                if(url[i] == "ticket" || url[i] == "newticket") {
                    break;
                }
                req_url += "/" + url[i];
            }
            req_url += "/WikiPreviewOverray/post";
            
            $.post(
                req_url,
                {"tohtml" : selected_value, "__FORM_TOKEN" : $('[name=__FORM_TOKEN]').attr('value')},
                function(data, status) {
                    $("div#WikiPreviewOverray_Html").html(data);
                    $("div#WikiPreviewOverray_Container")
                        .css( "top" ,  event.pageY)
                        .css( "left",  event.pageX);
                    $("#WikiPreviewOverray_Container").hide();
                    $("#WikiPreviewOverray_Container").show("slow");
                },
                "html"
            );
            selected_value = "";
        }
        
        // 右クリックメニュー（コンテキストメニュー）を非表示にする。
        return false;
    });
})
