var Feeds = {
	
	BlogParser : '/library/get_rss.php',
	
	Load : function() {
		Feeds.Twitter();
		Feeds.Blog();
	},
	
	PageInit : function() {
	
		if( $('#people_popup .mysay .blogg .no_address').is('span') ) {
			// We have no blog. Do you?
			$('#people_popup .mysay .blogg a').removeAttr('href');
			$('#people_popup .mysay .blogg a').click( Dlgs.Tip.InitRegular );
		}
		
		// Twitter
		if( $('#people_popup .mysay .twitter .no_address').is('span') ) {
			// We have no Twitter. Do you?
			$('#people_popup .mysay .twitter a').removeAttr('href');
			$('#people_popup .mysay .twitter a').click( Dlgs.Tip.InitTwitter );		
		}
		
	},
	
	Blog : function() {
		if( $('#people_popup .mysay .blogg .no_address').is('span') ) {
			// We have no blog. Do you?
			$('#people_popup .mysay .blogg a').removeAttr('href');
			$('#people_popup .mysay .blogg a').click( Dlgs.Tip.InitRegular );
			
		} else {
			// We have a blog!
			feed = $('#people_popup .mysay .blogg span').html();
			$.ajax({
				type: 'GET',
				url:	Feeds.BlogParser,
				dataType: 'json',
				data:	{
					format: 'json',				
					url: feed,
					noCache : Math.random()
				},
				success:
					function( data ) {
						bHTML = '';
						for( i = 0 ; i < data.items.length ; ++i ) {
							bHTML+= '<a href="' + data.items[i].link + '" title="Besök ' + Global.PersonFirstName + 's blogg" target="_blank">';
							bHTML+= '<strong>' + data.items[i].title + '</strong>';
							bHTML+= '<span class="date">' + data.items[i].date + '</span>';
							content2 = 'asd';
							
							if( data.items[i].content.length > 100 ) { 
								//content = 'asdsa';
								//content = data.items[i].content;
								content2 = data.items[i].content.substring(0, 100) + '…';
							} else {
								content2 = data.items[i].content;
							}
							
							bHTML+= '<span class="text">' + content2 + '</span>';
							bHTML+= '</a>';
						}
						$('#people_popup .mysay .blogg').html(bHTML);
					}
			});
		}
	},

	Twitter : function() {
		
		// Twitter
		if( $('#people_popup .mysay .twitter .no_address').is('span') ) {
			// We have no Twitter. Do you?
			$('#people_popup .mysay .twitter a').removeAttr('href');
			$('#people_popup .mysay .twitter a').click( Dlgs.Tip.InitTwitter );
			
		} else {
			// We have a Twitter!
			feed = $('#people_popup .mysay .twitter span').html();
			feed = feed.split('/');
			feed = 'http://twitter.com/status/user_timeline/' + feed[feed.length-1] + '.json';
			
			$.ajax({
				type: 'GET',
				url: feed + '?count=1&callback=?',
				dataType: 'json',
				success:			
				function(data) {
					tMonths = Array();
					tMonths['Jan'] = 'Januari';
					tMonths['Feb'] = 'Februari';
					tMonths['Mar'] = 'Mars';
					tMonths['Apr'] = 'April';
					tMonths['May'] = 'Maj';
					tMonths['Jun'] = 'Juni';
					tMonths['Jul'] = 'Juli';
					tMonths['Aug'] = 'Augusti';
					tMonths['Sep'] = 'September';
					tMonths['Oct'] = 'Oktober';
					tMonths['Nov'] = 'November';					
					tMonths['Dec'] = 'December';					
					
					//alert(data[0].text);
					created_at = data[0].created_at.split(' ');
					tHTML = '<a href="http://twitter.com/' + data[0].user.screen_name + '" ';
					tHTML+= 'title="Besök ' + Global.PersonFirstName + 's sida på Twitter" ';
					tHTML+= 'target="twitter_' + data[0].user.screen_name + '">';					
					tHTML+= '<span class="date">' + created_at[2] + ' ' + tMonths[created_at[1]] + ' kl: ' + created_at[3].substring(0, 5) + '</span>';
					tHTML+= data[0].text;
					tHTML+= '</a>';
					
					$('#people_popup .mysay .twitter').html(tHTML);
				}
			});
		}
	},
	
	Twingly : {
		SearchFile : '/hantera/xml/rss_out.php',
		LongDesc : new Array(),
		
		Search : function() {
			$.ajax({
				type: 'GET',
				url: Feeds.Twingly.SearchFile,
				dataType: 'json',
				data:	{
		  		blog: 1,
		  		parti: Global.PersonParty,
		  		person: Global.PersonFirstName + '+' + Global.PersonLastName,
		  		noCache: Math.random()		  		
		  	},
				success:			
				function( data ) {
					Feeds.Twingly.LongDesc.length =  0;
					tHTML = '';
					var j = 0;
					$.each( data.list, function( i, Item ) {
						tHTML+= '<li class="twingly">';
						tHTML+= '<a href="' + Item.ItemLink + '" ';
						if( Item.ItemDescription.length > 40 ) {
							Feeds.Twingly.LongDesc[j] = Item.ItemDescription;
							tHTML+= 'onmouseover="Feeds.ShowLongDescription(' + j + ')" ';
							tHTML+= 'onmouseout="Feeds.HideLongDescription()" ';							
							tHTML+= 'target="_blank">';							
							tHTML+= Item.ItemDescription.substring(0, 40) + '&#8230;';
						} else {
							tHTML+= 'target="_blank">';
							tHTML+= Item.ItemDescription;	
						}
						tHTML+= '</a>';
						tHTML+= '</li>';
						++j;
					});
					
					if( j > 0 ) {
						tHTML+= '<li class="more_of_others">';
						tHTML+= '<a href="http://www.twingly.com/search?';
						tHTML+= 'q=' + Global.PersonFirstName + '+' + Global.PersonLastName + '+' + Global.PersonParty + '"';
						tHTML+= 'target="_blank">Mer av vad andra säger om ' + Global.PersonFirstName + '</a></li>';
					}
					if( tHTML == '' ) {
						tHTML = '<li class="more_of_others">Tyvärr hittade vi ingenting som andra sagt om ' + Global.PersonFirstName + '</li>';
					}
					$('#otherssay ul').html(tHTML);
					
				}
			})
		}
	},
	
	ShowLongDescription : function( num ) {

		var text = '<p class="tt_content">' + Feeds.Twingly.LongDesc[num] + '<br />';
				text+= '<span style="padding-top:7px; display:block;">(Klicka på länken för att läsa mer.)</span></p>';
		var pos = $('#otherssay ul li:eq(' + num + ') a').offset();
		var posY = pos.top;
		
		Global.ShowInfo(text, 300, posY)
	},
	
	HideLongDescription : function() {
		Global.HideInfo();
	}
}
