

var Utils = new Object();

function getElementsByClassName(node, className) {
	var children = node.getElementsByTagName('*');
	var elements = new Array();
	for (var i=0; i<children.length; i++) {
		var child = children[i];
		var classNames = child.className.split(' ');
		for (var j = 0; j < classNames.length; j++) {
			if (classNames[j] == className) {
				elements.push(child);
				break;
			}
		}
	}
	return elements;
}

function getNodeID(parent, id) {
	var ln = parent.childNodes.length;
	for (var z=0; z<ln; z++) {
		if (parent.childNodes[z].id == id) return parent.childNodes[z];
	}
	return null;
}


Utils.getChildrenByTagName = function(node, tagName) {

	var ln = node.childNodes.length;
	var arr = [];	
	for (var z=0; z<ln; z++) {
		if (node.childNodes[z].nodeName==tagName) arr.push(node.childNodes[z]);
	}
	return arr;
}


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.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);

if (Browser.isMozilla) {
	HTMLElement.prototype.removeNode = function() {
		this.parentNode.removeChild(this);
	}
}

Utils.htmlEncode = function(text) {
	return text.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

/*
<!--[if IE]>
	<a href onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.bfor.cn');">Make Netvibes your homepage</a>
<![endif]-->
*/



String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g,"");
}






String.prototype.startsWith=function(e){return this.substr(0,e.length)==e;}
String.prototype.format=function()
{
var s=this;
for(var i=0;i<arguments.length;i++){s=s.replace("{"+i+"}",arguments[i]);}return s;
}
/*--------------------------------FeedObject--------------------------------------*/
var Feed = function(feed) {
    this.root =feed.documentElement; //feed.responseXML.documentElement;
    this.type = (this.root.nodeName=="feed") ? 1 : 0;
    // htmlUrl
    var root = (this.type==1) ? this.root : this.root.getElementsByTagName("channel")[0];
    var link =Utils.getChildrenByTagName(root, "link");
   
    if (this.type==1) {
        for (var z=0; z<link.length; z++) {
            if (link[z].getAttribute("type")=="text/html" || link.length==1) {
                this.htmlUrl = link[z].getAttribute("href");
            }
        }
    } else {
        this.htmlUrl = (link[0].firstChild) ? link[0].firstChild.nodeValue : "";
    }

    // title
    var title = this.root.getElementsByTagName("title")[0];
    this.title = (title.firstChild) ? title.firstChild.nodeValue : this.htmlUrl;

    // description
    var desc = (this.type==1) ? this.root.getElementsByTagName("tagline") : this.root.getElementsByTagName("description");
    this.description = (desc.length>0) ? ((desc[0].firstChild) ? desc[0].firstChild.nodeValue : '') : '';

    this.items = [];
    var items = (this.type==1) ? this.root.getElementsByTagName("entry") : this.root.getElementsByTagName("item");
    var ln = items.length;

    for (var z=0; z<ln; z++) {
        var obj = {};
        obj.node = items[z];
        obj.enclosures = items[z].getElementsByTagName("enclosure");

        var title = items[z].getElementsByTagName("title")[0];
        if (title && title.firstChild) {
            obj.title = title.firstChild.nodeValue;
        } else {
            var d = items[z].getElementsByTagName("description");
            if (d[0]) {
                //obj.title = items[z].getElementsByTagName("description")[0].firstChild.nodeValue.substring(0,40)+"...";
                var tmp = document.createElement("div");
                tmp.innerHTML = d[0].firstChild.nodeValue;
                obj.title = tmp.innerText.substring(0,50)+"...";
            } else {
                obj.title = '[...]';
            }
        }

        var dcDate = (Browser.isIE) ? items[z].getElementsByTagName("dc:date")[0] : items[z].getElementsByTagName("date")[0];
        if (items[z].getElementsByTagName("pubDate")[0]) {
            obj.date = (items[z].getElementsByTagName("pubDate")[0].firstChild) ? items[z].getElementsByTagName("pubDate")[0].firstChild.nodeValue : "";
        } else if (dcDate) {
            obj.date = dcDate.firstChild.nodeValue;
        } else if (items[z].getElementsByTagName("issued")[0]) {
            obj.date = items[z].getElementsByTagName("issued")[0].firstChild.nodeValue;
        }


        var link = Utils.getChildrenByTagName(items[z], "link");
        
        if (link.length>0) {
            if (this.type==1) {
                for (var n=0; n<link.length; n++) {
                    if (link[n].getAttribute("type")=="text/html" || link.length==1) {
                        obj.link = link[n].getAttribute("href");
                    }
                }
            } else {
                if (link[0] || link[0].firstChild) {
                    obj.link = (link[0].firstChild) ? link[0].firstChild.nodeValue : '';  
                } else if (items[z].getElementsByTagName("guid")[0].firstChild) {
                    obj.link = items[z].getElementsByTagName("guid")[0].firstChild.nodeValue;
                 
                }
            }
        } else {
            obj.link = this.htmlUrl;
        }

        //obj.hash = hex_md5(obj.date+obj.link+obj.title); // too slow
        if (obj.link!="" && obj.link!=this.htmlUrl) {
            var str = obj.link;
        } else {
            var str = obj.title+obj.date+obj.link;
        }
        obj.id = str.replace(/([^0-9a-zA-Z])/g,"");
        obj.id_old = (obj.title+obj.date+obj.link).replace(/([^0-9a-zA-Z])/g,"");

        if (this.type==1) {
            obj.content = items[z].getElementsByTagName("content")[0];
            obj.description = items[z].getElementsByTagName("summary")[0];
        } else {
            obj.content = (Browser.isIE) ? items[z].getElementsByTagName("content:encoded")[0] : items[z].getElementsByTagName("encoded")[0];
            obj.description = items[z].getElementsByTagName("description")[0];
        }
        this.items.push(obj);
    }
}





