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

function HideElem(elemId1, elemId2)
{
	if (El(elemId1)) El(elemId1).style.display = 'none';
	if (elemId2 && El(elemId2)) El(elemId2).innerHTML="";
}

function ToggleDisplay(id)
{
	if (!El(id)) return;

	if (El(id).style.display == "none")
		El(id).style.display = "";
	else
		El(id).style.display = "none";
}


function TrimStr(s)
{
	return s.replace(/^\s+|\s+$/, '');
}

function CheckEmail(address)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/;
	return reg.test(TrimStr(address));
}

function GetWindowSize()
{
	var w = 1024, h = 768;

	if (window.innerHeight)
		h = window.innerHeight;
	else if (document.documentElement.clientHeight)
		h = document.documentElement.clientHeight;

	if (window.innerWidth)
		w = window.innerWidth;
	else if (document.documentElement.clientWidth)
		w = document.documentElement.clientWidth;

	return { x: w, y: h }
}

function FixLocation(divId, w, h)
{
	var tmpDiv = El(divId);
	if (tmpDiv == null) return;

	if (w == 0) w = tmpDiv.offsetWidth;
	if (h == 0) h = tmpDiv.offsetHeight;

	var topDelta;
	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;
	else
		topDelta = Math.floor(h / 2);

	if (!oldIE)
	{
		topDelta = 0; // use position: fixed instead of absolute
		tmpDiv.style.position = "fixed";
	}
	topDelta -= Math.floor(h / 2);

	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 - Math.floor(w / 2)) + "px";
	else if (document.documentElement.clientWidth)
		tmpDiv.style.left = (document.documentElement.clientWidth / 2 - Math.floor(w / 2)) + "px";
}


function HideWaitLayer()
{
	HideElem("waitDiv");
	HideElem("waitImage");
}

var showWaitImage = true;

function ShowWaitLayer()
{
	var tmpImage = El("waitImage");
	tmpImage.style.opacity = 0.8;
	tmpImage.style.filter = "alpha(opacity=80)";
	if (showWaitImage)
		tmpImage.style.display = "block";
	else
		tmpImage.style.display = "none";
	FixLocation("waitImage", 0, 0);

	var tmpDiv = El("waitDiv");
	tmpDiv.style.opacity = 0.8;
	tmpDiv.style.filter = "alpha(opacity=80)";
	tmpDiv.style.top = "0px";
	tmpDiv.style.left = "0px";

	if (oldIE)
	{
		var h = 0;
		if (window.innerHeight)
			h = window.innerHeight;
		else if (document.documentElement.clientHeight)
			h = document.documentElement.clientHeight;

		if (document.body.scrollHeight > document.body.offsetHeight)
		{
			tmpDiv.style.width = document.body.scrollWidth + "px";
			tmpDiv.style.height = Math.max(h, document.body.scrollHeight) + "px";
		}
		else
		{
			tmpDiv.style.width = document.body.offsetWidth + "px";
			tmpDiv.style.height = Math.max(h, document.body.offsetHeight) + "px";
		}
	}
	else
	{
		tmpDiv.style.right = "0px";
		tmpDiv.style.bottom = "0px";
		tmpDiv.style.position = "fixed";
	}
	tmpDiv.style.display = "block";
}

function UnblockBackgroundAccess()
{
	FadeOut("blankDiv", 40);
	HideElem("iframeFix");
}

function BlockBackgroundAccess()
{
	var tmpDiv = El("blankDiv");
	tmpDiv.style.top = "0px";
	tmpDiv.style.left = "0px";

	if (oldIE)
	{
		var h = 0;
		if (window.innerHeight)
			h = window.innerHeight;
		else if (document.documentElement.clientHeight)
			h = document.documentElement.clientHeight;

		if (document.body.scrollHeight > document.body.offsetHeight)
		{
			tmpDiv.style.width = document.body.scrollWidth + "px";
			tmpDiv.style.height = Math.max(h, document.body.scrollHeight) + "px";
		}
		else
		{
			tmpDiv.style.width = document.body.offsetWidth + "px";
			tmpDiv.style.height = Math.max(h, document.body.offsetHeight) + "px";
		}

		var tmpFrame = El("iframeFix");
		if (tmpFrame)
		{
			// IE6 fix
			tmpFrame.style.width = tmpDiv.style.width;
			tmpFrame.style.height = tmpDiv.style.height;
			tmpFrame.style.filter = "alpha(opacity=0)";
			tmpFrame.style.display = "block";
		}
	}
	else
	{
		tmpDiv.style.right = "0px";
		tmpDiv.style.bottom = "0px";
		tmpDiv.style.position = "fixed";
	}
	FadeIn("blankDiv", 40, "block");
}

