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




$(document).ready(function() {  


	$('#searchbox').addClass('googlebox');
	
	var togglebranding = function () {
		if ($('#searchbox').val().length < 1) {
			$('#searchbox').addClass('googlebox')
		}
		else
		{
			$('#searchbox').removeClass('googlebox')
		}
	}
	
	$('#searchbox').bind('focus', function(){ $('#searchbox').removeClass('googlebox') } );	
	$('#searchbox').bind('blur', togglebranding );
	togglebranding();

  var $popup = $('#google_search_popup');
  if($popup.length == 0){
    $popup = $('<div id="google_search_popup" title="Search Results"></div>').appendTo(document.body);
        }

        // Dialog			
        $popup.dialog({
            autoOpen: false,
            width: 700,
            position: 'top',
            resizable: false,
            draggable: true
        });

	var searchControl = new google.search.SearchControl();
	searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
	
	// web search, open, alternate root
	var options = new google.search.SearcherOptions();
	options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
	options.setRoot(document.getElementById("google_search_popup"));

	// Add in a WebSearch
	var webSearch = new google.search.WebSearch();
	
	// Restrict our search to pages from our CSE
	webSearch.setSiteRestriction('014821279820407580497:u0kbq-tqdqy');
	
	// Add the searcher to the SearchControl
	searchControl.addSearcher(webSearch, options);


	// Draw Options
	var drawOptions = new google.search.DrawOptions();
	drawOptions.setInput(document.getElementById("searchbox"))

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

	// execute an inital search
	//if (document.getElementById('searchbox').val().length < 1) {
	//	searchControl.execute(document.getElementById('searchbox').val());
	//}
	
   $('.gsc-search-button').bind('click', function(){
    // create a search control
	

	$('#google_search_popup').dialog('open');
	
   });
   
      $('#google_search').bind('click', function(){
    // create a search control
	
	//searchControl
	$('#google_search_popup').dialog('open');
	
   });
    
	// Glossary tooltips

	$('.glossarylink').hover(
		function() {
		this.tip = this.title;
		$(this).append('<div id="tooltip"></div>');
		$('#tooltip').load($(this).attr("href") + '?alttemplate=tooltip');
		this.title = "";
		this.width = $(this).width();
		$(this).find('#tooltip').css({left:this.width-22})
		$('#tooltip').fadeIn(300, function() { this.style.removeAttribute('filter'); });
	},
	function() {
		$('#tooltip').fadeOut(100);
		$(this).children().remove();
			this.title = this.tip;
		}
	);

});
