
/**
 * Power 150 feed reader
 *
 * @author: Rahmin Pavlovic
 * @version: 08 January 2008
 * @version: 02 February 2008
 *
 * copyright (c) 2008, Crain Communications
 *
 */

var blog;
var power150 = {
	img:[],
	posts:[],
	feedCount:0,
	feedLimit:5,
	rssFeed:null,
	currBlog:null,
	summaryLimit:50,
	imgPath:'/img/power150',
	imgs:[this.imgPath+'/minus.gif', '/images/global/ajax-wheel-036.gif'],
	closeBtn:'<div class="close_btn"><a href="javascript:power150.hideFeed(1);" title="Close">x<\/a><\/div>',
	loadingMsg:'<div class="loading">Loading...<\/div><br /><div align="center"><img src="/images/global/ajax-wheel-036.gif" width="32" height="32" alt="" /><\/div><br />',

	getRSS:function(feed, blogID) {
		this.hideFeed();
		if(blogID != this.currBlog) {

			// remember which blog we're showing
			blog=this;
			this.currBlog=blogID;

			// toggle plus/minus icon
			if(document["img_"+blogID]) document["img_"+blogID].src=this.imgPath+"/minus.gif";

			// position reader relative to blog
			reader=DOM.getElementById("headlines_"+blogID);
			reader.style.top=parseInt(DHTML.getPosY(DOM.getElementById(blogID)) - 27) + 'px';
			reader.style.left=parseInt(DHTML.getPosX(DOM.getElementById(blogID)) - 15) + 'px';

			// position indicator relative to reader
			ind=DOM.getElementById("indicator");
			ind.style.top=parseInt(DHTML.getPosY(reader) + 14) + 'px';
			ind.style.left=parseInt(DHTML.getPosX(reader) - 6) + 'px';

			// display reader
			reader.style.visibility="visible";
			ind.style.visibility="visible";

			// fetch feed if not already set 
			if(reader.innerHTML=='') {
				reader.innerHTML=[this.closeBtn, this.loadingMsg].join('');
				new AJAX.FileRequest('/functions/ajax/get_rss_feed.php', this.setRSS, null, 'get', 'for=power150&return=json&url='+feed);
			}
		}
		else {

			// forget last blog
			this.currBlog=null;
		}
	},

	setRSS:function() {
		// process AJAX request
		if(this.request && this.response) {

			// reset blog post HTML
			blog.recentPosts=[blog.closeBtn];

			// parse feed as JSON
			blog.rssFeed=JSON.parse(this.response);

			// continue if JSON came through
			if(blog.rssFeed) {

				// some feeds are missing channels, so assume it's not there
				if(blog.rssFeed.channel && blog.rssFeed.channel.item) {
					blog.rssFeed=blog.rssFeed.channel;
				}

				if(blog.rssFeed.item && blog.rssFeed.item.length >= 0) {

					// reset feed counter
					blog.feedCount=0;

					// loop through feed items
					for(var n=0; n<blog.rssFeed.item.length; n++) {
						blog.posts[n]={};

						// pull summary instead of title
						if(isObject(blog.rssFeed.item[n].title) && !isObject(blog.rssFeed.item[n].summary)) {

							// try clipping summary
							unClippedSummary=null;
							unClippedSummary=(blog.rssFeed.item[n].summary.indexOf('.') <= blog.summaryLimit);
							end=(unClippedSummary) ? blog.rssFeed.item[n].summary.indexOf('.') : blog.summaryLimit;
							blog.posts[n]['title']=blog.rssFeed.item[n].summary.substring(0, end);

							// if it worked...
							if(unClippedSummary) {
								blog.posts[n]['title']+='...';
							}
						}
						else {
							blog.posts[n]['title']=blog.rssFeed.item[n].title;
						}

						// link tag with attributes
						if(isArray(blog.rssFeed.item[n].link) && blog.rssFeed.item[n].link[0]['@attributes'] && blog.rssFeed.item[n].link[0]['@attributes']['href']) {
							blog.posts[n]['link']=blog.rssFeed.item[n].link[0]['@attributes']['href'];
						}
						else if(isObject(blog.rssFeed.item[n].link) && blog.rssFeed.item[n].link['@attributes'].href) {
							blog.posts[n]['link']=blog.rssFeed.item[n].link['@attributes'].href;
						}
						// standard link tag
						else {
							blog.posts[n]['link']=blog.rssFeed.item[n].link;
						}

						// keep count
						blog.feedCount++;

						// stop if feeds in RSS 
						if(blog.feedCount >= blog.feedLimit) {
							break;
						}
					}

					// free browser memory of feed
					blog.rssFeed=null; 

					// build HTML for this feed
					blog.recentPosts.push('<ul>');
					for(var i=0; i<blog.posts.length; i++) {
						blog.recentPosts.push('<li><a href="', blog.posts[i].link, '" title="Read this post" target="_blank">', blog.posts[i].title,'<\/a><\/li>');
					}
					blog.recentPosts.push('<\/ul>');
				}
				else {
					blog.recentPosts.push('Unable to find recent posts');
				}
			}
			else {
				blog.recentPosts.push('Error parsing RSS feed');
			}
			blog.showFeed();
		}
	},

	showFeed:function() {
		if(this.currBlog!=null) {

			// display recent posts
			reader=DOM.getElementById('headlines_'+this.currBlog);
			reader.innerHTML=this.recentPosts.join('');

			// free browser memory of recent posts 
			this.posts.clear();
			this.recentPosts.clear();
		}
	},

	hideFeed:function() {
		if(this.currBlog!=null) {
			if(document["img_"+this.currBlog]) document["img_"+this.currBlog].src=this.imgPath+"/plus.gif";
			ind=DOM.getElementById("indicator");
			ind.style.visibility="hidden";
	
			reader=DOM.getElementById('headlines_'+this.currBlog);
			reader.style.visibility="hidden";

			// forget blog if told to (close button)
			if(this.hideFeed.arguments[0]) {
				this.currBlog=null;
			}
		}
	},

	preload:function() {
		for(var i=0; i<this.imgs.length; i++) {
			this.img[i]=new Image();
			this.img[i].src=this.imgs[i];
		}
	}
};

power150.preload();
