function $tag(parentElement,tagstring){//getElementsByTagName
		var element=parentElement,tags=tagstring,temp=[],tmp;
		element=$(element);
		if (isNull(element)) element=document;
		if (isNull(tags)) tags="*";
		try{
			tmp=element.getElementsByTagName(tags);
		}
		catch(para){
			tmp=[];
		}
		for (var i=0; i<tmp.length; i++) {
			temp.push(tmp[i])
			}
		tmp=null;
		return(temp);
}

function DrawImage(ImgD,x,y){
	var flag=false;
   var image=new Image();
   image.src=ImgD.src;
   if(image.width>0 && image.height>0){
    flag=true;
    if(image.width/image.height>= x/y){
     if(image.width>x){  
     ImgD.width=x;
     ImgD.height=(image.height*x)/image.width;
     }else{
     ImgD.width=image.width;  
     ImgD.height=image.height;
     }
     //ImgD.alt=image.width+"x"+image.height;
     }
    else{
     if(image.height>y){  
     ImgD.height=y;
     ImgD.width=(image.width*y)/image.height;     
     }else{
     ImgD.width=image.width;  
     ImgD.height=image.height;
     }
     //ImgD.alt=image.width+"x"+image.height;
     }
    }
}

function chkImageSize(ImgD,x,y){
   var image=new Image();
   image.src=ImgD;
   if(image.width>x || image.height>y){
		alert("图片的宽不能大于"+x+"像素;高不能大于"+y+"像素.");
		return false;
	}
	return true;
}

// 是否有效的扩展名
function IsExt(url, opt){
	var sTemp;
	var b=false;
	if(url==""){
		return b;
		}
	var s=opt.toUpperCase().split("|");
	for (var i=0;i<s.length ;i++ ){
		sTemp=url.substr(url.length-s[i].length-1);
		sTemp=sTemp.toUpperCase();
		s[i]="."+s[i];
		if (s[i]==sTemp){
			b=true;
			break;
		}
	}
	return b;
}

//设置选中
function checkAll(name,value){
   $("input[name='"+name+"']").each(function(i,e){if(!$(e).disabled)$(e).attr("checked",value);})
}


//至少有一个元素被选择
function checkSub(name){
  var k=0;
	$("input[name='"+name+"']").each(function(i,e){if($(e).attr("checked"))k++;})
 return k;
}
function goUrl(url){
	window.location.href=url;
}
function replaceUrl(url){
	window.location.replace(url);
}
function openUrl(url){
	window.open(url,'');
}

function isIE(){
		if(document.all){return true}else{return false}
}

function setCookie (name, value) {
  //document.cookie = name + "=" + value + ";expires=Thursday,01-Jan-2099 00:00:00 GMT";
  document.cookie = name + "=" + value + "; path=/;expires=Thursday,01-Jan-2099 00:00:00 GMT";
}

function getCookie(name) {
  var search;
  search = name + "=";
  offset = document.cookie.indexOf(search); 
  if (offset != -1) {
    offset += search.length ;
    end = document.cookie.indexOf(";", offset) ;
    if (end == -1)
      end = document.cookie.length;
    return decodeURIComponent(escape(unescape(document.cookie.substring(offset, end))));
  }
  else
    return "";
}

function deleteCookie(name) {
  setCookie(name, "");
}
function nohtml (s){ 
        var reTag = /<(?:.|\s)*?>/g; 
        return s.replace(reTag, ""); 
}
function getQueryString(queryname) {
    var qKeys = {};
    var re = /[?&]([^=]+)(?:=([^&]*))?/g;
    var matchInfo;
    while(matchInfo = re.exec(location.search)){
	    qKeys[matchInfo[1]] = matchInfo[2];
    }
    return typeof(qKeys[queryname])=='undefined'?'':qKeys[queryname];
}
function getpageurl(url,value){
	return url.replace(/\$page/ig,value);	
}


function ShowFormatBytesStr(bytes) {
	if(bytes > 1073741824) {
		document.write((Math.round((bytes/1073741824)*100)/100).toString()+' G');
	} else if(bytes > 1048576) {
		document.write((Math.round((bytes/1048576)*100)/100).toString()+' M');
	} else if(bytes > 1024) {
		document.write((Math.round((bytes/1024)*100)/100).toString()+' K');
	} else {
		document.write(bytes.toString()+' Bytes');
	}
}


