/* web devlopers forum - neewsfeed.js from  breitbart.com - copyright 2006 and onwards Infocious Inc. */


function tabPages(par, robj) {
    this.width=robj.width;
    this.par=robj;
    var uls=par.getElementsByTagName("ul");
    this.Mitems=null;
    for(var i = 0, l=uls.length; i<l; i++) {
	if (uls[i].className=='tabM') {
	    var ch=uls[i].childNodes;
	    this.Mitems=new Array();
	    for (var j=0; j<ch.length; j++) {
		this.Mitems[this.Mitems.length]=ch[j];
	    }
	    break;
	}
    }
    if (this.Mitems==null) { return; }
    this.menuDiv=null;

    this.pages=new Array();
    var divs = par.getElementsByTagName("div");
    for(var i = 0, l=divs.length; i<l; i++) {
	if (/RSSSpotTabC/i.test(divs[i].className)) {
	    var j=this.pages.length;
	    this.pages[j]=divs[i];
	    this.pages[j].fitted=false;
	} else if (divs[i].className=='RSSSpotTabM') {
	    this.menuDiv=divs[i];
	}
    }

    var tp=this;
    for(var i = 0, l=this.Mitems.length; i<l; i++) {
	this.Mitems[i].tp=this;
	this.Mitems[i].onmouseover=function(){
	    if (this.className != 'sel') { tp.switchP(this); return false; }
	}
    }

    this.switchP(this.Mitems[0]);
}


tabPages.prototype.switchP = function(selTab) {
    var tp=this;
    for(var i = 0, l=tp.Mitems.length; i<l; i++) {
	if (selTab==tp.Mitems[i]) {
	    for(var j = 0; j<l; j++) {
		if (selTab!=tp.Mitems[j]) {
		    tp.Mitems[j].className = '';
		    tp.pages[j].style.display='none';
		}
	    }
	    tp.Mitems[i].className='sel';
	    tp.pages[i].style.display='block';

	    if (tp.pages[i].fitted==false) {
		tp.par.fitlinks(tp.pages[i]);
	    }
	    break;
	}
    }

}
tabPages.prototype.showUI = function() {
    if (this.menuDiv != null) {
	this.menuDiv.style.display="block";
	this.switchP(this.Mitems[0]);
    }
}

var isIE=document.all;
var isNN=(!document.all&&document.getElementById);
//var isOpera=(navigator.userAgent.toLowerCase().indexOf("opera") != -1);


RSSSpot = function() {
    this.dataServer='http://search.lingospot.com';
    this.width=150;
    this.height=300;
    this.entryCount=10;
    this.logoH=20;
    this.noHeadlineChop=0;
    this.spotId=1;
}

RSSSpot.prototype.renderFeedInfo = function(feedInfo, rssObj) {
    if (rssObj.title != null) {
	t=rssObj.title;
    } else {
	return null;
    }
    var d=document.createElement('div');
    d.className="rssFeedTitle";
    d.innerHTML=t;
    return(d);
}

RSSSpot.prototype.renderEntry = function(entry) {
    try {
	var e=document.createElement('a');
	e.className="rssHeadline";
	e.target=this.linkTarget;
	e.href=entry['link'];
	e.innerHTML=entry['title'].replace(/\&apos\;/g, "'");

	var s=document.createElement('span');
	s.className="rssEntryDate";
	s.innerHTML=entry['pdate'];

	var d=document.createElement('div');
	d.appendChild(e);
	d.appendChild(document.createElement('br'));
	d.appendChild(s);

	if (entry['source'] != null && entry['source'] != '') {
	    var s=document.createElement('span');
	    s.className="rssEntrySource";
	    s.innerHTML=' ('+entry['source']+')';
	    d.appendChild(s);
	}

	return d;
    } catch(e) {
	return null;
    }
}

