var mousex = 0;
var mousey = 0;
var startx = 0;
var starty = 0;
var left = 0;
var top = 0;
var moving = false;
var loading = '<div style="margin: auto; margin-top: 50px; text-align: center;" class="heading">Loading...</span>';
var updateTimer;

function getmouse(e) {
	mousex = e.pageX;
	mousey = e.pageY;

	if (mousex < 0){mousex = 0;}
	if (mousey < 0){mousey = 0;}

	if (moving) {
		document.getElementById('chatWindow').style.left = (left + mousex - startx) + 'px';
		if (top+mousey-starty > -30)
			document.getElementById('chatWindow').style.top = (top + mousey-starty) + 'px';
	}
}

function createCooke(name, val) {
	document.cookie = name+'='+value;
}

function startMove() {
	setSizes();
	startx = mousex;
	starty = mousey;
	moving = true;
}

function hideChat() {
	Effect.DropOut('chatWindow');
	setCookie('display', '');
	window.clearInterval(updateTimer);
}

function showChat(quickShow) {
	setCookie('display', 'block');
	updateChat();
	updateTimer = window.setInterval("updateChat()", 2000);

	document.getElementById('chatWindow').style.left = readCookie('left');
	document.getElementById('chatWindow').style.top = readCookie('top');
	if (quickShow == null)
		$('chatWindow').appear({duration: 0.3});
	else
		$('chatWindow').show();
}

function setSizes() {
	left = document.getElementById('chatWindow').style.left;
	left = Number(left.substring(0, left.length-2));
	top = document.getElementById('chatWindow').style.top;
	top = Number(top.substring(0, top.length-2));
}

function stopMove() {
	moving = false;
	setSizes();
	setCookie('left', left+'px');
	setCookie('top', top+'px');
}

function updateChat() {
	xmlHttpMain = new XMLHttpRequest();
	xmlHttpMain.onreadystatechange = function() {
		if (xmlHttpMain.readyState==4) {
			document.getElementById('chatmain').innerHTML = xmlHttpMain.responseText;
			document.getElementById('chatmain').scrollTop = 400;
		}
	}
	xmlHttpMain.open("GET", '/ajax.php?p=chat&act=chat', true);
	xmlHttpMain.send(null);	

	xmlHttpUsers = new XMLHttpRequest();
	xmlHttpUsers.open("GET", '/ajax.php?p=chat&act=users', true);
	xmlHttpUsers.onreadystatechange = function() {
		if (xmlHttpUsers.readyState==4) {
			document.getElementById('chatusers').innerHTML = xmlHttpUsers.responseText;
		}
	}
	xmlHttpUsers.send(null);
}

function sendMsg(msg, action) {
	xmlHttp = new XMLHttpRequest();
	xmlHttp.open("GET", '/ajax.php?p=chat&act=msg&action='+action+'&msg='+msg.replace(/\&/gi, '%26'), true);
	xmlHttp.send(null);
}

function submitForm(action) {
	msg = document.getElementById('chatText').value;
	sendMsg(msg, action);
	document.getElementById('chatText').value='';
	document.getElementById('chatText').focus();
	updateChat();
}

function changeMainLinks() {
	links = document.getElementById('main').getElementsByTagName('a');

	for (var i = 0; i < links.length; i++) {
		if (links[i].href.substring(0, 29) != 'http://recharged.info/upload/' && links[i].href.substring(0, 21) == 'http://recharged.info' && links[i].href.substring(0, 32) != 'http://recharged.info/webalizer/')
			if (links[i].onclick == null)
				links[i].onclick = linkClicked;
	}
}

function linkClicked(e) {
	document.getElementById('permalink').href=this.href;
	document.getElementById('main').innerHTML = loading;

	xmlHttp = new XMLHttpRequest();

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			var r = xmlHttp.responseText;
			if (r.substring(0, 10) == 'Location: ') location.href=r.substring(10);
			else {
				document.getElementById('main').innerHTML = r;
				changeMainLinks();
			}
		}
	}

	if (this.href.substring(this.href.length-1) == '/')
		xmlHttp.open("GET", this.href+'?hide', true);
	else
		xmlHttp.open("GET", this.href+'/?hide', true);

	xmlHttp.send(null);
	return false;
}

function changeLinks() {
	links = document.getElementById('menu').getElementsByTagName('a');

	for (var i = 0; i < links.length; i++) {
		if (links[i].href == location.href)
		links[i].onclick = function() {
			document.getElementById('permalink').href=this.href;
			document.getElementById('main').innerHTML = loading;
			xmlHttp = new XMLHttpRequest();
			xmlHttp.onreadystatechange = function() {
				if (xmlHttp.readyState==4) {
					document.getElementById('main').innerHTML = xmlHttp.responseText;
					changeMainLinks();
				}
			}
			if (this.href.substring(this.href.length-1) == '/')
				xmlHttp.open("GET", this.href+'?hide', true);
			else
				xmlHttp.open("GET", this.href+'/?hide', true);
			xmlHttp.send(null);
			return false;
		};
	}

	changeMainLinks();
}

function hideCorner() {
	Effect.BlindUp('corner', {queue:'end', duration: 0.4});
	Effect.BlindDown('tab', {queue:'end', duration: 0.1});
}

function showCorner() {
	Effect.BlindDown('corner',{queue:'end', duration: 0.4});
	Effect.BlindUp('tab', {queue:'end',duration: 0.4});
}

function readCookie(name) {
	var name = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
	}
}

function setCookie(name, val) {
	document.cookie = name+'='+val+'; path=/';
}