function PopUp(divId, w, h)
{
	ContinueFixLocation(divId);
	FixLocation(divId, w, h);

	var tmpDiv = El(divId);
	if (tmpDiv == null) return;
	tmpDiv.style.display = "block";

	BlockBackgroundAccess();
	FixLocation(divId, w, h);
}

function PopUpAjax(url, divId1, divId2, w, h, postAction)
{
	if (!divId1) divId2 = "popUpDiv";
	if (!divId2) divId2 = divId1;
	if (!w) w = 0;
	if (!h) h = 0;
	if (!postAction) postAction = "";
	LoadAsyncToDiv(url, divId1, "FixLocation('" + divId2 + "', " + w + "," + h + "); setTimeout(\"PopUp('" + divId2 + "', " + w + "," + h + ")\", 100);" + postAction);
}

var fixLocationDisabled = new Object();

function KeepCenter(divId, w, h)
{
	if (!fixLocationDisabled[divId])
		FixLocation(divId, w, h);
	setTimeout("KeepCenter('" + divId + "', " + w + ", " + h + ")", 20);
}

function StopFixLocation(divId)
{
	fixLocationDisabled[divId] = true;
}

function ContinueFixLocation(divId)
{
	fixLocationDisabled[divId] = false;
}


var waitQueueCount = 0;

function IncreaseWaitQueue()
{
	waitQueueCount++;
	if (waitQueueCount == 1)
	{
		ShowWaitLayer();
	}
}

function DecreaseWaitQueue()
{
	waitQueueCount--;
	if (waitQueueCount <= 0)
	{
		// Hide the animated waiting icon
		HideWaitLayer();
	}
}

// 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 (TrimStr(r) != "")
			{
				if (r.substring(0, 100).search(/\<html/) > -1)
				{
					// sparta f*cked up
					alert("Session timeout! Please refresh your browser window and relogin.");
					return;
				}
				if (preAction != null)
					eval(preAction); // execute additional code

				if (divId == "script")
					eval(r);
				else if (divId == "alert")
					alert(r);
				else if (El(divId))
					El(divId).innerHTML = r; // insert the received content into the div

				if (postAction != null)
					eval(postAction); // execute additional code
			}
			else
			{
				PopUpJoinForm(joinLink);
			}
			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 (TrimStr(r) != "")
			{
				if (r.substring(0, 100).search(/\<html/) > -1)
				{
					// sparta f*cked up
					alert("Session timeout! Please refresh your browser window and relogin.");
					return;
				}
				if (preAction != null)
					eval(preAction); // execute additional code

				if (divId == "script")
					eval(r);
				else if (divId == "alert")
					alert(r);
				else if (El(divId))
					El(divId).innerHTML = r; // insert the received content into the div

				if (postAction != null)
					eval(postAction); // execute additional code
			}
			else
			{
				PopUpJoinForm(joinLink);
			}
			DecreaseWaitQueue();
		}
	}

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

var dragObject = null;
var dragObject2 = null; // for IE 6
var mouseOffset = {x: 0, y: 0};

function GetMousePos(e)
{
	if (e.pageX || e.pageY)
	{
		return { x: e.pageX, y: e.pageY };
	}
	else
	{
		var tmpx = e.clientX + document.body.scrollLeft - document.body.clientLeft;
		var tmpy = e.clientY + document.body.scrollTop - document.body.clientTop;
		return { x: tmpx, y: tmpy };
	}
}


