var solution = {
	id : '',
	action :'',
	url : 'do.php',
	logout : function(){
		//$.jConfrim('你确认要退出系统吗？');
	},
	
	changeState : function(_this, action, id){
		var state = parseInt($(_this).attr("alt"))==1?0:1;
		$.post('do.php', {'do' : action, 'id': id, 'state': state }, function(data){
			var data = eval('('+data+')');
			if(data.result == 'true'){
				$(_this).attr({"src":'images/state_'+state+'.gif', 'alt': state});
			}
		});
	},
	
	batchChangeState : function(action, id, state){
		$.post('do.php', {'do' : action, 'id': id, 'state': state }, function(data){
			var data = eval('('+data+')');
			if(data.result == 'true'){
				$.jAlert('操作完成！',function(){
					window.location.reload();
				});
				//$(_this).attr({"src":'images/state_'+state+'.gif', 'alt': state});
			}
		});
		
	},

	changeNum : function(_this, action, id){
		if ($("#ipt_num").length) return;
		if (action) solution.action = action;
		if (id) solution.id = id;
		
		var num = parseInt($(_this).text());
		$(_this).html('<input type="text" id="ipt_num" value="'+ num + '" size="4" />');
		$("#ipt_num").focus().blur(function(){
			var new_num = $(this).val();
			if (isNaN(new_num)) {
				$.jAlert('这不是个数字');
				return;	
			}else{
				if (new_num > 99999){
					$.jAlert('填写数字应在0~99999之间');
					return false;
				}
			}
			if (num == new_num) {
				$(_this).text(num);
				return;	
			}
			$.post('do.php', {'do' : solution.action, 'id':solution.id, num : new_num}, function(data){
				var data = eval('('+data+')');
				if(data.result == 'true'){
					$(_this).text(new_num);
				}
			});
		});

		$("#ipt_num").keyup(function(e){
			//alert(e);
			if (e.keyCode == 13) {
				$("#ipt_num").blur();
			}
		});
		
		return;
	},
	
	changePrice : function(_this, action, id){
		if ($("#ipt_price").length) return;
		if (action) solution.action = action;
		if (id) solution.id = id;

		var price = $(_this).text().replace(/\,/g,'');
		
		$(_this).html('<input type="text" id="ipt_price" value="'+ price + '" size="6" />');
		
		$("#ipt_price").focus().blur(function(){
			var new_price = $(this).val();
			if (isNaN(new_price)) {
				$.jAlert('这不是个数字');
				return;	
			}else{
				if (new_price > 999999999){
					$.jAlert('填写数字应在0~999999999间');
					return false;
				}
			}
			if (price == new_price) {
				$(_this).text(price);
				return;	
			}
			
			$.post('do.php', {'do' : solution.action, 'id':solution.id, price : new_price}, function(data){
				var data = eval('('+data+')');
				if(data.result == 'true'){
					$(_this).text(data.msg);
				}
			});
		});
		
		$("#ipt_price").keyup(function(e){
			//alert(e);
			if (e.keyCode == 13) {
				$("#ipt_price").blur();
			}
		});
		return;
	},
	
	editTitle : function(_this, action, id){
		if ($("#ipt_title").length) return;
		if (action) solution.action = action;
		if (id) solution.id = id;

		var title = $(_this).text();
		$(_this).html('<input type="text" id="ipt_title" value="'+ title + '" size="'+ (title.length * 1.5) +'"  />');
		
		$("#ipt_title").focus().blur(function(){
			var new_title = $(this).val();
			
			if (title == new_title) {
				$(_this).text(title);
				return;	
			}
			if (new_title == '') {
				$.jAlert('不能为空');
				return;	
			}
			$.post('do.php', {'do' : solution.action, 'id':solution.id, title : new_title}, function(data){
				var data = eval('('+data+')');
				if(data.result == 'true'){
					$(_this).text(new_title);
				}else{
					$.jAlert(data.msg);
				}
			});
		});
		
		$("#ipt_title").keyup(function(e){
			//alert(e);
			if (e.keyCode == 13) {
				$("#ipt_title").blur();
			}
		});
		
		
		return;
	},
	
	trash : function(_this, action, id){
		if (action) this.action = action;
		if (id) this.id = id;
		$.jConfirm('你确认移到回收站吗？',function (){
			$.post(solution.url, {'do' : solution.action, 'id':solution.id, 'state': '1'}, function(data){
				var data = eval('('+data+')');
				if(data.result == 'true'){
					if ($(_this).parents("tr").length){
					$(_this).parents("tr").remove();
					}else{
						window.history.back();
					}
				}else{
					$.jAlert(data.msg);
				}
			});
		});
	},
	
	batchTrash : function(action, id, state){
		$.jConfirm('你确认移到回收站吗？',function (){
			$.post('do.php', {'do' : action, 'id': id, 'state': state }, function(data){
				var data = eval('('+data+')');
				if(data.result == 'true'){
					$.jAlert('操作完成！',function(){
						window.location.reload();
					});
				}
			});
		});
	},
	
	restore : function(_this, action, id){
	if (action) this.action = action;
	if (id) this.id = id;
	//$.jConfirm('你确认移到回收站吗？',function (){
		$.post('do.php', {'do' : solution.action, 'id':solution.id, 'state': '0'}, function(data){
			var data = eval('('+data+')');
			if(data.result == 'true'){
				$(_this).parents("tr").remove();
			}else{
				$.jAlert(data.msg);
			}
		});
	//});
	},
	
	del : function(_this, action, id){
		if (action) this.action = action;
		if (id) this.id = id;

		$.jConfirm('你确认要删除吗？',function (){
			$.post('do.php', {'do' : solution.action, 'id':solution.id}, function(data){
				var data = eval('('+data+')');
				if(data.result == 'true'){
					$(_this).parents("tr").remove();
				}else{
					$.jAlert(data.msg);
				}
			});
		});
	},
	
	update : function(action, id){
		if (action) this.action = action;
		if (id) this.id = id;

		$.jConfirm('你确认要更新发布时间吗？',function (){
			$.post('do.php', {'do' : solution.action, 'id':solution.id}, function(data){
				var data = eval('('+data+')');
				if(data.result == 'true'){
					//window.location.reload();					
				}else{
					$.jAlert(data.msg);
				}
			});
			
		});
	},
	batchDel : function(action, id){
		if (action) this.action = action;
		if (id) this.id = id;
		$.jConfirm('你确认要删除吗？',function (){
			$.post('do.php', {'do' : solution.action, 'id':solution.id}, function(data){
				var data = eval('('+data+')');
				if(data.result == 'true'){
					window.location.href = window.location.href;
				}else{
					$.jAlert(data.msg);
				}
			});
		});
	},

	save : function(url,pm){
		$.post(url ,pm ,function(data){
			var data =eval('('+data+')');
			if(data.result == 'true'){
				$.jAlert(data.msg,function(){
					if (data.url){
						if (data.url == 'reload'){
						  window.location.href = window.location.href		
					  }else{
						  window.location.href = data.url;		
						}
					}
				});

			}else{
				$.jAlert(data.msg);	
			}
		});
	}
}



