//Where should we pull from?


	

	function assignListeners() {	
	
				setTimeout('sorteverything()',3000);
				setTimeout('whatsnewsort()',3000);
				numbers = new Array();
				others = new Array();	

	
	
	}
	
	
	//Twitter Gets the 10 most recent tweets (controlled by loop). Replace q= with the id of Breakthrough when twitter is created. http://dev.twitter.com/console
	function twitter() {
		$.getJSON('/wp-content/themes/breakthrough-wp-theme/proxy.php?url=http://search.twitter.com/search.json?q=from:BreakthroughNY&amp;rpp=10', function(data) {
			  
			  var size = data.results.length;
			  for (var i = 0; i < size; i++) {
				  var message = data.results[i].text;
				  if (message.length>64){
						var message = message.substr(0,64)+'...';	
					}
				  var time = data.results[i].created_at;
				  var link = '<a href="http://twitter.com/BreakthroughNY" target="_blank">(Read More)</a>';
				  var title = "Twitter";
				  var colorclass = "news-lightblue";
					  //Time Format:Wed, 25 Aug 2010 14:19:27 +0000
					  var timesplit = time.split(' ');
					  var day = timesplit[1];
					  var month = timesplit[2];
					  var year = timesplit[3];
					  var hour = timesplit[4];
					  var time = year+' '+month+' '+day+' '+hour;
					  
					  
					  // Posted at: 12:57:43 on 4/24/2010
					  
					  addarray(year, month, day, hour, message, title, colorclass, link);
					  
					
				  } //for loop
							});
		
	};
	
	
	
	//Facebook Function. This pulls the date and message from the JSON file returned. The link below includes the ID of the page. Messages can be limited in the loop
	function fb() {
		$.getJSON('/wp-content/themes/breakthrough-wp-theme/proxy.php?url=https://graph.facebook.com/66684772698/posts', function(data) {
			  
			  var size = data.data.length;
			  for (var i = 0; i < size; i++) {
				  var message = data.data[i].message;
				  	if (message != null){
						if (message.length>64){
							var message = message.substr(0,64)+'...';	
							
						}
					}
				  var time = data.data[i].created_time;
				  var title = "Facebook";
				  var link = '<a href="http://facebook.com/pages/Breakthrough-New-York/66684772698" target="_blank">(Read More)</a>';
				  var colorclass = "news-blue";
				  	//Time Format:2010-08-09T16:50:57+0000
					var timesplit = time.split('T');
					var date = timesplit[0];
					var time = timesplit[1];
					var datesplit = date.split('-');
					var year = datesplit[0];
					var month = datesplit[1];
					var day = datesplit[2];
					var timesplit2 = timesplit[1].split('+');
					var hour = timesplit2[0];
					var time = year+' '+month+' '+day+' '+hour;
					
				  addarray(year, month, day, hour, message, title, colorclass, link);
				  
					  if (message=undefined) {
						  message = "*";
					  }
				  			 
				  } //for loop
				});
		
	};
	
	//Take a wordpress RSS feed and display it the same way as the above API files
	function rss(){
		$.get('/wp-content/themes/breakthrough-wp-theme/proxy.php?url=http://breakthrough.catch24design.com/feed/', function(data) {
				
				//find each 'item' in the file and parse it
				$(data).find('item').each(function() {
					var $item = $(this);
					var title = $item.find('title').text();
					var colorclass = "news-purple";
					var links = $item.find('link').text();
					var link ='<a href="'+links+'">(Read More)</a>';
					var message = $item.find('description').text();
					if (message.length>64){
						var message = message.substr(0,64)+'...';	
					}
					var time = $item.find('pubDate').text();
						//Time Format:Fri, 13 Aug 2010 15:02:56 +0000
							var timesplit = time.split(' ');
							var day = timesplit[1];
							var month = timesplit[2];
							var year = timesplit[3];
							var hour = timesplit[4];
							var time = year+' '+month+' '+day+' '+hour;
					
					addarray(year, month, day, hour, message, title, colorclass, link);				

				});
								
			});

		
	};
	
	
	//Read in items... make sure month is not spelt out. Should be able to format everything in reverse order (time.day,month,year) seperated by ":" and fed into sort function. 
	function addarray(year,month,day,hour,message,title, colorclass, link){
		
		if(isNaN(month)){
			if (month == 'Jan'){month='01';}
			else if (month == 'Feb'){month='02';}
			else if (month == 'Mar'){month='03';}
			else if (month == 'Apr'){month='04';}
			else if (month == 'May'){month='05';}
			else if (month == 'Jun'){month='06';}
			else if (month == 'Jul'){month='07';}
			else if (month == 'Aug'){month='08';}
			else if (month == 'Sep'){month='09';}
			else if (month == 'Oct'){month='10';}
			else if (month == 'Nov'){month='11';}
			else if (month == 'Dec'){month='12';}
		}
		
		//var sortvar = hour+":"+day+":"+month+":"+year+" Message= "+message+"<br />";
		var prettydate = "Posted on "+month+"/"+day+"/"+year;
		var sortvar = year+":"+month+":"+day+":"+hour+"?%"+prettydate+"?%"+message+"?%"+title+"?%"+colorclass+"?%"+link; 
		
		if(title != "Facebook") {
			numbers.push(sortvar);
		}
	
			others.push(sortvar);

	}
	
	
	
	
	function sorteverything (){
		
		$('#inside-news').empty();
		$('#first-news').empty();
		//console.log("sorted");
		
		sortednumbers = numbers.sort();
		//console.log(sortednumbers);
		
		sortednumbers.reverse();
		
		for (var i = 0; i < 1; i++) {
			
			var finalhtml = "<div id=\"first-news\" class=\"news-item "+sortednumbers[i].split('?%')[4]+"\"><h3>"+sortednumbers[i].split('?%')[3]+"<\/h3>";
				finalhtml += "<p>"+sortednumbers[i].split('?%')[2];
				finalhtml += "<\/p><p><em class=\"date\">"+" " + sortednumbers[i].split('?%')[1] +"<\/em> "+sortednumbers[i].split('?%')[5]+ "<\/p><\/div>";
			$('#first-news').append(finalhtml);
		
		};
		
		for (var i = 1; i < 5; i++) {			
			
			var finalhtml = "<div class=\"news-item "+sortednumbers[i].split('?%')[4]+"\"><h3>"+sortednumbers[i].split('?%')[3]+"<\/h3>";
				finalhtml += "<p>"+sortednumbers[i].split('?%')[2];
				finalhtml += "<\/p><p><em class=\"date\">"+" " + sortednumbers[i].split('?%')[1] +"<\/em> "+sortednumbers[i].split('?%')[5]+ "<\/p><\/div>";
			$('#inside-news').append(finalhtml);
		
		};
	}
	
	function whatsnewsort (){
		
		facebook = new Array();
		twitter = new Array();
		
		$('#facebookpanel').empty();
		$('#twitterpanel').empty();
		
		sortednumbers = others.sort();
		sortednumbers.reverse();
		
		for(var i=0; i < sortednumbers.length; i++){
			if 	(sortednumbers[i].split('?%')[3] == "Facebook")
				{
					facebook.push(sortednumbers[i]);	
				}
			if (sortednumbers[i].split('?%')[3] == "Twitter")
				{	
					twitter.push(sortednumbers[i]);
				}
		
		}
		
		if (facebook.length < 5)
			flength=facebook.length;
		else
			flength = 5;
		
		if (twitter.length < 5)
			tlength = twitter.length;
		else
			tlength = 5;
		
		for (var i = 0; i < flength; i++) {
			
			
			var finalhtml = "<div class='whatsnewdiv'><p>"+facebook[i].split('?%')[2];
				finalhtml += "<\/p><p><span><em class=\"date\">"+" " + facebook[i].split('?%')[1] +"<\/em> "+facebook[i].split('?%')[5]+ "<\/p><\/span><\/div>";
			$('#facebookpanel').append(finalhtml);
		
		};
		
		for (var i = 0; i < tlength; i++) {
			
			
			var finalhtml = "<div class='whatsnewdiv'><p>"+twitter[i].split('?%')[2];
				finalhtml += "<\/p><p><span><em class=\"date\">"+" " + twitter[i].split('?%')[1] +"<\/em> "+twitter[i].split('?%')[5]+ "<\/p><\/span><\/div>";
			$('#twitterpanel').append(finalhtml);
		
		};
		
	}
