/**

gsearch.js 1.1
Copyright 2011 CMONOS Co.Ltd.
License: MIT License 

URI: http://cmonos.jp/
Last Modified: 2008-12-01
Author: CMONOS Co.Ltd.

【使い方の例-1】
head 内で gsearch.css を読み込みます。
<link type="text/css" rel="stylesheet" href="gsearch.css">

検索フォームを表示したい位置で JavaScript を読み込みます。
<script src="https://www.google.com/jsapi?key=APIKEY" type="text/javascript"></script>
<script language="Javascript" type="text/javascript" src="gsearch.js" charset="UTF-8"></script>
<script language="Javascript" type="text/javascript">
//<![CDATA[
gsearch_label = 'このサイト';
gsearch_site = 'YOURSITE.CO.JP';
gsearch_box_enabled = true; // true=検索フォームを囲うボックスを追加する
gsearch_css_enabled = false; // true=gsearch.css を読み込まずに gsearch_box 用の CSS を適用したい場合使用します。
gsearch_box_title = 'サイト検索';
writeGSearch();		// サイト検索ボックスを作成します。
//]]>
</script>

【使い方の例-2】
head 内で gsearch.css を読み込みます。
<link type="text/css" rel="stylesheet" href="gsearch.css">

この JavaScript ファイルでパラメータを記述します。
var gsearch_label = 'このサイト';
var gsearch_site = 'YOURSITE.CO.JP';
var gsearch_box_enabled = true; // true=検索フォームを囲うボックスを追加する
var gsearch_css_enabled = false; // true=gsearch.css を読み込まずに gsearch_box 用の CSS を適用したい場合使用します。
var gsearch_box_title = 'サイト検索';

検索フォームを表示したい位置で JavaScript を読み込みます。
<script language="Javascript" type="text/javascript" src="gsearch.js" charset="UTF-8"></script>
<script language="Javascript" type="text/javascript">
//<![CDATA[
writeGSearch();		// サイト検索ボックスを作成します。
//]]>
</script>

*/

//	【パラメータ】
var gsearch_label = '';
var gsearch_site = '';
var gsearch_box_enabled = false;
var gsearch_css_enabled = false;
var gsearch_box_title = '';

if (typeof google == 'undefined') document.write('<script src="https://www.google.com/jsapi" type="text/javascript"></script>');

function writeGSearch () {
	if (!gsearch_box_enabled) gsearch_css_enabled = false;
	if (gsearch_site == '') gsearch_site = document.domain;
	if (gsearch_label == '') gsearch_label = gsearch_site;
	if (gsearch_box_title == '') gsearch_box_title = 'Site Search';
	_writeGSearchCSS();
	if (typeof google == 'undefined' || google == null) {
		_writeGSearchBoxForLegacyBrowser();
	} else {
		_writeGSearchBox();
		google.load("search", "1");
		google.setOnLoadCallback(_writeGSearch);
	}
}

function _writeGSearchBox () {
	if (gsearch_box_enabled) document.write('<div id="gsearchbox"><a id="gsearch-toggle-button" onclick="_toggleGSearchBox()" title="' + gsearch_box_title + '"><span>' + gsearch_box_title + '</span></a>');
	document.write('<div id="gsearchcontrol">Loading...</div>');
	if (gsearch_box_enabled) document.write('</div>');
}

function _writeGSearch() {
	var searchControl = new google.search.SearchControl();
	searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
	var searcher = new GwebSearch();
	if (gsearch_site.search("/") == -1) {
		searcher.setSiteRestriction(gsearch_site);
	} else {
		searcher.setQueryAddition('site:' + gsearch_site.match(/^[^\/]+/) + ' inurl:' + gsearch_site);
	}
	searcher.setUserDefinedLabel(gsearch_label);
	searchControl.addSearcher(searcher);
	searchControl.setNoResultsString(google.search.SearchControl.NO_RESULTS_DEFAULT_STRING);
	var drawOptions = new GdrawOptions();
	drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
	var searchBox = document.getElementById("gsearchcontrol");
	searchControl.draw(searchBox, drawOptions);
}

