google.load("search", "1");

function OnLoad() {
  // Create a search control
  var searchControl = new google.search.SearchControl();

  // Add in a full set of searchers
	var a_search = new google.search.WebSearch();
	// point it to our CSE on google
	// http://www.google.com/coop/cse?cx=017308647820206462931%3Ayg0w1tfq3uo
	a_search.setSiteRestriction("017308647820206462931:yg0w1tfq3uo");
	a_search.setUserDefinedLabel('Asthma search');
  searchControl.addSearcher(a_search);
  // searchControl.addSearcher(new google.search.WebSearch());
  // searchControl.addSearcher(new google.search.VideoSearch());
  // searchControl.addSearcher(new google.search.BlogSearch());

  // Set the Local Search center point
  // localSearch.setCenterPoint("New York, NY");



	// create a drawOptions object
	var drawOptions = new google.search.DrawOptions();
	drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);

  // Tell the searcher to draw itself and tell it where to attach
  searchControl.draw(document.getElementById("searchcontrol"), drawOptions);

  // Execute an inital search
  searchControl.execute("inhalers");
}
google.setOnLoadCallback(OnLoad);


$(document).ready(function() {
	$.getJSON('http://pipes.yahoo.com/pipes/pipe.run?_id=uHKHx3Dw3BGtmVPUjtzu1g&_render=json&_callback=?',
		function(json){
			$.each(json.value.items, function(i, item) {
				var ti = '<a class="news-title" href="' + item.link + '">' + item.title + '</a>';
				var rm = '<a class="news-read-more" href="' + item.link + '"> Read more.</a>';
				// var short_des = item.description.split(/\s+/).slice(50).join(' ') + '...';
				// short_des += '<a class="news-read-more" href="' + item.link + '">  Read more.</a>';
				var des = '<p class="news-description">' + item.description + rm + '</p>';
				var content = ti + '<br />' + des;
				$('ul#news-list').append('<li>' + content +  '</li>');
			
			});
			$('ul#news-list li.ajax-loader').remove();
			$('ul#news-list').addClass('striped');
			$('ul#news-list.striped > li:even').addClass('striped');
	});
	$.getJSON('http://pipes.yahoo.com/pipes/pipe.run?_id=1c747e26898bb68149c940b06df7ecae&_render=json&_callback=?',
		function(json){
			$.each(json.value.items, function(i, item) {
				var ti = '<a class="article-title article-more-link" href="' + item.link + '">' + item.title + '</a>';
				var so = '<span class="article-source">' + item.source + ' &mdash; </span>';
				var au = '<span class="article-authors">' + item.author + '</span>';
				var des = '<p class="article-abstract hidden">' + item.description + '</span>';
				var full_text_link = '<a href="' + item.link + '">Full text</a>';
				var content = ti + '<p>' + so + au + '</p>' + des + '<br />' + full_text_link;
				$('ul#paper-list').append('<li>' + content +  '</li>');

			});
			$('a.article-more-link').siblings('p.article-abstract').hide();
			$('a.article-more-link').toggle(function() {
				$(this).siblings('p.article-abstract').each(function(index) {
					$(this).show('slow');
				});
			}, function() {
				$(this).siblings('p.article-abstract').each(function(index) {
					$(this).hide('slow');
				});
			});
			$('ul#paper-list li.ajax-loader').remove();
			$('ul#paper-list').addClass('striped');
			$('ul#paper-list.striped > li:odd').addClass('striped');
	});


	
});

//  These events take place on "load", which is later than "ready"
// $(window).bind("load",function(){
// 	GoogleOnLoad();
// });


