
function delay(msecs) 
{
	var date = new Date();
	var curDate = null;

	do
	{
		curDate = new Date();
	} 
	while(curDate - date < msecs);
}

function sqr(x)
{
	return x * x;
}

function distance(x, y, x1, y1)
{
	return Math.sqrt(distanceSqr(x, y, x1, y1));
}

function distanceSqr(x, y, x1, y1)
{
	return sqr(x1 - x) + sqr(y1 - y);
}

this.getDirection = function(x, y, x1, y1)
{
	return -Math.PI/2 + Math.atan2(-(x - x1), y - y1);
}

function Vec2(_x, _y)
{
	this.x = _x;
	this.y = _y;
}

this.pointToLineDistance = function(x, y, x0, y0, x1, y1, o){
    function lineLength(x, y, x0, y0){
        return Math.sqrt((x -= x0) * x + (y -= y0) * y);
    }
    if(o && !(o = function(x, y, x0, y0, x1, y1){
        if(!(x1 - x0)) return {x: x0, y: y};
        else if(!(y1 - y0)) return {x: x, y: y0};
        var left, tg = -1 / ((y1 - y0) / (x1 - x0));
        return {x: left = (x1 * (x * tg - y + y0) + x0 * (x * - tg + y - y1)) / (tg * (x1 - x0) + y0 - y1), y: tg * left - tg * x + y};
    }(x, y, x0, y0, x1, y1), o.x >= Math.min(x0, x1) && o.x <= Math.max(x0, x1) && o.y >= Math.min(y0, y1) && o.y <= Math.max(y0, y1))){
        var l1 = lineLength(x, y, x0, y0), l2 = lineLength(x, y, x1, y1);
        return l1 > l2 ? l2 : l1;
    }
    else {
        var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
        return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
    }
};

function deg2rad(degrees)
{
	return (Math.PI/180) * degrees;
}

function ParseXMLString(string)
{
	if (window.DOMParser)
	{
		parser=new DOMParser();
		xmlDoc=parser.parseFromString(string,"text/xml");
	}
	else // Internet Explorer
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(string); 
	}
	
	return xmlDoc;
}

function GetXMLRequest()
{
	try
	{
		xmlhttp = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E)
			{
				try
				{
					xmlhttp = window.createRequest();
				}
				catch (e)
				{
					gConsole.AssertNow("Unable to create http request");
					xmlhttp = null;
				}
			}
		}
	}
	
	return xmlhttp;
}

function Command(command)
{
	try
	{
		xmlhttp = new XMLHttpRequest();
		var noCache = "?nocache=" + Math.floor(Math.random() * Math.pow(2, 32));
		xmlhttp.open("GET", "command/" + command + noCache, false);
		xmlhttp.setRequestHeader('Command', 'text');
		xmlhttp.send();
		return xmlhttp;
	}
	catch (e)
	{
		return null;
	}
}

var gCommands = (Command("test") != null)

function OpenXML(filename, callback)
{
	xmlhttp = GetXMLRequest();

	xmlhttp.open("GET", filename, true);
	
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4)
		{
			callback(xmlhttp.responseText);
		}
	}
	
	xmlhttp.send(null);
	//setTimeout("xmlhttp.send(null)", 1000);
}

Array.prototype.push_unique = function(object)
{
	var length = this.length;
	for(var i=0; i<length; i++)
	{
		if(this[i] == object)
		{
			return false;
		}
	}
	
	this.push(object);
	return true;
};


Array.prototype.shuffle = function()
{
	for (var i = 0; i < this.length; i++)
	{
		var rand = Math.floor(Math.random() * this.length);
		var temp = this[i];
		this[i] = this[rand];
		this[rand] = temp;
	}
};

Math.clamp = function(value, min, max)
{
	return Math.min(Math.max(value, min), max);
};

Math.sqr = function(x)
{
	return x * x;
}

Math.randomRange = function(min, max)
{
	return Math.random() * (max - min) + min;
}

Object.extend = function(destination, source) {
    for (var property in source) {
        if (source.hasOwnProperty(property)) {
            destination[property] = source[property];
        }
    }
    return destination;
};

var gDebugVars = {};
function getUrlVars() {
    var vars = {};
	if (!gCommands)
	{
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
		function(m,key,value)
		{
			vars[key] = value.replace("#", "");
		});
		Object.extend(vars, gDebugVars);
	}
    return vars;
}

function Capatilize(text)
{
	return text.charAt(0).toUpperCase() + text.slice(1);
}

function LerpPos(speed)
{
	this.x = 0;
	this.y = 0;
	this.speed = speed;
	this.tx = 0;
	this.ty = 0;
	
	this.Update = function()
	{
		this.x += (this.tx - this.x) * this.speed;
		this.y += (this.ty - this.y) * this.speed;
	}
	
	this.SetTarget = function(tx, ty)
	{
		this.tx = tx;
		this.ty = ty;
	}
	
	this.SetPos = function(x, y)
	{
		this.x = x;
		this.y = y;
		this.tx = x;
		this.ty = y;
	}
	
	this.SetSpeed = function(speed)
	{
		this.speed = speed;
	}
}

function LerpPoint(speed)
{
	this.point = 0;
	this.speed = speed;
	this.tpoint = 0;
	
	this.Update = function()
	{
		this.point += (this.tpoint - this.point) * this.speed;
	}
	
	this.SetTarget = function(tpoint)
	{
		this.tpoint = tpoint;
	}
	
	this.SetPos = function(point)
	{
		this.point = point;
		this.tpoint = point;
	}
	
	this.SetSpeed = function(speed)
	{
		this.speed = speed;
	}
}


function SecondsToString(seconds)
{
	var text = "";
	var minutes = Math.floor(seconds/60);
	var hours = Math.floor(minutes/60);
	
	var displaySeconds = (seconds % 60);
	var secondSeperator = displaySeconds < 10 ? ":0" : ":";
	var displayMinutes = (minutes % 60);

	if (hours > 0)
	{
		var minuteSeperator = displayMinutes < 10 ? ":0" : ":";
		text += hours + minuteSeperator;
	}

	text += displayMinutes + secondSeperator + displaySeconds;
	
	return text;
}
