// 管理区共用脚本

// 表单DataGrid中全选checkbox
function checkFormAll(obj,chk){
	$(obj).parents("table:eq(0)").find(":checkbox[name][id]").attr("checked",$(obj).attr("checked")=="checked");
}


// 批量删除时确认
function confirm_batch_delete(){
	var chked = $("table.grid :checkbox:checked[name][id]").length;
	if(chked==0){
		alert("请选择要删除的记录");
		return false;
	}
	return confirm("确认要删除"+chked+"条记录?");
}

// 左侧菜单选中
function menuChange(menu)
{
	var o = document.getElementById(menu);
	if (o==null){
		return;
	}
	if (o.className=="lmenu"){
		o.className="lmenu lmenuShow";
	}else{
		o.className="lmenu";
	}
	return;
}

// 初始当前选中的左侧菜单
function initMenu()
{
	var cfile=location.href;
	if(cfile.indexOf("?")>0){
		cfile = cfile.substring(0,cfile.indexOf("?"));
	}
	cfile = cfile.substr(cfile.lastIndexOf("/")+1);
	// 不在菜单中的页面转为菜单中的地址
	var fileConv = [["admin_articleEdit.aspx","admin_article.aspx"],
								["admin_softEdit.aspx","admin_article.aspx"],
								["admin_member_ncView.aspx","admin_member.aspx"],
								["admin_power.aspx","admin_member.aspx"],
								["admin_sendMail.aspx","admin_member.aspx"],
								["admin_fileEdit.aspx","admin_config.aspx"]];
	
	for(var i=0;i<fileConv.length;i++){
		if(fileConv[i][0] == cfile){
			cfile = fileConv[i][1];
		}
	}
	
	var $a = $(".lmenu").find("a[href|='"+cfile+"']");

	
	var $p = $a.parents(".lmenu")
	$p.addClass("lmenuShow lmenuThis");
}

// 登出
function logout(backurl){
	iwms.cookie("iwmsAdmin","y",-1);
	setTimeout('top.location="'+backurl+'"',200);
}

var keepTimer;
function keeplog(){
	iwms.cookie("iwmsAdmin","y");
	iwms.logSave(iwms.user);
}

$(window).unload(function(){
	if (!isNaN(keepTimer)){
		window.clearInterval(keepTimer);
	}
});


$(function(){
	initMenu();
	// table.grid行变色
	$("table.grid tr.tdbg").mouseover(function(){this.className="tdbg-dark";})
		.mouseout(function(){this.className="tdbg";});
	// myLabel中操作结果显示
	var msg = $(".msgBar").html();
	if(msg && msg.length>0){
		$(".msgBar").css("display","block");
	}
	// 保持登录
	var interval = 480000;
	keepTimer = window.setInterval('keeplog()', interval);
});


// spin功能类
var spin = new Object();
spin.plus=function(obj,max,step){
	spin.plusOrMinus(1,obj,max,step);
};
spin.minus=function(obj,min,step){
	spin.plusOrMinus(0,obj,min,step);
};
spin.plusOrMinus=function(add,obj,limit,step){
	obj=$(obj).parent().parent().find("input:eq(0)").get(0);
	spin.add=add;
	spin.input=obj;
	spin.limit=limit;
	spin.step=step==undefined?1:step;
	spin.val=parseFloat(obj.value);
	if(isNaN(spin.val))spin.val=0;
	if(spin.exceed())return;
	spin.change();
	spin.interval=window.setInterval('spin.change()',120);
};
spin.stop=function(){
	window.clearInterval(spin.interval);
};
spin.change=function(){
	if(spin.add){
		spin.val+=spin.step;
	}else{
		spin.val-=spin.step;
	}
	if(!spin.exceed()){
		spin.input.value=spin.val;
	}
};
spin.exceed=function(){
	if((spin.add && spin.val>spin.limit) || (!spin.add && spin.val<spin.limit)){
		return true;
	}else{
		return false;
	}
};
spin.add=function(obj){
	if($.isArray(obj)){
			for(var i=0;i<obj.length;i++){
				spin.add(obj[i]);
			}
	}else{
		var step=obj.step==undefined?1:obj.step;
		var max=obj.max==undefined?999999:obj.max;
		var min=obj.min==undefined?-999999:obj.min;
		var $ipt=typeof(obj.input)=='string'?$('#'+obj.input):$(obj.input);
		if(obj.readonly)$ipt.attr("readonly","readonly");
		
		$ipt.wrap('<table border="0" cellpadding="0" cellspacing="0" class="spin"><tr><td></td><td>'+
			'<button onMouseDown="spin.plus(this,'+max+','+step+')" onMouseUp="spin.stop()" onMouseOut="spin.stop()" onClick="return false" class="spinPlus"></button>'+
			'<button onMouseDown="spin.minus(this,'+min+','+step+')" onMouseUp="spin.stop()" onMouseOut="spin.stop()" onClick="return false" class="spinMinus"></button>'+
			'</td></tr></table>');

	}
};
