$.zIndex = 998;
$.hasMask = false;
$.fn.extend({
  center: function(top, left, zIndex){
		      left = left ? left + $(window).scrollLeft() : $.center().left - this.outerWidth()/2;
			  top = top ? top + $(window).scrollTop() : $.center().top - this.outerHeight()/2;
			  zIndex = zIndex ? zIndex : (++$.zIndex);
		      $(this).css({position: "absolute", zIndex: zIndex, left: left, top: top});
			  return this;
  },
  vibrate: function(n, swing, speed, start, end){
	var a=['top','left'], b=0;
	n = n || 17, swing = swing || 2, speed = speed || 22;
	var offset = this.offset(), obj = this[0];
	var u=setInterval(function(){
		if(!b) start && start.call(obj);
		obj.style[a[b%2]]=((b)%4<2?offset[a[b++%2]]-swing:offset[a[b++%2]]+swing)+"px";
		if(b>n){clearInterval(u); obj.style['left']=offset.left+"px";obj.style['top']=offset.top+"px";end&&end.call(obj);}
	}, speed);
  },  
 drag: function(dragObj, limit, sFunc, mFunc, eFunc){
		return this.each(function(){
			var _this = this;
			if(dragObj === true)
				_this = this.parentNode;
			if(dragObj && dragObj.nodeType && dragObj.nodeType == 1)
				_this = dragObj;
			if($.browser.msie) {
				this.onselectstart = function(){return false};
				//if(_this.currentStyle.backgroundColor == "transparent") _this.style.background = "#fff";
			}
			if($.browser.mozilla) this.style.MozUserSelect = 'none';
			this.style.cursor = "move";
			$(this).mousedown(function(e){
				sFunc && sFunc.call(_this);
				var offset = $(_this).offset();
				var screenX = e.screenX, screenY = e.screenY, w = _this.offsetWidth, h = _this.offsetHeight;
				$(document).mousemove(function(e2){
					if($.browser.msie && e.which != 1 || e2.target.tagName == "INPUT"){
						if($.browser.mozilla) _this.style.MozUserSelect = '';
						if($.browser.msie) _this.onselectstart = function(){return true};
						$(document).unbind("mousemove mouseup mousedown"); 
						eFunc && eFunc.call(_this);
						return;
				    } 
					var curLeft = offset.left + e2.screenX - screenX, curTop = offset.top + e2.screenY - screenY;
					if(!limit) limit = {minX: -20000, maxX: 20000, minY: -20000, maxY : 20000};
					curLeft = curLeft < limit.minX ? limit.minX : ((curLeft + w) > limit.maxX ? (limit.maxX - w) : curLeft);
					curTop = curTop < limit.minY ? limit.minY : ((curTop + h) > limit.maxY ? (limit.maxY - h) : curTop);
					$(_this).css({position: "absolute", left: curLeft, top: curTop});
					if($.browser.msie && _this.tagName == "IMG") e2.preventDefault();
					mFunc && mFunc.call(_this);
				});
				$(document).mouseup(function(){
					$(document).unbind("mousemove mouseup mousedown");
					eFunc && eFunc.call(_this);
				});
				if(this.tagName == "IMG") e.preventDefault();
			});
		});
	 },
	 json2select: function(json,dft,name,deep) {
			//参数初始化
			var _this=this,				//保存呼叫的对象
				name=name||"sel",		//如果未提供名字，则为默认为sel
				deep=deep||0,			//深度，默认为0，即生成的select的name=sel0
				dft=dft||[];	//默认值
			//换内容的时候删除旧的select
			$("select[name="+name+deep+"]",_this).nextAll().remove();
			if (json[0]) {
				//新建一个select
				var slct=$("<select name='"+name+$("select",_this).length+"'></select>");
				//建立一个默认项，value为空，修改请保留为空
				$("<option value=''>请选择</option>").appendTo(slct);
				$.each(json,function(i,sd) {
					//添加项目，并用data将其子元素附加在这个option上以备后用。
					$("<option value='"+(sd.v||sd.t).trim()+"'>"+sd.t.trim()+"</option>").appendTo(slct).data("s",sd.s||[]);
				});
				//绑定这个select的change事件
				slct.change(function(e,dftFlag) {
					//如果选的不是value为空的，则调用方法本身。如果已经初始化过了,即，不是由trigger触发的，而是手工点的，则不将dftFlag传递进去。
					$(this).val()&&_this.json2select($(":selected",this).data("s"),dftFlag?dft.slice(1):[],name,$(this).attr("name").match(/\d+/)[0]);
					//设置初始值，并且触发change事件，传递true参数进去。
				}).appendTo(_this).val(dft[0]||'').trigger("change",[true]);
			}
			//返回jQuery对象
			return _this;
	} 
});
$.fn.dragHt = function(limit, mFunc, eFunc){
	$(this).mousedown(function(e){
	    var _this = this;
		var offset = $(_this).position();
		var screenX = e.screenX, w = _this.offsetWidth;
		if($.browser.mozilla) this.style.MozUserSelect = 'none';
		$(document).mousemove(function(e2){
			if($.browser.msie && e.which != 1 || e2.target.tagName == "INPUT"){
				$(document).unbind("mousemove mouseup"); 
				eFunc && eFunc.call(_this);
				return;
		    } 
			_this.style.cursor = "pointer";
			var curLeft = offset.left + e2.screenX - screenX;
			if(!limit) limit = {minX: -20000, maxX: 20000};
			curLeft = curLeft < limit.minX ? limit.minX : ((curLeft + w) > limit.maxX ? (limit.maxX - w) : curLeft);
			$(_this).css({position: "absolute", left: curLeft});
			mFunc && mFunc.call(_this);
		});
		$(document).mouseup(function(){
			$(document).unbind("mousemove mouseup");
			eFunc && eFunc.call(_this);
		});
		e.preventDefault();
     });
}
$.extend({
  center:  function(){ return {left: $(window).width()/2 + $(window).scrollLeft(), top: $(window).height()/2 + $(window).scrollTop()}},
  mask: function(color, opacity){
	$.hasMask = true;
	color = color ? color : "#000";
	opacity = opacity ? opacity : "0.2";
    width = $(document).width();
	height = $(document).height();
	$("<div/>").css({position: "absolute", left: 0, top: 0, zIndex: ++$.zIndex, filter: "alpha(opacity=" + opacity + ")", opacity: opacity, background: color, width: width, height:  height}).appendTo(document.body).attr("id", "mask").css("opacity", opacity).html('<iframe style="position:absolute; z-index: -1; width:100%; height:100%;filter:alpha(opacity=0);opacity:0" frameborder="0"></iframe>');
	return this;
  },
  removeMask: function(){
	 $.hasMask = false;	  
	 $("#mask").remove();
	 return this;
  },
  queryString: function(name, target){
		if(!/^\w+$/.test(name)) return null;
		var url = target||location.href;
		var re = new RegExp("(?:\\?|\\&)" + name + "=([^&]*)","i");
		if(!re.test(url)) return null;
		return decodeURIComponent(re.exec(url)[1]);
   },// var str ="<b>@{name}</b>";	 $("#test").template(str, {name: 'test'});
  template: function(str, data){
		for(var _prop in data){
            str = str.replace(new RegExp("@{" + _prop + "}", "g"), data[_prop]);
		}
		return str.replace(/@{\w+?}/g, "");
  },//("a:link{color: red;}" + "a:visited{color: red;}", document);
  addStyleSheet: function(cssText, doc){
  	doc = doc || document;
  	var oStyle = doc.createElement("style");
  	oStyle.type = "text/css"; 
  	if(oStyle.styleSheet)
  	{
  		oStyle.styleSheet.cssText = cssText; 
  	}
  	else
  	{
  		oStyle.appendChild(doc.createTextNode(cssText));
  	} 
  	doc.getElementsByTagName("head")[0].appendChild(oStyle);
  },  
  cookie: {
		setCookie: function(name, value, hours)
		{ 
			var expire = ""; 
			var path = arguments[3]?arguments[3]:"/";
			if(hours != undefined) { 
				expire = new Date((new Date()).getTime() + hours * 3600000); 
				expire = "; expires=" + expire.toGMTString(); 
			} 
			document.cookie = name + "=" + value + expire + "; path=" + path; 
		},
		getCookie: function(name)
		{
			var re=new RegExp("(?:^|\s*)"+name+"=([^;]*)");
			var tmp=re.exec(document.cookie);
			if(tmp)
			return tmp[1];
			return null;
	    },
	    remove: function(name){ 
	    	document.cookie = name + "=null; path=/; expires=" + new Date(0).toGMTString();
	    }
	},
	ie6: /msie\s*6/i.test(navigator.userAgent),
	showToggle: function (){
		  $.each(arguments, function(){
		    $("#"+this.toString()).toggle();
		  });
		  return false;
	},	
	isErrorReply:function(reply){
		reply = reply || {}
		return reply.error ? true : false;
	},
	postJSONEx:function(url, data, onSuccess,onError){
		$.post(url, data, function(reply){ if($.isErrorReply(reply)){onError(reply,reply.error)}else{onSuccess(reply)}}, "json");
	},
	getJSONEx:function(url, data, onSuccess,onError){
		$.get(url, data, function(reply){ if($.isErrorReply(reply)){onError(reply,reply.error)}else{onSuccess(reply)}}, "json");
	},
	deleteJSONEx:function(url, data, onSuccess,onError){
		var sUrl = url + (/\?/.test(url) ? "&" : "?") + (typeof data === "object" ? jQuery.param(data) : data);
		jQuery.ajax({
    		type: 'DELETE',
    		url: sUrl,
    		dataType: "json",
    		success: function(reply){
				if($.isErrorReply(reply)){onError(reply,reply.error)}else{onSuccess(reply)}
    		}
	    });		
	},
	putJSONEx:function(url, data, onSuccess,onError){
		var sUrl = url + (/\?/.test(url) ? "&" : "?") + (typeof data === "object" ? jQuery.param(data) : data);
		jQuery.ajax({
			type: 'PUT',
			url: sUrl,
			dataType: "json",
			success: function(reply){
			if($.isErrorReply(reply)){onError(reply,reply.error)}else{onSuccess(reply)}
		}
		});		
	}
	
	
});
