var timeoutInt = -1;

function html_entity_decode(str) {
	var ta = document.createElement("textarea");
		ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	toReturn = ta.value;
		ta = null;
	return toReturn
}

function pageSearch(type, keywords, el) {
		
	return false;

	var cont = document.getElementById('pagesearch');
		
	cont.setAttribute('lastSearch', keywords);
		
	if(keywords == '' || keywords == ' Search ') {
		cont.style.display = 'none';
		return;
	}

	var params = new Array();
		params['type'] = type.toLowerCase();
		params['input'] = keywords;
	doRequest('http://www.collectionstation.com/tools/search', function(res) {
		if(!res.responseXML) {
			alert(res.responseText);
			return;
		}
		if(params['input'] != cont.getAttribute('lastSearch'))
			return;
		//searchMouseOut();
		var doc = res.responseXML.documentElement;
		var results = doc.getElementsByTagName('result');

		var ul = document.createElement('UL');
		cont.innerHTML = '';
		var h1 = document.createElement('strong');
			h1.appendChild(document.createTextNode(type));
			cont.appendChild(h1);
			cont.appendChild(document.createElement('BR'));
		if(results.length > 0) {
			
			var len = results.length;
			if(len > 20)
				len = 20;
			
			for(var i = 0; i < len; i++) {
				var li = document.createElement('LI');				
				var a = document.createElement('A');
					a.href = results[i].getAttribute('url');

					a.setAttribute('name', html_entity_decode(results[i].getAttribute('name')));
					/*a.onclick = function(){
						el.value = this.getAttribute('name');
						cont.style.display = 'none';
					};*/
					//a.onmouseout = searchMouseOut;
					a.appendChild(document.createTextNode(html_entity_decode(results[i].getAttribute('name'))));
				li.appendChild(a);
				ul.appendChild(li);
			}
			if(results.length > 20) {
				var li = document.createElement('LI');
				var a = document.createElement('A');
					a.href = 'http://www.collectionstation.com/search/'+type.toLowerCase()+'/keywords/'+keywords;
					a.appendChild(document.createTextNode('More results'));
					li.appendChild(a);
				ul.appendChild(li);
			}
		} else {
			var li = document.createElement('LI');
				li.appendChild(document.createTextNode('No results for \''+doc.getAttribute('keywords')+'\'!'));
			ul.appendChild(li);
		}
		cont.appendChild(ul);


		var top = (el.offsetTop + el.offsetHeight);
		var left = el.offsetLeft;

		if(el.offsetParent) {
			left = 0;
			var tmpel = el;
			do {
				left += tmpel.offsetLeft;
			} while(tmpel = tmpel.offsetParent);
		}

		/*cont.style.top = top + 'px';
		cont.style.left = left + 'px';
		cont.style.width = el.offsetWidth + 'px';*/


		/*cont.style.top = (el.offsetTop + el.offsetHeight) + 'px';
		cont.style.left = el.offsetLeft + 'px';
		cont.style.width = el.offsetWidth + 'px';*/		

		var top = (el.offsetTop + el.offsetHeight);
		var left = el.offsetLeft;

		if(el.offsetParent) {
			left = 0;
			top = 0;
			var tmpel = el;
			do {
				left += tmpel.offsetLeft;
				top += tmpel.offsetTop;
			} while(tmpel = tmpel.offsetParent);
		}

		cont.style.top = (top + el.offsetHeight) + 'px';
		cont.style.left = left + 'px';
		cont.style.width = el.offsetWidth + 'px';

		/*cont.onmouseout = function() {
			searchMouseOut();
		};
		cont.onmouseover = function() {
			window.clearTimeout(timeoutInt);
		};*/
		cont.onblur = function() {
			this.style.display = 'none';	
		}
		cont.style.display = 'block'; 
		document.onmousedown = function(e) {
			searchMouseDown(e);
		}
	}, params);
	
}

var searchOldEvent = document.onmousedown;
function searchMouseDown(e) {
	if(!e) e = window.event;
	if(typeof(searchOldEvent) != 'undefined')
		searchOldEvent(e);
	var target = (window.event) ? e.srcElement : e.target;
	var tmp = target;
	try {
		while(tmp.id != 'pagesearch')
			tmp = tmp.parentNode;
	} catch(e) {}
	if(tmp == null || tmp.id != 'pagesearch') {
		document.getElementById('pagesearch').style.display = 'none';
		if(typeof(searchOldEvent) != 'undefined')
			document.onmousedown = function(e) { searchOldEvent(e); };
	}
}

function setFormAction(el) {

	var criteria = el.options[el.selectedIndex].value;
	var url = 'http://www.collectionstation.com/search/' + criteria + '/keywords';
	var tmp = el;
	while(tmp.nodeName != 'FORM')
		tmp = tmp.parentNode;
	tmp.action = url;
	tmp.setAttribute('action', url);
	if(typeof(as) != 'undefined') {
		as.oP.params = {
			type : criteria
		}
	}

}

function clearInput(which) {

	if(which.value == 'Search')
		which.value = '';

}

function populateInput(which) {

	if(which.value == '')
		which.value = 'Search';

}

function searchMouseOut() {
	
	timeoutInt = setTimeout(function() {
		var cont = document.getElementById('pagesearch');	
			cont.style.display = 'none';
	}, 3000);
	
}

var as = null;

addEvent(window, 'load', function() {
	var options = {
			script: function(input) { return 'http://www.collectionstation.com/tools/search'; },
			meth : 'POST',
			json: false,
			maxresults: 10,
			params : {
				type : 'Collectibles',
				input : ''
			}
		};
	as = new bsn.AutoSuggest('headerSearchInput', options);
});