var mouseX = 0;
var mouseY = 0;
var picXMin = 0;
var picXMax = 0;
var picYMin = 0;
var picYMax = 0;
var showBigPicture = false;


var modifySearchIndexInsertPos = null;


document.onmousemove = getMouseXY;

function getMouseXY(e) {
	if (!e) e = window.event;
	
	if (e) {
		if (e.pageX || e.pageY) {
			mouseX = e.pageX;
			mouseY = e.pageY;
		}
		else if (e.clientX || e.clientY) {
			mouseX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mouseY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	}

	if (showBigPicture) {
		// Calculate 
		if (mouseX < picXMin || mouseX > picXMax || mouseY < picYMin || mouseY > picYMax){
			onHidePicture();
		}
	}
}


function onShowPicture(url)
{
	delta = 10;

	// Set window position
	x = mouseX - delta - 2;
	y = mouseY - delta - 2;
	document.getElementById("bigPictureWindow").style.left = x + "px";
	document.getElementById("bigPictureWindow").style.top = y + "px";
	// Set image url
	document.images["produktbild"].src = url;

	// Update boundaries
	picXMin = mouseX - 3*delta;
	picXMax = mouseX + 3*delta + 180;
	picYMin = mouseY - 3*delta;
	picYMax = mouseY + 3*delta + 180;

	// Make window visible
	showBigPicture = true;
	document.getElementById("bigPictureWindow").style.visibility = "visible";
}

function onHidePictureDelayed()
{
	//showBigPicture = false;
}

function onHidePicture()
{
	showBigPicture = false;
	document.images["produktbild"].src = "";
	document.getElementById("bigPictureWindow").style.visibility = "hidden";
}


function showCreateSearchIndex(linkStr)
{
	document.getElementById("createSearchIndexLink").innerHTML=linkStr;
	if (document.getElementById("createSearchIndex").style.display == "none")
		document.getElementById("createSearchIndex").style.display="block";
	else
		document.getElementById("createSearchIndex").style.display="none";
}

function hideCreateSearchIndexAndSubmit()
{
	showCreateSearchIndex("");
	disableCreateSearchIndex(false);
	document.form2.submit();
}


function validateCreateSearchIndexForm()
{
	err = "";
	ret = true;
	
	if (document.form2.categoryselect.selectedIndex < 1)
	{
		err += "-Du m&aring;ste v&auml;lja en kategori f&ouml;r din s&ouml;kning<br/>\n";
		document.form2.categoryselect.style.backgroundColor = "#ffbbbb";
		//document.form2.categoryselect.style.border = "2px solid #ff0000";
		ret = false;
	}

	if (document.form2.displayterm.value == "")
	{
		err += "-Du m&aring;ste skriva nyckelord f&ouml;r din s&ouml;kning<br/>\n";
		document.form2.displayterm.style.backgroundColor = "#ffbbbb";
		//document.form2.displayterm.style.border = "2px solid #ff0000";
		ret = false;
	}

	//document.getElementById("createSearchIndexLink").innerHTML="<font color=\"#cc0000\">"+err+"</font>\n";
	return ret;
}

function submitCreateSearchIndex(handle)
{
	if (validateCreateSearchIndexForm())
	{
		disableCreateSearchIndex(true);
		setTimeout("hideCreateSearchIndexAndSubmit()", 1000)
	}
	return false;
}

function disableCreateSearchIndex(doDisable)
{
	document.form2.term.disabled = doDisable;
	document.form2.categoryselect.disabled = doDisable;
	document.form2.displayterm.disabled = doDisable;
	document.form2.submitbutton.disabled = doDisable;
}


function removeSpecificSearchIndex(caller, catId, itemId)
{
	if (window.confirm("Vill du ta bort den här raden?"))
	{
		loadurl('GET', '/showsearchindex.php?serveraction=delsingleindex&catid='+catId+'&iid='+itemId);
	}
}
/*
function modifySearchIndexTriggered() { 
    // if the readyState code is 4 (Completed)  
    // and http status is 200 (OK) we go ahead and get the responseText  
    // other readyState codes:  
    // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive  
    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) { 
        // xmlhttp.responseText object contains the response.  
        modifySearchIndexInsertPos.innerHTML = xmlhttp.responseText; 
    }
}
*/
function editSpecificSearchIndex(caller, catId, itemId, abort)
{
	/*
	var elem = caller;
	while (elem.parentNode && elem.parentNode.tagName != "TR"){
		elem = elem.parentNode;
	}
	modifySearchIndexInsertPos = elem.parentNode;
	*/
	var action = "requestmodsingleindex";
	if (abort)
		action = "abortmodsingleindex";
	//loadUrlCallback('GET', '/showsearchindex.php?serveraction=' + action + '&iid='+itemId, modifySearchIndexTriggered);
	loadurl('GET', '/showsearchindex.php?serveraction=' + action + '&catid=' + catId + '&iid=' + itemId);
}

function submitEditSpecificSearchIndex(caller, catId, itemId)
{
	var objStr = "form_"+itemId;
	var url = "/showsearchindex.php?serveraction=commitmodsingleindex?&catid=" + catId;//+Form.serialize($("form3"));
	for (var i = 0; i < $(objStr).length; i++)
	{
		var val = swedishEscapeHTML($(objStr).elements[i].value.escapeHTML());
		url += "&" + $(objStr).elements[i].name + "=" + escape(val);
	}
	/*
	elem = caller;
	while (elem.parentNode && elem.parentNode.tagName != "TR"){
		elem = elem.parentNode;
	}

	//modifySearchIndexInsertPos = elem.parentNode;
	//modifySearchIndexInsertPos.style.class='result_item_row';
	loadUrlCallback('GET', url, modifySearchIndexTriggered);
	*/
	loadurl('GET', url);
	return false;
}

function swedishEscapeHTML(str)
{
	var s = str;
	s = s.replace(/å/g, "&aring;");
	s = s.replace(/Å/g, "&Aring;");
	s = s.replace(/ä/g, "&auml;");
	s = s.replace(/Ä/g, "&Auml;");
	s = s.replace(/ö/g, "&ouml;");
	s = s.replace(/Ö/g, "&Ouml;");
	return s;
}
