var waitQueueCount = 0;

function El(id)
{
	return document.getElementById(id);
}

// JavaScript Document
function switchimage(imgDocID,imgObjName)
{
	document[imgDocID].src = eval(imgObjName + ".src");
}

function InArray(a, e)
{
	if (a == null)
		return false;
	if (a.length == 0)
		return false; // empty array
	for (var i = 0; i < a.length; i++)
		if (a[i] == e) return true; // found
	return false; // not found
}

function GetPosition(elem)
{
	var left = 0;
	var top  = 0;

	while (elem.offsetParent)
	{
		left += elem.offsetLeft;
		top  += elem.offsetTop;
		elem = elem.offsetParent;
	}

	left += elem.offsetLeft;
	top += elem.offsetTop;

	return { x: left, y: top };
}

function SetOpacity(e, op)
{
	e.style.opacity = op;
	e.style.filter = "alpha(opacity=" + (op * 100) + ")";
}

// Increase the number of pending ajax requests and display a waiting animation if needed
function IncreaseWaitQueue()
{
	waitQueueCount++;
	if (waitQueueCount == 1)
	{
		// Show the animated waiting icon
		var tmpDiv = El("waitingAnimationDiv");
		var topDelta = 0;

		// Get the scrolly delta
		if (self.pageYOffset)
			topDelta = self.pageYOffset;
		else if (document.documentElement && document.documentElement.scrollTop)
			topDelta = self.document.documentElement.scrollTop;
		else if (document.body)
			topDelta = document.body.scrollTop;

		// set the position
	    if (window.innerHeight)
			tmpDiv.style.top = (window.innerHeight / 2 + topDelta) + "px";
    	else if (document.documentElement.clientHeight)
			tmpDiv.style.top = (document.documentElement.clientHeight / 2  + topDelta) + "px";
		else
			tmpDiv.style.top = (300 + topDelta) + "px";

		if (window.innerWidth)
			tmpDiv.style.left = (window.innerWidth / 2 - 10) + "px";
    	else if (document.documentElement.clientWidth)
			tmpDiv.style.left = (document.documentElement.clientWidth / 2 - 10) + "px";

		tmpDiv.style.display = "block";
	}
}

// Decrease the number of pending ajax requests and hide the waiting animation if needed
function DecreaseWaitQueue()
{
	waitQueueCount--;
	if (waitQueueCount <= 0)
	{
		// Hide the animated waiting icon
		var tmpDiv = El("waitingAnimationDiv");
		tmpDiv.style.display = "none";
	}
}

// Left-trim a string and return the result
function LTrimStr(sString)
{
	if (sString == '') return '';

	i = 0;
	while (i < sString.length && sString.charAt(i) == ' ') i++;

	sString = sString.substring(i, sString.length);
	return sString;
}

function IsNumber(st)
{
	return (st.match(/[ ]*\-?[0-9]+[.]?[0-9]*[ ]*/) == st)
}

// Get an instance of the XMLHttpRequest object
function GetXHRObject()
{
	var xhr = null; // The XMLHTTPRequest object

	if (window.XMLHttpRequest) // Gecko and Opera
		xhr = new XMLHttpRequest();
	else if (window.ActiveXObject) // Internet Explorer
		xhr = new ActiveXObject("Microsoft.XMLHTTP");

	return xhr;
}

// Send a GET request and load the received content into the given div
function LoadAsyncToDiv(url, divId, postAction, preAction)
{
	var xhr = GetXHRObject();

	if (xhr == null)
	{
		alert("Sorry, your browser doesn't support AJAX");
		return;
	}
	xhr.open("GET", url, true);

	IncreaseWaitQueue();

	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4) // data received
		{
			var r = xhr.responseText;
			if (LTrimStr(r) != "")
			{
				if (preAction != null)
					eval(preAction); // execute additional code
				// insert the received content into the div
				El(divId).innerHTML = r;
				if (postAction != null)
					eval(postAction); // execute additional code
			}
			else
			{
				alert("Transmission failed! Connection error or server too busy!");
			}
			DecreaseWaitQueue();
		}
	}
	// Send the request
	xhr.send(null);
}

// Send a POST request and load the received content into the given div
// POST paramerters are urlencoded and stored in encodedData
function LoadAsyncToDivPost(url, encodedData, divId, postAction, preAction)
{
	var xhr = GetXHRObject();

	if (xhr == null)
	{
		alert("Sorry, your browser doesn't support AJAX");
		return;
	}
	xhr.open("POST", url, true);
	IncreaseWaitQueue();

	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4) // data received
		{
			var r = xhr.responseText;
			if (LTrimStr(r) != "")
			{
				if (preAction != null)
					eval(preAction); // execute additional code
				// insert the received content into the div
				El(divId).innerHTML = r;
				if (postAction != null)
					eval(postAction); // execute additional code
			}
			else
			{
				alert("Transmission failed! Connection error or server too busy!");
			}
			DecreaseWaitQueue();
		}
	}

	// Send the request
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.send(encodedData);
}