function GetMouseOffset(elem, e)
{
	if (!e) e = window.event;

	var offset = GetPosition(elem);
	var mousePos = GetMousePos(e);
	return { x: mousePos.x - offset.x, y: mousePos.y - offset.y };
}

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 MouseMoved(e)
{
	if (!e) e = window.event;

	if (dragObject)
	{
		var mousePos = GetMousePos(e);
		dragObject.style.top = (mousePos.y - mouseOffset.y) + "px";
		dragObject.style.left = (mousePos.x - mouseOffset.x) + "px";
		if (dragObject2)
		{
			dragObject2.style.top = (mousePos.y - mouseOffset.y) + "px";
			dragObject2.style.left = (mousePos.x - mouseOffset.x) + "px";
		}
		return false;
	}
}

function DummyHandler(e)
{
	// do nothing
}

function MouseReleased()
{
	if (dragObject) dragObject.onmousedown = DummyHandler;

	dragObject = null;
	dragObject2 = null;
}

function StartDrag(id, id2)
{
	var item = El(id);

	if (!item) return;

	dragObject = item;
	dragObject2 = El(id2);

	item.onmousedown = function(e)
	{
		dragObject = this;
		mouseOffset = GetMouseOffset(this, e);
		return false;
	}
}

document.onmousemove = MouseMoved;
document.onmouseup = MouseReleased;

var preloadImageTmpImage = null;
var preloadImageAction = "";
var preloadImageCounter = 0;
var preloadImageIntervalId = null;
var lastPreloadedImage = null;

function PreloadImage(url, onDone)
{
	if (preloadImageIntervalId != null)
	{
		clearInterval(preloadImageIntervalId);
		preloadImageIntervalId = null
	}
	var oldAction = preloadImageAction;
	preloadImageTmpImage = new Image(200, 200);
	preloadImageTmpImage.src = url;

	preloadImageCounter = 0;
	preloadImageAction = onDone;

	preloadImageIntervalId = setInterval("CheckImagePreloading();", 10);

	// fire the old action if any
	if (oldAction != "") eval(oldAction);
}

function CheckImagePreloading()
{
	preloadImageCounter++; // max 5 second

	if (preloadImageTmpImage == null ||
		preloadImageTmpImage.complete || preloadImageCounter > 500)
	{
		clearInterval(preloadImageIntervalId);
		lastPreloadedImage = preloadImageTmpImage;
		preloadImageIntervalId = null;
		preloadImageTmpImage = null;

		var oldAction = preloadImageAction;
		preloadImageAction = "";
		preloadImageCounter = 0;

		// fire the old action if any
		if (oldAction != "") eval(oldAction);
	}
}

var sessionLifeCounter = 0;

function KeepSessionAlive()
{
	var xhr = GetXHRObject();

	if (xhr == null)
	{
		return;
	}
	xhr.open("GET", "get.php?active=1", true);

	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4) // data received
		{
			var r = xhr.responseText;
			sessionLifeCounter++;
			if (sessionLifeCounter < 15)
				setTimeout("KeepSessionAlive();", 300000);
			// do nothing else
		}
	}
	// Send the request
	xhr.send(null);
}

function SetAutoScrollH(obj)
{
	obj.scrollDirection = 0;
	obj.onmousemove = function (e)
	{
		var mouseOffset = GetMouseOffset(this, e);
		if (mouseOffset.x < 60)
			this.scrollDirection = -1;
		else if (mouseOffset.x > this.offsetWidth - 60)
			this.scrollDirection = 1;
		else
			this.scrollDirection = 0;
	};
	obj.onmouseout = function(e)
	{
		// this.scrollDirection = 0;
	};
	setInterval("CheckScrollH(El('" + obj.id + "'));", 40);
}

function CheckScrollH(obj)
{
	if (obj.scrollDirection > 0)
	{
		var oldScroll = obj.scrollLeft;
		obj.scrollLeft += 15;
		if (oldScroll + 15 > obj.scrollLeft)
			obj.scrollDirection = 0;
	}
	else if (obj.scrollDirection < 0)
	{
		obj.scrollLeft -= 15;
		if (obj.scrollLeft <= 0)
			obj.scrollDirection = 0;
	}
}

