﻿jQuery.fn.Init=function(func)
{
	this.each(function(){
		var f=func;
		if(f==null)
		{
			f=eval("class_"+this.getAttribute("id"));
		}
		this.behavior=new f(this);
		if(this.behavior.Init!=null)
			this.behavior.Init();
		if(this.behavior.Ready!=null)
		{
			var ready=this.behavior.Ready;
			$(this).ready(function(){ready();});
		}
	});
};
jQuery.fn.ajaxSubmit=function(callback)
{
	var form=this;
	var items=$("*[name]",this);

	var strChecked="";
	var post="";

	function formFilter()
	{
		if((this.type=="radio"||this.type=="checkbox")&&this.checked==false)
			return false;
		return true;
	}

	items.each(function(){
		var item=$(this);
		var key=item.attr("name");
		if(strChecked.indexOf("["+key+"]")>=0)
			return true;
		strChecked+="["+key+"]";

		var obj=$("*[name='"+key+"']:enabled:visible",form).filter(formFilter);
		var value="";
		if(obj.size()>1)
			value=obj.val();
		else if(obj.size()==1)
			value=obj.val();
		post+="&"+encodeURI(key)+"="+encodeURI(value);
	});

	if(post.indexOf("&")==0)
		post=post.substring(1);
	var m_Url=form.attr("action");
	var m_Type="GET";
	if(form.attr("method").toLowerCase()=="post")
	{
		m_Type="POST";
	}
	
	m_Url+=m_Url.indexOf("?")>=0?"&":"?";
	m_Url+="t="+Math.random();

	$.ajax({type:m_Type,url:m_Url,dataType:"json",data:post,success:callback,error:function(msg){alert(msg.responseText);}});
};
//在事件type触发前先用fn检查
jQuery.fn.preBind=function(type,fn)
{
	this.each(function(){
		var preHandler=this["on"+type];
		this["on"+type]=null;
		var newHandler=function(e){
			if(fn(e)==true)
			{
				if(preHandler!=null)
					preHandler(e);
				e.returnValue=true;
				return true;
			}else{
				e.returnValue=false;
				return false;
			}
		};
		$(this).bind(type,newHandler);
	});
};
jQuery.getAjax=function(url,data,callback)
{
	url+=url.indexOf("?")>=0?"&":"?";
	url+="t="+Math.random();
	var parameter=new Object();
	parameter.type="POST";
	parameter.url=url;
	parameter.data=data;
	parameter.success=callback;
	parameter.dataType="json";
	parameter.error=function(msg){alert(msg.responseText);};

	$.ajax(parameter);
};
jQuery.ShowError=function(sender,msg)
{
	var td=sender.parents("td:first");
	if(sender.attr("tagName")=="INPUT"&&sender.attr("type")=="text")
	{
		sender.addClass("errborder");
	}
	td.find("div.errortip").empty();
	var div=$("<div class='errortip'></div>");
	td.append(div);
	div.text(msg);
};
jQuery.ClearError=function(parent)
{
	if(parent==null)
	{
		$("div.errortip").remove();
		$("input.errborder").removeClass("errborder");
	}
	else
	{
		$(parent).find("div.errortip").remove();
		$(parent).find("input.errborder").removeClass("errborder");
	}
};
jQuery.HasError=function()
{
	if($("div.errortip").length>0)
		return true;
	else
		return false;
};
jQuery.CheckError=function()
{
	var first=null;
	$("*[errmsg]").each(function(){
		if(first==null)
			first=$(this);
		$.ShowError($(this),$(this).attr("errmsg"));
	});
	if(first!=null)
	{
		first.focus();
		first.select();
	}
};
jQuery.CheckEmpty=function(parent)
{
	var th=null;
	if(parent==null)
		th=$("table th:has(b:visible)");
	else
		th=$(parent).find("table th:has(b)");
	var result=true;
	th.each(function(){
		var obj=$(this).next("td").find(":input");
		for(var i=0;i<obj.length;i++)
		{
			var item=obj.eq(i);
			var empty=false;
			if(item.attr("type")!="radio"&&item.attr("type")!="checkbox")
			{
				if(item.val()=="")
					empty=true;
			}
			else
			{
				var items=$(this).next("td").find(":checked");
				if(items.length==0)
					empty=true;
			}
			if(empty==true)
			{
				$.ShowError(item,"Can not empty");
				item.focus();
				$(document.body).scrollTop(item.offset().top-100);
				result=false;
				return false;
			}
			if(item.attr("format")!=null)
			{
				if((item.attr("format")=="float"||item.attr("format")=="int")&&isNaN(item.val())==true)
				{
					$.ShowError(item,"Error format");
					item.focus();
					item.select();
					result=false;
					return false;
				}
			}
		}
	});
	return result;
};
jQuery.setEditDiv=function(sender)
{
	$(sender).each(function(){SetMode(this);});

	function SetMode(div)
	{
		div.contentEditable=true;
		div=$(div);
		div.bind("paste",div_Paste);
	}

	function div_Paste()
	{
		var rng=document.selection.createRange();
		rng.pasteHTML(window.clipboardData.getData("text"));
		return false;
	}
};
jQuery.AutoRedirect=function()
{
	var linkRedirect=$("a#linkRedirect");
	if(linkRedirect.length>0)
	{
		window.setTimeout(function(){document.location=linkRedirect.attr("href");},1000);
	}
};
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
//使点击caption时能够移动form
jQuery.bindMove=function(caption,form,endCallBack,movingCallBack)
{
	caption=$(caption);
	form=$(form);
	caption.css("cursor","move");
	var moving=false;
	var prePos={x:0,y:0,left:0,top:0};
	var fn_MouseDown=function(e){
		prePos.x=e.pageX;
		prePos.y=e.pageY;
		var pos=form.offset();
		prePos.left=pos.left-e.pageX;
		prePos.top=pos.top-e.pageY;
		moving=true;
		if(e.target.setCapture!=null)
			e.target.setCapture();
		else{
			window.addEventListener("mousemove",fn_MouseMove, true);
			window.addEventListener("mouseup",fn_MouseUp,true);
		}
	};

	var fn_MouseMove=function(e){
		if(e.which==1&&moving==true)
		{
			form.css("left",(e.pageX+prePos.left)+"px");
			form.css("top",(e.pageY+prePos.top)+"px");
			if(movingCallBack!=null)
				movingCallBack();
		}
	};
	var fn_MouseUp=function(e){
		if(moving==true)
		{
			moving=false;
			if(e.target.releaseCapture!=null)
				e.target.releaseCapture();
			else{
				window.removeEventListener("mousemove",fn_MouseMove,true);
				window.removeEventListener("mouseup",fn_MouseUp,true);
			}
			if(endCallBack!=null)
				endCallBack();
		}
	};
	caption.mousedown(fn_MouseDown);
	caption.mousemove(fn_MouseMove);
	caption.mouseup(fn_MouseUp);
}
//创建弹出菜单
jQuery.createPopup=function(topmenu,fnCallBack)
{
	var topmenu=$(topmenu);
	var strMenu="li:not(.line)";
	var submenu=topmenu.find(strMenu);
	var subtitle=topmenu.find(strMenu+":has(ul)");
	var myFocus=false;
	var timeID=0;

	//鼠标移动到当前行
	submenu.mouseover(function(e)
	{
		var me=$(this);
		me.addClass("hover");
		if(this.menu!=null)
		{
			var p=me.offset();
			var menu=$(this.menu);
			menu.css("left",p.left+me.width()+25);
			menu.css("top",p.top);
			menu.show();
		}
		me.siblings(".hover").each(function(){
			$(this).removeClass("hover");
			if(this.menu!=null)
				$(this.menu).hide();
		});
	});
	submenu.bind("selectstart",function(){return false;});
	submenu.add(topmenu[0]).mousedown(function(){myFocus=true;});
	submenu.mouseout(function(e)
	{
		if(this.menu==null)
			$(this).removeClass("hover");
	});
	submenu.mousedown(function(e)
	{
		//myFocus=true;
	});
	submenu.click(function(e)
	{
		if(this.menu!=null)
			return;
		timeID=1;
		fnHide();
		var tm=$(this).parent("ul")[0].topmenu;
		if(tm!=null)
			tm=$.trim($(tm).find("span:first").text());
		else
			tm="";
		if(fnCallBack!=null)
			fnCallBack($(this),tm);
	});
	topmenu.showPop=function(e)
	{
		var left=e.pageX;
		var top=e.pageY;

		if(top>$(document.body).height()-topmenu.height())
		{
			top=top-topmenu.height();
		}
		if(left>$(document.body).width()-topmenu.width())
		{
			left=left-topmenu.width();
		}
		topmenu.css("left",left+"px");
		topmenu.css("top",top+"px");
		topmenu.show();
		timeID=0;
		$(document.body).focus();
		subtitle.each(function(){
			if(this.menu!=null)
			{
				topmenu.parent().append(this.menu);
			}
		});
		$(document).bind("keydown",fnPreventKey);
	};
	function fnHide()
	{
		if(timeID==0)
		{
			timeID=window.setTimeout(fnHide,0);
			return;
		}else{
			timeID=0;
			if(myFocus==true)
			{
				$(document.body).focus();
				myFocus=false;
				return;
			}
		}
		subtitle.each(function(){
			if(this.menu!=null)
			{
				$(this).append(this.menu);
			}
		});
		$(document).unbind("keydown",fnPreventKey);
		topmenu.hide();
		topmenu.parent().find("li.hover").removeClass("hover");
		submenu.parent("ul").hide();
	}
	function fnPreventKey()
	{
		return false;
	}
	function Init()
	{
		subtitle.each(function(){
			this.menu=$(this).find("ul")[0];
			this.menu.topmenu=this;
			$(this.menu).mouseover(function(){$(this.topmenu).addClass("hover");});
			});
		$(document.body).bind("blur",fnHide);
	}
	Init();
	return topmenu;
}
$(document).ready(function(){try{document.execCommand("BackgroundImageCache", false, true);}catch(ex){}});
