
// 标题 : 基本功能支持脚本
// 版本号 : 1.0
// 最后一次更新时间 : 2013-08-07
// 描述 : 控件大小调整/编辑对话框/提示窗/非空验证/日期格式化, 框架页面需要引用此JS文件



$.fn.extend({
    resizePanel: function (heightMargin, widthMargin, minHeight, minWidth) {
        var height = $(document.body).height() - heightMargin;
        var width = $(document.body).width() - widthMargin;
        height = height < minHeight ? minHeight : height;
        width = width < minWidth ? minWidth : width;

        $(this).panel('resize', {
            height: height - 150,
            width: width - 223
        });
    },
    resizeDataGrid: function (heightMargin, widthMargin, minHeight, minWidth) {
        var height = $(document.body).height() - heightMargin;
        var width = $(document.body).width() - widthMargin;

        height = height < minHeight ? minHeight : height;
        width = width < minWidth ? minWidth : width;

        $(this).datagrid('resize', {
            height: height - 170,
            width: width - 243
        });
    }

});


var ele_content;
var menuContainer;
var ele_container;

// 重新调整窗体大小时触发
function ResizeSidebar() {
    $('#main').resizePanel(0, 0, 0, 0);
    //$('#grid').resizeDataGrid(20, 60, 600, 800);
    ShowHideScrollButton()
}

// 向下滚动侧边菜单
function ScrollSidebarNext(elem) {
    var ele_contentBottom = ele_content.offsetTop + ele_content.offsetHeight;  // 菜单的底部
    var containerBottom = menuContainer.offsetHeight;    // 容器的底部
    ShowHideScrollButton();
    if (ele_contentBottom < containerBottom) {
        elem.style.opacity = 0.5;
        return;
    } else {
        elem.previousElementSibling.style.opacity = 1;
    }
    ele_content.style.top = (ele_content.offsetTop - 55) + "px";

}

// 向上滚动侧边菜单
function ScrollSidebarPrevious(elem) {
    ShowHideScrollButton();
    if (ele_content.offsetTop >= 0) {
        elem.style.opacity = 0.5;
        return;
    } else {
        elem.nextElementSibling.style.opacity = 1;
    }
    ele_content.style.top = (ele_content.offsetTop + 55) + "px";
}

// 控制侧边菜单按钮显隐
function ShowHideScrollButton() {
    ShowSidePreviousButton();
    ShowSideNextButton()
}


// 显示侧边向上滚动
function ShowSidePreviousButton() {
    // 当菜单内容位置小于容器顶部的时候显示此按钮
    var content_top = ele_content.offsetTop;  // 菜单内容顶部坐标
    var container_top = ele_container.offsetTop;  // 菜单容器高
    var sidePrevious = document.getElementById("scroll_previous");   // 向上滚动按钮
    if (content_top < container_top) {
        sidePrevious.style.opacity = 1;
    } else {
        sidePrevious.style.opacity = 0.5;
    }
}
// 显示侧边向下滚动
function ShowSideNextButton() {
    // 当菜单内容底部位置大于容器底部位置的时候显示此按钮
    var content_bottom = ele_content.offsetTop + ele_content.offsetHeight;  // 菜单内容底部坐标
    var container_bottom = ele_container.offsetTop + ele_container.offsetHeight;    // 菜单容器询问坐标
    var sideNext = document.getElementById("scroll_next");
    if (content_bottom >= container_bottom) {
        sideNext.style.opacity = 1;
    } else {
        sideNext.style.opacity = 0.5;
    }
}




// 编辑dialog
// EditDialog(对象标识, 表单id, 宽, 高, ok执行的函数 )
function ShowEditItemDialog(itemId, divId, width, height, okFunc) {
    if (typeof (width) != "number") {
        width = 400;
    }
    if (typeof (height) != "number") {
        height = 430;
    }

    var isEdit = (itemId.length > 0);
    var dialog = $("#" + divId);
    if (dialog.attr("srcTitle") == undefined)
        dialog.attr("srcTitle", dialog.attr("title"));	// title属性会在创建对话框后被清除！
    var dlgTitle = (isEdit ? "编辑" : "添加") + dialog.attr("srcTitle");

    dialog.show().dialog({
        width: width, height: height, modal: true, resizable: true, title: dlgTitle, closable: true,
        buttons: [
            {
                text: (isEdit ? "保存" : "创建"), iconCls: 'icon-ok', plain: true,
                handler: function () {
                    if (typeof (okFunc) == "function")
                        okFunc(dialog);
                }
            },
            {
                text: '取消', iconCls: 'icon-cancel', plain: true,
                handler: function () {
                    dialog.dialog('close');
                }
            }],
    });
}

// 非空验证
function ValidateControl(expression, message) {
    if ($.trim($(expression).val()).length == 0) {
        $.messager.alert("Hello", message, 'warning');
        return false;
    }
    return true;
}


// 删除确定
function CommonDeleteRecord() {
    if (confirm('确定要删除此记录吗？？')) {
        $.ajax({
            url: $(this).attr("href"),
            success: function (responseText) {
                $('#grid1').datagrid('reload');
            }
        });
    }
    // 无论如何，都返回false
    return false;
}

// 日期格式化
function ChangeDateFormat(time) {
    if (time != null) {
        var date = new Date(parseInt(time.replace("/Date(", "").replace(")/", ""), 10));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }
    return "";
}



// 数字格式化
function BothNum(num) {
    var c = parseInt(num);
    return c < 10 ? "0" + num : num;
}
