/**
 * AJAX_THUMBS
 *
 * (c) 2006 Plura http://www.plura.pt
 * 
 * 1. Ajax XMLHttpRequest Init
 * 2. Form validation
 * 3. Form variable 'harvest'
 */
var tooltip;
var offsetx = 20;
var offsety = 20;
var ie = (navigator.appName == "Microsoft Internet Explorer")? true : false;

var tooltip_thumb;


/*  A  J  A  X     I  N  I  T  */
function createRequestObject() {
	if(navigator.appName == "Microsoft Internet Explorer") var ro = new ActiveXObject("Microsoft.XMLHTTP");
	else var ro = new XMLHttpRequest();

	return ro;
}

var http = createRequestObject();

function sndReq(url, parameters) {
	http.onreadystatechange = handleResponse;
	
   	http.open('post', url);
   	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
	http.setRequestHeader("Content-length", parameters.length);
	http.send(parameters);
}

function handleResponse() { if(http.readyState == 4) showthumb(http.responseText); }


//get url query vars
function get(src, name) {
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var tmpURL = src;
	var results = regex.exec(tmpURL);
	if(results == null)
    	return "";
	else
    	return results[1];
}


function showhide(status, src){
	if(status){
		tooltip.style.visibility = "visible";
		tooltip_thumb.src = src;
	} else {
		tooltip.style.visibility  = "hidden";	
	}
}

function initthumb(){ sndReq(this.path, this.query);}
function showthumb(src){ showhide(true, src); }
function hidethumb(){ showhide(false); }


var offsetx = 20;
var offsety = 20;

function getmouseposition(e){

        pagex = (ie)? document.documentElement.scrollLeft : window.pageXOffset;
        pagey = (ie)? document.documentElement.scrollTop : window.pageYOffset;
        mousex = (ie)? event.x : e.clientX;
        mousey = (ie)? event.y : e.clientY;

        tooltip.style.left = (mousex+pagex+offsetx) + 'px';
        tooltip.style.top = (mousey+pagey+offsety) + 'px';
}


function init_tooltip(){
	var a = document.getElementById('submenu').getElementsByTagName('a');
	
	for(i=0; i<a.length; i++){
		a[i].query =	"query=SELECT CONCAT(";
		a[i].query +=	"'admin/common/thumb.php?width=100%26height=100%26path=../../pa_uploads/files/',"
		a[i].query +=	"(SELECT file FROM pa_files WHERE itemID=" + get(a[i].href, 'itemID0') + " ORDER BY fileID DESC LIMIT 1)"
		a[i].query +=	") FROM pa_items WHERE itemID=" + get(a[i].href, 'itemID0');
		
		a[i].path = 'admin/common/query.php';
		a[i].onmouseover = initthumb;
		a[i].onmouseout = hidethumb;
	}
	
	tooltip = document.createElement('div');
	tooltip.setAttribute("id", 'tooltip');
	
	tooltip_thumb = new Image();
	tooltip.appendChild(tooltip_thumb);

	document.body.appendChild(tooltip);

	document.onmousemove = getmouseposition;
	
	showhide(false); //initial thumb status
}


if (window.addEventListener) window.addEventListener("load", init_tooltip, false);
else if (window.attachEvent) window.attachEvent("onload", init_tooltip);
else if (document.getElementById) window.onload=init_tooltip;