var dragobject = 
{
	z: 0,
	x: 0,
	y: 0,
	offsetx : null,
	offsety : null,
	targetobj : null,
	dragapproved : 0,
	
	initialize:function()
	{
		document.onmousedown = this.drag
		document.onmouseup = function(){ if(tabMouseDown == true){ document.getElementById('aboutwindow-dragarea-' + currentTab).className = "drag"; tabMouseDown = false; } this.dragapproved = 0 }
	},
	
	drag:function(e)
	{
		var evtobj = window.event ? window.event : e
		this.targetobj = window.event ? event.srcElement : e.target
		if (this.targetobj.className == "drag")
		{
			this.dragapproved = 1
			
			if (isNaN(parseInt(this.targetobj.parentNode.style.left)))
			{
				this.targetobj.parentNode.style.left = 0
			}
			if (isNaN(parseInt(this.targetobj.parentNode.style.top)))
			{
				this.targetobj.parentNode.style.top = 0
			}
			
			this.offsetx = parseInt(this.targetobj.parentNode.style.left)
			this.offsety = parseInt(this.targetobj.parentNode.style.top)
			this.x = evtobj.clientX
			this.y = evtobj.clientY
			
			if (evtobj.preventDefault) evtobj.preventDefault()
			
			document.onmousemove = dragobject.moveit
		}
		else if(this.targetobj.id == "catalogue-l-1-r-scrollm-scrollbar")
		{
			this.dragapproved = 1
			
			this.x = evtobj.clientX
			this.y = evtobj.clientY
			
			if (evtobj.preventDefault) evtobj.preventDefault()
			
			document.onmousemove = dragobject.moveit
		}
	},
	
	moveit:function(e)
	{
		var evtobj = window.event ? window.event : e
		
		if(this.dragapproved == 1 && this.targetobj.id == "catalogue-l-1-r-scrollm-scrollbar")
		{
			var yOffset = parseInt(this.targetobj.parentNode.parentNode.parentNode.parentNode.parentNode.style.top.substr(0, this.targetobj.parentNode.parentNode.parentNode.parentNode.parentNode.style.top.length - 2)) + 56;
			var curY = evtobj.clientY - yOffset;
			
			var localScrollCount = Math.floor(curY / catalogueScrollbarChange);
			
			if(localScrollCount == 0)
			{
				catalogueScrollbarOffset = 0;
				catalogueScrollCount = localScrollCount;
			}
			else if(localScrollCount == catalogue.length - 5)
			{
				catalogueScrollbarOffset = 107;
				catalogueScrollCount = localScrollCount;
			}
			else
			{
				if(catalogueScrollCount < localScrollCount)
				{
					if(localScrollCount < catalogue.length - 5)
					{
						catalogueScrollbarOffset = localScrollCount * catalogueScrollbarChange;
						catalogueScrollCount = localScrollCount;
					}
				}
				else if(catalogueScrollCount > localScrollCount)
				{
					if(localScrollCount > 0)
					{
						catalogueScrollbarOffset = localScrollCount * catalogueScrollbarChange;
						catalogueScrollCount = localScrollCount;
					}
				}
			}
			
			document.getElementById('catalogue-l-1-l-1').innerHTML = catalogue[catalogueScrollCount][0];
			document.getElementById('catalogue-l-1-l-1').onclick = function() { showCatPage(catalogue[catalogueScrollCount][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-2').innerHTML = catalogue[catalogueScrollCount + 1][0];
			document.getElementById('catalogue-l-1-l-2').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 1][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-3').innerHTML = catalogue[catalogueScrollCount + 2][0];
			document.getElementById('catalogue-l-1-l-3').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 2][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-4').innerHTML = catalogue[catalogueScrollCount + 3][0];
			document.getElementById('catalogue-l-1-l-4').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 3][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-5').innerHTML = catalogue[catalogueScrollCount + 4][0];
			document.getElementById('catalogue-l-1-l-5').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 4][1], 0); return false; };
			
			document.getElementById('catalogue-l-1-r-scrollm-scrollbar').style.marginTop = catalogueScrollbarOffset + "px";
			
			return false
		}
		else if (this.dragapproved == 1)
		{
			this.targetobj.parentNode.style.left = this.offsetx + evtobj.clientX - this.x + "px"
			this.targetobj.parentNode.style.top = this.offsety + evtobj.clientY - this.y + "px"
			
			return false
		}
	}
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function openAboutWindow()
{
	currentTab = 1;
	tabMouseDown = false;
	document.getElementById('aboutwindow-dragarea-1').className = "drag"
	document.getElementById('aboutwindow-dragarea-2').className = ""
	document.getElementById('aboutwindow-dragarea-3').className = ""
	document.getElementById('aboutwindow-body-1').style.display = "block"
	document.getElementById('aboutwindow-body-1').scrollTop = 0
	document.getElementById('aboutwindow-body-2').style.display = "none"
	document.getElementById('aboutwindow-body-3').style.display = "none"
	document.getElementById('aboutwindow').style.backgroundImage = "url(../images/about_bg_1.png)"
	document.getElementById('aboutwindow').style.top = (window.innerHeight) ? ((window.innerHeight  - (225 + 55)) / 2) + "px" : ((document.documentElement.clientHeight - (225 + 55)) / 2) + "px"
	document.getElementById('aboutwindow').style.left = (window.innerWidth) ? ((window.innerWidth  - 321) / 2) + "px" : ((document.documentElement.clientWidth - 321) / 2) + "px"
	document.getElementById('aboutwindow').style.display = "block"
}

var currentTab = 1;
var tabMouseDown = false;

function changeTab(e)
{
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
		
	var id = parseInt(targ.id.substr(targ.id.length - 1, 1));
	if(currentTab != id)
	{
		if(e.preventDefault)
		{
			e.preventDefault();
		}
		tabMouseDown = true;
		document.getElementById('aboutwindow').style.backgroundImage = "url(../images/about_bg_" + id + ".png)"
		document.getElementById('aboutwindow-dragarea-' + currentTab).className = ""
		document.getElementById('aboutwindow-body-' + currentTab).style.display = "none"
		document.getElementById('aboutwindow-body-' + id).style.display = "block"
		document.getElementById('aboutwindow-body-' + id).scrollTop = 0
		currentTab = id;
	}
}

function createWindow(id)
{
	document.getElementById('window').style.backgroundImage = "url(/images/profile_" + id.toLowerCase() + ".png)"
	document.getElementById('window').style.top = (window.innerHeight) ? ((window.innerHeight  - (163 + 55)) / 2) + "px" : ((document.documentElement.clientHeight - (163 + 55)) / 2) + "px"
	document.getElementById('window').style.left = (window.innerWidth) ? ((window.innerWidth  - 294) / 2) + "px" : ((document.documentElement.clientWidth - 294) / 2) + "px"
	document.getElementById('window-mail-i').onclick = function(){ openContact(id); return false; }
	document.getElementById('window').style.display = "block"
}

var currentContactId = "";

function openContact(id)
{
	currentContactId = id
	document.getElementById('contactwindow-tb').innerHTML = id
	document.getElementById('contactwindow-ta-i').value = ""
	document.getElementById('contactwindow-send-i').onclick = function(){ sendMessage(); return false; }
	document.getElementById('contactwindow').style.top = (window.innerHeight) ? ((window.innerHeight  - (280 + 55)) / 2) + "px" : ((document.documentElement.clientHeight - (280 + 55)) / 2) + "px"
	document.getElementById('contactwindow').style.left = (window.innerWidth) ? ((window.innerWidth  - 271) / 2) + "px" : ((document.documentElement.clientWidth - 271) / 2) + "px"
	document.getElementById('contactwindow').style.backgroundImage = "url(/images/contactwindow_bg.png)"
	document.getElementById('contactwindow-tb').style.display = "block"
	document.getElementById('contactwindow-ta-i').style.display = "block"
	document.getElementById('contactwindow-send-i').style.display = "block"
	document.getElementById('contactwindow').style.display = "block"
}

function showAlert(msg)
{
	document.getElementById('alertwindow-tb').innerHTML = msg;
	document.getElementById('alertwindow').style.top = (window.innerHeight) ? ((window.innerHeight  - (163 + 55)) / 2) + "px" : ((document.documentElement.clientHeight - (163 + 55)) / 2) + "px"
	document.getElementById('alertwindow').style.left = (window.innerWidth) ? ((window.innerWidth  - 295) / 2) + "px" : ((document.documentElement.clientWidth - 295) / 2) + "px"
	document.getElementById('alertwindow').style.display = "block"
}

function sendMessage()
{	
	if(document.getElementById('contactwindow-ta-i').value != "")
	{
		if(window.XMLHttpRequest)
		{
			var xmlhttp = new XMLHttpRequest();
		}
		else
		{
			var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.open("GET", "/home/contact/" + currentContactId + "/" + Url.encode(document.getElementById('contactwindow-ta-i').value) + "/", true);
		xmlhttp.send(null);
		
		document.getElementById('contactwindow').style.backgroundImage = "url(/images/contactwindow_sent_bg.png)"
		document.getElementById('contactwindow-tb').style.display = "none"
		document.getElementById('contactwindow-ta-i').style.display = "none"
		document.getElementById('contactwindow-send-i').style.display = "none"
	}
	else
	{
		showAlert("Please fill in a message to send.");
	}
}

function login()
{
	var username = document.cl_f.cl_u.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	var password = document.cl_f.cl_p.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	
	if(username != "")
	{
		if(password != "")
		{
			if(window.XMLHttpRequest)
			{
				var xmlhttp = new XMLHttpRequest();
			}
			else
			{
				var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			xmlhttp.open("GET", "/control/index/" + Url.encode(username) + "/" + Url.encode(password) + "/", true);
			xmlhttp.send(null);
			
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4 && xmlhttp.responseText == "X")
				{
					window.location = "/";
				}
			}
		}
	}
}

function openCatalogue()
{
	document.getElementById('catalogue').style.top = (window.innerHeight) ? ((window.innerHeight  - (371 + 55)) / 2) + "px" : ((document.documentElement.clientHeight - (371 + 55)) / 2) + "px"
	document.getElementById('catalogue').style.left = (window.innerWidth) ? ((window.innerWidth  - 471) / 2) + "px" : ((document.documentElement.clientWidth - 471) / 2) + "px"
	document.getElementById('catalogue').style.display = "block"
}

var catalogueScrollCount = 0;

var catalogueScrollbarChange = Math.round(107 / (catalogue.length - 5));

var catalogueScrollbarOffset = 0;

var currentCat = [1, 0];

function catScroll(d)
{
	if(d == 0)
	{
		if(catalogueScrollCount != 0)
		{
			catalogueScrollCount -= 1;
			document.getElementById('catalogue-l-1-l-1').innerHTML = catalogue[catalogueScrollCount][0];
			document.getElementById('catalogue-l-1-l-1').onclick = function() { showCatPage(catalogue[catalogueScrollCount][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-2').innerHTML = catalogue[catalogueScrollCount + 1][0];
			document.getElementById('catalogue-l-1-l-2').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 1][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-3').innerHTML = catalogue[catalogueScrollCount + 2][0];
			document.getElementById('catalogue-l-1-l-3').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 2][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-4').innerHTML = catalogue[catalogueScrollCount + 3][0];
			document.getElementById('catalogue-l-1-l-4').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 3][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-5').innerHTML = catalogue[catalogueScrollCount + 4][0];
			document.getElementById('catalogue-l-1-l-5').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 4][1], 0); return false; };
			
			if(catalogueScrollCount != 0)
			{
				catalogueScrollbarOffset -= catalogueScrollbarChange;
			}
			else
			{
				catalogueScrollbarOffset = 0;
			}
			
			document.getElementById('catalogue-l-1-r-scrollm-scrollbar').style.marginTop = catalogueScrollbarOffset + "px";
		}
	}
	else
	{
		if(catalogueScrollCount != catalogue.length - 5)
		{
			catalogueScrollCount += 1;
			document.getElementById('catalogue-l-1-l-1').innerHTML = catalogue[catalogueScrollCount][0];
			document.getElementById('catalogue-l-1-l-1').onclick = function() { showCatPage(catalogue[catalogueScrollCount][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-2').innerHTML = catalogue[catalogueScrollCount + 1][0];
			document.getElementById('catalogue-l-1-l-2').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 1][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-3').innerHTML = catalogue[catalogueScrollCount + 2][0];
			document.getElementById('catalogue-l-1-l-3').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 2][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-4').innerHTML = catalogue[catalogueScrollCount + 3][0];
			document.getElementById('catalogue-l-1-l-4').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 3][1], 0); return false; };
			document.getElementById('catalogue-l-1-l-5').innerHTML = catalogue[catalogueScrollCount + 4][0];
			document.getElementById('catalogue-l-1-l-5').onclick = function() { showCatPage(catalogue[catalogueScrollCount + 4][1], 0); return false; };
			
			if(catalogueScrollCount != catalogue.length - 5)
			{
				catalogueScrollbarOffset += catalogueScrollbarChange;
			}
			else
			{
				catalogueScrollbarOffset = 107;
			}
			
			document.getElementById('catalogue-l-1-r-scrollm-scrollbar').style.marginTop = catalogueScrollbarOffset + "px";
		}
	}
}

function showCatPage(id, pageNo)
{
	document.getElementById('catalogue-r').innerHTML = '<div id="catalogue-r-loading"><img alt="" src="/images/catalogue_loading.gif" /></div>';
	
	if(window.XMLHttpRequest)
	{
		var xmlhttp = new XMLHttpRequest();
	}
	else
	{
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open("GET", "/home/cat/" + id + "/" + pageNo + "/", true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4)
		{
			document.getElementById('catalogue-r').innerHTML = xmlhttp.responseText;
			currentCat = [id, pageNo];
		}
	}
}

function showCatItem(id)
{
	document.getElementById('catalogue-r').innerHTML = '<div id="catalogue-r-loading"><img alt="" src="/images/catalogue_loading.gif" /></div>';
	
	if(window.XMLHttpRequest)
	{
		var xmlhttp = new XMLHttpRequest();
	}
	else
	{
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open("GET", "/home/item/" + id + "/", true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4)
		{
			document.getElementById('catalogue-r').innerHTML = xmlhttp.responseText;
		}
	}
}

function goBack()
{
	showCatPage(currentCat[0], currentCat[1]);
}

function stockCatalogue()
{
	catalogueScrollCount = 0;
	document.getElementById('catalogue-l-1-l-1').innerHTML = catalogue[0][0];
	document.getElementById('catalogue-l-1-l-1').onclick = function() { showCatPage(catalogue[0][1], 0); return false; };
	document.getElementById('catalogue-l-1-l-2').innerHTML = catalogue[1][0];
	document.getElementById('catalogue-l-1-l-2').onclick = function() { showCatPage(catalogue[1][1], 0); return false; };
	document.getElementById('catalogue-l-1-l-3').innerHTML = catalogue[2][0];
	document.getElementById('catalogue-l-1-l-3').onclick = function() { showCatPage(catalogue[2][1], 0); return false; };
	document.getElementById('catalogue-l-1-l-4').innerHTML = catalogue[3][0];
	document.getElementById('catalogue-l-1-l-4').onclick = function() { showCatPage(catalogue[3][1], 0); return false; };
	document.getElementById('catalogue-l-1-l-5').innerHTML = catalogue[4][0];
	document.getElementById('catalogue-l-1-l-5').onclick = function() { showCatPage(catalogue[4][1], 0); return false; };
	showCatPage(1, 0);
	catalogueScrollbarOffset = 0;
	document.getElementById('catalogue-l-1-r-scrollm-scrollbar').style.marginTop = 0;
}

var isCtrl = false;

document.onkeyup = function(e)
{
	if(e.which == 17) isCtrl=false;
}

document.onkeydown = function(e)
{
	if(e.which == 17) isCtrl = true;
	
	if(e.which == 13 && isCtrl == true)
	{
		document.getElementById('control').style.display = "block";
		return false;
	}
}

dragobject.initialize()

function showonline()
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('http://www.lasuni.com/online.php', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=200,height=200,left = 320,top = 150');");
}

function usersOnline()
{
	if(window.XMLHttpRequest)
	{
		var xmlhttp = new XMLHttpRequest();
	}
	else
	{
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open("GET", "/api/online/", true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4)
		{
			var count = eval("(" + xmlhttp.responseText + ')');
			
			if(count.error == false)
			{
				count = count.online.split(" ")[0];
				
				if(count == 1)
				{
					document.getElementById('logo').innerHTML = "<img alt=\"Users Online\" src=\"/images/usersonline.png\"> <span style=\"color:#000000;\">1</span> user playing Lasuni";
				}
				else
				{
					document.getElementById('logo').innerHTML = "<img alt=\"Users Online\" src=\"/images/usersonline.png\"> <span style=\"color:#000000;\">" + count + "</span> users playing Lasuni";
				}
			}
			else
			{
				document.getElementById('logo').innerHTML = count.error;
			}
		}
	}
}

