/*

Livemercial Feed Generator
http://www.livemercial.com/

Copyright (c) 2009 livemercial
Written By: Matt Fiocca

Date: 2009-02-19 17:34:21 -0500 (Thu, 19 Feb 2009)

*/
 
function createModule(color,feed_data,customTitle)
{
	var total = (feed_data["opensearch:totalResults"].content) ? parseInt(feed_data["opensearch:totalResults"].content) : 0;
	var perPage = (feed_data["opensearch:itemsPerPage"].content) ? parseInt(feed_data["opensearch:itemsPerPage"].content) : 0;
	var startIndex = (feed_data["opensearch:startIndex"].content) ? parseInt(feed_data["opensearch:startIndex"].content) : 0;
	var title = (feed_data.title.content) ? feed_data.title.content : '';
	title = (customTitle) ? customTitle : title;
	var urlTitle = title.split(' ');
	urlTitle = urlTitle.join('-');
	
	var html = '<table cellpadding="0" cellspacing="0" width="100%" class="module">';
	html += '<tr>';
	html += '<td class="moduleCornerTL' + color + '"></td>';
	html += '<td class="moduleGutterHorz' + color + '"></td>';
	html += '<td class="moduleCornerTR' + color + '"></td>';
	html += '</tr>';
	
	html += '<tr>';
	html += '<td class="moduleGutterVert' + color + '"></td>';
	html += '<td class="moduleContent' + color + '">'	
	html += '<h1>' + title + '</h1>';
	html += '<p><a href="http://livemercial.magnify.net/playlist/' + urlTitle + '" target="_blank">View all videos</a></p>';
	if(feed_data.entry.length > 0)
	{
		html += '<table width="100%">';
		html += '<tr>';
		for(var i in feed_data.entry)
		{
			var lnk = (feed_data.entry[i].link[1].href) ? feed_data.entry[i].link[1].href : '';
			var thumb = (feed_data.entry[i]["media:thumbnail"].url) ? feed_data.entry[i]["media:thumbnail"].url : '';
			var title = (feed_data.entry[i].title.content) ? feed_data.entry[i].title.content : '';
			var content = (feed_data.entry[i].content.content) ? feed_data.entry[i].content.content : '';
			var totalViews = (feed_data.entry[i].totalviews.content) ? feed_data.entry[i].totalviews.content : '';
			
			content = (content.length > 50) ? content.substr(0,50) + '...' : content;
			
			html += '<td><a href="' + lnk + '" target="_blank"><img src="' + thumb + '" width="150" border="0"><br />';
			html +=  title + '<br />';
			html +=  totalViews + ' Views</a></td>';
		}
		html += '</tr>';
		html += '</table>';
	}
	html += '</td>';
	html += '<td class="moduleGutterVert' + color + '"></td>';
	html += '</tr>';
		
	html += '<tr>';
	html += '<td class="moduleCornerBL' + color + '"></td>';
	html += '<td class="moduleGutterHorz' + color + '"></td>';
	html += '<td class="moduleCornerBR' + color + '"></td>';
	html += '</tr>';
	
	return html;
}

$(function(){
	$.getJSON('http://blog.livemercial.com/feed/json/?category=pressrelease&callback=?', function(json){
																								  
		if(json.items.length > 0)
		{
			var result = '<h1>' + json.items[0].title + '</h1>';
			result += '<h2>' + json.items[0].pubDate + '</h2>';
			result += '<div class="newspost">' + json.items[0].description + '</div>';
			$("#blogfeedshow").html(result);
		}
	});	
		
//	$.getJSON('http://search.twitter.com/search.json?q=%23livemercial+OR+pitchtothepros+OR+pitchittothepros&callback=?', function(json){
//		var result = '';
//		var explode;
//		var count=0;
//		$.each(json.results, function(){
//			if(count < 3)
//			{
//				result+='<table>';
//				result+='<tr>';
//				result+='<td valign="top"><a href="http://tweetfeed.com/livemercial/livemercial" target="_blank"><img src="'+this.profile_image_url+'" width="50px" height="50px" title="'+this.from_user+'" alt="'+this.from_user+'" align="left" hspace="10" border="0"/></a></td>';
//				result+='<td valign="top"><div style="width:130px;overflow:hidden;"><a href="http://tweetfeed.com/livemercial/livemercial" target="_blank">'+this.text+'</a><blockquote>- '+this.from_user+'</blockquote></div></td>';
//				result+='</tr>';
//				result+='</table>';
//			}
//			count++;
//		})
//		result += '';
//		$("#lmtweets").html(result);
//	});
	
	// LIVE PITCHES
	$.getJSON('http://livemercial.magnify.net/api/list/show?id=H3M3BK1LKYXNCPJ7&per_page=4&page=1&sort=popularity&key=RKPLGLHK34683X1K&format=json&callback=?', function(json){
		$("#magnify").append(createModule('gray',json));
	});
	
	// Most Popular
	$.getJSON('http://livemercial.magnify.net/api/content/browse?per_page=4&page=1&sort=popularity&key=RKPLGLHK34683X1K&format=json&callback=?', function(json){
		$("#magnify").append(createModule('gray',json,'Recently Viewed'));
	});
	
	// PRODUCTS
	$.getJSON('http://livemercial.magnify.net/api/list/show?id=J1R9G7GP9TFTDKHX&per_page=4&page=1&sort=popularity&key=RKPLGLHK34683X1K&format=json&callback=?', function(json){
		$("#magnify").append(createModule('gray',json));
	});
	
});