/*
 * edit by liuft 2016.04.11
 */
if (typeof jQuery === 'undefined') {
    throw new Error('Alert requires jQuery')
}

$(function () {
    function Alert() {
//    	var isAppend = false;
    	
        function appendAlertDom() {
//        	if (isAppend) return;
//        	isAppend = true;

            var alertDom =  '<div class="modal fade" id="global-alert" tabindex="-1" role="dialog">' +
				                '<div class="modal-dialog" style="width:290px; margin: 0 auto;">' +
					                '<div class="modal-content">' +						                
						                '<div class="modal-header self-modal-header">' +
//							            	'<button type="button" class="close" data-dismiss="modal">&times;</button>' +
							            	'<h4 class="modal-title">提示</h4>' +
							         	'</div>' +						                
						                '<div class="modal-body custom-scroll" style="overflow: auto;">' +
							                '<p style = "font-size: 12px;margin: 5px 0;"></p>' +
						                '</div>' +
						                '<div class="modal-footer self-modal-footer">' +
							                '<div class="btn-confirm" data-dismiss="modal" style="margin-right: 10px;">确定</div>' +
							                '<div id="btn-cancel" class="btn-cancel" data-dismiss="modal">取消</div>' +
						                '</div>' +						                
					                '</div>' +
				                '</div>' +
			                '</div>';

            $('body').prepend(alertDom);
            $("#global-alert").css("z-index", 10001);
        }

//        function hideAlertDialog() {
//            $("#global-alert").modal('hide');
//        }

        function resizeAlert() {
            var client_height = document.documentElement.clientHeight;
            var client_width = document.documentElement.clientWidth;
            var alert_body = $("#global-alert .modal-body").outerHeight(true);
            
            if (alert_body > 300) {
            	$("#global-alert .modal-body").height(300);
            }
            
            var alert_height = $("#global-alert .modal-content").outerHeight(true);
            $("#global-alert .modal-dialog").css("top", (client_height - alert_height) / 2);
        }
        
        function alert(infoType, text, okCallback, isShowCancel, cancelCallback, title) {        	
        	$("#global-alert").modal({
        		backdrop: 'static',
        		show: false
        	});
        	$("#global-alert").modal('show');
//            $("#global-alert button").unbind();
//            $("#global-alert button").click(hideAlertDialog);
        	
        	if (okCallback == undefined) {
        		$("#global-alert .btn-confirm").unbind('click');
        		$("#global-alert .btn-confirm").one('click', function(){});
        	} else if (okCallback !== undefined && typeof okCallback === "function") {
            	var okClickCallback = function() {
            		if (cancelClickCallback != undefined) {
            			$("#global-alert .btn-cancel").unbind();
            		}                	
                	okCallback();
                };
                $("#global-alert .btn-confirm").unbind('click');
                $("#global-alert .btn-confirm").one('click', okClickCallback);
            }
        	
        	if (cancelCallback == undefined) {
        		$("#global-alert .btn-cancel").unbind('click');
        		$("#global-alert .btn-cancel").one('click', function(){});
        	} else if (cancelCallback !== undefined && typeof cancelCallback === "function") {
            	var cancelClickCallback = function() {
            		if (okClickCallback != undefined) {
            			$("#global-alert .btn-confirm").unbind();
            		}
            		cancelCallback();
                };
                $("#global-alert .btn-cancel").unbind('click');
                $("#global-alert .btn-cancel").one('click', cancelClickCallback);
            }

            $("#global-alert p").html(text);
            
            if (title != undefined) {
                $("#global-alert .modal-title").text(title);
            }
            
            if (isShowCancel) {
            	$('#btn-cancel').show();
            } else {
            	$('#btn-cancel').hide();
            }

            $("#global-alert").show();
            resizeAlert(); //添加文本信息后，重新计算大小
        }
        
        appendAlertDom();
        $(window).resize(resizeAlert);
        window.alert = alert;
    }

    Alert();
})