/**
 * jQuery scripts for These Days
 * 
 */


/* SETTINGS */
	
	var rssFeed2		= 'http://feeds.feedburner.com/ericpeterson';
	//var rssFeed2		= 'http://feeds.feedburner.com/minorissues';
	var rssFeed1		= 'http://feeds.feedburner.com/Activemetrics';
	var twitterFeed		= 'http://search.twitter.com/search.atom?q=wawbe';
	
	var updateSec		= 60; // seconds
	var scrollSpeed		= 5; // pixels
	var feedUrl			= '/lib/getFeed.php?feed=';
	
	var iv;



/* PAGE LOAD */
	
	$(document).ready(function() {
		
		setInterval(function() {
			//getRss();
			getTwitter();
		}, (1000*updateSec));
		
		getRss();
		getTwitter();
		
	});



/* TWITTER FUNCTIONS */
	
	function getTwitter() {
		var url = feedUrl+twitterFeed;
		$.get(url, {}, function(xml) {
			
			// reset list
				$('#twitter ul').html('');
			
			// loop through xml
				$(xml).find('entry').each(function() {
					
					// get content
						var content = $(this).find('content').text();
						var link = $(this).find('link[rel="alternate"]').attr('href');
						var author = $(this).find('author').find('name').text();
						var authorLink = $(this).find('author').find('uri').text();
						var time = $(this).find('published').text().split('T').join(' ').split('Z').join('');
					
					// generate html
						var html = '<li><p>'+content+'</p><p class="meta clearfix"><a href="'+authorLink+'" class="r">'+author+'</a> <a href="'+link+'" title="'+time+'" class="l">∞</a></p></li>';
						$('#twitter ul').append(html);
					
				});
			
			// add scrollbars
				if(!$('#twitter div').hasClass('loaded')) {
				
					// mark as loaded
						$('#twitter div').addClass('loaded');
					
					// add scroll buttons
						$('#twitter div').before('<img src="/img/arrow_up_right.gif" alt="" id="twitterUp" />');
						$('#twitter div').after('<img src="/img/arrow_down_right.gif" alt="" id="twitterDown" />');
					
					// add scroll actions
						$('#twitterUp').hover(function() {
							clearInterval(iv);
							iv = setInterval(function() {
								newY = parseFloat($('#twitter ul').css('marginTop')) + scrollSpeed;
								if(newY > 0) newY = 0;
								$('#twitter ul').css('marginTop', newY);
							}, 1);
						}, function() {
							clearInterval(iv);
						});
						$('#twitterDown').hover(function() {
							clearInterval(iv);
							iv = setInterval(function() {
								newY = parseFloat($('#twitter ul').css('marginTop')) - scrollSpeed;
								if(-newY > $('#twitter ul').height() - $('#twitter div').height()) newY = -($('#twitter ul').height() - $('#twitter div').height() - 1);
								$('#twitter ul').css('marginTop', newY);
							}, 1);
						}, function() {
							clearInterval(iv);
						});
					
				}
			
		}, 'xml');
	}



/* COMBINED RSS FUNCTIONS */
	
	var xml1, xml2, xml1arr;
	
	function getRss() {
		
		// reset variables
			xml1 = false;
			xml2 = false;
			xml1arr = new Array();
		
		// get xml feeds
			$.get(feedUrl+rssFeed1, {}, function(xml) { xml1 = xml; printRss(); }, 'xml');
			$.get(feedUrl+rssFeed2, {}, function(xml) { xml2 = xml; printRss(); }, 'xml');
		
	}
	
	function printRss() {
		if(xml1 && xml2) {
			
			// populate array with first feed data
				$(xml1).find('item').each(function() { xml1arr.push(getFeedData(this)); });
			
			// reset list
				$('#rss ul').html('');
			
			// loop over each entry from the second feed, compare date with first feed date and sort them
				$(xml2).find('item').each(function() {
					
					// set data
						var date = new Date($(this).find('pubDate').text());
						var item = xml1arr[0];
						var posted;
						var html;
					
					// loop data
						while(date > item.date) {
							
							// set and delete first item
								xml1arr.unshift();
								item = xml1arr[0];
							
							// generate html
								posted = item.date.getDay()+'/'+item.date.getMonth()+'/'+item.date.getFullYear();
								html = '<li><h3><a href="'+item.link+'" target="_blank">'+item.title+'</a></h3><p class="meta">'+posted+'</p><p>'+item.text+'</p></li>';
								$('#rss ul').append(html);
							
						}
					
					// set second feed
						item = getFeedData(this);
						posted = item.date.getDay()+'/'+item.date.getMonth()+'/'+item.date.getFullYear();
					
					// generate html
						html = '<li><h3><a href="'+item.link+'" target="_blank">'+item.title+'</a></h3><p class="meta">'+posted+'</p><p>'+item.text+'</p></li>';
						$('#rss ul').append(html);
					
				});
				
			
			// add scrollbars
				if(!$('#rss div').hasClass('loaded')) {
					
					// mark as loaded
						$('#rss div').addClass('loaded');
					
					// add scroll buttons
						$('#rss div').before('<img src="/img/arrow_up_left.gif" alt="" id="rssUp" />');
						$('#rss div').after('<img src="/img/arrow_down_left.gif" alt="" id="rssDown" />');
					
					// add scroll actions
						$('#rssUp').hover(function() {
							clearInterval(iv);
							iv = setInterval(function() {
								newY = parseFloat($('#rss ul').css('marginTop')) + scrollSpeed;
								if(newY > 0) newY = 0;
								$('#rss ul').css('marginTop', newY);
							}, 1);
						}, function() {
							clearInterval(iv);
						});
						$('#rssDown').hover(function() {
							clearInterval(iv);
							iv = setInterval(function() {
								newY = parseFloat($('#rss ul').css('marginTop')) - scrollSpeed;
								if(-newY > $('#rss ul').height() - $('#rss div').height()) newY = -($('#rss ul').height() - $('#rss div').height() - 1);
								$('#rss ul').css('marginTop', newY);
							}, 1);
						}, function() {
							clearInterval(iv);
						});
					
				}
			
		}
	}

	

/* RETURNS AN OBJECT WITH FEED DATA */
	
	function getFeedData(item) {
			var ns = "http://search.yahoo.com/mrss/";
		
		// fetch data
			var date = new Date($(item).find('pubDate').text());
			var title = "test";
			var title = $(item).find('title').filter(function() { return this.namespaceURI != ns; }).text();
			var text = $(item).find('description').text();
			var link = $(item).find('guid').text();
			
		// create object
			return {
					'date': date,
					'title': title,
					'text': text,
					'link': link
					};
		
	}