// menu handler
function HighlighTopMenu(id)
{
	document.onmouseover = null;
	menuTopMenus[id].highlighted = true;

	// change style
	El(id + "_left").className = "top-menu-left-hover";
	El(id + "_right").className = "top-menu-right-hover";
	El(id + "_middle").className = "top-menu-middle-hover";

	UnHighlighMenusExcept(menuTopMenus[id].keepHighlight);
	if (menuTopMenus[id].openChild != "")
	{
		offset = GetPosition(El(id + "_left"));
		OpenMenuBlock(menuTopMenus[id].openChild, offset.x, offset.y + El(id + "_left").offsetHeight);
	}
	CloseMenuBlocksExcept(menuTopMenus[id].keepOpen)
}

function UnHighlighMenusExcept(exceptions)
{
	if (!menuTopMenus) return;

	for (var id in menuTopMenus)
	{
		if (menuTopMenus[id].highlighted && !InArray(exceptions, id))
		{
			menuTopMenus[id].highlighted = false;
			// change style
			El(id + "_left").className = "top-menu-left";
			El(id + "_right").className = "top-menu-right";
			El(id + "_middle").className = "top-menu-middle";
		}
	}

	for (var id in menuEntries)
	{
		if (menuEntries[id].highlighted && !InArray(exceptions, id))
		{
			menuEntries[id].highlighted = false;
			// change style
			El(id).className = "menu-entry";
		}
	}
}

function HighlightMenuEntry(id)
{
	document.onmouseover = null;
	menuEntries[id].highlighted = true;

	// change style
	El(id).className = "menu-entry-hover";
	UnHighlighMenusExcept(menuEntries[id].keepHighlight);
	if (menuEntries[id].openChild != "")
	{
		offset = GetPosition(El(id));
		OpenMenuBlock(menuEntries[id].openChild, offset.x + El(id).offsetWidth, offset.y - 1);
	}
	CloseMenuBlocksExcept(menuEntries[id].keepOpen)
}

function CloseMenuBlocksExcept(exceptions)
{
	if (!menuBlocks) return;

	for (var id in menuBlocks)
	{
		if (menuBlocks[id].opened && !InArray(exceptions, id))
		{
			CloseMenuBlock(id);
		}
	}
}

function FadeInMenuBlock(id)
{
	if (!menuBlocks[id].opened) return; // not open
	if (menuBlocks[id].opacity > 0.8) return;
	menuBlocks[id].opacity += 0.1;
	SetOpacity(El(id), menuBlocks[id].opacity);
	setTimeout("FadeInMenuBlock('" + id + "');", 35);
}

function FadeOutMenuBlock(id)
{
	if (menuBlocks[id].opened) return; // not closed
	if (menuBlocks[id].opacity < 0.1)
	{
		El(id).style.display = "none";
		return;
	}
	menuBlocks[id].opacity -= 0.1;
	SetOpacity(El(id), menuBlocks[id].opacity);
	setTimeout("FadeOutMenuBlock('" + id + "');", 35);
}


function OpenMenuBlock(id, o_x, o_y)
{
	if (menuBlocks[id].opened) return; // already open
	menuBlocks[id].opened = true;
	El(id).style.display = "block";
	SetOpacity(El(id), menuBlocks[id].opacity);
	El(id).style.top = o_y + "px";
	El(id).style.left = o_x + "px";

	setTimeout("FadeInMenuBlock('" + id + "');", 35);
}

function CloseMenuBlock(id)
{
	if (!menuBlocks[id].opened) return; // already closed
	menuBlocks[id].opened = false;
	setTimeout("FadeOutMenuBlock('" + id + "');", 35);
}

function DefaultOnMouseOver()
{
	UnHighlighMenusExcept(null);
	CloseMenuBlocksExcept(null);
}

function RestoreOnMouseOver()
{
	document.onmouseover = DefaultOnMouseOver;
}

function ToggleZoomBox(num)
{
	boxData[num].zoomDir = 1 - boxData[num].zoomDir;
	if (boxData[num].zoomDir == 0)
	{
		SetOpacity(El('opacityBox' + num), 1.0);
		El('textBox' + num).innerHTML = "";
		El('textBox' + num).style.padding = "0px";
		El('zoomLink' + num).innerHTML = txtReadMore;
		ZoomBoxOut(num);
	}
	else
	{
		El('zoomLink' + num).innerHTML = txtReadLess;
		El('imageBox' + num).style.height = "auto";
		ZoomBoxIn(num);
		for (var i = 1; i <= 4; i++)
		{
			if (i != num)
			{
				boxData[i].zoomDir = 0;
				El('textBox' + i).innerHTML = "";
				El('textBox' + i).style.padding = "0px";
				El('zoomLink' + i).innerHTML = txtReadMore;
				ZoomBoxOut(i);
				SetOpacity(El('opacityBox' + i), 1.0);
			}
		}
	}
}

