
function feedItem(feedTitle, feedLink, feedDescription, itemTitle, itemLink, itemPubDate,itemDescription, itemGuid ) {
	this.feedTitle = feedTitle;
	this.feedLink = feedLink;
	this.feedDescription = feedDescription;
	this.itemTitle = itemTitle;
	this.itemLink = itemLink;	
	this.itemPubDate = itemPubDate;
	this.itemDescription = itemDescription;
	this.itemGuid = itemGuid;
}

function ASfeedItem(feedTitle, feedLink, feedDescription, itemTitle, itemLink, itemPubDate,itemDescription, itemGuid, itemCategory ) {
	this.feedTitle = feedTitle;
	this.feedLink = feedLink;
	this.feedDescription = feedDescription;
	this.itemTitle = itemTitle;
	this.itemLink = itemLink;	
	this.itemPubDate = itemPubDate;
	this.itemDescription = itemDescription;
	this.itemGuid = itemGuid;
	this.itemCategory = itemCategory;
}


function getLatestRSSFeeds(number, rssfeed, div_id, itemtitle, itempubdate, itemdescription ){	
					
	jQuery.get('/includes/rss/rssfunction.php', {rssfeed:rssfeed, nbitems:number}, function(response) {
		/* We make use of the eval function, which converts the properly formatted string from the server into a javascript object */
		rss2json = eval('('+ response +')'); 
		jsonfeeds = new Array();
			
		for(var i = 0; i < number; i++) {
			jsonfeeds[i] = new feedItem(rss2json.channel.channel.title,
									rss2json.channel.channel.link,
									rss2json.channel.channel.description,
									rss2json.channel.channel.item[i].title,
									rss2json.channel.channel.item[i].link,									
									rss2json.channel.channel.item[i].pubDate,
									rss2json.channel.channel.item[i].description,
									rss2json.channel.channel.item[i].guid
									);
		}	
		formatJsonFeeds(number, div_id, itemtitle, itempubdate, itemdescription);	
		
		
	});	
}

function getLatestASRSSFeeds(number, rssfeed, div_id, itemtitle, itempubdate, itemdescription ){	
					
	jQuery.get('/includes/rss/rssfunction.php', {rssfeed:rssfeed, nbitems:number}, function(response) {
		/* We make use of the eval function, which converts the properly formatted string from the server into a javascript object */
		rss2json = eval('('+ response +')'); 
		asjsonfeeds = new Array();
		var j = 0;	
		for(var i = 0; ;i++) {
			var pattern = rss2json.channel.channel.item[i].link;			
			if (pattern.indexOf('Itemid=31') != -1 | pattern.indexOf('Itemid=32') != -1 | pattern.indexOf('Itemid=34') != -1  ) {
				if (pattern.indexOf('Itemid=31') != -1) {
					category = "Politics";
				} else if (pattern.indexOf('Itemid=32') != -1) {
					category = "Economics/Business";
				} else if (pattern.indexOf('Itemid=34') != -1) {
					category = "Culture";
				}		
				category = " (" + category + ")";	
								
				asjsonfeeds[j] = new ASfeedItem(rss2json.channel.channel.title,
										rss2json.channel.channel.link,
										rss2json.channel.channel.description,
										rss2json.channel.channel.item[i].title,
										rss2json.channel.channel.item[i].link,									
										rss2json.channel.channel.item[i].pubDate,
										rss2json.channel.channel.item[i].description,
										rss2json.channel.channel.item[i].guid,
										category
										);	
				j++;			
			} 
			if (j == number) {
				break;
			}

		}
		
		formatJsonASFeeds(asjsonfeeds, div_id, itemtitle, itempubdate, itemdescription);
		
		
	});	
}


function formatJsonFeeds(number, div_id, itemtitle, itempubdate, itemdescription) {
	var str = '';	
	str += '<div class="rss_wrap_items">';	
	for(var i = 0; i < jsonfeeds.length; i++) {			
		str += '<div class="rss_item">';
		if (itemtitle){
			str += getJsonItemTitle(i, itempubdate);
		}
		str += '</div>';
	}
	str += '</div>';	
	jQuery("div#"+div_id).html(str);
	jQuery("div#"+div_id).slideDown('slow');
}

function formatJsonASFeeds(asjsonfeeds, div_id, itemtitle, itempubdate, itemdescription) {
	var str = '';
	str += '<img src="/images/section/asia/asiasentinel_180x30.jpg" alt="Asia Sentinel News"></img>';
	str += '<div class="sectionfront_b_col_item"><ul>';
	for(var i = 0; i < asjsonfeeds.length; i++) {	
		str += '<li><a class="rss_link" href="' + asjsonfeeds[i].itemLink + '">' + asjsonfeeds[i].itemTitle + "" + asjsonfeeds[i].itemCategory  + '</a></li>';		

	}
	str += '</ul></div><br/>';
	jQuery("div#"+div_id).html(str);
}





/**
 * Returns the title of an item
 * @param {Object} i
 */
function getJsonItemTitle(i, itempubdate) {
	if (itempubdate) {
		return '<div class="rss_item_title">' + getJsonItemPubDate(i) + '<a class="rss_link" href="' + jsonfeeds[i].itemLink + '">' + jsonfeeds[i].itemTitle  + '</a></div>';		
	} else {
		return '<div class="rss_item_title"><a class="rss_link" href="' +  jsonfeeds[i].itemLink + '">' + jsonfeeds[i].itemTitle  + '</a></div>';
	}
	
}

/**
 * Returns the publication date of the item
 * @param {Object} i
 */
function getJsonItemPubDate(i) {
	return '<span class="rss_item_date">' + jsonfeeds[i].itemPubDate.substring(5,11) + '&nbsp;:&nbsp;</span>';
}



/**
 * Returns the description of the item
 * @param {Object} i
 */
function getJsonItemDescription(i){
	return '<div class="rss_item_description">' + jsonfeeds[i].itemDescription.substring(0,150) + '...()</div><a class="blog_readon" href="">Read on</a>';
}

// ensure that jQuery doesn't conflict with other libraries
// but still uses short names for objects
jQuery.noConflict();