$.extend($, {
	'alert': function(options) {
		if (Object.prototype.toString.call(options) == '[object String]') {
			options = {
				'content': options
			};
		}
		var options = $.extend({
			'content'	: '',
			'title'		: '提示',
			'init'		: function(e, dom){},
			'callback'	: function(e, dom){},
			'size'		: '',
			'bgcolor'	: '',
			'btntext'	: '确认',
			'btnbg'		: 'btn-primary',
			'btncancel'	: false
		}, options);
		var id = 'msgbox_id_' + Math.floor(Math.random() * 100000000);
		var btncancel = options.btncancel ? '<button type="button" class="btn btn-sm btn-default" data-dismiss="modal" aria-label="Close">取消</button>' : '';
		var html = `<div class="modal fade" tabindex="-1" role="dialog" id="${id}">
		  <div class="modal-dialog ${options.size}" role="document">
		    <div class="modal-content ${options.bgcolor}">
		      <div class="modal-header">
		        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
		          <span aria-hidden="true">&times;</span>
		        </button>
		        <h4 class="modal-title">${options.title}</h4>
		      </div>
		      <div class="modal-body">${options.content}</div>
		      <div class="modal-footer">
		        <button type="button" class="btn btn-sm ${options.btnbg}">${options.btntext}</button> ${btncancel}
		      </div>
		    </div>
		  </div>
		</div>`;
		var jq = $(html);
		$(document.body).append(jq);
		$('.btn-primary', jq).click(function(e){
			if (options.callback && options.callback(jq) !== false) {
				jq.modal('hide');
			}
		});
		jq.on('show.bs.modal', function(e){
			if (options.init) {
				options.init(jq);
			}
		});
		jq.on('hidden.bs.modal', function(e){
			$(this).remove();
		});
		jq.modal('show');
	}
});

function time2str(timestamp, cnFormat) {
	var obj = new Date(timestamp * 1000);
	if(cnFormat) {
		var Y = obj.getFullYear(),
			m = obj.getMonth() + 1,
			d = obj.getDate(),
			H = obj.getHours(),
			i = obj.getMinutes(),
			s = obj.getSeconds();
		return Y
			+ '-' + (m < 10 ? '0'+m : m)
			+ '-' + (d < 10 ? '0'+d : d)
			+ ' ' + (H < 10 ? '0'+H : H)
			+ ':' + (i < 10 ? '0'+i : i)
			+ ':' + (s < 10 ? '0'+s : s);
	} else {
		return obj.toLocaleString();
	}
}

function size2str(size) {
	if(size < 1024) {
		return size + '字节';
	} else if(size < 1024*1024) {
		return numberRound(size / 1024, 2) + 'K';
	} else if(size < 1024*1024*1024) {
		return numberRound(size / 1024 / 1024, 2) + 'M';
	} else {
		return numberRound(size / 1024 / 1024 / 1024, 2) + 'G';
	}
}

function numberFormat(n, len) {
	n = parseInt(n);
	len = len == 4 ? 4 : 3;
	if(n == NaN) {
		return 0;
	}
	var sign = n < 0 ? '-' : '';
	var str = '' + Math.abs(n), ret = '';
	var mod = str.length % len;
	if(mod > 0) {
		ret += str.substr(0, mod) + ',';
	}
	for(var i=mod; i<str.length; i+=len) {
		ret += str.substr(i, len) + ',';
	}
	return sign + ret.substr(0, ret.length - 1);
}

function numberRound(n, len) {
	n = parseFloat(n);
	len = len || 2;
	if(n == NaN || len < 1) {
		return 0;
	}
	var x = Math.pow(10, len);
	var str = '' + Math.round(n * x) / x;
	var pos = str.indexOf('.');
	var declen = pos == -1 ? 0 : str.length - pos - 1;
	if (declen == len) {
		return str;
	}
	if (pos == -1) {
		str += '.';
	}
	for (var i=declen; i<len; i++) {
		str += '0';
	}
	return str;
}