RSSSpot.prototype.fitlinks=function(ele) {
    if (this.noHeadlineChop) return;
    try { 
	if (ele.loaded==false || ele.style.display!='block') return;

	var parW=this.width;
	var arNodes=ele.getElementsByTagName("a");

	if (arNodes==null) return;
	var len=0;
	if (typeof(arNodes.length)=='undefined') {
	    var t=arNodes;
	    arNodes=new Array();
	    arNodes[0]=t;
	    len=1;
	} else {
	    len=arNodes.length;
	}
	for (var i=0;i<len; i++) {
	    if (arNodes[i].offsetWidth > parW) {
		arNodes[i].setAttribute("title", arNodes[i].innerHTML);
		var arW=arNodes[i].innerHTML.split(' ');
		var l=arW.length-1;
		while (arNodes[i].offsetWidth > parW) {
		    var t=arW[0];
		    for (var j=1; j<l; j++) {
			t+=' '+arW[j];
		    }
		    l=l-1;
		    arNodes[i].innerHTML=t+'...';
		}
	    }
	}
	ele.fitted=true;

    } catch(e) {
	//alert(e);
    }

}



RSSSpot.prototype.renderRSS = function(tidx, doc) {
    var tDiv=null;
    if (tidx >= this.srcArr.length) return;
    else tDiv=this.srcArr[tidx].div;
    if (tDiv==null) return;

    try {
	eval('var data='+doc);
    } catch(e) {
	//alert(e);
	alert(doc);
	return;
    }

    var fi=data['feedInfo'];
    var eArr=data['entryArr'];

    var t=document.createElement('div');
    if (!this.noHeadlineChop) {
	t.style.width="999px";
    }
    for (var i=0, l=eArr.length; i<l; i++) {
	if (this.entryCount != -1 && i>=this.entryCount) break;
	var e=this.renderEntry(eArr[i]);
	if (e != null)
	    t.appendChild(e);
    }

    var sp=document.createElement('div');
    //sp.style.width='inherit';
    sp.style.textAlign='right';
    sp.style.position='relative';
    sp.style.height=this.logoH+"px";

    var logo=document.createElement('a');
    logo.href="http://www.exactmedia.com";
    var limg=document.createElement('img');
    limg.border="0";
    limg.src="http://www.exactmedia.com/addons/em_sm.gif";
    if (this.spotId == 2) {
	logo.href="http://www.exactmedia.com";
	limg.src="http://www.exactmedia/addons/em_sm.gif";
    }
    logo.appendChild(limg);
    sp.appendChild(logo);
    var h=this.height-this.logoH;
    if (isNN) {
	h+=5;
    }
    sp.style.top=h+'px';
    sp.style.zIndex=2;

    var cdiv;
    cdiv=document.createElement('div');
    cdiv.className="rssContentDiv";
    cdiv.style.width=this.width+'px';

    cdiv.style.overflow="hidden";

    cdiv.style.height=this.height-this.logoH+"px";
    cdiv.style.zIndex=1;
    cdiv.style.position='relative';
    cdiv.style.bottom=this.logoH+"px";
    cdiv.appendChild(t);

    if (this.srcArr.length > 1) {

	tDiv.appendChild(sp);
	tDiv.appendChild(cdiv);

    } else {
	var header=this.renderFeedInfo(fi, this.srcArr[tidx]);
	if (header != null) {
	    tDiv.appendChild(header);
	}

	var h=this.height-this.logoH-header.offsetHeight+2;
	sp.style.top=h+'px';
	sp.style.background='#fff';
	sp.style.borderTop="solid 1px #1F1F1F";
	tDiv.appendChild(sp);

	cdiv.style.height=h+'px';
	tDiv.appendChild(cdiv);
    }

    tDiv.loaded=true;
    this.fitlinks(tDiv);
    //t.style.width=this.width+"px";


    return;


}