function GetXmlHttpObject()
{

 var xmlHttp;
 if (!window.XMLHttpRequest) {
 try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0"); return xmlHttp; } catch (ex) {}
 try { xmlHttp = new ActiveXObject("MSXML2.XMLHTTP"); return xmlHttp; } catch (ex){}
  try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); return xmlHttp; } catch (ex) {}

 return null;
 }
 else
 {
 try {xmlHttp = new XMLHttpRequest(); return xmlHttp;} catch(ex) {}
 return null;
 }
}
function XMLHttp(formFields, strUrl, httpGET)
{
 var strText = "";

 var getorpost=(typeof(httpGET)!="undefined" && httpGET==true)? "GET" : "POST";
 xmlhttp = GetXmlHttpObject();
 try {
 if (xmlhttp != null)
 {
 xmlhttp.open(getorpost, strUrl, false);
 xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xmlhttp.send(formFields);
 strText = xmlhttp.responseText;
 }
 }catch(ex){}

 return strText;
}
function GetFeed(str)
{
//alert(str);
if(str==null&&str.trim()=='')
return null;
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(str);
return new Feed(xmlDoc);
}
/*------------------------Request ----------------------------------------------*/
var Request = new Object();

Request.send = function(url, method, callback, data, urlencoded) {
    var req;
   /* if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    */
    req=GetXmlHttpObject();
    req.onreadystatechange = function() {
              
        if (req.readyState == 4) {// only if req shows "loaded"
            if (req.status < 400) {// only if "OK"
                (method=="POST") ? callback(req.responseXML) : callback(req.responseXML,data);
            } else {
                //alert(url+"There was a problem loading data :\n" + req.status+ "/" + req.statusText);
            }
        }
    }
    if (method=="POST") {
        req.open("POST", url, true);
        if (urlencoded) req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send(data);
    } else {
        req.open("GET", url, true);
        req.send(null);
    }

    return req;
}

