/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jQuery.noConflict();

jQuery(document).ready(function()
{
	var contentDiv = jQuery('div#rbContent');
	if ( contentDiv.length > 0 )
	{
		// center the image
		var topDiv = jQuery('div#rssBrief');
		var pos = jQuery('#rbloadImg').position();
		if ( pos != undefined )
		{
			jQuery('#rbloadImg').fadeIn('normal');

			// get the xml
			jQuery.get(RSSBriefSettings.siteUrl+'/?rss_brief', {},
			function(data,textStatus)
			{
				// convert XML to JSON
				var jsonObj = jQuery.xmlToJSON(data);

				// loop over objects
				var theHTML = '';
				var linkStart = '';
				var linkEnd = '';
				for ( idx=0; idx < jsonObj.channel[0].item.length; idx++ )
				{
					// start the table
					theHTML = '<table><tr>';

					// link
					if ( jsonObj.channel[0].item[idx].link[0].Text.length > 0 )
					{
						linkStart = '<a href="'+jsonObj.channel[0].item[idx].link[0].Text+'" target="_blank">';
						linkEnd = '</a>';
					}
					else
					{
						linkStart = '';
						linkEnd = '';
					}

					if ( jsonObj.channel[0].item[idx].image != undefined
						&& jsonObj.channel[0].item[idx].image[0].url[0].Text.length > 0 )
					{
						theHTML = theHTML + '<td style="width: 100px;vertical-align:top;">';

						// image
						theHTML = theHTML + linkStart+'<img src="'+jsonObj.channel[0].item[idx].image[0].url[0].Text+'" />'+linkEnd;
						theHTML = theHTML + '</td><td>';
					}
					else
					{
						theHTML = theHTML + '<td style="vertical-align:top;" colspan="2">';
					}

					// title
					if ( jsonObj.channel[0].item[idx].title[0].Text.length > 0 )
						theHTML = theHTML + '<span class="title">'+linkStart+jsonObj.channel[0].item[idx].title[0].Text+linkEnd+'</span>';

					// pubDate
					if ( jsonObj.channel[0].item[idx].pubDate[0].Text.length > 0 )
						theHTML = theHTML + '<span class="pubDate">'+jsonObj.channel[0].item[idx].pubDate[0].Text+'</span>';

					// description
					if ( jsonObj.channel[0].item[idx].description[0].Text.length > 0
						&& jsonObj.channel[0].item[idx].description[0].Text != jsonObj.channel[0].item[idx].title[0].Text )
					{
						theHTML = theHTML + '<span class="description">'+jsonObj.channel[0].item[idx].description[0].Text+'</span>';
					}

					// end
					theHTML = theHTML + '</td></tr></table>';

					// append HTML
					contentDiv.append(theHTML);
				}

				// hide the loading image, slide down content
				jQuery('#rbloadImg').fadeOut('fast', function()
				{
					jQuery('#rbloadImg').hide();
					contentDiv.slideDown('slow');
				});
			},
			"xmlDoc");
		}
	}
});
