/* jQuery Json Parser widget application developed by Ari Koinuma, September 2008.  
This script will parse an Json file and spits out a div which contains the listing of entries.  What gets displayed are user-configurable.  

Documentation: http://infoserverwiki.publicradio.org/index.php/JQuery
Dependency: jquery 1.2.6, feed_widget_class.js
*/

//This portion is the blog-specific code. 
//Here are blog-specific URLs.  Be sure to put the blog initials to the variable names in case there are more than one widget embedded on a page. 
var ptthPrgUrl = 'http://prairiehome.publicradio.org/';
var ptthPrgLogoUrl = 'http://www.publicradio.org/columns/prairiehome/posthost/feed_widget/phc_logo.gif';
var ptthCssUrl = 'http://www.publicradio.org/columns/prairiehome/posthost/feed_widget/feed_widget.css'
var ptthFormURL = "http://prairiehome.publicradio.org/tools/ptth_widget/";
var ptthButtonURL = "http://www.publicradio.org/columns/prairiehome/posthost/feed_widget/ptth_widget_add_button.gif";
var ptthTagLine = "Host Garrison Keillor answers your questions";

//Here's a blog-specific customizations

//modifying the buildEntry method to strip out the first image in each entry, because it's a graphic version of the entry title
Widget.prototype.buildEntry = function(entry){ //entry is an blogEntryobject
	
	var entryDiv = $('<div></div>').addClass('feedEntry').attr('id', this.divIdPrefix+entry.id);
	
	
	
	if (this.headlineType != 'date') {
		var title = $('<h4 class="feedTitle"><a href="'+entry.link+' " title="'+ entry.title+'">'+entry.title+'</a></h4>');
		entryDiv.append(title); 
	}
	
	if (this.headlineType != 'title') {
		if (this.headlineType == 'date') { //if the headline type is date, then convert the date into a link to that entry
			var date = $('<h4 class="feedDate"><a href="'+entry.link+'" title="'+entry.pubDate+'">'+entry.pubDate+'</a></h4>');
		} else {
			var date = $('<h4 class=" feedDate feedDateSmaller">'+entry.pubDate+'</h4>');
		}
		entryDiv.append(date);
	}
	
	if (this.showText != false) {
		var description = $('<div class="feedDesc"></div>');
		if (this.limitText > 0){
				entry.desc = truncate(entry.desc, this.limitText, entry.link, entry.title);
			}
		
			
		//check to see if the paragraph tags are already in the body
		if (entry.desc.search('<p>') == -1 ) {
			description.append('<p>'+entry.desc+'</p>');
		} else {
			description.append(entry.desc);
		}
		
		entryDiv.append(description);
	}
	
	//here's the line to remove the first image
	entryDiv.find('img:first').remove();
	
	
	if (this.showFlash == false) {
		entryDiv.find('object').replaceWith('[<a href="'+entry.link+'" title="See this video/Flash presentation">See the video/Flash presentation</a>]');
		entryDiv.find('embed').remove();
	}
	
	if (this.showImage == false) {
		entryDiv.find('img').replaceWith('<br /><br />[<a href="'+entry.link+'" title="See the image">See the image</a>]<br /><br />');
	}
	
	return entryDiv;		
}



function ptthExecute() {
	/*Here's the constructor of the widget object.  feedId+Widget is the target div ID.  logoCode will be already assigned to a global variable by the class -- so it's going to be either apmLogo or mprLogo.*/
	var widgetName = new Widget(ptthFeed, 'ptthWidget', ptthPrgUrl, ptthPrgLogoUrl, ptthCssUrl, ptthFormURL, ptthButtonURL, ptthTagLine);
	widgetName.parseEntries();
	return widgetName;
}

//Construct the widget object.  But the actual displaying of the widget will be taken care of by the inline code user embeds. 
var ptthWidget = ptthExecute();

