var smod={
	edit:function(obj){
		var i=0;
		var oldvalue = obj.html();
		var width = obj.attr("width");
		if(!width) width="25px";
		obj.html('<input type="text" class="smodipt" value="'+oldvalue+'" style="width:'+(parseInt(width,10)-2)+'px">');
		obj.css({width:width});
		obj.attr('oldvalue',oldvalue);
		obj.unbind();
		obj.find('input.smodipt').select().focus().bind('blur keyup',function(event){if(event.type=='blur' || event.keyCode==13){
					var val = $(this).val();
					var oldval = obj.attr('oldvalue');
					if(oldval==val){
						obj.html(val);
						obj.bind('dblclick',function(){smod.edit($(this));});
						return;
					}
					var l = smod.getScript();
					var id = obj.attr("sid");
					if(!id){id=obj.parent("td").attr("sid");}
					if(!id){id=obj.parent("td").parent("tr").attr("sid");}
					var name = obj.attr("name");
					var type = obj.attr("type");
					if(type!=null && type!=""){
						if(type=="integer" && !/([0-9]+)/.test(val)){
							alert("类型不匹配");
							$(this).focus();
							return false;
						}
					}else{
						type="string";
					}
					var sdata = "inajax=1&act=ajaxEdit&id="+id+"&col="+name+"&val="+encodeURIComponent(val)+"&oldval="+encodeURIComponent(oldval)+"&type="+type;
					//window.clipboardData.setData("text",l+"?"+sdata);
					$('#smodTips').html('<img src="/images/loading.gif">');
					$.ajax({
						type:"POST",
						url:l,
						data:sdata,
						cache:true,
						dataType:"json",
						success:function(msg){
							if(msg.success){
								obj.html(val);
								obj.bind('dblclick',function(){smod.edit($(this));});
								$('#smodTips').html('');
							}else{
								if(msg.message){
									alert(msg.message);
								}
							}
						}
					})
			
				};
		});
	},
	getScript:function(){
		var tempstr = window.location.href;
		return tempstr.substring(0,tempstr.indexOf("?")>-1?tempstr.indexOf("?"):tempstr.length);	
	},
	iptedit:function(obj){
			
	},
	toggle:function(obj){
		var smodconfirm = obj.attr("class");
		if(smodconfirm && smodconfirm.indexOf("smodconfirm")!=-1){
			if(!confirm("确认更改状态?")) return false;	
		}
		var l = smod.getScript();
		var id = obj.attr("sid");
		if(!id){id=obj.parent("td").attr("sid");}
		if(!id){id=obj.parent("td").parent("tr").attr("sid");}
		var col = obj.attr("name");
		if(!id || !col){return;}
		var src = obj.attr("src");
		var val = 0;
		if (src.indexOf('no.gif')>-1)
		{
			val = 1;
			src=src.replace("no","yes");
		}else{
			val = 0;
			src=src.replace("yes","no");
		}
		$.ajax({
			type:"POST",
			url:l,
			data:"inajax=1&act=ajaxEdit&id="+id+"&col="+col+"&val="+val,
			cache:false,
			dataType:"json",
			success:function(msg){//alert(msg)
				if(msg.success){
					obj.attr("src",src);
				}else{
					alert("处理失败:\n"+msg.message);
				}
			}
		});
	},bind:function(){
		$('.smodedit').unbind();
		$('.smodtoggle').unbind();
		$('.smodedit').each(function(index){
			if($(this).html()=="&nbsp;" || $(this).html()==""){
				$(this).html("");
				var wid = $(this).attr("width");
				if(wid){wid = wid.replace("px","");}else{wid=30;}
				wid = parseInt(wid,10);
				wid = wid-2;
				$(this).css({width:wid,display:"inline-block",height:24});
			}
		});
		$('.smodedit').css({cursor:'pointer',display:'inline-block'}).bind('dblclick',function(){smod.edit($(this));})
		$('.smodtoggle').css('cursor','pointer').bind('click',function(){smod.toggle($(this));});
	}
};
$(document).ready(function(){
	$('body').ajaxStart(function(){$('#smodTips').html('<img src="/images/loading.gif">');});
	$('body').ajaxError(function(){$('#smodTips').html('Error');});
	$('body').ajaxComplete(function(){$('#smodTips').html('');});
	$('.smodedit').each(function(index){
		//if($(this).html()=="&nbsp;" || $(this).html()==""){
		//	$(this).html("");
			var wid = $(this).attr("width");
			if(wid){wid = wid.replace("px","");}else{wid=30;}
			wid = parseInt(wid,10);
			wid = wid-2;
			$(this).css({width:wid,display:"inline-block",height:24});
		//}
	});
	$('.smodedit').css({cursor:'pointer',display:'inline-block'}).bind('dblclick',function(){smod.edit($(this));})
	$('.smodtoggle').css('cursor','pointer').bind('click',function(){smod.toggle($(this));});
});