function _toggleGSearchBox () {
	var searchBox = document.getElementById("gsearchcontrol");
	searchBox.style.display = (searchBox.style.display != 'block') ? 'block' : 'none';
}

function _writeGSearchBoxForLegacyBrowser () {
	var document_charset = (typeof document.characterSet == 'undefined' || document.characterSet == null) ? (typeof document.charset == 'undefined') ? 'UTF-8' : document.charset : document.characterSet;
	document_charset = document_charset.replace('x-','');
	var site_domain = gsearch_site.match(/^[^\/]+/);
	if (gsearch_box_enabled) document.write('<div id="gsearchbox"><a id="gsearch-toggle-button" onclick="_toggleGSearchBox()" title="' + gsearch_box_title + '"><span>' + gsearch_box_title + '</span></a>');
	document.write('<div id="gsearchcontrol">');
	document.write('<form method="get" action="http://www.google.co.jp/search" id="gsearch-legacy">');
	document.write('<a href="http://www.google.co.jp/"><img src="http://www.google.co.jp/intl/ja/logos/Logo_25wht.gif" border="0" alt="Google" /></a>');
	document.write('<input type="text" name="q" size="16" maxlength="255" value="">');
	document.write('<input type="hidden" name="ie" value="' + document_charset + '">');
	document.write('<input type="hidden" name="oe" value="' + document_charset + '">');
	document.write('<input type="hidden" name="hl" value="ja">');
	document.write('<input type="submit" name="btnG" value="Google 検索">');
	if (gsearch_box_enabled) document.write(' <a onclick="_toggleGSearchBox()" title="閉じる" id="gsearch-closebox">[x]</a>');
	document.write('<input type="hidden" name="domains" value="' + site_domain + '">');
	document.write('<div id="gsearch-option">');
	if (gsearch_site.search("/") == -1) {
		document.write('<input type="radio" name="sitesearch" value=""> WWW を検索 ');
		document.write('<input type="radio" name="sitesearch" value="' + gsearch_site + '" checked="checked" /> ' + gsearch_site + ' を検索');
	} else {
		document.write('<input type="radio" name="hq" value=""> WWW を検索 ');
		document.write('<input type="radio" name="hq" value="site:' + site_domain + ' inurl:' + gsearch_site + '" checked="checked" /> ' + gsearch_site + ' を検索');
	}
	document.write('</div>');
	document.write('</form>');
	document.write('</div>');
	if (gsearch_box_enabled) document.write('</div>');
}

function _writeGSearchCSS () {
	if (!gsearch_css_enabled) return;
	document.write('<style type="text/css">');
	document.write('#gsearchbox {');
	document.write('  position: absolute;');
	document.write('  left: 0;');
	document.write('  top: 0;');
	document.write('  text-align: left;');
	document.write('}');
	document.write('#gsearch-toggle-button {');
	document.write('  display: block;');
	document.write('  margin: 0;');
	document.write('  width: 80px;');
	document.write('  height: 13px;');
	document.write('  text-align: center;');
	document.write('  color: #FFF;');
	document.write('  font-size: 10px;');
	document.write('  border: 1px #000 solid;');
	document.write('  background-color: #444;');
	document.write('}');
	document.write('#gsearchcontrol {');
	document.write('  border: 1px solid #000;');
	document.write('  width: 340px;');
	document.write('  color: #000;');
	document.write('  font-size: 0.8em;');
	document.write('  padding: 8px;');
	document.write('  background-color: #FFF;');
	document.write('  opacity: 0.9;');
	document.write('  -moz-opacity: 0.9;');
	document.write('  filter:alpha(opacity=90);');
	document.write('  display: none;');
	document.write('}');
	document.write('#gsearchcontrol div {');
	document.write('  margin-left: auto;');
	document.write('  margin-right: auto;');
	document.write('}');
	document.write('#gsearch-legacy {');
	document.write('  text-align: center;');
	document.write('}');
	document.write('a#gsearch-closebox {');
	document.write('  color: #999;');
	document.write('}');
	document.write('a#gsearch-closebox:hover {');
	document.write('  color: #666;');
	document.write('}');
	document.write('</style>');
}