function writeFlash(flash){
	
}

//表单提交处理函数，带文件上传的表单
$.fn.ifrPost = function(todo) {
	var submited = false;
	if (!$("#ifr").length) $('<iframe id="ifr" name="ifr" style="display:none;"></iframe>').appendTo($(this));
	if ($(this).attr("target") != "ifr") $(this).attr("target","ifr");
	if (!submited) $(this).submit();	
	var doc = document.all?$(document.frames('ifr').document.body).html():$(document.getElementById('ifr').contentWindow.document.body).html();
	if (doc == '') doc = null;
	if (doc != null){
		//alert(doc);
		document.all?$(document.frames('ifr').document.body).html(''):$(document.getElementById('ifr').contentWindow.document.body).html('');
		todo(doc);
	}else{
		//alert(document.getElementById("ifr").contentDocument.body.innerHTML);
		setTimeout(function(){$(this).ifrPost( todo );},100);
	}
}

$.fn.checkAll = function(name){
	$(this).click(function(){
		if(this.checked == true){
			$(":input[name="+name+"]").attr("checked", true);
			$("#btn_batch").attr("disabled", false);
		}else{
			$(":input[name="+name+"]").attr("checked", false);
			$("#btn_batch").attr("disabled", true);
		}
	});
}


$.fn.notice = function(tof, msg){
	if (tof == 1){
		$(this).next('span').remove();
		$(this).after('<span class="check_right">'+msg+'</span>');
	}else if(tof == 0){
		$(this).next('span').remove();
		$(this).after('<span class="check_error">'+msg+'</span>');
	}else if(tof == 2){
		$(this).next('span').remove();
		$(this).after('<span class="check_note">'+msg+'</span>');
	}
}


$.fn.note = function(type, note){
	if (!note) var note = '';
	if (type == 1){ //填写正确
		$(this).parent().find('.note').removeClass('err').addClass('ok').text(note).css('visibility','visible');
	}else if(type == 0){
		//$(this).addClass('ipt_err');
		$(this).parent().find('.note').removeClass('ok').addClass('err').text(note).css('visibility','visible');
	}else if(type == -1){
		$(this).parent().find('.note').removeClass('ok err').text(note).css('visibility','hidden');
	}
	return this;
}