function convertdate(strdate) {
	strdate = strdate.replace(/-/ig,'/');
	var d = new Date(strdate);
	var now = new Date();
	var result;

	if (d.getYear() == now.getYear() && d.getMonth() == now.getMonth()) {
		var xday = now.getDate() - d.getDate();

		switch (xday) {
			case 0:
				result = "今天 " + d.format("hh") + ":" + d.format("mm");
				break;
			case 1:
				result = "昨天 " + d.format("hh") + ":" + d.format("mm");
				break;
			case 2:
				result = "前天 " + d.format("hh") + ":" + d.format("mm");
				break;
			default:
				result = d.format("yyyy-MM-dd hh:mm");
				break;		
		}
	} else {
		result = d.format("yyyy-MM-dd hh:mm");
	}
	
	return result;
}

function convertdate2(strdate)
{
	strdate = strdate.replace(/-/ig,'/');
	var d = new Date(strdate);
	var now = new Date();
	var result = now - d;
	if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDate() - d.getDate() > 0){
		result = convertdate(strdate);
	} else if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDate() == d.getDate() && now.getHours() - d.getHours() > 0){
		result = convertdate(strdate);
	} else if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDate() == d.getDate() && now.getHours() == d.getHours() && now.getMinutes() - d.getMinutes() > 0){
		result = (now.getMinutes() - d.getMinutes()) + " 分钟前"
	} else if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDate() == d.getDate() && now.getHours() == d.getHours() && now.getMinutes() == d.getMinutes() && now.getSeconds() - d.getSeconds()> 0){
		result = (now.getSeconds() - d.getSeconds()) + " 秒前"
	} else {
		result = d.format("yyyy-MM-dd hh:mm");
	}
	return result;

}

