Loafer.Gajax = {

	execute: function () {
		Loafer.include("http://www.google.com/jsapi?key=ABQIAAAA38XHNE0zgTUBtvHIWdZEDhSQnwm-yElXFeMoYI3MRTzxQE4bPxRsUYJo8s8gyv9ZRAix3rpdfUN9pg&&callback=Loafer.Gajax.create");
	},

	create: function () {
		google.load("search", "1", {
			language: "ja_JP",
			nooldnames: true,
			callback: Loafer.Gajax.invoke
		});
	},

	invoke: function () {

		var control = new google.search.SearchControl();

		control.setResultSetSize(google.search.Search.LARGE_RESULTSET);
		control.setLinkTarget(google.search.Search.LINK_TARGET_SELF);

		var search;
		var options;

		// blog specific search
		if (document.getElementById("fireball-blog-url").value) {

			search = new google.search.WebSearch();
			search.setUserDefinedLabel(document.getElementById("fireball-blog-title").value);
			search.setUserDefinedClassSuffix("internal");
			search.setQueryAddition("site:" + document.getElementById("fireball-blog-url").value);
			search.setSiteRestriction("006367419109540010558:xzw7drm16is");

			options = new google.search.SearcherOptions();
			options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
			control.addSearcher(search, options);

		}

		// fireball search
		search = new google.search.WebSearch();
		search.setUserDefinedLabel("Fireball Blogs");
		search.setSiteRestriction("006367419109540010558:xzw7drm16is");

		options = new google.search.SearcherOptions();
		options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
		control.addSearcher(search, options);

		// web search
		search = new google.search.WebSearch();
		search.setUserDefinedLabel("Web");
		options = new google.search.SearcherOptions();
		options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
		control.addSearcher(search, options);

		// blog search
		search = new google.search.BlogSearch();
		search.setUserDefinedLabel("Blog");
		options = new google.search.SearcherOptions();
		options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
		control.addSearcher(search, options);

		// news search
		search = new google.search.NewsSearch();
		search.setUserDefinedLabel("News");
		options = new google.search.SearcherOptions();
		options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
		control.addSearcher(search, options);

		var q = encodeURIComponent(document.getElementById("fireball-query").value)
				.replace(/&/g,"&amp;")
				.replace(/"/g,"&quot;")
				.replace(/</g,"&lt;")
				.replace(/>/g,"&gt;");

		control.setNoResultsString('見つかりませんでした。<a href="http://www.google.co.jp/search?hl=ja&amp;ie=UTF-8&amp;q=' + q + '">[Google で検索]</a>');

		var options = new google.search.DrawOptions();
		options.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
		control.draw(document.getElementById("searchControl"), options);

		control.execute(document.getElementById("fireball-query").value);

	} // ,
};

Loafer.load(Loafer.Gajax.execute);

