/*--------------------------------------------------------------------*/

Event.observe(window, 'load', init, false);

function init() {
	initToolTips();
}

/*--------------------------------------------------------------------*/

function toggleNav(divId) {
	Effect.toggle(divId,'slide',{duration:.5});
	return false;
}

/*--------------------------------------------------------------------*/

function initToolTips(){
	
	var tooltips = document.getElementsByClassName('tooltip');
	for(var i = 0; i < tooltips.length; i++) {
		Event.observe(tooltips[i], 'click',function(){
			(this.nextSibling && this.nextSibling.id == 'tooltip')
				? removeTitle(this.nextSibling)
				: showTitle(this);
		}.bind(tooltips[i]), false);
		/*
		Event.observe(tooltips[i], 'mouseout',function(){
			if (this.nextSibling && this.nextSibling.id == 'tooltip')
				removeTitle(this.nextSibling);
		}.bind(tooltips[i]), false);
		*/
		tooltips[i].onclick = function(){return false;}
	}
}

/*--------------------------------------------------------------------*/

function showTitle(element) {    
	if ($("tooltip")) removeTitle($("tooltip"));
	var titlenode = Builder.node('h1', element.title);
    var relnode = Builder.node('span', {id:'content'});
	relnode.innerHTML = element.rel;
    var title = Builder.node('span', {style:'display:none', id:'tooltip', onclick:'removeTitle (this)'}, [
		titlenode,
		relnode
	]);
	element.parentNode.insertBefore(title, element.nextSibling);
	Effect.Appear(title, {duration: 0.3});
}

function removeTitle(element) {
	Effect.Fade(element, {afterFinish:function() {Element.remove(element)},duration:0.3});
}

function buildsuggest(sugstr) {
	
	if (sugstr.length==3) return false;
	
	$('syncontent').innerHTML = "";
	
	var sugarray = sugstr.split("###");

	if (sugarray[0].length>0) {
		var synarray = sugarray[0].split("; ");
		new Insertion.Bottom('syncontent','您还可以尝试检索以下的词: ');
		for (i=0; i<synarray.length; i++) {	
			if (i>0) {
				new Insertion.Bottom('syncontent',', ');
			}
			var synnode = Builder.node('a',{href:"#", onclick:"filltable('tit','"+synarray[i]+"');return false"},synarray[i]);
			$('syncontent').appendChild(synnode);
		}
		new Insertion.Bottom('syncontent','<br>');
	}
	
	if (sugarray[1].length>0) {
		var ipcarray = sugarray[1].replace(/; $/g, "").split("; ");
		new Insertion.Bottom('syncontent','您还可以尝试检索以下的IPC分类: ');
		for (i=0; i<ipcarray.length; i++) {	
			if (i>0) {
				new Insertion.Bottom('syncontent',', ');
			}
			var synnode = Builder.node('a',{href:"#", onclick:"filltable('mip','"+ipcarray[i]+"');return false"},ipcarray[i]);
			$('syncontent').appendChild(synnode);
		}
		new Insertion.Bottom('syncontent','<br>');
	}
	
	if (sugarray[2].length>0) {
		var namearray = sugarray[2].replace(/; $/g, "").split("; ");
		new Insertion.Bottom('syncontent','您还可以尝试检索以下的机构: ');
		for (i=0; i<namearray.length; i++) {	
			if (i>0) {
				new Insertion.Bottom('syncontent',', ');
			}
			var synnode = Builder.node('a',{href:"#", onclick:"filltable('app','"+namearray[i]+"');return false"},namearray[i]);
			$('syncontent').appendChild(synnode);
		}
		new Insertion.Bottom('syncontent','<br>');
	}
	
	if (sugarray[3].length>0) {
		var phrasearray = sugarray[3].replace(/;$/g, "").split("; ");
		new Insertion.Bottom('syncontent','您还可以尝试检索以下的下位词: ');
		for (i=0; i<phrasearray.length; i++) {	
			if (i>0) {
				new Insertion.Bottom('syncontent',', ');
			}
			var synnode = Builder.node('a',{href:"#", onclick:"filltable('tit','"+phrasearray[i]+"');return false"},phrasearray[i]);
			$('syncontent').appendChild(synnode);
		}
		new Insertion.Bottom('syncontent','<br>');
	}
	
	return false;
}


function sendRequest()
{
	if ($('smartseed').value.length<2) {
		return false;
	}
	
	if (navigator.userAgent.toLowerCase().indexOf('compatible')<0) {
    	netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	}

	//alert('word='+encodeURIComponent($('smartseed').value));
	_timeout=setTimeout(function(){
		new Ajax.Request(
			'suggest.html',
			{
				method:'get',
				parameters:'word='+encodeURIComponent($('smartseed').value.replace(/\s*/g, "")),
				onComplete:callbackOnComplete
			}
		);
	},1000);
	
	return false;
}

function callbackOnComplete(originalRequest)
{
	//alert(unescape(originalRequest.responseText));
	buildsuggest(originalRequest.responseText.replace(/(^\s*)|(\s*$)/g, ""));
	return false;
}

function filltable(elementid, query)
{
	if (query.indexOf(" ")>0) query = '"' + query + '"';
	$(elementid).value = query;
	return false;
}
