$(document).ready(function(){ 
	
	$("ul.tnav").supersubs({ minWidth: 12, maxWidth: 27, extraWidth: 1 }).superfish({delay: 500});
	$(".hideSearch").click(HideForm); 
	$('.section-cat').accordion({header: "h3"});
	$('.event').tooltip({ track: true, delay: 0, showURL: false, showBody: " - ", fade: 250});
	$('.gimg').tooltip({ track: true, delay: 0, showURL: false, showBody: " - ", fade: 250, positionLeft: true});
	
	$('#gnext').click( function () { $("#guides").scrollTo('+=180px',250); });
	$('#gprev').click( function () { $("#guides").scrollTo('-=180px',250); });
	
	$('#anext').click( function () { $("#attract").scrollTo('+=180px',250); });
	$('#aprev').click( function () { $("#attract").scrollTo('-=180px',250); });
	
	$('#pnext').click( function () { $("#parks").scrollTo('+=180px',250); });
	$('#pprev').click( function () { $("#parks").scrollTo('-=180px',250); });

	$("#tabs").tabs({ cache: true, cookie: { expires: 7 } });
	
	$('.refine').click(function() {
		var origText = $.cookie('searchString');
		var refineText = $(this).attr('title');
		var finalText = origText + " " + refineText;
		$(".gsc-input").val(finalText);
		searchControl.execute(finalText);
		$.cookie('searchString', finalText);				
	});
	
	$(".clearcopied").click(function () { $("#saved_results").html(''); }); 
	
});

google.load("search", "1", {"nocss" : true});

// Set Globals
var searchControl;
var searchForm;
var siteRestrict = '012225295600887766229:7qpd3zuqtiw';

function OnLoad() {
// Create a search control
searchControl = new google.search.SearchControl();

//create search form
searchForm = new google.search.SearchForm(true, document.getElementById("search-form"));
searchForm.setOnSubmitCallback(null, CaptureForm);
searchForm.setOnClearCallback(null, HideForm);

searchForm.input.focus();


// create searchers with options

var nmrSearch = new google.search.WebSearch();
nmrSearch.setSiteRestriction('012225295600887766229:7qpd3zuqtiw');
nmrSearch.setUserDefinedLabel("Site Search");

var pnrSearch = new google.search.BlogSearch();
pnrSearch.setSiteRestriction("petoskeynews.com");
pnrSearch.setResultOrder(google.search.Search.ORDER_BY_DATE);
pnrSearch.setUserDefinedLabel("PNR");

var guideSearch = new google.search.WebSearch();
guideSearch.setUserDefinedLabel("Guides");
guideSearch.setSiteRestriction("miseasons.com");

var graSearch = new google.search.WebSearch();
graSearch.setUserDefinedLabel("The Graphic");
graSearch.setSiteRestriction("thegraphicweekly.com");

var pgSearch = new google.search.WebSearch();
pgSearch.setUserDefinedLabel("PhoneGuide");
pgSearch.setSiteRestriction("thephoneguide.com");

var newsSearch = new google.search.NewsSearch();
newsSearch.setSiteRestriction('012225295600887766229:7qpd3zuqtiw');

var imageSearch = new google.search.ImageSearch();
imageSearch.setSiteRestriction('012225295600887766229:7qpd3zuqtiw');	

var localSearch = new google.search.LocalSearch();
localSearch.setCenterPoint("Petoskey, MI");
localSearch.setUserDefinedLabel("Local");

// expanded result set
searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
searchControl.setNoResultsString(google.search.SearchControl.NO_RESULTS_DEFAULT_STRING);

// establish a keep callback
searchControl.setOnKeepCallback(this, KeepResults);

// move items to sidebar
//var sidebar = new google.search.SearcherOptions();
//sidebar.setRoot(document.getElementById("search-sidebar"));
	
// add searchers to search control
searchControl.addSearcher(nmrSearch);
searchControl.addSearcher(guideSearch);
searchControl.addSearcher(graSearch);
searchControl.addSearcher(pgSearch);
searchControl.addSearcher(newsSearch);	
searchControl.addSearcher(localSearch);
searchControl.addSearcher(imageSearch);
searchControl.addSearcher(pnrSearch);


// move search form, tabbed results
var drawOptions = new google.search.DrawOptions();
//drawOptions.setSearchFormRoot(document.getElementById("search-form"));
drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
                                                                                                                                                                                                                                            
//draw search control, execute search
searchControl.draw(document.getElementById("search-results"), drawOptions);
//searchControl.execute("Petoskey");

}

// Cancel the form submission, executing an AJAX Search API search.
function CaptureForm(searchSubmit) {
	if (searchSubmit.input.value) {
		searchControl.execute(searchSubmit.input.value);
		$("#search-string").html(searchSubmit.input.value);
		$.cookie('searchString', searchSubmit.input.value);
	  }
	if ($("#Main-Search").is(":hidden")) {
		$("#Main").fadeOut("normal",function() { 
		  $("#Main-Search").fadeIn("normal");
		});
	  }
	return false;
}

function HideForm() {
	$("#Main-Search").fadeOut("normal",function() { 
		searchControl.clearAllResults();
		$("#Main").fadeIn("normal");
	});
	//$("#Main").fadeIn("normal");
	$(".gsc-input").val("");
	$.cookie('searchString', '', { expires: -1 });
	return false;
}

function KeepResults(result) {
  // clone the result html node
  var node = result.html.cloneNode(true);

  // attach it
  var savedResults = document.getElementById("saved_results");
  savedResults.appendChild(node);
}


google.setOnLoadCallback(OnLoad);