﻿var Browser = new Object();
Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isIE7 = Browser.isIE && window.XMLHttpRequest;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari")!=-1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);

//优化setTimeout
var _st = window.setTimeout;
if(!window.setTimeout.constructor)
window.setTimeout = function(fRef, mDelay) {
    if(typeof fRef == 'function'){
        var argu = Array.prototype.slice.call(arguments,2);
        var f = (function(){ fRef.apply(null, argu); });
        return _st(f, mDelay);
    }
    return _st(fRef,mDelay);
}

//优化setInterval
var _int = window.setInterval;
if(!window.setInterval.constructor)
window.setInterval = function(fRef, mDelay) {
    if(typeof fRef == 'function'){
        var argu = Array.prototype.slice.call(arguments,2);
        var f = (function(){ fRef.apply(null, argu); });
        return _int(f, mDelay);
    }
    return _int(fRef,mDelay);
}
function $(id){
	return document.getElementById(id);
}


function _$(id){
	return document.getElementsByName(id);
}
function closeHTML(s){ //闭合HTML
		var arrTags = ["a","div","span","table","font","b","u","i","center","marquee"];
		var str = s;
		for (var i=0;i<arrTags.length;i++)
		{
			var re1 = new RegExp("<" + arrTags[i] + "( [^<>]+|)>","ig");
			var re2 = new RegExp("</" + arrTags[i] + ">","ig");
			var openTags = str.match(re1);
			var closeTag = str.match(re2);

			var openTagLength = openTags?openTags.length:0;
			var closeTagLength = closeTag?closeTag.length:0;
			var fixHTML = "";
			for (var j=0;j<openTagLength-closeTagLength;j++) fixHTML += "</" + arrTags[i] + ">";
			str += fixHTML;
		}
		return str;
};


//滚动支持
function scrollBlog(){
	if (isSmall || event.srcElement.tagName == "TEXTAREA" || event.srcElement.tagName == "INPUT") return;
	if (event.keyCode<=40 && event.keyCode>=33) {
		top.document.getElementById("mbody").focus();
	}
}

//取消数组中的重复项目除了undefined
function destinctArray(arr){
	var res=[];
	for(var i=0;i<arr.length;i++){
		if(arr[i]===void(0))
			continue;
		for(var j=i+1;j<arr.length;j++){
			if(arr[i]===arr[j])
				arr[j]=void(0);
		}
		res.push(arr[i]);
	}
	return res;
}

//获取element的位置
function getPosition(obj) {
	var top=0;
	var left=0;
	var width=obj.offsetWidth;
	var height=obj.offsetHeight;
	while (obj.offsetParent) {
			top += obj.offsetTop;
			left += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	return {"top":top,"left":left,"width":width,"height":height};
}

//获取web参数
function getParameter(name,cancelBubble){
	var r = new RegExp("(\\?|#|&)"+name+"=([^&#]*)(&|#|$)");
	var m = location.href.match(r);
	if ((!m || m=="") && !cancelBubble) m = top.location.href.match(r);
	return (!m?"":m[2]);
}
//删除DOM
function removeElement(element){
	if((typeof element)=="string")
		element=$(element);

	if(!!element && (typeof element)=="object"){
		element.parentNode.removeChild(element);
	}
}
//错误输出
function dalert(o,s,t,type){
	if(!type)
		type=3;
	if(!!o && !!o.error)
		s=o.error.msg;
	top.showMsgbox(s,type,(!!t)?t:1500);
}
//由CGI返回的long型时间转换成友好时间字符串
function long2time(n){
	var _time=new Date(n*1000);
	return ([_time.getFullYear(),"年",_time.getMonth()+1,"月",_time.getDate(),"日 ",_time.getHours(),"时",_time.getMinutes(),"分",_time.getSeconds(),"秒"]).join("");
}
//分离效果位域
function effectSplit(n){
	if(n==0)
		return [];
	var resultArr=[];
	var s=n.toString(2);
	var m=s.length;
	var re=/^1[0]*/;
	while(m>0){
		resultArr.push((1<<(m-1))+"");
		s=s.replace(re,"");
		m=s.length
	}
	return resultArr;
}





//基础函数
String.prototype.trim=function(t){
	return (!t)?(this.replace(/^\s+|\s+$/,"")):((t=="L")?(this.replace(/^\s+/,"")):((t=="R")?(this.replace(/\s+$/,"")):(this)));
};

String.prototype.convCR=function(r){
	return (!!r)?this.replace(/<br \/>/g,"\n"):this.replace(/\n/g,"<br />");
};

String.prototype.convSP=function(r){
	return (!!r)?this.replace(/&nbsp;/g," "):this.replace(/ /g,"&nbsp;");
};

String.prototype.s2n=function(){
	var t=parseInt(this.replace(/px$/i,""),10);
	return isNaN(t)?0:t;
};

String.prototype.toInnerHTML=function(){
	var s=this.replace(/&/g,"&amp;").replace(/\"/g,"&quot;").replace(/\'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
	return Browser.isIE?s.replace(/&apos;/g,"&#39;"):s;
};

String.prototype.toRealStr=function(){
	return this.replace(/&quot;/g,"\"").replace(/(?:&#39;)|(?:&apos;)/g,"\'").replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&");
};

String.prototype.getRealLength = function(){
	return this.replace(/[^\x00-\xff]/g,"aa").length;
};

String.prototype.URLencode=function(){
	return this.replace(/[\x09\x0A\x0D\x21-\x29\x2B\x2C\x2F\x3A-\x3F\x5B-\x5E\x60\x7B-\x7E]/g,function(a){return "%"+((a.charCodeAt(0)<16)?("0"+a.charCodeAt(0).toString(16)):(a.charCodeAt(0).toString(16)))}).replace(/[\x00-\x20 ]/g,"+");
};