$.fn.mytab = function(e){
	if (!e) e = "click";
	var _tab = $(this);
	$(this).find(".tab_head > li").bind(e,function(e){
		$(_tab).find(".tab_head > .tab_on").removeClass("tab_on");
		$(this).addClass("tab_on");
		$(_tab).find(".tab_body > div:visible").hide();//
		$(_tab).find(".tab_body > div:hidden").eq($(this).parent().children().index($(this))).show();//
	});	
}


$.extend({
	jAlert : function(msg,fn){
		if (!msg) var msg = '无消息内容';
			$("#tb_box_alert").remove(); tb_remove();
			setTimeout(function(){  
				$('<div id="tb_box_alert" style="display:none;" ><p style="display:block; height:60px;line-height:24px" >'+ msg +'</p><div style="text-align:center"><input type="button" name="btn_ok" id="btn_ok" value="   确定   " onclick="javascript: tb_remove();" /></div></div>').appendTo($("body"));
				tb_show('提示消息', "#TB_inline?height=100&width=360&inlineId=tb_box_alert&modal=false" , null);
				$("#TB_window").draggable({handdle:"#TB_title" ,cursor : 'move', cancel : '#TB_ajaxContent'});
				if (fn) {
					$("#btn_ok").click(function(){
						tb_remove();
						fn.call();
					});
					$("#TB_closeWindowButton").click(function(){
						fn.call();
					});
				}
			}
		,300)
		//alert('ss');
	},
	
	jConfirm : function(msg, fn){
		if (!msg) var msg = '无消息内容';
		$("#tb_box_alert").remove(); tb_remove();
		setTimeout(function(){  
		$('<div id="tb_box_alert" style="display:none;" ><p style="display:block; height:60px;line-height:24px" >'+ msg +'</p><div style="text-align:center"><input type="button" name="btn_ok" id="btn_ok" value="   确定   " onclick="javascript: tb_remove();" /> <input type="button" name="btn_alert" id="btn_cancel" value="   取消   " onclick="javascript: tb_remove();" /></div></div>').appendTo($("body"));
		tb_show('提示消息', "#TB_inline?height=100&width=360&inlineId=tb_box_alert&modal=false" , null);
		$("#TB_window").draggable({handdle:"#TB_title" ,cursor : 'move', cancel : '#TB_ajaxContent'});
		$("#btn_ok").click(function(){
			tb_remove();
			fn.call();
		});
		},300);
	},	
	jWindow : function(target, title, width, height, file)
	{
		if (file){
			if (target.indexOf('?') != -1) {
				target += '&';
			}else{
				target += '?';
			}
			tb_show(title, target + "width=" + width + "&height=" + height + "&modal=false" , null);
		}else{
			tb_show(title, "#TB_inline?inlineId=" + target + "&width=" + width + "&height=" + height + "&modal=false" , null);
		}
		$("#TB_window").draggable({handdle:"#TB_title" ,cursor : 'move', cancel : '#TB_ajaxContent'});
	}
	
});

Number.prototype.format=function(decimalPoints,thousandsSep,decimalSep){
 var val=this+'',re=/^(-?)(\d+)/,x,y;
 if (decimalPoints!=null) val = this.toFixed(decimalPoints);
 if (thousandsSep && (x=re.exec(val))){
  for (var a=x[2].split(''),i=a.length-3;i>0;i-=3) a.splice(i,0,thousandsSep);
  val=val.replace(re,x[1]+a.join(''));
 }
 if (decimalSep) val=val.replace(/\./,decimalSep);
 return val;
}
if (typeof Number.prototype.toFixed!='function' || (.9).toFixed()=='0' || (.007).toFixed(2)=='0.00') Number.prototype.toFixed=function(f){
 if (isNaN(f*=1) || f<0 || f>20) f=0;
 var s='',x=this.valueOf(),m='';
 if (this<0){ s='-'; x*=-1; }
 if (x>=Math.pow(10,21)) m=x.toString();
 else{
  m=Math.round(Math.pow(10,f)*x).toString();
  if (f!=0){
   var k=m.length;
   if (k<=f){
    var z='00000000000000000000'.substring(0,f+1-k);
    m=z+m;
    k=f+1;
   }
   var a = m.substring(0,k-f);
   var b = m.substring(k-f);
   m = a+'.'+b;
  }
 }
 if (m=='0') s='';
 return s+m;
}

function gotoPage(url, pm , page){
	window.location.href = url + '?page=' + page + pm ;	
}
