/* vim: set enc=utf8 ts=4 fdm=marker: */
/***************************************************************
 * SurfThis - The Gear for Websurfing
 * COPYRIGHT (C) 2007 MADDY PROJECT ALL RIGHTS RESERVED.
 * $Id: surfthis.js 300 2010-02-28 04:02:19Z  $
 ***************************************************************/

var g_debug = false;
var g_msie7 = (navigator.userAgent.indexOf('MSIE 7') >= 0);
var g_history_nosup = $.browser.msie ? !g_msie7 : false;

jQuery.extend({
	// namespace SurfThis {{{
	SurfThis:
	{
		// namespace Mode {{{
		Mode:
		{
			Home:			'home',
			WebSearch:		'web',
			BlogSearch:		'blog',
			DictSearch:		'dict',
			ImageSearch:	'image_',
			GourmetSearch:	'gourmet',
			VideoSearch:	'video',
			RecipeSearch:	'recipe',
			AmazonSearch:	'amazon',
			WordSearch:		'word',
			NewsSearch:		'news'
		},
		// }}}

		// namespace DOM {{{
		DOM:
		{
			// string appendClass(DOMElement node, string name) {{{
			appendClass: function(node, name)
			{
				if($.browser.msie)
					node.className = 'selected';
				else
					node.setAttribute('class', 'selected');
			},
			// }}}
			// string removeClass(DOMElement id, string name) {{{
			removeClass: function(node)
			{
				if($.browser.msie)
					node.className = '';
				else
					node.removeAttribute('class');
			}
			// }}}
		},
		// }}}

		// namespace Ajax {{{
		Ajax:
		{
			// void request(DOMDocument dest, string url) {{{
			request: function(dest, url)
			{
				dest.empty();
				dest.prepend('<img class="loading" src="img/loading.gif" alt="" />&nbsp;<img class="loading" src="img/loading_text.gif" alt="Now Loading" />');
				dest.load(encodeURI(url));
			},
			// }}}
			// void load(DOMDocument dest, string mode) {{{
			load: function(dest, mode)
			{
				var url = jQuery.SurfThis.URL.create(mode);
				if($.browser.msie)
					jQuery.SurfThis.Ajax.request(dest, url);
				else
				{
					dest.hide('normal', function(){
							jQuery.SurfThis.Ajax.request(dest, url);
							dest.show('slow');
						});
				}

				urchinTracker(url);
			},
			// }}}
			// void search(string word[, int start[, int count]]) {{{
			search: function(word, start, count)
			{
				// prepare forms
				var curword = jQuery.SurfThis.Form._word();
				if(!word) word = curword;
				jQuery.SurfThis.Form.set(word, start, count);
				jQuery.SurfThis.Form.hide();
				// search related words
				if(word != curword)
				{
					$('#SearchWord').text(word);
					$('#WordListTitle').text('関連キーワード');
					jQuery.SurfThis.Ajax.load($('#WordList'), jQuery.SurfThis.Mode.WordSearch);
					jQuery.SurfThis.Ajax.load($('#Amazon'), jQuery.SurfThis.Mode.AmazonSearch);
				}
				// search sites
				var mode = jQuery.SurfThis.Tab.get();
				if(mode && mode != jQuery.SurfThis.Mode.Home)
				{
					jQuery.SurfThis.Tab.select(mode);
					jQuery.SurfThis.Ajax.load($('#SiteList'), mode);
				}
			}
			// }}}
		},
		// }}}

		// namespace Form {{{
		Form:
		{
			// void onSubmit() {{{
			onSubmit: function ()
			{
				jQuery.SurfThis.Form.hide();
			//	var word = jQuery.SurfThis.Form._word();
				var word = jQuery.SurfThis.Form._word().replace('&', '＆').replace('\|', '｜');
				jQuery.SurfThis.Form._word('');
				search(word, jQuery.SurfThis.Form._start(), jQuery.SurfThis.Form._count());
				return false;
			},
			// }}}
			// void show() {{{
			show: function()
			{
				if(!g_debug)
				{
					$('#SearchWord').hide();
					$('#SearchForm').show();
				}
				document.searchform.word.focus();
			},
			// }}}
			// void hide() {{{
			hide: function()
			{
				if(!g_debug)
				{
					$('#SearchForm').hide();
					$('#SearchWord').show();
				}
			},
			// }}}
			// string _word([string word]) {{{
			_word: function(word)
			{
				return $('input[@name=word]').val(word);
			},
			// }}}
			// int _start([int start]) {{{
			_start: function(start)
			{
				return $('input[@name=start]').val(start);
			},
			// }}}
			// int _count([int count]) {{{
			_count: function(count)
			{
				return $('select[@name=count]/option[@selected]').val(count);
			},
			// }}}
			// void set([string word[, int start[, int count]]]) {{{
			set: function(word, start, count)
			{
				jQuery.SurfThis.Form._word(word ? word : '');
				jQuery.SurfThis.Form._start(start ? start : 1);
				jQuery.SurfThis.Form._count(count ? count : 10);
			},
			// }}}
			// array get() {{{
			get: function()
			{
				return {
					word:	jQuery.SurfThis.Form._word(),
					start:	jQuery.SurfThis.Form._start(),
					count:	jQuery.SurfThis.Form._count()};
			}
			// }}}
		},
		// }}}

		// namespace Tab {{{
		Tab:
		{
			// void select(string id) {{{
			select: function(id)
			{
				var nodes = $('#SiteTabSet').children();
				$.each(nodes, function(idx, node){
					if(node.getAttribute('id') == id)
						jQuery.SurfThis.DOM.appendClass(node, 'selected');
					else
						jQuery.SurfThis.DOM.removeClass(node);
				});
			},
			// }}}
			// string get() {{{
			get: function()
			{
				var nodes = $('#SiteTabSet').children();
				var id = '';
				$.each(nodes, function(idx, node){
					if($.browser.msie)
					{
						if(node.className == 'selected')
							id = node.id;
					}
					else
					{
						if(node.getAttribute('class') == 'selected')
							id = node.getAttribute('id');
					}
				});
				return id;
			}
			// }}}
		},
		// }}}

		// namespace History {{{
		History:
		{
			// void push(string mode, string word[, int start[, int count]]) {{{
			push: function(mode, word, start, count)
			{
				if(!start) start = 1;
				if(!count) count = 10;
				$.historyLoad(jQuery.SurfThis.Anchor.create(mode, word, start, count));
			}
			// }}}
		},
		// }}}

		// namespace Anchor {{{
		Anchor:
		{
			_Busy: false,

			// void onChange(string anchor) {{{
			onChange: function(anchor)
			{
				if(!jQuery.SurfThis.Anchor._Busy)
				{
					jQuery.SurfThis.Anchor._Busy = true;
					if(anchor)
					{
						var query = jQuery.SurfThis.Anchor.parse();
						jQuery.SurfThis.Tab.select(query.mode);
						jQuery.SurfThis.Ajax.search(query.word, query.start, query.count);
					}
					else if(!jQuery.SurfThis.hasQuery() && jQuery.SurfThis.Form._word())
					{
						callHome();
					}
					jQuery.SurfThis.Anchor._Busy = false;
				}
			},
			// }}}
			// string create(string mode, string word, int start, int count) {{{
			create: function(mode, word, start, count)
			{
				return encodeURI(word + '|' + mode + '|' + start + '|' + count);
			},
			// }}}
			// array parse() {{{
			parse: function()
			{
				var query = decodeURI(location.hash).split('|');
				return {
					word:	query[0] ? query[0].substr(1) : '',
					mode:	query[1] ? query[1] : 'web',
					start:	query[2] ? query[2] : 1,
					count:	query[3] ? query[3] : 10};
			}
			// }}}
		},
		// }}}

		// namespace URL {{{
		URL:
		{
			// string create(string mode) {{{
			create: function(mode)
			{
				var word = jQuery.SurfThis.Form._word();
				return '?mode=' + mode + (word ? '&word=' + word : '') + '&start=' + jQuery.SurfThis.Form._start() + '&count=' + jQuery.SurfThis.Form._count();
			}
			// }}}
		},
		// }}}

		// namespace Ajax {{{
		Ajax:
		{
			// void request(DOMDocument dest, string url) {{{
			request: function(dest, url)
			{
				dest.empty();
				dest.prepend('<img class="loading" src="img/loading.gif" alt="" />&nbsp;<img class="loading" src="img/loading_text.gif" alt="Now Loading" />');
				dest.load(encodeURI(url));
			},
			// }}}
			// void load(DOMDocument dest, string mode) {{{
			load: function(dest, mode)
			{
				var url = jQuery.SurfThis.URL.create(mode);
				if($.browser.msie)
					jQuery.SurfThis.Ajax.request(dest, url);
				else
				{
					dest.hide('normal', function(){
							jQuery.SurfThis.Ajax.request(dest, url);
							dest.show('slow');
						});
					urchinTracker(url);
				}

				//urchinTracker(url);
			},
			// }}}
			// void search(string word[, int start[, int count]]) {{{
			search: function(word, start, count)
			{
				// prepare forms
				var curword = jQuery.SurfThis.Form._word();
				if(!word) word = curword;
				jQuery.SurfThis.Form.set(word, start, count);
				jQuery.SurfThis.Form.hide();
				// search related words
				if(word != curword)
				{
					$('#SearchWord').text(word);
					$('#WordListTitle').text('関連キーワード');
					jQuery.SurfThis.Ajax.load($('#WordList'), jQuery.SurfThis.Mode.WordSearch);
					jQuery.SurfThis.Ajax.load($('#Amazon'), jQuery.SurfThis.Mode.AmazonSearch);
				}
				// search sites
				var mode = jQuery.SurfThis.Tab.get();
				if(mode && mode != jQuery.SurfThis.Mode.Home)
				{
					jQuery.SurfThis.Tab.select(mode);
					jQuery.SurfThis.Ajax.load($('#SiteList'), mode);
				}
			}
			// }}}
		},
		// }}}

		// bool hasQuery() {{{
		hasQuery: function()
		{
			return(g_query && g_query.mode);
		},
		// }}}

		// void onLoad() {{{
		onLoad: function()
		{
			if(g_debug)
			{
				$('#SearchWord').show();
				$('#SearchForm').show();
			}
			document.searchform.onsubmit = jQuery.SurfThis.Form.onSubmit;
			$('#SearchWord').click(jQuery.SurfThis.Form.show);
			$('#SearchWord').empty();
			jQuery.SurfThis.Form.set('', 1, 10);
			if(jQuery.SurfThis.hasQuery())
			{
				jQuery.SurfThis.Tab.select(g_query.mode);
				jQuery.SurfThis.Ajax.search(g_query.word, g_query.start, g_query.count);
			}
			else
			{
				callHome();
				$('#PopularList').addClass('selected');
				$('#PopularTree').removeClass();
			}

			if(!g_history_nosup)
				$.historyInit(jQuery.SurfThis.Anchor.onChange);
		},
		// }}}

		// void callSearch(string mode) {{{
		callSearch: function(mode)
		{
			if(jQuery.SurfThis.Tab.get() != mode)
				jQuery.SurfThis.Form._start(1);
			jQuery.SurfThis.Tab.select(mode);
			var query = jQuery.SurfThis.Form.get();
			search(query.word, query.start, query.count);
		}
		// }}}
	}
// }}}
});

	// void callHome() {{{
	function callHome()
	{
		// popular words
		$('#WordListTitle').text('人気キーワード');
		jQuery.SurfThis.Form._word('');
		jQuery.SurfThis.Ajax.load($('#WordList'), jQuery.SurfThis.Mode.WordSearch);
	//	jQuery.SurfThis.Ajax.load($('#Amazon'), jQuery.SurfThis.Mode.AmazonSearch);
		// home contents
		jQuery.SurfThis.Tab.select(jQuery.SurfThis.Mode.Home);
		jQuery.SurfThis.Ajax.load($('#SiteList'), jQuery.SurfThis.Mode.Home);
		$('#SearchWord').empty();
		$('#SearchWord').append('<span class="attention">ここをクリックするとキーワードを入力できます</span>');
	}
	// }}}

	// void callSites() {{{
	function callSites()
	{
		jQuery.SurfThis.callSearch(jQuery.SurfThis.Mode.WebSearch);
	}
	// }}}

	// void callBlogs() {{{
	function callBlogs()
	{
		jQuery.SurfThis.callSearch(jQuery.SurfThis.Mode.BlogSearch);
	}
	// }}}

	// void callDicts() {{{
	function callDicts()
	{
		jQuery.SurfThis.callSearch(jQuery.SurfThis.Mode.DictSearch);
	}
	// }}}

	// void callImages() {{{
	function callImages()
	{
		jQuery.SurfThis.callSearch(jQuery.SurfThis.Mode.ImageSearch);
	}
	// }}}

	// void callGourmets() {{{
	function callGourmets()
	{
		jQuery.SurfThis.callSearch(jQuery.SurfThis.Mode.GourmetSearch);
	}
	// }}}

	// void callVideos() {{{
	function callVideos()
	{
		jQuery.SurfThis.callSearch(jQuery.SurfThis.Mode.VideoSearch);
	}
	// }}}

	// void callRecipes() {{{
	function callRecipes()
	{
		jQuery.SurfThis.callSearch(jQuery.SurfThis.Mode.RecipeSearch);
	}
	// }}}

	// void callNews() {{{
	function callNews()
	{
		jQuery.SurfThis.callSearch(jQuery.SurfThis.Mode.NewsSearch);
	}
	// }}}

	// void search(string word[, int start[, int count]]) {{{
	function search(word, start, count)
	{
		if(!start) start = 1;
		if(!count) count = 10;
		var mode = jQuery.SurfThis.Tab.get();
		if(!mode || mode == jQuery.SurfThis.Mode.Home)
			mode = jQuery.SurfThis.Mode.WebSearch;
		jQuery.SurfThis.Form.hide();
		if(g_history_nosup)
		{
			jQuery.SurfThis.Form._word('');
			jQuery.SurfThis.Tab.select(mode);
			jQuery.SurfThis.Ajax.search(word, start, count);
		}
		else
			jQuery.SurfThis.History.push(mode, word, start, count);
	}
	//}}}

	// void searchPairWords(string query[, int start[, int count]]) {{{
	function searchPairWords(query, start, count)
	{
		var curquery = {
			word:	jQuery.SurfThis.Form._word(),
			start:	jQuery.SurfThis.Form._start(),
			count:	jQuery.SurfThis.Form._count()};
	//	jQuery.SurfThis.Form.set(query, start, count);
		jQuery.SurfThis.Form.set('', start, count);
		search(query, start, count);
		jQuery.SurfThis.Form.set(query, curquery.start, curquery.count);
	}
	// }}}

	// void showPageLink() {{{
	function showPageLink()
	{
		var query = jQuery.SurfThis.Form.get();
		var mode = jQuery.SurfThis.Tab.get();
		location.href = jQuery.SurfThis.Anchor.create(mode, query.word, query.start, query.count);
	}
	// }}}


$(document).ready(jQuery.SurfThis.onLoad);

