// JavaScript Document
jQuery(document).ready(function(){

    jQuery("span[id^='verdict']:contains('Waiting...')").each(function(){
        //        var index = jQuery("span[id='" + this.id + "']").index(this);
        // alert("index="+index);
        //        var solutionid = jQuery("td[id='solutionid']:eq(" + index + ")").text();
        var solutionid = $(this).attr("solutionid");
        doRejudge(solutionid);
    });
    
    jQuery("span[id='img_rejudge']").bind('click', function(){
        //var index = jQuery("span[id='img_rejudge']").index(this);
        // jQuery("td[id='solutionid']");
        var solutionid = $(this).attr("solutionid");
        //var solutionid = jQuery("td[id='solutionid']:eq(" + index + ")").text();
        doRejudge(solutionid);
    });
    
    jQuery("span[id='showcode']").click(function(){
        var solutionid = $(this).attr("solutionid");
        if ($(this).children('a').val() == undefined) {
            return false;
        }
        
        jQuery.ajax({
            type: "POST",
            url: "./Solution.ajax",
            data: "action=getCode&solutionid=" + solutionid,
            async: false,
            timeout: 5000,
            success: function(result){
                jQuery("#code").val(result);
            }
        });
        
        var $dialog = $("#showcode_dialog").dialog({
            autoOpen: false,
            width: '60%',
            title: 'Code',
            buttons: {
                "返回": function(){
                    $(this).dialog("close");
                }
            }
        });
        $dialog.dialog('open');
        return false;
    });
    
    jQuery("span[id='verdict']").click(function(){
        //var index = jQuery("span[id='showdetail']").index(this);
        //var solutionid = jQuery("span[id='solutionid']:eq(" + index + ")").text();
        //var solutionid = $(this).attr("alt");
        var solutionid = $(this).attr("solutionid");
        if ($(this).children('a').val() == undefined) {
            return false;
        }
        
        jQuery("div[id=jsondetail]").hide();
        jQuery("pre[id=details]").hide();
        
        showDetails(solutionid);
        
        var $dialog = $("#showdetail_dialog").dialog({
            autoOpen: false,
            width: '60%',
            title: ' Detail',
            buttons: {
                "返回": function(){
                    $(this).dialog("close");
                }
            }
        });
        
        $dialog.dialog('open');
        return false;
    });
    
    jQuery("span[id='imglock']").click(function(){
        var index = jQuery("span[id='imglock']").index(this);
        var solutionid = $(this).attr("alt");
        $("#codelock_dialog").attr("alt", index + "," + solutionid);
        if ($(this).children('img').attr("id") == "openlock") {
            var $dialog = $("#codelock_dialog").dialog({
                autoOpen: false,
                width: '30%',
                title: 'Lock',
                buttons: {
                    "取消": function(){
                        $(this).dialog("close");
                    },
                    "確定": function(){
                        index = $("#codelock_dialog").attr("alt").split(",")[0];
                        solutionid = $("#codelock_dialog").attr("alt").split(",")[1];
                        //alert("index=" + index + ", solutionid=" + solutionid);
                        jQuery.ajax({
                            type: "POST",
                            url: "./Codelock.ajax",
                            data: "solutionid=" + solutionid + "&delay=" + jQuery('input[name=delay]:checked').val(),
                            async: false,
                            timeout: 5000,
                            success: function(result){
                            }
                        });
                        jQuery("span[id='imglock']:eq(" + index + ")").html(getCodelocker(solutionid));
                        $(this).dialog("close");
                    }
                }
            });
            
            $dialog.dialog('open');
            return false;
        }
        else 
            if ($(this).children('img').attr("id") == "closelock") {
                closelock(solutionid, index);
            }
    });
});



function doShowCode(solutionid, index){


}

function getCodelocker(solutionid){
    var text;
    jQuery.ajax({
        type: "POST",
        url: "./Solution.ajax",
        data: "action=getImgCodelocker&solutionid=" + solutionid,
        async: false,
        timeout: 5000,
        success: function(result){
            text = result;
            //alert(result);
            //jQuery("#alert").html(result);
            //jQuery("span[id='imglock']:eq(" + index + ")").html(result);
        }
    });
    //alert("getCodelocker solutionid="+solutionid+" text="+text);
    return text;
}

function closelock(solutionid, index){
    //				var text = getCodelocker();
    //				jQuery("#alert").text(text);
    
    jQuery.ajax({
        type: "GET",
        url: "./Codelock.ajax",
        data: "solutionid=" + solutionid,
        async: false,
        timeout: 5000,
        success: function(result){
            jQuery("img[id='closelock']:eq(" + index + ")").hide();
            jQuery("img[id='openlock']:eq(" + index + ")").show();
        }
    });
    jQuery("span[id='imglock']:eq(" + index + ")").html(getCodelocker(solutionid));
}

