/* ui.js
 *
 * Code for manipulating the interface for EveKnows.com
 *
 * Copyright 2009 Aidan Trent <aidan@eveknows.com>
 */

/* Sets the cursor focus on 'widget' */
function setFocus(widget) {
	if (document.getElementById) {	
		var w = document.getElementById(widget);
		if (w.createTextRange) {
			var range = w.createTextRange();
			range.select();
		} else if (w.setSelectionRange) {
			w.setSelectionRange(0, w.value.length);
		}
		w.focus();
	}
}

/* Installs our search provider via a standard interface */
function installSearchPlugin() {
	if (window.external && ("AddSearchProvider" in window.external)) {
		window.external.AddSearchProvider('http://eveknows.com/opensearch.xml');
	} else {
		alert ("Sorry, your web browser does not support OpenSearch plugins");
	}
}

/* Display element 'id' */
function showOverview (id) {
	var w = document.getElementById (id);
	if (w.style.display != '') {
		w.style.display = '';
	}
}

/* Hide element 'id' */
function hideOverview (id) {
	var w = document.getElementById (id);
	if (w.style.display != 'none') {
		w.style.display = 'none';
	}
}

function showReportDialog (url)
{
	YAHOO.myDialog.show ();
	var w = document.getElementById ('reportDialogURL');
	w.value = url;
}

function hideReportDialog ()
{

}

function result_hover (id)
{
	var rb = document.getElementById ('result' + id);
	rb.style.backgroundColor='#ffeeee';
	rb.style.borderBottom='1px dashed #999';

	if (timerIdOut) {
		clearTimeout (timerIdOut);
		timerIdOut = 0;
	}

	if (currentResult != id) {
		var old_cb = document.getElementById ('command' + currentResult);
		if (old_cb) {
			old_cb.style.display = 'none';
		}
		currentResult = id;
		if (timerId) {
			clearTimeout (timerId);
		}
		if (timerIdThumb) {
			clearTimeout (timerIdThumb);
		}
		
		// Only rotate thumbs if the thumbnails are different
		if (window['thumb' + id + '_1'] != window['thumb' + id + '_2']) { 
			timerIdThumb = setTimeout ('rotate_thumb (' + id + ')', 500);
		}
		show_command_box (id);
	}
}

function result_unhover (id)
{
	var rb = document.getElementById ('result' + id);
	timerIdOut = setTimeout ('hide_command_box (' + id + ')', 300);
	
	rb.style.backgroundColor='';
	rb.style.borderBottom='1px solid #fff';
}

function rotate_thumb (id)
{
	var img = document.getElementById ('thumb' + id);
	var thumbIndex = window['thumb' + id + '_index'];
	var preload = new Image ();
	preload.src = window['thumb' + id + '_' + thumbIndex];
	fade_out_thumb (img, window['thumb' + id + '_' + thumbIndex], 100);
	timerIdThumb = setTimeout ('rotate_thumb (' + id + ')', 3000);
	thumbIndex++;
	if (thumbIndex > 3) {
		thumbIndex = 1;
	}
	window['thumb' + id + '_index'] = thumbIndex;
}

function fade_out_thumb (img, src, opacity)
{
	img.style.opacity = (opacity / 100);
	opacity -= 5;
	if (img.filters) {
		img.filters.alpha.opacity = opacity;
	}
	if (img.style.opacity > 0.01) {
		setTimeout (function () { fade_out_thumb (img, src, opacity); }, 15 + Math.log (opacity));
	} else {
		img.src = src;
		fade_in_thumb (img, 1);
	}
}

function fade_in_thumb (img, opacity)
{
	img.style.opacity = (opacity / 100);
	opacity += 5;
	if (img.filters) {
		img.filters.alpha.opacity = opacity;
	}
	if (img.style.opacity < 0.99) {
		setTimeout (function () { fade_in_thumb (img, opacity); }, 25 + Math.log (opacity * 1000));
	}
}

function show_command_box (id)
{
	var o = document.getElementById ('command' + id);
	o.style.opacity = 0;
	o.style.display = 'block';
	if (o.filters) {
		o.filters.alpha.opacity = 0;
	}
	setTimeout ('fade_in_command_box (' + id + ',5)', 10);
}

function hide_command_box (id)
{
	var o = document.getElementById ('command' + id);
	currentResult = 0;
	clearTimeout (timerIdThumb);
	timerIdThumb = 0;
	fade_out_command_box (id, 100);
}

function fade_in_command_box (id, opacity)
{
	var o = document.getElementById ('command' + id);
	o.style.opacity = (opacity / 100);
	opacity += 5;
	if (o.filters) {
		o.filters.alpha.opacity = opacity;
	}
	if (o.style.opacity < 1.0) {
		setTimeout ('fade_in_command_box (' + id + ',' + opacity + ')', 15);
	}
}

function fade_out_command_box (id, opacity)
{
	var o = document.getElementById ('command' + id);
	o.style.opacity = (opacity / 100);
	opacity -= 5;
	if (o.filters) {
		o.filters.alpha.opacity = opacity;
	}
	if (o.style.opacity > 0) {
		setTimeout ('fade_out_command_box (' + id + ',' + opacity + ')', 10);
	} else {
		o.style.display = 'none';
	}
}

function get_search_code ()
{
	var domain = document.getElementById ('domain');
	var textbox = document.getElementById ('textbox');
	
	var domainname = domain.value;
	var prefix = domainname.substring (0, 7);
	if (prefix == 'http://') {
		var tmp = domainname.substring (7);
		domainname = tmp;
	}
	prefix = domainname.substring (0, 4);
	if (prefix == 'www.') {
		var tmp = domainname.substring (4);
		domainname = tmp;
	}
	textbox.value = '<div style="border: solid 1px #000; width: 400px; padding: 6px; background-color: #b8edfb;"><form action="http://eveknows.com/" method="get" target="eve"><label for="eve_knows_q" style="display: block; font-size: 16px; font-weight: bold; text-align: center; font-family: Georgia, serif; margin-bottom: 6px; color: #000;">Search ' + domainname + ':</label><input type="text" name="q" id="eve_knows_q" style="border: solid 1px #666; width: 300px;" /><input type="hidden" name="src" value="' + domainname + '" /><input type="submit" value="Ask Eve" style="color: #fff; font-weight: bold; background-color: #33e659; border: solid 1px #32a44a; margin-left: 6px;" /></form></div>';
}