Request.sendRawPOST = function(url, data, callback) {
    Request.send(url, "POST", callback, data, false);
}
Request.sendPOST = function(url, data, callback) {
    Request.send(url, "POST", callback, data, true);
}
Request.sendGET = function(url, callback, args) {
    return Request.send(url, "GET", callback, args);
}
function vvv(w)//替换特殊符号
{
w=w.replace(/&/g,"&amp;");
w=w.replace(/</g,"&lt;");
w=w.replace(/>/g,"&gt;");
w=w.replace(/\"/g,"&quot;");
w=w.replace(/\s/g,"&nbsp;");
return w;
}
var allowedProtocals=["http://","https://","mms://","ftp://","rtsp://","mailto:","news:","feed:","msnim:"];
function formatURL(u)//格式化url
{
var normalizedURL=u.toLowerCase();
var safeProtocalURL="";
for(var i=0;i<allowedProtocals.length;i++)
{
 if(normalizedURL.startsWith(allowedProtocals[i]))
  {
  safeProtocalURL=u;
  break;
  }
}
return  vvv(safeProtocalURL);
}
var feedData=null;

  
  function getRSSFeed(response)
 {
        if (response.responseText == '' || response.responseXML.documentElement==null || response.responseXML.documentElement.nodeName=="parsererror") 
        { //alert('出错的处理...');
        return null;
        }
        else
        {
        var feed = new Feed(response);
        return feed;
        }
  }
function createRssErrTable()
{
var rssViewTable=document.createElement("table");
    rssViewTable.className="fixedTable rssViewTable";
var rssViewTitleRow=rssViewTable.insertRow(rssViewTable.rows.length);
var rssViewTitleCell=rssViewTitleRow.insertCell(0);
    rssViewTitleCell.className="ellipse rssViewTitleLinkContainer";
var rssViewTitleLink=document.createElement("a");


 rssViewTitleLink.innerText='暂时无法从该Rss源加载信息';
 rssViewTitleCell.appendChild(rssViewTitleLink);
return rssViewTable;

}
function createRssViewTable(feedData)
{
var rssViewTable=document.createElement("table");
    rssViewTable.className="fixedTable rssViewTable";
var rssViewTitleRow=rssViewTable.insertRow(rssViewTable.rows.length);
var rssViewTitleCell=rssViewTitleRow.insertCell(0);
    rssViewTitleCell.className="ellipse rssViewTitleLinkContainer";
var rssViewTitleLink=document.createElement("a");
 if(!feedData||feedData.htmlUrl=='system_err')
 {

 rssViewTitleLink.innerText='暂时无法从该Rss源加载信息';
 rssViewTitleCell.appendChild(rssViewTitleLink);
return rssViewTable;
 }
rssViewTitleLink.href=feedData.htmlUrl;
rssViewTitleLink.target="_blank";
rssViewTitleLink.className="rssViewTitleLink";
rssViewTitleLink.innerText=feedData.title;//Rss标题
rssViewTitleCell.appendChild(rssViewTitleLink);
index=rssViewTable.rows.length;
var rssContentDelimeterRow=rssViewTable.insertRow(rssViewTable.rows.length);
rssContentDelimeterRow.className="rssContentDelimeter";
rssContentDelimeterRow.insertCell(0);
o(rssViewTable,feedData);
//n.replaceChild(rssViewTable,n.firstChild);
//if(n.childNodes.length>1)
//n.removeChild(n.childNodes[1]);

return rssViewTable;
}

function o(p,feedData){
var amountitems=feedData.items.length;
for(var i=0;i<amountitems;i++)
{
var item=feedData.items[i];
var itemTitleRow=p.insertRow(p.rows.length);
var itemTitleCell=itemTitleRow.insertCell(0);
var itemTitleLink=document.createElement("a");
var link=item.link;
link=rsssss(link);
itemTitleLink.href=link;
itemTitleLink.target="_blank";
itemTitleLink.innerText=item.title;
itemTitleCell.appendChild(itemTitleLink);
var authorStr;
var interval;
var msecondsPerDay;
var days;

try{interval=new Date()-new Date(item.date);msecondsPerDay=86400000;
days=Math.floor(interval/msecondsPerDay);}
catch(e)
{item.date=null;
}
if(item.date==null)authorStr="{0}{2}{1}";
else{
authorStr="{1} - {3}";
//authorStr=authorStr.replace("{3}",days);
if(days==0)
{
authorStr=authorStr.replace("{3}","今天");
}else
{
 authorStr=authorStr.replace("{3}",days+'天前');
}
}
authorStr=authorStr.replace("{1}",feedData.title);
var itemAuthorRow=p.insertRow(p.rows.length);
var itemAuthorCell=itemAuthorRow.insertCell(0);
itemAuthorCell.className="ellipse";
var itemAuthorDiv=document.createElement("div");
itemAuthorDiv.className="ellipse graytitle";
itemAuthorDiv.innerText=authorStr;
itemAuthorCell.appendChild(itemAuthorDiv);
if(i<amountitems-1)
{
var rssContentDelimeter=p.insertRow(p.rows.length);
rssContentDelimeter.className="rssContentDelimeter";
rssContentDelimeter.insertCell(0);
var line=p.insertRow(p.rows.length);
line.className="Line";
line.insertCell(0);
}
}

}
function rsssss(s)
{
var workingUrl=s.trim();
if(workingUrl.length==0)
 return workingUrl;
var hasProtocol=workingUrl.indexOf(":")>=0;
if(!hasProtocol)workingUrl="http://"+workingUrl;
return formatURL(workingUrl);
}
/*
function l(m,n)
{if(!m){
if(!n.firstChild.innerText)n.innerText=c_feedinvalid;
else n.firstChild.innerText=c_feedinvalid;}
else{
var channel=m;
var rssViewTable=document.createElement("table");
    rssViewTable.className="fixedTable rssViewTable";
var rssViewTitleRow=rssViewTable.insertRow(rssViewTable.rows.length);
var rssViewTitleCell=rssViewTitleRow.insertCell(0);rssViewTitleCell.className="ellipse rssViewTitleLinkContainer";
var rssViewTitleLink=document.createElement("a");
rssViewTitleLink.href=channel.link;
rssViewTitleLink.target="_blank";
rssViewTitleLink.className="rssViewTitleLink";
rssViewTitleLink.innerText=channel.title;
rssViewTitleCell.appendChild(rssViewTitleLink);
index=rssViewTable.rows.length;
var rssContentDelimeterRow=rssViewTable.insertRow(rssViewTable.rows.length);
rssContentDelimeterRow.className="rssContentDelimeter";
rssContentDelimeterRow.insertCell(0);
o(rssViewTable,channel);
n.replaceChild(rssViewTable,n.firstChild);
if(n.childNodes.length>1)
n.removeChild(n.childNodes[1]);
}
}

function o(p,q){
var amountitems=q.items.length;
for(var i=0;i<amountitems;i++)
{
var item=q.items[i];
var itemTitleRow=p.insertRow(p.rows.length);
var itemTitleCell=itemTitleRow.insertCell(0);
var itemTitleLink=document.createElement("a");
var link=item.link;link=r(link);
itemTitleLink.href=link;itemTitleLink.target="_blank";
itemTitleLink.innerText=item.title;itemTitleCell.appendChild(itemTitleLink);
var authorStr;
var interval;
var msecondsPerDay;
var days;
try{
interval=new Date()-new Date(item.pubDate);
msecondsPerDay=86400000;
days=Math.floor(interval/msecondsPerDay);
}catch(e)
{item.pubDate=null;
}
if(item.pubDate==null)
authorStr="{0}{2}{1}";
else{
authorStr=c_RssFooterTitleDaysago;
authorStr=authorStr.replace("{3}",days);
  }
authorStr=authorStr.format("","",q.title);
var itemAuthorRow=p.insertRow(p.rows.length);
var itemAuthorCell=itemAuthorRow.insertCell(0);
itemAuthorCell.className="ellipse";
var itemAuthorDiv=document.createElement("div");
itemAuthorDiv.className="ellipse graytitle";
itemAuthorDiv.innerText=authorStr;
itemAuthorCell.appendChild(itemAuthorDiv);
if(i<amountitems-1)
{
var rssContentDelimeter=p.insertRow(p.rows.length);
rssContentDelimeter.className="rssContentDelimeter";
rssContentDelimeter.insertCell(0);
var line=p.insertRow(p.rows.length);
line.className="Line";
line.insertCell(0);
 }
}
}
*/