function FadingIn(id, op, maxOp)
{
	if (!El(id)) return;
	if (El(id).fading != "in") return;

	op += 10;
	if (op > maxOp) op = maxOp;

	El(id).currentOpacity = op;
	El(id).style.opacity = op / 100;
	El(id).style.filter = "alpha(opacity=" + op + ")";
	if (op < maxOp)
		setTimeout("FadingIn('" + id + "', " + op + ", " + maxOp + ")", 40);
}

function FadingOut(id, op)
{
	if (!El(id)) return;
	if (El(id).fading != "out") return;

	op -= 10;
	if (op <= 0)
	{
		op = 0;
		El(id).style.display = "none";
	}
	El(id).currentOpacity = op;
	El(id).style.opacity = op / 100;
	El(id).style.filter = "alpha(opacity=" + op + ")";
	if (op > 0)
		setTimeout("FadingOut('" + id + "', " + op + ")", 40);
}

function FadeIn(id, maxOp, display, start)
{
	if (!El(id)) return;

	if (!maxOp) maxOp = 100;
	if (El(id).currentOpacity)
		op = El(id).currentOpacity;
	else
		op = 0;
	if (start && op < start)
		op = start;

	El(id).fading = "in";
	El(id).currentOpacity = op;
	El(id).style.opacity = op / 100;
	El(id).style.filter = "alpha(opacity=" + op + ")";
	if (!display)
		El(id).style.display = "";
	else
		El(id).style.display = display;
	FadingIn(id, op, maxOp);
}

function FadeOut(id, maxOp)
{
	if (!El(id)) return;

	if (!maxOp) maxOp = 100;
	if (El(id).currentOpacity)
		maxOp = El(id).currentOpacity;

	El(id).fading = "out";
	El(id).currentOpacity = maxOp;
	El(id).style.opacity = maxOp / 100;
	El(id).style.filter = "alpha(opacity=" + maxOp + ")";
	FadingOut(id, maxOp);
}

function PreloadJoinForm(url)
{
	var content = "<iframe src='" + url + "' style='border: none; width: 604px; height: 600px; background: none;' scrolling='no' frameborder='0' name='joinFrame'></iframe>";
	El("joinFormDiv").innerHTML = content;
}

function PopUpJoinForm(url)
{
	HideElem('popUpDiv', 'frameDiv');
	HideElem('imageDiv', 'imgFrameDiv');
	HideElem('galleryDiv', 'galFrameDiv');
	HideElem('playerDiv', 'playerFrameDiv');
	CancelPopUp(null);
	PopUp("joinFormDiv", 0, 0);
}

var cycleImagesList = { prefix: null, currentIndex: 2, count: 1 };

function CycleImages(prefix, currentIndex, count)
{
	cycleImagesList.prefix = prefix;
	cycleImagesList.count = count;

	if (currentIndex == 0)
		cycleImagesList.currentIndex = Math.min(cycleImagesList.currentIndex, count);
	else
		cycleImagesList.currentIndex = currentIndex;
}

function StopCycleImages(prefix)
{
	if (cycleImagesList.prefix == prefix)
		cycleImagesList.prefix = null;
}

function DoCycleImages()
{
	if (cycleImagesList.prefix !== null)
	{
		FadeOut(cycleImagesList.prefix + cycleImagesList.currentIndex);
		cycleImagesList.currentIndex++;
		if (cycleImagesList.currentIndex > cycleImagesList.count)
			cycleImagesList.currentIndex = 1;
		FadeIn(cycleImagesList.prefix + cycleImagesList.currentIndex);
	}
}

setInterval("DoCycleImages();", 1500);

function CancelPopUp(e)
{
	try
	{
		if (typeof natsShowExit != 'undefined')
			natsShowExit = false;
	} catch (ex) { }
}


var oldIE = false;

document.onmousedown = CancelPopUp;
document.onclick = CancelPopUp;