/** 
 * jquery 1.3.2 base, extend by author.
 * It's used in hizhe.com
 * @author fire-j created on 2009-07-20
 * @lastModifiedDate fire-j 2010-01-21
 * @version 1.1 xm.fanzhe
 * */
;(function($){
	$.extend({
		"isEmpty" : function(v){
			if (typeof v == 'string') {
				return $.trim(v).length <= 0;
			} else {
				return true;
			}
		},
		"isNotEmpty" : function(v){
			return !$.isEmpty(v);
		},
		"json" : function(url,data,success,error){
			data = $.extend({"ctime":new Date()}, data);
			$.ajax({"cache":false,"url":url,"data":data,"dataType":"json","success":success,"type":"POST", "error":function(){
				if(!error){
					alert("友情提示：\r\n抱歉，操作不成功！\r\n可能因为网络故障或网页过期，请稍后再操作或重新登录！");
				} else {
					error();
				}
			}});
		},
		"alertActionError": function(smtIdSltor, callback){
			//just for actionError, you should write <div id="actionErrorId"></div> on page
			var html = $("#actionErrorId").html();
			if(html) {
				$(window).ready(function(){
					if(smtIdSltor){
						$(smtIdSltor).attr("disabled", "disabled");
					}
					$.prop_alertError(html, function(){
						if(callback){
							callback();
						}
						if(smtIdSltor){
							$(smtIdSltor).attr("disabled", "");
						}
					});
				});
			}
		},
		"alert": function(msg){
			var html = "";
			if(msg instanceof Array) {
				var l = msg.length;
				for(var i = 0; i < l; i ++) {
					html += ( (i + 1) + ". " + msg[i]);
					if(i < l - 1) {html+="；<br/>";} else {html+="。";}
				}
			} else {
				html = msg;
			}
			$.prop_alertError(html);
		},
		"followShow" : function(follow, show, callback, offset){
			var ost = $.extend({"width":3,"height":3}, offset);
			var os = $(follow).offset();
			$(show).css({"position":"absolute","z-index":100,"left":(os.left+ost.width)+"px","top":(os.top+ost.height)+"px","display":"none"});
			$(show).fadeIn(1000, callback);
		},
		"fireShow" : function(follow, show, offset){
			var ost = $.extend({"width":3,"height":3}, offset);
			var os = $(follow).offset();
			$(show).css({"position":"absolute","z-index":100,"left":(os.left+ost.width)+"px","top":(os.top+ost.height)+"px","display":"none"});
			var delayTimeout = 1800;
			var wst = null;
			$(show).fadeIn(800).focus().mouseover(function(){
				if(wst) {
					window.clearTimeout(wst);
				}
				$(show).show();
			}).mouseout(function(){
				wst = window.setTimeout(function(){$(show).hide();}, delayTimeout);
			}).blur(function(){
				$(this).fadeOut(250);
			});
			wst = window.setTimeout(function(){$(show).hide();}, delayTimeout);
		},
		"inputValues" : function(sltor){
			var tmp = new Array();
			$.each($(sltor),function(){
				tmp.push(this.value);
			});
			return tmp.join(",");
		},
		"showAsVote" : function(maxWidth, sltorForShow, sltorForScale, maxVotes, sltorVotes){
			if(maxVotes == 0) {$(sltorForScale).html("0.00%");return;}
			var showWidth = parseInt(sltorVotes/maxVotes*maxWidth);//显示宽度
			var showScale = (sltorVotes/maxVotes*100);//比例人数
			var showTime = 18*parseInt(showScale);
			$(sltorForShow).animate({"width": showWidth+"px"}, showTime, function(){
				$(sltorForScale).html(showScale.toFixed(2) + "%");
			});
		},
		"mouseTouch" : function(sltor, activeClassName, stay){
			$.each($(sltor), function(){
				if(stay) {
					$(this).mouseover(function(){
						$(sltor + "." + activeClassName).removeClass(activeClassName);
						$(this).addClass(activeClassName);
					});
				} else {
					$(this).mouseover(function(){
						$(this).addClass(activeClassName);
					});
					$(this).mouseout(function(){
						$(this).removeClass(activeClassName);
					});
				}
			});
		},
		"mouseSelected" : function(sltor, selectedObj,sltClassName){
			$.each($(sltor), function(){
				if(this == selectedObj) {
					$(this).addClass(sltClassName);
				} else {
					$(this).removeClass(sltClassName);
				}
			});
		},
		"showWordByBreak" : function (sltor, showSize, appendHtml){
			showSize = showSize || 3;
			appendHtml = appendHtml || false;
			var rx = /^[^\x00-\xff]$/;//^[\u3447-\uFA29]$/
			$.each($(sltor), function(){
				var newStr = "";
				var ttl = $(this).attr("title");
				if(ttl == null || $.trim(ttl) == "") {
					ttl = $(this).html();
				}
				if(ttl == null || $.trim(ttl) == "") {
					return "";
				}
				if(appendHtml) {
					ttl = $(this).html() + ttl;
				}
				for(var i = 0, j = 0; i < ttl.length; i++) {
					var c = ttl.substring(i,i+1);
					newStr += c;
					if(rx.test(c)) {
						j += 1;
					} else {
						j += 0.5;
					}
					if(j >= showSize) {
						newStr += "...";
						break;
					}
				}
				$(this).html(newStr);
			});
		},
		"showTip" : function(obj,tipTxt){
			if($("#assShowId").length > 0) $("#assShowId").remove();
			var os = $(obj).offset();
			var html="<div id='assShowId' style='display: none;'>";
			html+="<div class=\"task_float\">";
			html+="<div class=\"task_float_text\">"+tipTxt+"</div>";
			html+="</div></div>";
			$(document.body).append(html);
			var w = os.left-parseInt($(".task_float").css("width").replace("px",""))/2 + 50;
			var h = os.top-parseInt($(".task_float").css("height").replace("px",""));
			$("#assShowId").css({"position": "absolute","left": w+"px", "top": h+"px"}).slideDown(700);
			$(obj).mouseout(function(){$("#assShowId").remove();});
		},
		"inputJustNum" : function(obj, maxNum){
			var v = obj.value;
			var html="";
			for(var i = 0; i < v.length; i++){
				var ttt = v.substring(i, i+1);
				if(!isNaN(ttt)){
					html+=ttt;
				}
			}
			if(html > maxNum){
				html = maxNum;
			}
			obj.value = html;
		},
		"stripForm" : function(formname){
			var f = document.forms[formname];
			var fes = f.elements;
			for(var i = 0; i < fes.length; i++) {
				var e = fes[i];
				if(e.typeName == "button" || e.typeName == "submit" || e.typeName == "reset"){
					continue;
				} else {
					e.value = e.value.stripScripts();
				}
			}
		},
		"followMsgShow" : function(msg){
			var sltorId = "#msgShowFloat";
			if($(sltorId).size() == 0){
				var html="<div id='msgShowFloat' style='color:#558EAC;position:absolute;z-index:120;'>" + msg+ "</div>";
				$(document.body).append(html);
				$(document).mousemove(function(e){
					e = e || window.event;
					var x = 0;
					var y = 0;
					if($.browser.msie){
						x = e.clientX + 5 + document.documentElement.scrollLeft - document.body.clientLeft;
						y = e.clientY - 5 + document.documentElement.scrollTop - document.body.clientTop;
					} else {
						x = e.pageX + 5;
						y = e.pageY - 5;
					}
					$(sltorId).css({"left":x+"px","top":y+"px"});
					$(sltorId).fadeOut(1500,function(){$(this).html("");});
				});
			} else {
				$(sltorId).html(msg);
				$(sltorId).fadeIn(100);
				$(sltorId).fadeOut(1500,function(){$(this).html("");});
			}
		},
		"isIE6":function(){
			return $.browser.msie && ($.browser.version == "6.0") && !$.support.style;
		},
		"rzeAllImg":function(){
			$.each($("img"), function(){$(this).rzeImg();});
		},
		"delaySrc" : function(drc){
			drc = drc || "drc";
			$.each($("img"), function(){
				this.src = $(this).attr(drc);
				this.removeAttribute(drc);
				this.onload = function(){
					$(this).rzeImg();
				}
			});
		},
		"nopx":function(from){
			if(from){
				if(typeof from == "string"){
					return parseInt(from.replace("px",""));
				} else {
					return from;
				}
			} else {
				return 0;
			}
		},
		"forEachClick":function(active_element, active_class, func){
			$(active_element).each(function(){
				$(this).click(function(){
					if(active_class) {
						$(active_element + "." + active_class).removeClass(active_class);
						$(this).addClass(active_class);
					}
					if(func) {
						func(this);
					}
				});
			});
		},
		"cover" : function(showCtt){
			var s = $(".s_cover_plat").size();
			var scpId = "scpId_" + s;
			var plat = "<div id=\""+ scpId +"\" class=\"s_cover_plat\"></div><div class=\"s_cover_plat_ctt\">"+showCtt+"</div>";
			$(plat).appendTo(document.body);
			var $scpId = $("#" + scpId);
			var $ctt = $scpId.css({"z-index":100+s*2}).next().css({"z-index":101+2*s}).children().hide();
			var h = window.screen.availHeight;
			if(document.body.scrollHeight > h) {h = document.body.scrollHeight;}
			$ctt.cxy().show();//.fadeIn(480)
			$scpId.css({"width":document.body.scrollWidth, "height": h}).show();//.animate({"opacity":0.6}, 360)
			return $scpId;
		},
		"bindCloseCover" : function(bindSltor, coverSltor, callback){
			$(bindSltor).click(function(){
				$.closeCover(coverSltor, callback);
			});
		},
		"closeCover" : function(coverSltor, callback){
			coverSltor = coverSltor || ".s_cover_plat";
			$(coverSltor).next().remove();//slideUp(150,function(){$(this).remove();});
			$(coverSltor).remove();//.fadeOut(180,function(){$(this).remove();});
			if(callback) callback();
		},
		"createHidInput" : function(hidName, hidValue){
			var ih = null;
			if($.browser.msie) {
				ih = document.createElement("<input type='hidden' name='"+hidName+"' value='"+hidValue+"'>");
			} else {
				ih = document.createElement("input");
				ih.setAttribute("type", "hidden");
				ih.setAttribute("value", hidValue);
				ih.setAttribute("name", hidName);
			}
			return ih;
		},
		"createForm" : function(action, formName){
			var f = null;
			if($.browser.msie) {
				f = document.createElement("<form name='"+formName+"' action='"+action+"' method='post'>");
			} else {
				f = document.createElement("form");
				f.setAttribute("action", action);
				f.setAttribute("method", "post");
				f.setAttribute("name", formName);
			}
			return f;
		},
		"linkGo" : function(action, hidName, hidValue){
			var f = $.createForm(action, "fireLinkGoForm");
			if(typeof hidName == "object") {
				for(var i = 0; i < hidName.length; i++) {
					f.appendChild(hidName[i]);
				}
			} else {
				var ih = $.createHidInput(hidName, hidValue);
				f.appendChild(ih);
			} 
			document.body.appendChild(f);
			f.submit();
		},
		"marquee":function(obj, inNum, sub_element, interClass, total, liHeight, speed){
			var $t = $(obj);
			$t.animate({"top":"-=1"}, speed, function(){
				var times = $.nopx($t.css("top"));
				if(times/liHeight < -inNum) {
					var $sub = $(this).find(sub_element);
					var $batch = $(this).find(sub_element + ":lt("+inNum+")");
					if(interClass && inNum%2 == 0) {
						$batch.each(function(){
							if($(this).hasClass(interClass)){
								$(this).removeClass(interClass);
							} else {
								$(this).addClass(interClass);
							}
						});
					} 
					$batch.insertAfter($sub[total - 1]);
					$t.css({"top":"0px"});
				}
				$.marquee(obj, inNum, sub_element, interClass, total, liHeight, speed);
			});
		}
	});
	$.fn.extend({
		"stripScripts" : function(){
			var text = this.val().replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, "");
			return this.val(text);
		},
		"maxLength":function(maxlth){
			var tn = this[0].tagName;
			if(tn == "TEXTAREA"){
				this.keyup(function(){
					var $o = $(this).val();
					var rl = $o.length;
					if(rl > maxlth){
						var t = $o.substring(0, maxlth);
						$(this).val(t);
					}
				});
			} else if(tn == "INPUT"){
				this.attr("maxlength", maxlth);
			}
		},
		"cxy" : function(){
			var st=sl=cw=ch=0;
			if(document.documentElement){
				with(document.documentElement){
					st = scrollTop;
					sl = scrollLeft;
					cw = clientWidth;
					ch = clientHeight;
				}
			} 
			var mw = this.width(), mh = this.height();
//			console.log("st:" + st + "; sl:" + sl + "; cw:" + cw + "; ch:" + ch + "; mw:" + mw + "; mh" + mh);
			var _top = $.nopx(st) + $.nopx(ch)/2 - $.nopx(mh)/2;
			var _left = $.nopx(sl) + $.nopx(cw)/2 - $.nopx(mw)/2;
//			console.log("_top:" + _top + "; _left:" + _left);
			return this.css({"left": _left, "top": _top, "position":"absolute"});
		},
		"bgImg": function(imgurl,ie6csspad){//for div
			var is6 = $.isIE6();
			var bgimgurl = imgurl || "";
			if(!imgurl){
				$.each(this, function(){
					bgimgurl = $(this).css("background-image") + "";
					bgimgurl = bgimgurl.replace("url(","");
					bgimgurl = bgimgurl.replace(")","");
					bgimgurl = bgimgurl.replace(/\"/gi, "");
					var pre = bgimgurl.split("images")[0];
					bgimgurl = bgimgurl.replace(pre, "");
					if(is6) {
						$(this).css("background","none").css(ie6csspad);
						this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='"+bgimgurl+"')";
					} 
				});
			} else {
				$.each(this, function(){
					if(is6) {
						$(this).css("background","none").css(ie6csspad);
						this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='"+bgimgurl+"')";
					} else {
						$(this).css("background-image","url("+imgurl+")");
					}
				});
			}
		},
		"bugQueenShow":function(sub_element, interval){
			interval = interval || 2000;
			var $lis = $(this).find(sub_element);
			var liHeight = $.nopx($lis.eq(0).css("height"));
			var total = $lis.size();
			var marqueeTimes = 0;
			var refUlObj = this;
			window.setInterval(function(){
				var $wmli = $(refUlObj).find(sub_element);
				if(marqueeTimes >= total) {
					marqueeTimes = 0;
					for(var i = 0; i < total; i ++) {
						var $curIndex = $wmli.eq(i);
						$curIndex.css({"top":"0px"}).hide().fadeIn(1000);
					}
				} else {
					var $first = $wmli.eq(0);
					marqueeTimes++;
					for(var i = 0; i < total; i ++) {
						var speed = 300+i*65;
						if(marqueeTimes > (total - 17)) {
							speed = speed - marqueeTimes * 30;
						}
						$wmli.eq(i).animate({"top":"-=" + liHeight}, speed);
					}
				}
			},interval);
		},
		"flyQueenShow":function(sub_element, contentSize, interClass){
			var $lis = $(this).find(sub_element);
			var total = $lis.size();
			var liHeight = $.nopx($lis.eq(0).css("height"));
			var totalSize = $(this).height() / liHeight;
			if(totalSize > contentSize) {
				$(this).height($(this).height() + liHeight);//over blank!
				var inNum = totalSize - contentSize;
				var speed = 25;
				if($.isIE6()) {speed = 35;}
				$(this).hover(function(){$(this).stop();}, function(){$.marquee(this, inNum, sub_element, interClass, total, liHeight, speed);});
				$.marquee(this, inNum, sub_element, interClass, total, liHeight, speed);
			}
		},
		"errImg":function(errImg){
			var $pImg = this;
			$(window).ready(function(){
				if(errImg) errImg = true;else errImg = false;
				$pImg.one("error", function(){
					this.src="/images/common/nothing.png";
					var $p=$pImg.parent();
					var w = $p.css("width"), h = $p.css("height");
					$pImg.css({"width":w,"height":h,"margin":0}).rzeImg(null, null, errImg);
				});
			});
		},
		"rzeImg":function(maxWidth, maxHeight, errImg){//for img
			var $imgObj = $(this), test = new Image();
			$(window).ready(function(){
				test.src = $imgObj.attr("src");
				var mw = test.width, mh = test.height;
				if(!maxWidth) {maxWidth=$imgObj.attr("width");if(!maxWidth) {maxWidth = $imgObj.css("width");}}
				if(!maxHeight){maxHeight=$imgObj.attr("height");if(!maxHeight) {maxHeight = $imgObj.css("height");}}
				if (mw <= maxWidth && mh <= maxHeight) {
				} else if (mw <= maxWidth && mh > maxHeight) {
					mw = mw * maxHeight / mh;
					mh = maxHeight;
				} else if (mw > maxWidth && mh <= maxHeight) {
					mh = mh * maxWidth / mw;
					mw = maxWidth;
				} else {
					if (mw / mh > maxWidth / maxHeight) {
						mh = maxWidth * mh / mw;
						mw = maxWidth;
					} else {
						mw = maxHeight * mw / mh;
						mh = maxHeight;
					}
				}
				if(!mw || !mh) {
					mw = maxWidth;
					mh = maxHeight;
				}
				if(mw > 0 && mh > 0) {
					$imgObj.css({"cursor":"pointer","width" :mw + "px","height" :mh + "px","max-width" :maxWidth + "px","max-height" :maxHeight + "px"});
					var dh = $imgObj.parent().height(), ih = $imgObj.height(), pbw=$imgObj.parent().attr("border-width") || 0;
					if(pbw)pbw.replace("px","");
					var off = (dh - ih - pbw) / 2;
					if (off > 0) {$imgObj.css( {"margin-top" :off + "px"});}
					$imgObj.parent().css({"text-align":"center"});
				} else {
					//alert("maxWidth:" + maxWidth + ";maxHeight:" + maxHeight + ";mw:" + mw + ";mh:" + mh);
				}
				if(!errImg) {
					$imgObj.errImg(true);
				} 
				return $imgObj;
			});
		}
	});
})(jQuery);