// 用于记录被打开窗口的ID
var div_windows = {};

/**
 * 打开窗口
 * 
 * @param window_title
 *            窗口标题
 * @param content_div
 *            窗口内容
 * @param window_id
 *            窗口ID
 * @param window_class
 *            窗口使用的css样式(默认为空)
 * @return
 */
function openWindow(window_title, content_div, window_id, window_class) {
	if (!window_class) {
		window_class = '';
	}

	var win = new DivWindow(window_id, window_class, 1);
	win.create(window_title, content_div);
	win.open();
//	$('#' + window_id + " :text:first").focus();
//	$('#' + window_id + ' :text:first').select();
//	window.scrollTo(0, 0);

	div_windows.window_id = win;
}

/**
 * 关闭窗口
 * 
 * @param window_id
 *            string 窗口DIV的ID
 */
function closeWindow(window_id) {
	div_windows.window_id.close();
	div_windows.window_id = null;
}

/**
 * 重新调整窗口大小，并根据大小自动调整位置
 * 
 * @param window_id
 *            string 窗口DIV的ID
 * @param width
 *            int 新的宽度
 * @param height
 *            int 新的高度(默认为null)
 * @return boolean 返回是否成功
 */
function resizeWindow(window_id, width, height) {
	div_windows.window_id.resize(width, height);
}

function dodivkeydown(event) {
	switch (event.keyCode) {
	case 27:
		$(".windowclose").click();
		return false;
		break;
	}
}
function myclose() {
	$(".windowclose").click();
}
function DivWindow(window_id, window_class, model) {

	this.close = function() {
			$("#" + window_id + " > *").unbind();
			$(document).unbind('keydown');
			$("#" + window_id).empty();
			$("#" + window_id).remove();
	
			$("#divbg").hide();
			$("#iframeBg").hide();
		try {
			ajax_time_count++;
		} catch (e) {
//			alert('e');
		}
	};

	this.create = function(window_title, content_div) {
		this.createWindow(window_title, content_div);
		if (model && model == 1) {
			this.createBg();
		}
		
		$(".windowcancel").click(myclose);
		$("#windowcannel").click(myclose);
		$(".windowclose").click(this.close);
		$(document).keydown(dodivkeydown);

	};

	this.open = function() {

	};

	this.resize = function(width, new_height) {
		if (!new_height) {
			height = $('#' + window_id).height();
		}
		else {
			height = new_height;
		}

		var pos = this.getTopLeft(width, height);
		$('#' + window_id).css("left", pos.left + "px");
		$('#' + window_id).css("top", pos.top + "px");
		
		if (new_height) {
			$('#' + window_id).css('height', height + 'px');
		}
		$('#' + window_id).css('width', width + 'px');
	};

	this.createWindow = function(window_title, content_div) {

		var html = '';

		
		html += '<div id="' + window_id + '" class="window_base ' + window_class + '">';
		html += '<div id="windowInner" class="window">';
		
		html += '<div class="winTitleLeft">';
		html += '<div class="winTitleRight">';
		html += '<div class="winTitleContent"><h2>' + window_title + '</h2>';
		html += '<span class="closeBtn windowclose" ></span></div></div>';
		html += '</div>';
		
		html += '<div class="content" id="' + window_id + '_content">';
		html += content_div;
		html += '</div>';
		
		html += '<div class="winFootLeft">';
		html += '<div class="winFootRight"><div class="winFootContent"></div></div>';
		html += '</div>';
		
		html += '</div>';
		html += '</div>';
		
		$("body").append(html);

		var width = $('#' + window_id).width();
		var height = $('#' + window_id).height();

		var pos = this.getTopLeft(width, height);
		$('#' + window_id).css("left", pos.left + "px");
		$('#' + window_id).css("top", pos.top + "px");
		$("#" + window_id).css("display", "block");
	}

	this.createBg = function() {
		var sheight = document.documentElement.scrollHeight;
		var swidth = document.documentElement.scrollWidth;
		if (!$("#divbg").attr('id')) {
			var html = '<div id="divbg" class="overlayContent"></div>';
			$("body").append(html);
			// iframe的作用可以屏蔽对iframe覆盖下的控件的操作，同时在IE底下
			// 可以遮盖住select控件
			html = '<iframe id="iframeBg" width="100%" heigth="100%" frameborder="0" style="filter: alpha(opacity = 0); opacity: 0;"></iframe>';
			$("body").append(html);
		}
		if (model && model == 1) {
			$("#divbg").css("left", "0px");
			$("#divbg").css("top", "0px");
			$("#divbg").css("height", sheight + "px");
			$("#divbg").css("width", swidth + "px");

			$("#iframeBg").css("left", "0px");
			$("#iframeBg").css("top", "0px");
			$("#iframeBg").css("height", sheight + "px");
			$("#iframeBg").css("width", swidth + "px");

			$("#divbg").css("display", "block");
			$("#iframeBg").css("display", "block");
		}
	};

	this.getTopLeft = function(width, height) {
		var v_height = document.documentElement.clientHeight;
		var v_width = document.documentElement.clientWidth;
		var pos = {};
		pos.top = (v_height - height) / 2;
		if (pos.top < 0) {
			pos.top = 30;
		}
		pos.top += document.documentElement.scrollTop;
		pos.left = (v_width - width) / 2;

		return pos;
	};
}

