
    google.load("search", "1");
    google.load("jquery", "1");
    var term = '';

    function OnLoad() {
    
      var term = $('#newsPanel > div').html();
      if (term && term.length > 0) {
    	$('#newsPanel').before('<h2>Google News search for: ' + term + '</h2>');
      	newsSearch = new google.search.NewsSearch();
      	//newsSearch.setResultOrder(google.search.Search.ORDER_BY_DATE);
      	//newsSearch.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS, {"topic" : "t"});
      	newsSearch.setSearchCompleteCallback(this, searchComplete, null);
      	newsSearch.execute('"' + term + '"');
      }

      var term = $('#imagePanel > div').html();
      if (term && term.length > 0) {
    	$('#imagePanel').before('<h2>Google Images search for: "' + term + '"</h2>'); 
      	imageSearch = new google.search.ImageSearch();
	    imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE, google.search.ImageSearch.IMAGESIZE_MEDIUM);
      	imageSearch.setSearchCompleteCallback(this, searchCompleteImage, null);
      	imageSearch.execute(term);
      }

      var term = $('#videoPanel > div').html();
      if (term && term.length > 0) {
    	$('#videoPanel').before('<h2>Google Video search for: "' + term + '"</h2>');
      	videoSearch = new google.search.VideoSearch();
      	videoSearch.setSearchCompleteCallback(this, searchCompleteVideo, null);
      	videoSearch.execute(term);
      }	
    }
    
    function searchComplete() {	
  	  if (newsSearch.results && newsSearch.results.length > 0) {
		$('#newsPanel').empty();
  		var results = newsSearch.results;
  	    for (var i = 0; i < results.length; i++) {
  	      // For each result write it's title and image to the screen
  	      var result = results[i];
  	      var imgContainer = document.createElement('div');
  	      var title = document.createElement('div');
  	      // We use titleNoFormatting so that no HTML tags are left in the title
  	      title.innerHTML = '<a href="' + result.unescapedUrl + '" rel="nofollow" target="_blank">' + result.titleNoFormatting + '</a>';
  	      // There is also a result.url property which has the escaped version
	  	    var newContent = document.createElement('div');
	  	    newContent.innerHTML = result.content;
	    	var pub = document.createElement('div');
		    pub.innerHTML = '<b>' + result.publisher + '</b> - ' + result.publishedDate.substr(0,17);
		    var lf=document.createElement('br')
	  	    imgContainer.appendChild(title);
	  	    imgContainer.appendChild(pub);
	  	    //imgContainer.appendChild(pubdt);
	  	    imgContainer.appendChild(newContent);
	  	    imgContainer.appendChild(lf);
	  	  $('#newsPanel').append(imgContainer);
  	    }
	  	 addPaginationLinks(newsSearch, 'newsSearch','newsPanel');
  	  }
    }
  	  
  	function searchCompleteImage() {
  	  if (imageSearch.results && imageSearch.results.length > 0) {
  		$('#imagePanel').empty(); 		
        var textToInsert = '';
        $.each(imageSearch.results, function(count, item) {
           textToInsert  += '<div class="imgs-google"><a href="' + item.unescapedUrl + '" rel="nofollow" style="background-color:transparent;" target="_blank"><img src="' + item.tbUrl + ' "/></a></div>';
       	});
	   	$('#imagePanel').append(textToInsert);
	   	$('#imagePanel').append('<div style="clear:both"></div>');
  	   	addPaginationLinks(imageSearch, 'imageSearch','imagePanel');
  	  }
    }

  	function addPaginationLinks(obj, objName, panelName) {
  	  // The cursor object has all things to do with pagination
  	  var cursor = obj.cursor;
  	  var curPage = cursor.currentPageIndex; // check what page the app is on
  	  var pagesDiv = document.createElement('div');
  	  pagesDiv.innerHTML = 'Pages: ';
		if ( $('#page' + panelName).length > 0 ) {
  			$('#page' + panelName).empty();
  		} else {	
  			$("<div>").attr('id', 'page' + panelName).attr('verticle-align','bottom').insertAfter('#' + panelName); 
  		}  
  	  for (var i = 0; i < cursor.pages.length; i++) {	  
    	    var page = cursor.pages[i];
    	    if (curPage == i) { // if we are on the curPage, then don't make a link
    	      var label = document.createTextNode(' ' + page.label + ' ');
    	      pagesDiv.appendChild(label);
    	    } else {
    	      // If we aren't on the current page, then we want a link to this page.
    	      // So we create a link that calls the gotoPage() method on the searcher.
    	      var link = document.createElement('a');
    	      link.href = 'javascript:' + objName + '.gotoPage('+i+');';
    	      link.innerHTML = page.label;
    	      link.style.marginRight = '2px';
    	      pagesDiv.appendChild(link);
    	      pagesDiv.style.margin = '20px';
    	      pagesDiv.style.textAlign = 'center';
    	    }
    	  }
  	  $('#page' + panelName).append(pagesDiv);
  	}

  	function searchCompleteVideo() {
    	  if (videoSearch.results && videoSearch.results.length > 0) {
   	  		$('#videoPanel').empty();
    	    var imgContainer1 = document.createElement('div');
    	    imgContainer1.id = 'Gvideo';
    	    var results = videoSearch.results;
    	    for (var i = 0; i < results.length; i++) {
    	      var result = results[i];
      	      imgContainer1.appendChild(result.html);     //title);
    	    }
    		$('#videoPanel').append(imgContainer1);
    		$(".gs-watermark").css("display","none");
    		$(".gs-image").css("background-color","transparent");
    		$(".gs-image").css("border-color","transparent");
    		$(".gs-snippet").css("height","auto");
    	  }
    	  addPaginationLinks(videoSearch, 'videoSearch','videoPanel');
    }
    	    
    google.setOnLoadCallback(OnLoad);

