function gE(e) { return document.getElementById(e); }
function wH(e,h) { e.innerHTML='';e.innerHTML=h; }

var http_request = false;
var buttonText = false;

var actionType = "";


/* connection */

function makeRequest(para, loadingFunc, doneFunc)
{
	/* important!! callFunction must be name of function, without any parameter */
	
	http_request = false;
	xmldoc = false;
	
	// create the XMLHttpRequest object
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			xmldoc = new ActiveXObject("Msxml2.DOMDocument");
			alert (xmlDoc)
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
		
	// oops...
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function()
	{	
		if (http_request.readyState == 4) {
			if (http_request.status == 200)
			{
				eval(doneFunc + "(http_request.responseText)" );
			} else {
				alert('Erm, there seems to be some problem with the server. Please try again later.');
			}
		} else if (http_request.readyState != 4) {
			// loading...
			eval(loadingFunc + "(http_request.responseText)" );
		}
	}
	
	// connect!
	// change the address to your domain!!
	http_request.open('GET', "http://www.search-this.com/data.php?"+para, true);
	http_request.send(null);
}



/* search functions */
function sOp(e)
{
	// show the option buttons
	if (gE("rowButtons-" + e.id).className != "optionsHide") {
		// class .optionsHidden is a flag set when the preview box is open
		// since it's not, so show the buttons!
		gE("rowButtons-" + e.id).style.display = "inline"
	}
}

function hOp(e) {
	// hide the option buttons
	gE("rowButtons-" + e.id).style.display = "none"
}

var postID = 0;

function preview(id) {
	// id as post ID
	// set to a global variable, we need the post ID for searchLoading() and searchDone()
	postID = id;
	
	// class .optionsHidden is a flag set when the preview box is open
	gE("rowButtons-" + id).className = "optionsHide";
	
	var previewBox = gE("preview-" + id);
	
	// show div.preview
	previewBox.style.display = "block";
	
	// start retrieving data...
	makeRequest("a=getPost&p=" + id, "searchLoading", "searchDone");
}

function searchLoading() {
	wH(gE("preview-" + postID), "Fetching data...");
}

function searchDone(out) {
	
	var teaser = out;
	var id = postID;
		
	// put into preview-id
	var permalink = gE("permalink-" + id).href;
	teaser += '<p class="previewFooter"><span><a href="javascript:closePreview(' + id + ')" title="Close Preview"><img src="http://www.search-this.com/wp-content/themes/big-blue/images/cancel.png" alt="Close Preview" height="16" width="16" /></a></span>';
	teaser += '<img src="http://www.search-this.com/wp-content/themes/big-blue/images/arrow_right.png" alt="View Post" height="16" width="16" /> <a href="' + permalink + '" title="View This Post Together with It\'s Comments">View Full Post</a></p>'	;

	wH(gE("preview-" + id), teaser);
	
	postID = 0;
}

function closePreview(id) {
	gE("preview-" + id).style.display = "none";
	gE("rowButtons-" + id).className = "options";
}
