
    var webSearch;
	function loadGoogleStuff(){

google.load('search', '1');
    
	var contentDiv = 'searchResults';
    function addPaginationLinks() {
      /* The cursor object has all things to do with pagination*/
      var cursor = webSearch.cursor;
      var curPage = cursor.currentPageIndex; /* check what page the app is on*/
      var pagesDiv = document.createElement('div');
      pagesDiv.className = 'gs-pages';
      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:webSearch.gotoPage('+i+');';
          link.innerHTML = page.label;
          link.style.marginRight = '2px';
          pagesDiv.appendChild(link);
        }
      }
    
      var contentDivEl = document.getElementById(contentDiv);
      contentDivEl.appendChild(pagesDiv);
    }
    
    function searchComplete() {
    /* Grab our content div, clear it.*/
    var contentDivEl = document.getElementById(contentDiv);
    contentDivEl.innerHTML = '';
    
      /* Check that we got results */
      if (webSearch.results && webSearch.results.length > 0) {
    	
		var resultTitle = document.createElement('div');
		resultTitle.innerHTML = webSearch.cursor.estimatedResultCount+' results for <b>'+lastQuery+'</b>';
		contentDivEl.appendChild(resultTitle);
		
		var hr = document.createElement('hr');
		contentDivEl.appendChild(hr);

		
        /* Loop through our results, printing them to the page. */
        var results = webSearch.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 resultContainer = document.createElement('div');
          resultContainer.className = 'gs-result';
    
          var title = document.createElement('a');
          /* We can use titleNoFormatting so that no HTML tags are left in the title */
          title.innerHTML = result.title;
          title.href = result.unescapedUrl;
    
          var content = document.createElement('div');
          content.innerHTML = result.content;
          content.className = 'gs-result-content';
    
          var url = document.createElement('div');
          url.innerHTML = result.unescapedUrl;
          url.className = 'gs-result-url';
    
          resultContainer.appendChild(title);
          resultContainer.appendChild(content);
          resultContainer.appendChild(url);
    
          /* Put our title + image in the content */
          contentDivEl.appendChild(resultContainer);
        }
    
		var hr = document.createElement('hr');
		contentDivEl.appendChild(hr);
		
        /* Now add the paging links so the user can see more results. */
        addPaginationLinks(webSearch);
      }else{
        var resultContainer = document.createElement('div');
        resultContainer.className = 'gs-result';
        
        resultContainer.innerHTML = 'No Results for <b>'+lastQuery+'</b>';

        contentDivEl.appendChild(resultContainer);
      }
    }
    
    function OnLoad() {
      
      /* Add in a WebSearch */
      webSearch = new google.search.WebSearch();

      /* Restrict our search to a custom search engine */
      webSearch.setSiteRestriction('016109463025896457844:7xh_-ltcso4');

      /* Set the Search Control to get the most number of results */
      webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
      webSearch.setSearchCompleteCallback(this, searchComplete, null);
    
    }
    
    google.setOnLoadCallback(OnLoad);
    

    /* The function called from the search box */
    siteSearch = function (query){
	
		lastQuery = query;
		/* Lightbox if there's not a div defined to put the content in */
		if($('#'+contentDiv).length==0){
			
			$.lightbox({
				height:500,
				width:800,
				content:'<div style="margin: 20px 0px 0px 0px;width:780px;height:480px;overflow:auto;padding:10px"><div id="searchResults" class="txt text copy" style="border:1px solid #e8e9e4;padding:10px;overflow:auto;height:430px;"></div></div>'
			});
			
		}
	
	
        var contentDivEl = document.getElementById(contentDiv);
        contentDivEl.innerHTML = '';
        var resultContainer = document.createElement('div');
        resultContainer.className = 'gs-result';
        resultContainer.innerHTML = 'Searching for <b>'+query+'</b> ...';
        contentDivEl.appendChild(resultContainer);
        
        webSearch.execute(query);
    }

}


/*
	Super simple lightbox, by Derek
	Goals
		1) No images or linked stylesheets required
		2) Can accept customization for styling as needed either through stylesheet or javascript
		3) Can accept just simple content and work okay
		4) Lightweight, small bit of code to include
*/

jQuery.lightbox = function(options){
	
	var c = '';
	if(typeof(options)=='string')c=options;
	
	var o = {
		height: '400',
		width: '600',
		content: c,
		modalcss: {
			background:'#000000',
			filter: 'alpha(opacity=30)',
			opacity: .3
		},
		wincss: {
			background: '#FFFFFF',
			border: '1px solid #000000',
			zIndex: '9999'
		},
		closeboxcss: {
			display: 'inline'
		}
	};
	$.extend(o,options);
	
	$(document.body).css('overflow','hidden');
	var removeIt = function(){
		$(modal).remove();
		$(win).remove();
		$(window).unbind('resize scroll',moveIt);
		$(document.body).css('overflow','');
	};
	var modal = document.createElement('div');
	$(modal).css($.extend(o.modalcss,{
		position:	'absolute',
		top: $(document).scrollTop()+'px',
		left: $(document).scrollLeft()+'px',
		height: $(window).height()+'px',
		width: $(window).width()+'px'
	})).click(removeIt).appendTo(document.body);
	
	var closebox = document.createElement('div');
	$(closebox).css($.extend(o.closeboxcss,{
		position: 'absolute',
		cursor: 'pointer',
		top: '0px',
		left:  (o.width-90)+'px'
	})).html('<div class="gs-closebox"></div>').click(removeIt);
	
	var win = document.createElement('div');
	$(win).css($.extend(o.wincss,{
		position: 'absolute',
		top: ($(document).scrollTop()+($(window).height()-o.height)/2)+'px',
		left: ($(document).scrollLeft()+($(window).width()-o.width)/2)+'px',
		height: '38px',
		width: '0px'
	})).html(o.content).prepend(closebox).appendTo(document.body).animate({
		width: o.width+'px'
	},500,'linear',function(){
		$(this).animate({
			height: o.height+'px'
		},500,'linear',function(){
			$(window).bind('resize scroll',moveIt);
		});
	});
	var moveIt = function(){
		$(win).css({
			top: ($(document).scrollTop()+($(this).height()-o.height)/2)+'px',
			left: ($(document).scrollLeft()+($(this).width()-o.width)/2)+'px'
		});
		$(modal).css({
			top: $(document).scrollTop()+'px',
			left: $(document).scrollLeft()+'px',
			height: $(this).height()+'px',
			width: $(this).width()+'px'
		});
	};
};