Date.prototype.format = function(format) {
	var o = {
	"M+" : this.getMonth()+1, //month
	"d+" : this.getDate(),    //day
	"h+" : this.getHours(),   //hour
	"m+" : this.getMinutes(), //minute
	"s+" : this.getSeconds(), //second
	"q+" : Math.floor((this.getMonth()+3)/3),  //quarter
	"S" : this.getMilliseconds() //millisecond
	};
	if(/(y+)/.test(format)) {
		format = format.replace(RegExp.$1,
			(this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for(var k in o) {
		if(new RegExp("("+ k +")").test(format))
			format = format.replace(RegExp.$1,
				RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
	}
	return format;
}


var __format = function(n, m) {
	if (!n) return 0;
	var _m;
	var s;
	var _n;
	_m = m === undefined ? 0 : m;
	_n = n * Math.pow(10, _m);
	_n = Math.round(_n);
	_n = _n / Math.pow(10, _m);
	return _n.toString();
};
var __formatSize = function(n) {
	var s;
	var m;
	var a = [];
	var i;
	for (i = 0; n > 0; i++) {
		m = Math.floor(n / 1024);
		a[i] = n - m * 1024;
		n = m;
	}
	switch (a.length) {
		case 0:
			return '0KB';
		case 1:
			return __format(a[0]) + 'B';
		case 2:
			return __format(a[1] + a[0] / 1024) + 'KB';
		case 3:
			return __format(a[2] + a[1] / 1024) + 'MB';
		case 4:
			return __format(a[3] + a[2] / 1024) + 'GB';
		case 5:
			return __format(a[4] + a[3] / 1024) + 'TB';
		default:
			;
	}
};
var __formatTime = function(n, isEn) {
	var s;
	isEn = isEn ? true : false;
	if (n < 1000) s = __format(n) + (isEn ? 'ms' : '毫秒');
	else if (n < 60 * 1000) s = __format(n / 1000) + (isEn ? 's' : '秒');
	else if (n < 60 * 60 * 1000) s = __format(n / (60 * 1000)) + (isEn ? 'm' : '分钟');
	else if (n < 24 * 60 * 60 * 1000) s = __format(n / (60 * 60 * 1000)) + (isEn ? 'h' : '小时');
	else s = __format(n / (24 * 60 * 60 * 1000)) + (isEn ? 'd' : '天');
	return s;
};
var __formatClock = function(ms) {
	var n = parseInt(ms / 1000);
	var s;
	var hour = parseInt(n / 3600);
	var minute = parseInt((n - hour * 3600) / 60);
	var second = parseInt(n % 60);
	return (hour < 10 ? '0' + hour : hour) + ':' + (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second);
};
var __sprintf = function($s, $a) {
	for (var $i in $a) {
		$s = $s.replace('%' + $i, $a[$i]);
	}
	return $s;
};
function GetScrollXY(){
    var x=0,y=0
    if(document.documentElement.scrollTop){
        x=document.documentElement.scrollLeft;
        y=document.documentElement.scrollTop;
    }
    else{
        x=document.body.scrollLeft;
        y=document.body.scrollTop;
    }
    return {x:x,y:y}
}

function getEventXY(e){
    var posx=0,posy=0;
    if(e==null) e=window.event;
    if(e.pageX || e.pageY)
    {
        posx=e.pageX; posy=e.pageY;
    }
    else if(e.clientX || e.clientY)
    {
       posx=e.clientX+GetScrollXY().x;
       posy=e.clientY+GetScrollXY().y;
    }
    return {"x":posx, "y":posy};
}

function realOffset(o)
{
  if(!o) return null; var e=o, x=y=l=t=0;
  do{l+=e.offsetLeft||0; t+=e.offsetTop||0; e=e.offsetParent;}while(e);
  do{x+=o.scrollLeft||0; y+=o.scrollTop||0; o=o.parentNode;}while(o);
  var xy=GetScrollXY();
  return {"x":l-x+xy.x, "y":t-y+xy.y};
};
 
function BaseAlert(str){
		alert(str);
}


function initCountDownRedirector(){
	var x	= document.getElementById("TimeCountNum");
	if(!x) return;
	var t	= x.innerHTML;
	window.setTimeout("countDownRedirector()",1000);
}
function countDownRedirector(){
	var x	= document.getElementById("TimeCountNum");
	if(!x) return;
	var t	= x.innerHTML;
	if(t > 0 ){
		t--;
		x.innerHTML = t;
		window.setTimeout("countDownRedirector()",1000);
	}else{
		window.location	= x.title;
		//window.location.replace(x.title)
	}
}
//发表文章推荐图片
function filterArray(arr,str)
{
	if(!isArray(arr)){return false;}
		for(var i=0;i<arr.length;i++)
		{	
			try{if(arr[i].toString()==str.toString()){return true}}catch(e){}
		}
		return false;
	}
function chooseImg(v)
{
	$('#out_img_list li').each(function(){
											if($(this).children("img").attr("src")==v)
												$(this).addClass("choose")
											else
												$(this).removeClass("choose");
											});
}
function getImgList(str)
{
	var re =/<img.+?>/ig;
	var arr=str.match(re);
	var i=0;
	var picArray=new Array();
	if(isArray(arr))
	for(i=0;i<arr.length;i++)
	{
		var re1=/http:\/\/.+?\.(jpg|gif|png)/ig;
		if(arr[i].match(re1))
		{
			if(!filterArray(picArray,arr[i].match(re1)))
			picArray[i]=arr[i].match(re1);
			}
		else{
			var re2=/\/.+?\.(jpg|gif|png)/ig;
			if(!filterArray(picArray,arr[i].match(re2)))
			picArray[i]=arr[i].match(re2);
		}
	}
	return picArray;

}
function showImgList(c)
	{
		var content;
		var out=$('#out_img_list');
		if(!$('#'+c)||!out){alert("找不到图片对象和列表！");return false;}
		content=getEditorHTMLContents(c);
		var list=getImgList(content);
	if(list.length<1){alert("内容中暂无图片");return false;}
		
		out.hide()
			
		var out_html="";
		for(i=0;i<list.length;i++)
		{
			if(list[i]!="" && list[i]!='null' && list[i]!=null)
			out_html=out_html+"<li onclick=\"selectImg('"+list[i]+"')\"><img src=\""+list[i]+"\" border=0  alt=\"选择当前图片请点击\" onload=\"DrawImage(this,100,100)\" ></li>";
		}
		out.html(out_html);
		out.show();
	}
function updateImgList(img)
{
		var out=$('#out_img_list');
		out.innerHTML=out.innerHTML+"<li onclick=\"selectImg('"+img+"')\"><img src="+img+" border=0  alt=\"选择当前图片请点击\" onload=\"DrawImage(this,100,100)\" ></li>";

	}
function selectImg(v)
	{
    	$("#images").val(v);
		chooseImg(v);
	}
function openWin(url)
{
	if(url!="")
	window.open(url, '', 'height=100%, width=100%, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
	}
function getEditorHTMLContents(EditorName) { 
 var oEditor = FCKeditorAPI.GetInstance(EditorName);
 return(oEditor.GetXHTML(true));
}

function Rows(t,o){
 if(t=="+")
	$("#"+o).attr("rows",$("#"+o).attr("rows")+2)
 else
 	$("#"+o).attr("rows",$("#"+o).attr("rows")-2>3?$("#"+o).attr("rows")-2:3)
}
