

//checkbox的全选
function selectAll(obj, form) {
	if (form == 'undefined') var form = document.forms[0];
	if ($(obj).attr('checked') == false) {
		$('input:checkbox', form).attr('checked', '');
	} else {
		$('input:checkbox', form).attr('checked', 'checked');
	}
}
//获得checkbox的值
function getChkBox() {
	var id = '';
	var cb = $("input[type=checkbox][name=select_id]:checked");
	cb.each(function (i) {
		id += cb[i].value + ',';
	})
	if (id == '') {
		showMsg('至少选择一个');
		return false;
	}
	return id;
}
//添加为收藏
function addFavorite() {
	var url = 'http://www.bidaround.cn';
	var title = "周边淘，找到你周边的淘宝网店";
	if (window.sidebar) window.sidebar.addPanel(title, url, "");
	else if (document.all) window.external.AddFavorite(url, title);
	else if ( window.opera && window.print) return true;
}
//设置主页
function setHomePage(obj) {
	try {
		obj.style.behavior='url(#default#homepage)';
		obj.setHomePage('http://www.bidaround.cn');
	} catch(e) {
		alert("您现在使用的浏览器无法自动设为首页，请手动设置！");
	}
}
//获取cookie
function getCookie(sName) {
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++) {
		var aCrumb = aCookie[i].split("=");
		if (encodeURIComponent(sName) == aCrumb[0])
			return decodeURIComponent(aCrumb[1]);
	}
	return null;
}
//设置cookie
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) {
	try {
	   var sCookie = sName + "=" + encodeURIComponent(sValue);
	   if (oExpires) 
		   sCookie += "; expires=" + oExpires.toGMTString();
	   if (sPath)
		   sCookie += "; path=" + sPath;
	   if (sDomain)
		   sCookie += "; domain=" + sDomain;
	   if (bSecure)
		   sCookie += "; secure";
		document.cookie = sCookie;
	} catch(e) {}
}
//ui的alert
function showMsg(msg, fn) {
	if(msg == 'undefined' || msg.length == 0){
		return false;
	}
	if (typeof $('#ajaxMsg')[0] == 'undefined') {
		var ajaxMsg = $('<div id="ajaxMsg"><p class="p_msg"></p></div>').hide();
		$('body').append(ajaxMsg);
	}

	$('#ajaxMsg .p_msg').html(msg);
	$('#ajaxMsg').dialog({
		bgiframe: true,
		minHeight: 10,
		resizable: false,
		modal: true,
		stack: true,
		title: '温馨提示',
		buttons: {
			'确定': function () {
				$(this).dialog('close');
			}
		},
		close: function () {
			if (typeof fn != 'undefined') fn();
			$(this).dialog('destroy');
		}
	})
}
//会自动消失的提示框
function quickMsg(msg, time) {
	if (typeof $('#quickMsg')[0] == 'undefined') {
		var ajaxMsg = $('<div id="quickMsg"><p class="p_msg" style="color:#f60; font-size:24px; font-weight:bold;"></p></div>').hide();
		$('body').append(ajaxMsg);
	}

	$('#quickMsg .p_msg').html(msg);
	$('#quickMsg').dialog({
		bgiframe: true,
		minHeight: 10,
		modal: false,
		resizable: false,
		stack: true,
		title: '温馨提示'
	})

	if(typeof time == 'undefined')
		time = 3000;
	setTimeout(function(){
		$("#quickMsg").fadeOut(500, function(){
			$(this).dialog('destroy')
		});
	}, time);
}
//ui的dialog确认modal
function showConfirmModal(title, fn) {
	var confirmButton = $('<button type="button" class="ui-state-default ui-corner-all">确定</button>');
	var escButton     = $('<button type="button" class="ui-state-default ui-corner-all">取消</button>');
	var confirmModal  = $('<div id="confirmModal"></div>');
	confirmModal.append(escButton).append(confirmButton);
	confirmButton.click(function () {
		fn();
		confirmModal.dialog('close');
	})
	escButton.click(function () {
		confirmModal.dialog('close');
	})
	$('body').append(confirmModal);
	confirmModal.dialog({
		bgiframe: true,
		minHeight: 10,
		height:100,
        resizable: false,
		title: title,
		close: function () {
			$(this).dialog('destroy');
		}
	})
	confirmButton[0].blur();
	confirmModal.addClass('ui-dialog-buttonpane ui-widget-content');
}
//ui的dialog表单modal
function showFormModal(option) {
	var title, modal, confirmFn, width, height;
	title     = option.title;//标题，必选
	confirmFn = option.confirmFn;//确定后执行的函数，必选
	modal     = option.modal;//jquery对象，必选

	//不重复生成html
	modal.hide();
	var modalId = modal.attr('id');
	if (typeof $('#'+modalId).attr('id') == 'undefined') {
		$('body').append(modal);
	} else {
		modal = $('#' + modalId);
	}

	//确定高度、宽度
	if (typeof modal.attr('height') != 'undefined') {
		height = parseInt(modal.attr('height'));
	} else {
		height = 'auto';
	}
	if (typeof modal.attr('width') != 'undefined') {
		width = parseInt(modal.attr('width'));
	} else {
		width = modal.width() + 60;//不能用auto，ie下出错
		if (width < 300) width = 300;
	}

	if (typeof option.isModal == 'undefined') option.isModal = true;

	modal.dialog({
		bgiframe: true,
		minHeight: 10,
		resizable: false,
		title: title,
		modal: option.isModal,
		width: width,
		height: height,
		buttons: {
			'取消': function () {
				$(this).dialog('close');
			},
			'确定': function () {
				var res = confirmFn();
				if (res != false) $(this).dialog('destroy');
			}
		},
		close: function () {
			if (typeof option.cancelFn != 'undefined') option.cancelFn();//取消后执行的函数，可选
			$(this).dialog('destroy');
		}
	})
}
//ajax提交表单
function ajaxFormPost(method, modalOption, param) {
	if (typeof param == 'undefined') param = {ajax : 1};
	else param.ajax = 1;

	$.post(
		'/index.php/' + method,
		param,
		function (data) {
			if (data.indexOf('{"msg"') >= 0) {
				data = eval('(' + data + ')');
				showMsg(data.msg, function () {
					if (method == 'user/add') {
						quickMsg('自动采集中，稍等3秒，几分种后会有新的数据了');
						$.post(
							'/index.php/collect/manual/' + data.userId
						)
						setTimeout(function () {
							location.reload();
						}, 4000);
					} else {
						location.reload();
					}
				})
				return false;
			}
			var ajaxPostModal = $('<div id="' + modalOption.id + '" width="' + 
					modalOption.width + 'px" height="' + modalOption.height + 'px"></div>');
			
			showFormModal({
				modal : ajaxPostModal,
				confirmFn : function () {
					quickMsg('Loading', 100);
					var postParam = {asubmit : 1};
					$.each($('#' + modalOption.id + ' form').serializeArray(), function (i, field) {
						eval("postParam." + field.name +  "='" + field.value + "'");
					})
					ajaxFormPost(method, modalOption, postParam);
					return false;
				},
				title : modalOption.title
			})
			$('#' + modalOption.id).html(data);
		}
	)
}
//ajax确认
function ajaxConfirmPost(method, param, fn) {
	if (typeof param == 'undefined') param = {ajax : 1};
	else param.ajax = 1;
	showConfirmModal('确定执行该操作？', function () {
		$.post(
			'/index.php/' + method,
			param,
			function (data) {
				if (data.msg) {
					showMsg(data.msg, function () {
						if (typeof fn != 'undefined') fn();
						else location.reload();
					})
					return false;
				}
			},
			'json'
		)
	})
	return false;
}
function showIframeModal(src, option) {
	var id = option.id;
	var width = option.width;
	var height = option.height;
	var iwidth = width - 15;
	var iheight = height - 50;
	
	var modal = $('<div id="modal-'+id+'" width="'+width+'" height="'+height+'">'+
		'<iframe id="iframe-'+id+'" src="'+src+'" width="'+iwidth+'" height="'+iheight+'" frameborder="0"></iframe></div>');

	//不重复生成html
	modal.hide();
	var modalId = modal.attr('id');
	if (typeof $('#'+modalId).attr('id') == 'undefined') {
		$('body').append(modal);
	} else {
		modal = $('#' + modalId);
	}

	//确定高度、宽度
	if (typeof modal.attr('height') != 'undefined') {
		height = parseInt(modal.attr('height'));
	} else {
		height = 'auto';
	}
	if (typeof modal.attr('width') != 'undefined') {
		width = parseInt(modal.attr('width'));
	} else {
		width = modal.width() + 60;//不能用auto，ie下出错
		if (width < 300) width = 300;
	}

	modal.dialog({
		bgiframe: true,
		minHeight: 10,
		resizable: false,
		title: option.title,
		modal: true,
		width: width,
		height: height
	})

	$('#iframe-' + id).unbind('load');
	$('#iframe-' + id).bind("load", function(){
		if (typeof option.load != 'undefined') {
			var data = $('#iframe-' + id)[0].contentWindow.document.body;
			if (true == option.load(data)) $('#modal-' + id).dialog('close');
		}
	})
}