/* jQuery Json widget code generation form and related scripts, developed by Ari Koinuma, September 2008.  
Here are the scripts for generating code and previewing for the widget home page.   Not for end-user use.  It assumes that the widget home page has one widget form and preview.  There also these assumptions: that the page has a div with id of blog-initials+"WidgetDiv", and it has two text areas, one for the code that goes into the head section (#code) and the other for the target div (#targetDiv), and that the forms and buttons 

Documentation: http://infoserverwiki.publicradio.org/index.php/JQuery
Dependency: jquery 1.2.6
*/

/*------Global Variables --------*/
//I'm assuming that there is only one instance of this feed widget form on the widget home page.
var coreCode = '<script type="text/javascript" src="http://www.publicradio.org/columns/feed_widget/widget_core_include.js"></script>';
var jsonCode = '<script type="text/javascript" src="http://www.publicradio.org/columns/sustainability/greenwash/json.js"></script>';
var instanceCode = '<script type="text/javascript" src="http://www.publicradio.org/columns/sustainability/greenwash/feed_widget/feed_widget.js"></script>';


/* ------- Utility Functions ---------*/
function checkbox(id) {
	var value;
	var checked = $('#'+id).attr('checked');
	if (checked == true) {
		value = true;
	} else {
		value = false;
	}
	return value;
}

//Selects text within a textarea 
function selectIt(fieldID) {
	var tempval=$('#'+fieldID);
	tempval.focus();
	tempval.select();
}

//Extend the Widget object found in the feed_widget_class.js.  obj is the name of the object defined.  Be sure that all the field names match those on the page. 
Widget.prototype.generateCode = function() {
	//Collect all the values from the form. 
	this.numEntries = $("#postNumber").val();
	this.showLogo = checkbox('showLogo');
	this.showGetButton = checkbox('showGetButton');
	this.showBlogTitle = checkbox('showBlogTitle');
	this.showTagLine = checkbox('showTagLine');
	this.headlineType = $('#headlineType').val();
	this.showText = checkbox('showText');
	this.useCss = checkbox('useDefaultCSS');
	
	//Assemble and output the codes
	var headCodes = coreCode + '\n';
	headCodes = headCodes + jsonCode + '\n';
	headCodes = headCodes + instanceCode + '\n';
	headCodes = headCodes + '<script type="text/javascript">\n';
	headCodes = headCodes + this.widgetId+'.numEntries = ' + this.numEntries + ';\n';	
	headCodes = headCodes + this.widgetId+'.showLogo = ' + this.showLogo + ';\n';
	headCodes = headCodes + this.widgetId+'.showGetButton = ' + this.showGetButton + ';\n';
	headCodes = headCodes + this.widgetId+'.showBlogTitle = ' + this.showBlogTitle + ';\n';	
	headCodes = headCodes + this.widgetId+'.showTagLine = ' + this.showTagLine + ';\n';
	headCodes = headCodes + this.widgetId+'.headlineType = "' + this.headlineType + '";\n';
	headCodes = headCodes + this.widgetId+'.showText = ' + this.showText + ';\n';
	headCodes = headCodes + this.widgetId+'.useCss = ' + this.useCss + ';\n';
	headCodes = headCodes + '$(document).ready(function(){';
	headCodes = headCodes + this.widgetId+'.buildWidget();';
	headCodes = headCodes + '});' + '\n';
	headCodes = headCodes + '</script>';
	$('#code').text(headCodes);
	
	var targetCodes = '<div id="'+this.targetId+'"></div><script type="text/javascript" src="http://secure-us.imrworldwide.com/v53.js"></script>';
	$('#targetDiv').text(targetCodes);
}


//execution function
function runForm(widget) { //assuming that widget is the feedWidget object
	widget.generateCode();
	widget.buildWidget();
	
	//Every time the generate button is clicked, it reinitializes the widget and shows new preview.  I'm assuming that the button to generate code has this ID
	$('#generate').click(function(){
			widget = tgbExecute(); //whatever the construction function(s) is
			
			$('#'+widget.targetId).hide().empty();
			widget.generateCode();
			widget.buildWidget();
			$('#'+widget.targetId).fadeIn('medium');
	});
}


//execute
$(document).ready(function(){
	runForm(tgbWidget);
}); 