function ZoomBoxOut(num)
{
	if (boxData[num].zoomDir != 0)
		return;

	if (boxData[num].currentSize <= boxData[num].minWidth)
	{
		boxData[num].currentSize = boxData[num].minWidth;
		boxData[num].opacity = 1.0;
		El('imageBox' + num).width = boxData[num].currentSize;
		El('imageBox' + num).style.height = boxData[num].minHeight + "px";
		boxData[num].boxHeight = 0;
		El('textBox' + num).style.height ="0px";
		El('textBoxRow' + num).style.display = "none";

		SetOpacity(El('opacityBox' + num), boxData[num].opacity);
	}
	else
	{
		boxData[num].currentSize -= 25;
		boxData[num].opacity += 0.02;
		El('imageBox' + num).width = boxData[num].currentSize;

		// boxData[num].boxHeight -= Math.round(El('tmpBox' + num).offsetHeight / 4);
		boxData[num].boxHeight -= 37; // 148 / 4
		if (boxData[num].boxHeight < 30) boxData[num].boxHeight = 0;
		El('textBox' + num).style.height = boxData[num].boxHeight + "px";

		setTimeout("ZoomBoxOut(" + num + ")", 30);
	}
}

function ZoomBoxIn(num)
{
	if (boxData[num].zoomDir != 1)
		return;

	if (boxData[num].currentSize >= boxData[num].maxWidth)
	{
		boxData[num].currentSize = boxData[num].maxWidth;
		El('imageBox' + num).width = boxData[num].currentSize;

		// boxData[num].boxHeight = El('tmpBox' + num).offsetHeight;
		boxData[num].boxHeight = 143;

		El('textBox' + num).style.height = boxData[num].boxHeight + "px";
		El('textBox' + num).style.padding = "4px 0px 4px 4px";
		El('textBox' + num).style.overflow = "auto";
		El('textBox' + num).innerHTML = "<div style='width: 291px; height: 143px; padding-right: 4px; overflow: auto;'>" + El('tmpBox' + num).innerHTML + "</div>";
		SetOpacity(El('opacityBox' + num), boxData[num].opacity);
	}
	else
	{
		boxData[num].currentSize += 25;
		boxData[num].opacity -= 0.01;
		El('imageBox' + num).width = boxData[num].currentSize;

		// boxData[num].boxHeight += Math.round(El('tmpBox' + num).offsetHeight / 6);
		boxData[num].boxHeight += 24; // 148 / 6
		El('textBox' + num).style.height = boxData[num].boxHeight + "px";
		El('textBoxRow' + num).style.display = "block";
		setTimeout("ZoomBoxIn(" + num + ")", 30);
	}
}

function AddSubscription()
{
	if (LTrimStr(document.newsSubscrForm.email.value) == "")
	{
		document.newsSubscrForm.email.focus();
		return false;
	}

	document.newsSubscrForm.disabled = true;
	var url = "main.php?do=subscribe";
	var encodedData = "email=" + encodeURIComponent(document.newsSubscrForm.email.value);

	El('submitButton').disabled = true;
	LoadAsyncToDivPost(url, encodedData, "subscriptionResult", "El('submitButton').disabled = false;");
	return false;
}

function PostMessage()
{
	if (LTrimStr(document.contactForm.firstname.value) == "")
	{
		document.contactForm.firstname.focus();
		return false;
	}
	if (LTrimStr(document.contactForm.lastname.value) == "")
	{
		document.contactForm.lastname.focus();
		return false;
	}
	if (LTrimStr(document.contactForm.email.value) == "")
	{
		document.contactForm.email.focus();
		return false;
	}
	if (LTrimStr(document.contactForm.subject.value) == "")
	{
		document.contactForm.subject.focus();
		return false;
	}
	if (LTrimStr(document.contactForm.content.value) == "")
	{
		document.contactForm.content.focus();
		return false;
	}

	// Ok
	var url = "main.php?do=postmessage";
	var encodedData = "firstname=" + encodeURIComponent(document.contactForm.firstname.value) +
		"&lastname=" + encodeURIComponent(document.contactForm.lastname.value) +
		"&title=" + encodeURIComponent(document.contactForm.n_title.value) +
		"&email=" + encodeURIComponent(document.contactForm.email.value) +
		"&company=" + encodeURIComponent(document.contactForm.company.value) +
		"&website=" + encodeURIComponent(document.contactForm.website.value) +
		"&subject=" + encodeURIComponent(document.contactForm.subject.value) +
		"&content=" + encodeURIComponent(document.contactForm.content.value);

	El('resetButton').disabled = true;
	El('submitButton').disabled = true;

	LoadAsyncToDivPost(url, encodedData, "postResultSpan", "MessagePosted();");
	return false;
}

function MessagePosted()
{
	if (El('messageOK'))
	{
		alert(El('messageOK').innerHTML);
		document.contactForm.reset();
	}
	else if (El('messageError'))
	{
		alert(El('messageError').innerHTML);
		document.contactForm.email.focus();
	}
	El('resetButton').disabled = false;
	El('submitButton').disabled = false;
}
RestoreOnMouseOver();