RSSSpot.prototype.genUI = function(instId, pardiv) {
    if (typeof(rssSpot_id) != 'undefined' && rssSpot_id != null && typeof(rssSpot_id)=='number')
       this.spotId=rssSpot_id;
    if (typeof(rssSpot_width) != 'undefined' && rssSpot_width != null && typeof(rssSpot_width)=='number')
       this.width=rssSpot_width;
    if (typeof(rssSpot_height) != 'undefined' && rssSpot_height != null  && typeof(rssSpot_height)=='number')
       this.height=rssSpot_height;
    if (typeof(rssSpot_charset) != 'undefined' && rssSpot_charset != null) { 
	this.charset=rssSpot_charset;
	rssSpot_charset=null;
    } else {
	this.charset=null;
    }
    if (typeof(rssSpot_timezone) != 'undefined' && rssSpot_timezone != null) { 
	this.timezone=rssSpot_timezone;
	rssSpot_timezone=null;
    } else {
	this.timezone=null;
    }
    this.srcArr=new Array();
    var tidx=0;
    if (typeof(rssSpot_src0) != 'undefined' && rssSpot_src0 != null && rssSpot_src0 != '') { 
	this.srcArr[tidx]=new Object();
	this.srcArr[tidx].title=null;
	this.srcArr[tidx].src=rssSpot_src0;
	if (typeof(rssSpot_src0_title) != 'undefined' && rssSpot_src0_title != null) { 
	    this.srcArr[tidx].title=rssSpot_src0_title;
	}
	tidx+=1;
	rssSpot_src0=null;
    }
    if (typeof(rssSpot_src1) != 'undefined' && rssSpot_src1 != null && rssSpot_src1 != '') { 
	this.srcArr[tidx]=new Object();
	this.srcArr[tidx].title=null;
	this.srcArr[tidx].src=rssSpot_src1;
	if (typeof(rssSpot_src1_title) != 'undefined' && rssSpot_src1_title != null) { 
	    this.srcArr[tidx].title=rssSpot_src1_title;
	}
	tidx+=1;
	rssSpot_src1=null;
    }
    if (typeof(rssSpot_src2) != 'undefined' && rssSpot_src2 != null && rssSpot_src1 != '') { 
	this.srcArr[tidx]=new Object();
	this.srcArr[tidx].title=null;
	this.srcArr[tidx].src=rssSpot_src2;
	if (typeof(rssSpot_src2_title) != 'undefined' && rssSpot_src2_title != null) { 
	    this.srcArr[tidx].title=rssSpot_src2_title;
	}
	tidx+=1;
	rssSpot_src2=null;
    }
    if (typeof(rssSpot_src3) != 'undefined' && rssSpot_src3 != null && rssSpot_src3 != '') { 
	this.srcArr[tidx]=new Object();
	this.srcArr[tidx].title=null;
	this.srcArr[tidx].src=rssSpot_src3;
	if (typeof(rssSpot_src3_title) != 'undefined' && rssSpot_src3_title != null) { 
	    this.srcArr[tidx].title=rssSpot_src3_title;
	}
	tidx+=1;
	rssSpot_src3=null;
    }
    if (typeof(rssSpot_src4) != 'undefined' && rssSpot_src4 != null && rssSpot_src4 != '') { 
	this.srcArr[tidx]=new Object();
	this.srcArr[tidx].title=null;
	this.srcArr[tidx].src=rssSpot_src4;
	if (typeof(rssSpot_src4_title) != 'undefined' && rssSpot_src4_title != null) { 
	    this.srcArr[tidx].title=rssSpot_src4_title;
	}
	tidx+=1;
	rssSpot_src4=null;
    }
    if (typeof(rssSpot_src5) != 'undefined' && rssSpot_src5 != null && rssSpot_src5 != '') { 
	this.srcArr[tidx]=new Object();
	this.srcArr[tidx].title=null;
	this.srcArr[tidx].src=rssSpot_src5;
	if (typeof(rssSpot_src5_title) != 'undefined' && rssSpot_src5_title != null) { 
	    this.srcArr[tidx].title=rssSpot_src5_title;
	}
	tidx+=1;
	rssSpot_src5=null;
    }
    if (typeof(rssSpot_src6) != 'undefined' && rssSpot_src6 != null && rssSpot_src6 != '') { 
	this.srcArr[tidx]=new Object();
	this.srcArr[tidx].title=null;
	this.srcArr[tidx].src=rssSpot_src6;
	if (typeof(rssSpot_src6_title) != 'undefined' && rssSpot_src6_title != null) { 
	    this.srcArr[tidx].title=rssSpot_src6_title;
	}
	tidx+=1;
	rssSpot_src6=null;
    }

    if (tidx==0) return;
    /*
    if (typeof(rssSpot_render_feed_fn) != 'undefined' && rssSpot_render_feed_fn != null) { 
	this.renderFeedInfo=rssSpot_render_feed_fn;
	rssSpot_render_feed_fn=null;
    }
    if (typeof(rssSpot_render_entry_fn) != 'undefined' && rssSpot_render_entry_fn != null) { 
	this.renderEntry=rssSpot_render_entry_fn;
	rssSpot_render_entry_fn=null;
    }
    if (typeof(rssSpot_template) != 'undefined' && rssSpot_template != null) { 
	this.template=rssSpot_template;
	rssSpot_template=null;
	}*/
    if (typeof(rssSpot_entry_count) != 'undefined' && rssSpot_entry_count != null) { 
	this.entryCount=rssSpot_entry_count;
	rssSpot_entry_count=null;
    }
    if (typeof(rssSpot_no_headline_chop) != 'undefined' && rssSpot_no_headline_chop != null) { 
	this.noHeadlineChop=rssSpot_no_headline_chop;
	rssSpot_no_headline_chop=null;
    }
    if (typeof(rssSpot_link_target) != 'undefined' && rssSpot_link_target != null) { 
	this.linkTarget=rssSpot_link_target;
	rssSpot_link_target=null;
    } else {
	this.linkTarget="rssSpot";
    }

    var pdiv=document.createElement('div');
    pdiv.className="rssSpotContainer";
    pdiv.style.width=this.width+'px';
    pdiv.style.height=this.height+'px';
    var tdiv=null;
    var ule=null;
    if (this.srcArr.length > 1) { 
	tdiv=document.createElement('div');
	tdiv.className="RSSSpotTabM";
	var ule=document.createElement('ul');
	ule.className="tabM";
	tdiv.appendChild(ule);
    }
    for (var i=0; i<this.srcArr.length; i++) {
	var div=document.createElement('div');
	div.loaded=false;
	if (this.srcArr.length > 1) { 
	    div.className="RSSSpotTabC";
	    div.style.height=this.height+'px';
	}
	this.srcArr[i].div=div;

	var ele=document.createElement('script');
	ele.setAttribute('language','javascript');
	var url=this.dataServer+'/rssSpotData?iid='+this.instId+'&tidx='+i+'&src='+escape(this.srcArr[i].src);
	if (this.charset != null) {
	    url+='&chset='+escape(this.charset);
	}
	if (this.timezone != null) {
	    url+='&tz='+escape(this.timezone);
	}
	ele.setAttribute('src', url);
	div.appendChild(ele);

	pdiv.appendChild(div);

	if (ule != null) {
	    var lie=document.createElement('li');
	    if (typeof(this.srcArr[i].title) != 'undefined' && this.srcArr[i].title != null) { 
		lie.innerHTML=this.srcArr[i].title;
	    } else {
		lie.innerHTML='News';
	    }
	    ule.appendChild(lie);
	}
    }
    if (tdiv != null) {
	pdiv.insertBefore(tdiv, pdiv.childNodes[0]);
	this.tPUI=new tabPages(pdiv, this);
    } else if (this.srcArr.length > 0) {
	this.srcArr[0].div.style.display='block';
    }

    //var sd=document.getElementById('rssSpotDiv'+instId);
    if (pardiv != null) {
	pardiv.appendChild(pdiv);

	pardiv.style.width=this.width+"px";

	h=this.height+15;
	if (this.srcArr.length > 1) { 
	    h+=25;
	}
	pardiv.style.height=h+"px";
    }
}
RSSSpot.prototype.init = function(instId) {
    this.instId=instId;
    document.write('<div id="rssSpotDiv'+instId+'" style="width:inherit"></div>');

    var sd=document.getElementById('rssSpotDiv'+instId);
    if (sd != null) {
	if (sd.offsetWidth>0)
	    this.width=sd.offsetWidth-20;
	this.genUI(instId, sd);
    }

}

if (typeof(rssSpotInstId)=='undefined' || rssSpotInstId==null) {
    eval('var rssSpotInstId=0');
} else {
    rssSpotInstId++;
}
eval('var rssSpot'+rssSpotInstId+' = new RSSSpot(); rssSpot'+rssSpotInstId+'.init('+rssSpotInstId+');');