function pager(options) {
	options = $.extend({
		'id': '',
		'total': 0,
		'page_size': 20,
		'page_num': 1,
		'bar_size': 9,
		'show_prev_next': true,
		'show_first_last': true,
		'callback': function(page) {}
	}, options);
	if (options.id == '') {
		return;
	}
	if (options.bar_size % 2 == 0) {
		options.bar_size++;
	}
	var bar_side = (options.bar_size-1) / 2;
	var pages = Math.max(Math.ceil(parseInt(options.total) / parseInt(options.page_size)), 1);
	if (options.page_num < 1 || options.page_num > pages) {
		return;
	}
	var html = '<nav><ul class="pagination"><li class="disabled"><a href="#">共'+ options.total +'条'+ pages +'页</a></li>';
	if (options.show_first_last) {
		if (options.page_num == 1) {
			html += '<li class="disabled"><a class="page-link" href="#" data-value="1"><i class="glyphicon glyphicon-step-backward"></i></a></li>';
		} else {
			html += '<li><a class="page-link" href="#" data-value="1"><i class="glyphicon glyphicon-step-backward"></i></a></li>';
		}
	}
	if (options.show_prev_next) {
		if (options.page_num > 1) {
			html += '<li><a class="page-link" href="#" data-value="' + (options.page_num-1) + '"><i class="glyphicon glyphicon-chevron-left"></i></a></li>';
		} else {
			html += '<li class="disabled"><a class="page-link" href="#" data-value="1"><i class="glyphicon glyphicon-chevron-left"></i></a></li>';
		}
	}
	if (pages <= options.bar_size) {
		for (var i=1; i<=pages; i++) {
			if (i == options.page_num) {
				html += '<li class="disabled"><a class="page-link" href="#" data-value="' + i + '">' + i + '</a></li>';
			} else {
				html += '<li><a class="page-link" href="#" data-value="' + i + '">' + i + '</a></li>';
			}
		}
	} else if (options.page_num <= bar_side + 1) {
		for (var i=1; i<=options.bar_size; i++) {
			if (i == options.page_num) {
				html += '<li class="disabled"><a class="page-link" href="#" data-value="' + i + '">' + i + '</a></li>';
			} else {
				html += '<li><a class="page-link" href="#" data-value="' + i + '">' + i + '</a></li>';
			}
		}
	} else if (options.page_num >= pages - bar_side) {
		for (var i=pages-options.bar_size+1; i<=pages; i++) {
			if (i == options.page_num) {
				html += '<li class="disabled"><a class="page-link" href="#" data-value="' + i + '">' + i + '</a></li>';
			} else {
				html += '<li><a class="page-link" href="#" data-value="' + i + '">' + i + '</a></li>';
			}
		}
	} else {
		for (var i=options.page_num-bar_side, j=0; j<options.bar_size; i++, j++) {
			if (i == options.page_num) {
				html += '<li class="disabled"><a class="page-link" href="#" data-value="' + i + '">' + i + '</a></li>';
			} else {
				html += '<li><a class="page-link" href="#" data-value="' + i + '">' + i + '</a></li>';
			}
		}
	}
	if (options.show_prev_next) {
		if (options.page_num < pages) {
			html += '<li><a class="page-link" href="#" data-value="' + (options.page_num+1) + '"><i class="glyphicon glyphicon-chevron-right"></i></a></li>';
		} else {
			html += '<li class="disabled"><a class="page-link" href="#" data-value="' + pages + '"><i class="glyphicon glyphicon-chevron-right"></i></a></li>';
		}
	}
	if (options.show_first_last) {
		if (options.page_num == pages) {
			html += '<li class="disabled"><a class="page-link" href="#" data-value="' + pages + '"><i class="glyphicon glyphicon-step-forward"></i></a></li>';
		} else {
			html += '<li><a class="page-link" href="#" data-value="' + pages + '"><i class="glyphicon glyphicon-step-forward"></i></a></a></li>';
		}
	}
	html += '</ul></nav>';
	var jq = $('#' + options.id);
	jq.html(html);
	$('a', jq).click(function(){
		var p = $(this).attr('data-value');
		if (!p || p == "") {
			return false;
		}
		options.callback(parseInt(p));
		return false;
	});
}