var Search = {
  init_cse : function(searchFieidId, gseKey) {
    google.load('search', '1', {language : 'en', style : google.loader.themes.ESPRESSO});
    google.setOnLoadCallback(function() {
			var customSearchControl = new google.search.CustomSearchControl(gseKey);
			customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
			customSearchControl.setNoResultsString("No results  found.")
			customSearchControl.setSearchCompleteCallback(null, 
			function() { 
			  setTimeout(function() {
			    Portal.alignHeights();
			  }, 500);
			});
	
			setTimeout(function() {
			  var drawOptions = new google.search.DrawOptions();
			  drawOptions.setInput(document.getElementById(searchFieidId));
			  customSearchControl.draw('cse', drawOptions);
			  
			  var href = location.href;
			  var index = href.indexOf('q=');
			  
			  if (index > -1) {

				var q = href.substring(index+2);
				
				if (q) {
				  q = q.replace(/%20/g, ' ');
				  customSearchControl.execute(q);
				}
			  }
			}, 1000);
			
	  }, 
	  true);
  },
  
  _doSearch : function(searchBox) {
    var searchField = searchBox.find('input');
	var val = searchField.val();
    if (searchBox.hasClass('business-search')) {
		 Search._internalDoSearch(searchField, 'https://hbe.ehawaii.gov/documents/search.html', {'beginsWith' : 'false', 'query' : val});
	 }
	 
	 if (searchBox.hasClass('legislature-search')) {
		 Search._internalDoSearch(searchField, '/search/legislature.html', {'q' : val});
	 }
	 
	 if (searchBox.hasClass('counties-search')) {
		 Search._internalDoSearch(searchField, '/search/county.html', {'q' : val});
	 }
  },
  
  _internalDoSearch : function(field, action, params) {
    var sb = field.parents('.search-box');
    if (field.attr('title') != val) {
	   var val = field.val();
	   
	   if (val) {
		var form = sb.find('form');
		
		if (form.length == 0) {
		  var f = Search._createForm(action, params);
		  sb.append(f);
		  form = sb.find('form');
		  form.submit();
		}
	  }
			
	}
  },
  
  initSearchBoxes : function() {
    $('.searches li > a').click(function() {
	  var $this = $(this);
	  var searchBox = $this.next();
	  
	  if (searchBox.is(':visible')) {
	    $('.searches .search-box').slideUp("fast");
	  } else {
		$('.searches .search-box').slideUp("fast");
		searchBox.slideDown("fast");
	  }
	  
	});
	
    $('.search-box').each(function() {
	   var sb = $(this);
	   var searchField = sb.find('input');
	   searchField.toggleVal({populateFrom: 'custom', text : searchField.attr('title')});
	   var sbButton = sb.find('button');
	   
	   sbButton.click(function() {
		 Search._doSearch(sb);
	   });
	   
	   searchField.keypress(function(event) {
		 if (event.which == 13) {
		   Search._doSearch(sb);
		 }
	   });
    });
    
    Search._attacheQTip($('.business-search').prev(), "Search for information on all businesses registered in Hawai'i.");
    Search._attacheQTip($('.legislature-search').prev(), "Search the Hawai'i legislature website for Hawai'i laws, legislation, bill statuses, and information about the House & Senate.");
    Search._attacheQTip($('.counties-search').prev(), "Search all county websites: Kaua'i, Maui, Hawai'i, and Honolulu.");
    
  },
  
  _createForm : function(action, params) {
    var form = '<form action="' + action + '" method="get" style="display:inline; padding: 0px;">';
	
	if (params) {
	  $.each(params, function(name, value) {
	    form += '<input type="hidden" name="' + name + '" value="' + value + '"/>\n';
	  });
	}
	form += '<input type="submit" style="display:none"/></form>';
    return form;
  },
  
  _attacheQTip : function(elem, tooltip) {
    elem.qtip({
		content : {
		  text: tooltip
		},
		position: {
		  target : 'mouse',
		  corner: {
			 target: 'bottomMiddle',
			 tooltip: 'bottomMiddle'
		  }
	   },
	   style: { 
			  width: 300,
			  tip: 'bottomMiddle',
			  border: {
				  width: 7,
				  radius: 5
			  }
		},
		
		show : {delay: 100, solo: true},
		hide : {fixed: true, delay : 100}
	});
  }
};
