/**
 * Quicksilver... sortof behaviour...
 *
 * @author Jonathan Wan
 */

var searchVisible = false;
var timer = false;
var typing = false;

var defaultText = 'QuickSilver';

defaultStatus = 'iAmJon :: Now with QuickSilver';

function jwSearchFade() { 
	searchVisible = false;
	new Effect.Fade('search', { from: 0.9, to: 0.5, duration: 0.75, queue: {position: 'end', scope: 'quicksilver'} });
}

function jwSearchFocus(obj) {
	if (obj.type == 'focus') {
		typing = true;
		if ($F('search_keywords').strip() == defaultText)
			$('search_keywords').value = '';
	}
	if (!searchVisible) { searchVisible = true; new Effect.Appear('search', { from: 0.5, to: 0.9, duration: 0.1, queue: {position: 'end', scope: 'quicksilver'} });}
	else { if (timer) clearTimeout(timer); timer = false; }
}

function jwSearchBlur(obj) {
	if (obj.type == 'blur') typing = false;
	if (typing) return;
	if ($F('search_keywords').strip() == '')
		$('search_keywords').value = defaultText;
	if (searchVisible) {
		if (timer) clearTimeout(timer);
		try {
			timer = setTimeout('jwSearchFade()', 1000);
		}
		catch (err) {}
	}
}

function jwParseSearchShortcuts() {
	var e = $('search_keywords');
	var t = e.value.strip();
	var s = t.toLowerCase();
	if (s == '') { e.value = defaultText; return false; }
	if (0 == s.indexOf('gl ')) { $('search_extra').name = 'btnI'; t = t.substring(3); }
	if (0 == s.indexOf('im ')) { $('search_form').action = 'http://images.google.com.au/images'; t = t.substring(3); }
	if (0 == s.indexOf('co ')) { $('search_form').action = 'http://www.google.com/codesearch'; t = t.substring(3); }
	if (0 == s.indexOf('wp ')) { t = 'wikipedia ' + t.substring(3); }
	if (0 == s.indexOf('uwa ')) { t = 'site:uwa.edu.au ' + t.substring(4); }
	if (s == 'wm' || s == 'email' || s == 'webmail') { jwGo('https://webmail.uwa.edu.au/'); return false; }
	if (s == 'hm' || s == 'hotmail') { jwGo('http://www.hotmail.com/'); return false; }
	if (s == 'wl' || s == 'msn' || s == 'wlm' || s == 'msg') { jwGo('http://webmessenger.msn.com/'); return false; }
	if (s == 'gm' || s == 'gmail') { jwGo('http://gmail.google.com/'); return false; }
	if (s == 'wp' || s == 'wikipedia') { jwGo('http://wikipedia.org/'); return false; }
	if (s == 'cs' || s == 'csse') { jwGo('http://web.csse.uwa.edu.au/undergraduate'); return false; }
	if (s == 'ee' || s == 'eece') { jwGo('http://student.ee.uwa.edu.au/units'); return false; }
	if (s == 'xk' || s == 'xkcd') { jwGo('http://xkcd.com/'); return false; }
	if (s == 'ma' || s == 'math' || s == 'maths') { jwGo('http://www.maths.uwa.edu.au/'); return false; }
	if (s == 'ls' || s == 'lb' || s == 'lib' || s == 'library') { jwGo('http://www.library.uwa.edu.au/'); return false; }
	if (s == 'ca' || s == 'cad' || s == 'ctrl' || s == 'ctrlaltdel') { jwGo('http://www.ctrlaltdel-online.com/comic.php'); return false; }
	if (s == 'ph' || s == 'phd') { jwGo('http://www.phdcomics.com/comics.php'); return false; }
	if (s == 'da' || s == 'art') { jwGo('http://www.deviantart.com/'); return false; }
	if (s == 'ga' || s == 'api') { jwGo('http://www.gotapi.com/'); return false; }
	if (s == 'cm' || s == 'cmo') { jwGo('http://cmo.library.uwa.edu.au/rlms/'); return false; }
	if (s == 'fw' || s == 'fuel' || s == 'fuelwatch') { jwGo('http://fuelwatch.wa.gov.au/'); return false; }
	if (s == 'bl' || s == 'blog') { jwGo('http://nazrila.homeftp.org/wordpress/'); return false; }
	e.value = t;
	return true;
}

function jwGo(url) { setTimeout("document.location = '" + url + "'", 2); }

function jwKeypress(obj) {
	if (searchVisible) return true;
	if (obj.altKey || obj.ctrlKey || obj.metaKey) return true;
	var code;
	if (obj.keyCode) code = obj.keyCode;
	else if (obj.which) code = obj.which;
	var c = String.fromCharCode(code).strip();
	if (c == "\t") {
		setTimeout("Form.Element.activate('search_keywords')", 2);
		return false;
	}
	Form.Element.activate('search_keywords');
	setTimeout("$('search_keywords').value = '" + c + "'", 2);
	return false;
}

function jwOnload(obj) {
	Element.hide('search_keywords');
	Event.observe('search_keywords', 'keypress', function(e){if (!searchVisible) jwSearchFocus(e); });
	//$A(Form.getInputs('search_form')).each(function(item){Event.observe(item, 'focus', function(e){if (!e) e = window.event; e.target.hasFocus=true;})});
	//$A(Form.getInputs('search_form')).each(function(item){Event.observe(item, 'blur', function(e){if (!e) e = window.event; e.target.hasFocus=false;})});
	searchVisible = false;
	typing = false;
	$('search_keywords').blur();
	Element.show('search_keywords');//, { duration: 1 }
	new Effect.Appear('search', { from: 0, to: 0.5, duration: 1 });
}