/**
 * 弹出消息窗口
 * 
 * @param window_title
 *            窗口标题
 * @param msg
 *            消息内容
 * @param type
 *            消息窗口类型 'OK' 'OKCancel'
 * @param icon
 *            提示图标类型
 * @param callback
 *            回调函数，窗口关闭时调用callback(event)，可以根据event的值判断点击了哪个按钮
 */
function messageBox(window_title, msg, type, icon, callback) {
	if (!type) {
		type = 'OK';
	}
	if (!icon) {
		icon = 'warning';
	}

	this.onBtnConfirm = function() {
		closeWindow('window_message_box');
		callback('OK');
	}

	this.onBtnCancel = function() {
		closeWindow('window_message_box');
		callback('Cancel');
	}

	var content_div = '';
	content_div += '<div>';
	content_div += '<div class="center">';
	content_div += '<center><table><tr><td>';
	// 根据图标类型添加图标
	if (icon == 'warning') {
		content_div += '<img src="/admin/templates/default/images/icon_01.gif" align="absmiddle" />';
	}

	// 如果msg信息太长则换行，对于长单词，IE下可以强制换行，FireFox下则添加滚动条
	content_div += '</td><td><div style="max-width:320px;word-break:break-all;overflow:auto;">';
	content_div += msg;
	content_div += '</div></td></tr></table></center></div>';
	content_div += '<div class="center window-btn-bord">';

	// 根据类型创建按钮
	if (type == 'OK' || type == 'OKCancel') {
		content_div += '<input type="button" id="window_message_box_confirm" class="btn" value="确定" />';
	}
	if (type == 'OKCancel') {
		content_div += '&nbsp;&nbsp;&nbsp;&nbsp;';
		content_div += '<input type="button" id="window_message_box_cancel" class="btn" value="取消" />';
	}

	content_div += '</div>';
	content_div += '</div>';

	openWindow(window_title, content_div, 'window_message_box',
			'window_message_box');
	$('#window_message_box_confirm').click(this.onBtnConfirm);
	$('#window_message_box_cancel').click(this.onBtnCancel);
}

function showErrorMsg(id, msg, format) {
	$('#' + id).empty();
	$('#' + id).removeClass();
	$('#' + id).addClass('r_error');
	if (format == null) {
		format = '';
	}
	
	if (format == 'no_title') {
		var html = '<div class="msgText"><strong>' + msg + '</strong></div>';
	}
	else {
		var html = '<span class="msgTitle"><strong>错误提示</strong></span><br><div class="msgText">' 
			+ msg + '</div>';
	}
	$('#' + id).append(html);
}

function showSuccessMsg(id, msg) {
	$('#' + id).empty();
	$('#' + id).removeClass();
	if ('donotshowonsuccess'==msg) return;
	$('#' + id).addClass('r_ok');
	var html = '<span class="msgTitle"><strong>操作成功</strong></span><br><span class="msgText">' 
		+ msg + '</span>';
	$('#' + id).append(html);
}

function showAjaxResultMsg(id, data) {
	if (data.success()) {
		showSuccessMsg(id, data.getMsg());
	} else {
		showErrorMsg(id, data.getMsg());
	}
}

function hideErrorMsg(id, msg) {
	$('#' + id).empty();
}

jQuery.fn.showFormatError = function(msg) {
	$(this).parent().append('<span class="error_note erro-stop">'+msg+'</span>');
}

jQuery.fn.hideFormatError = function(msg) {
	$(this).parent().children(".error_note").remove();
}

/**
 * 列表排序
 * @param string id 排序标题的span id
 * @param string key_name 排序关键字名
 * @param string url 表单提交的URL地址
 * @param string query_form 查询表单的id
 * @return
 */
function orderList(id, key_name, url, query_form) {
	var order_class = $('#'+id).attr('class');
	var order_type = 'asc';
	var new_order_class = 'orderByIconAsc';
	if (order_class == 'orderByIconAsc' || order_class == 'orderIconAsc') {
		order_type = 'desc';
		new_order_class = 'orderByIconDesc';
	}
	
	url += '&order='+key_name+'&order_type='+order_type;
	
	getandmake(url+'&'+$('#query_form').serialize(),'grid_tbody','switcher_container');

	$('.orderByIconAsc').each(function(){
		$(this).attr('class', 'orderIconAsc');
	});
	$('.orderByIconDesc').each(function(){
		$(this).attr('class', 'orderIconDesc');
	});	
	
	$('#'+id).attr('class', new_order_class);
}
