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

$(document).ready(function () {
	$('#buscar').attr('autocomplete', 'off');
	
	/*
	____________________________________________ Buscador de google
	*/

	//Control
	var sc = new GSearchControl();

	//Engadir sitio
	var siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedClassSuffix("siteSearch");
	siteSearch.setSiteRestriction("wononom.com");
	//siteSearch.setQueryAddition('site:wononom.com');  
	
	sc.addSearcher(siteSearch);
	
	//Opcions
	var drawOptions = new GdrawOptions();
	drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
	drawOptions.setInput(document.getElementById("buscar"));

	sc.setNoResultsString(GSearchControl.NO_RESULTS_DEFAULT_STRING);
    sc.draw(document.getElementById("resultados"), drawOptions);
	sc.execute(document.getElementById("header-buscador").value);
	
	//Eventos
	
	$('#buscar').keydown(function(event) {
		if ($(this).val() != ""){
			$("#resultados").show();
		}
	});
	
	$("#buscar").focus(function() {
		if ($(this).val() != ""){
			$("#resultados").show();
		}

		$(document).one('click', function(event) {
			$("#resultados").hide();
		});
		
		$("#buscar").click(function(e){
			e.stopPropagation();
		});

		$(document).keyup(function(event){
			if (event.keyCode == 27) {
				$("#resultados").hide();
			}
		});
	});
});