$(function() {
    renderCorner();
    renderGrid();
});
function renderCorner() {
    $(".member_block").corner();
    $(".block_menu").corner();
    $(".block_topic").corner("top");
    $(".block").corner();
    $(".button_logout").corner();
    $(".pagination").corner();
    $(".pagination strong").corner();
}
function renderPreload() {
    $("#content").ajaxSend(function() {
        $("#preload").show();
    });
    $("#content").ajaxSuccess(function() {
        $("#preload").hide();
    });
}
function renderGrid() {
    // render header row
    $("table.grid thead tr").each(function() {
        $(this).find("input[type=checkbox]").each(function(index) {
            var checkbox = $(this);
            
            // check all
            checkbox.click(function() {
                var checked = checkbox.attr("checked");
                
                $("table.grid tbody tr").each(function() {
                    var cell_checkbox = $(this).find("td:eq(" + index + ") input[type=checkbox]");
                    var row = $(this);
                    
                    cell_checkbox.attr("checked", checked);

                    if (checked) {
                        row.addClass("grid_row_selected");
                    } else {
                        row.removeClass("grid_row_selected");
                    }
                });
            });
        });
    });

    // render body row
    $("table.grid tbody tr").each(function() {
        $(this).mouseover(function() {
            $(this).find("td").addClass("grid_row_over");
        });
        $(this).mouseout(function() {
            $(this).find("td").removeClass("grid_row_over");
        });
        $(this).click(function(sender) {
            if (sender.target.type == "checkbox") {
                // click checkbox
                if (sender.target.checked) {
                    $(this).addClass("grid_row_selected");
                } else {
                    $(this).removeClass("grid_row_selected");
                }
            } else if (!(sender.target instanceof HTMLImageElement)) {
                // chick row
                var chk = $(this).find("input[type=checkbox]");
                chk.attr("checked", !chk.attr("checked"));

                if (chk.attr("checked") == true) {
                    $(this).find("td").addClass("grid_row_selected");
                } else {
                    $(this).find("td").removeClass("grid_row_selected");
                }
            }
        });
    });
}
function renderPagination() {
    $("p.pagination a").each(function() {
        var href = $(this).attr("href");
        var a = $(this);

        a.attr("href", "#");
        a.attr("lang", href);
        a.click(function() {
            $("#div_grid").load(href);
            return false;
        });
    });
}
function openModalDialog(url) {
    var dialogWidth = 650;
    var dialogHeight = 480;
    var dialogTop = (screen.availHeight / 2) - (dialogHeight / 2);
    var dialogLeft = (screen.availWidth / 2) - (dialogWidth / 2);
    var options = "dialogWidth: " + dialogWidth + "; dialogHeight: " + dialogHeight + "; dialogTop: " + dialogTop + "; dialogLeft: " + dialogLeft + " ;";
    
    var w = window.showModalDialog(url, null, options);

    return w;
}
