var debug = 0;
function gE(id)
{
	var e = document.getElementById(id);
	
	if(!e)
	{
		if(debug)
			alert("ERROR: Couldn't get element with id: "+id);
		
		return null;
	}
	
	return e;
}

function mE(ids)
{
	this.es = new Array();
	
	var et = ids.split(",");
	
	for(var i = 0; i < et.length; i++)
	{
		var e = gE(et[i]);
	
		if(!e)
			continue;
		
		this.es.push(e);
	}
	
	this.hide = function()
	{
		try
		{
			for(var i = 0; i < this.es.length; i++)
				this.es[i].style.display = "none";
		}
		catch(e)
		{
			if(debug)
				alert(e);
		}
	}
	
	this.show = function(display)
	{
		if(!display)
			display = "block";
		
		try
		{
			for(var i = 0; i < this.es.length; i++)
				this.es[i].style.display = display;
		}
		catch(e)
		{
			if(debug)
				alert(e);
		}
	}
	
	this.content = function(content)
	{
		try
		{
			for(var i = 0; i < this.es.length; i++)
				this.es[i].innerHTML = content;
		}
		catch(e)
		{
			if(debug)
				alert(e);
		}
	}
	
	this.style = function(style)
	{
		try
		{
			for(var i = 0; i < this.es.length; i++)
				this.es[i].style = style;
		}
		catch(e)
		{
			if(debug)
				alert(e);
		}
	}
	
	this.classname = function(className)
	{
		try
		{
			for(var i = 0; i < this.es.length; i++)
				this.es[i].className = className;
		}
		catch(e)
		{
			if(debug)
				alert(e);
		}
	}
	
	return this;
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
		  c_start=c_start + c_name.length+1;
		  c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			  return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function getScrollHeight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : getWindowHeight();
}

function getDocHeight()
{
  var d = document;
  return Math.max(
      Math.max(d.body.scrollHeight, d.documentElement.scrollHeight),
      Math.max(d.body.offsetHeight, d.documentElement.offsetHeight),
      Math.max(d.body.clientHeight, d.documentElement.clientHeight)
  );
}

function getWindowHeight()
{
  var d = document;
  return Math.min(
      Math.max(d.body.scrollHeight, d.documentElement.scrollHeight),
      Math.max(d.body.clientHeight, d.documentElement.clientHeight)
  );
}

function lbitshift(num, bits) {
    return num * Math.pow(2, bits);
}

function isIE()
{
  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function isMacChrome()
{
	return /chrome/i.test(navigator.userAgent) && /mac/i.test(navigator.userAgent);
}

var mouseEvents = new CMouseEvents();

if(!isIE())
{
  document.onmousemove = function(e){ mouseEvents.update(e, "onmousemove"); };
  document.onclick = function(e){ mouseEvents.update(e, "onclick"); };
}

function CMouseEvents()
{
	this.events = new Array();
	
	this.update = function(e, type)
	{
		switch(type)
		{
			case "onclick":
				try
				{
					e.positionX = mousex;
					e.positionY = mousey;
				}
				catch(e){}
			break;
			
			case "onmousemove":
				try
				{
					getMouse(e);
				}
				catch(e){}
			break;
		}
		
		for(var i in this.events)
		{
			if(this.events[i].type != type)
				continue;
			
			try
			{
				this.events[i].func(e);
			}
			catch(e)
			{
				if(debug)
					alert(e);
			}
		}
	}
	
	this.addEvent = function(name, type, func)
	{
		//search for the same event
		for(var i in this.events)
		{
			if(this.events[i].name == name)
			{
				this.events[i] = new mouseEvent(name, type, func);
				return;
			}
		}
		
		this.events.push(new mouseEvent(name, type, func));
	}
	
	this.removeEvent = function(name)
	{
		for(var i in this.events)
		{
			if(this.events[i].name == name)
			{
				this.events.splice(i, 1);
			}
		}
	}
}

function mouseEvent(name, type, func)
{
	this.name = name;
	this.type = type;
	this.func = func;
}

var mousex = 0
var mousey = 0

function getMouse(e)
{
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
  {
		mousex = e.pageX;
		mousey = e.pageY;
	}
	else if (e.clientX || e.clientY)
  {
		mousex = e.clientX + document.documentElement.scrollLeft;
		mousey = e.clientY + document.documentElement.scrollTop;
	}
}

function fixAspectRatio(image, size)
{
	image.style.display = "inline";
	
	if(image.width > image.height)
	{
		image.width = size;
		image.style.marginTop = Math.round((size / 2) - (image.height / 2)) + "px"; //horizontal centering
	}
	else
		image.height = size;
	
	try{ game.message.updatePosition(); }catch(e){}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function floatToPct(num)
{
	return Math.floor(num * 100);
}

function getURLVar(variable)
{
	var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++)
	{
    var pair = vars[i].split("=");
    if (pair[0] == variable)
		{
      return pair[1];
    }
  }
  
  return null;
}
