function gE(id)
{
	var e = document.getElementById(id);
	
	if(e)
	{
		e.getSize = function()
		{
			return new CVec2D(this.clientWidth, this.clientHeight);
		};
		
		e.getPosition = function()
		{
			var x = 0;
			var y = 0;
			
			for(var obj = this; obj != null; obj = obj.offsetParent)
			{
				x += obj.offsetLeft;
				y += obj.offsetTop;
			}
			
			return new CVec2D(x, y);
		};
		
		e.getRelativePosition = function()
		{
			var p = this.target.getPosition();
			return new CVec2D(this.pageX - p.x, this.pageY - p.y);
		};
		
		e.getSelection = function()
		{
			var el = this;
			if (el.selectionStart) { 
			return el.selectionStart; 
			} else if (document.selection) { 
				el.focus(); 
				
				var r = document.selection.createRange(); 
				if (r == null) { 
				  return 0; 
				} 
				
				var re = el.createTextRange(), 
				    rc = re.duplicate(); 
				re.moveToBookmark(r.getBookmark()); 
				rc.setEndPoint('EndToStart', re); 
				
				return rc.text.length; 
			}
		
			return 0; 
		};
	}
	
	return e;
}

/*
function gE(id){ return document.getElementById(id); }

Element.prototype.getSize = function()
{
	return new CVec2D(this.clientWidth, this.clientHeight);
}

Element.prototype.getPosition = function()
{
	var x = 0;
	var y = 0;
	
	for(var obj = this; obj != null; obj = obj.offsetParent)
	{
		x += obj.offsetLeft;
		y += obj.offsetTop;
	}
	
	return new CVec2D(x, y);
}

Event.prototype.getRelativePosition = function()
{
	var p = this.target.getPosition();
	return new CVec2D(this.pageX - p.x, this.pageY - p.y);
}
*/

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.removeClass = function(name)
	{
		try
		{
			for(var i = 0; i < this.es.length; i++)
			{
				var cn = this.es[i].className.split(" ");
				for(var j = 0; j < cn.length; j++)
				{
					if(cn[j] == name)
					{
						cn.splice(j, 1);
						j--;
					}
				}
				this.es[i].className = cn.join(" ");
			}
		}
		catch(e)
		{
			if(debug)
				alert(e);
		}
	};
	
	this.addClass = function(name)
	{
		try
		{
			for(var i = 0; i < this.es.length; i++)
			{
				this.es[i].className += " "+name;
			}
		}
		catch(e)
		{
			if(debug)
				alert(e);
		}
	};
	
	this.hide = function()
	{
		this.removeClass("visible");
		this.addClass("hidden");
	};
	
	this.show = function(display)
	{
		this.removeClass("hidden");
		this.addClass("visible");
	};
	
	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);
		}
	};
	
	this.enable = function()
	{
		try
		{
			for(var i = 0; i < this.es.length; i++)
				this.es[i].disabled = false;
		}
		catch(e)
		{
			if(debug)
				alert(e);
		}
	};
	
	this.disable = function()
	{
		try
		{
			for(var i = 0; i < this.es.length; i++)
				this.es[i].disabled = true;
		}
		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 CVec2D(x, y)
{
	this.x = x;
	this.y = y;
	
	this.normalize = function()
	{
		var l = Math.sqrt(this.x * this.x + this.y * this.y);
		return new CVec2D(this.x / l, this.y / l);
	};
	
	this.dot = function(vector)
	{
		return this.x * vector.x + this.y * vector.y;
	};
	
	this.pdot = function(vector)
	{
		return this.x * vector.y - this.y * vector.x;
	};
	
	this.reflect = function(normal)
	{
		var r = new CVec2D(0, 0);
		var d = this.dot(normal);
		r.x = 2 * d * normal.x - this.x;
		r.y = 2 * d * normal.y - this.y;
		return r;
	};
}

function CVec3D(x, y, z)
{
	this.x = x;
	this.y = y;
	this.z = z;
}

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

function CEvents()
{
	this.events = new Array();
	
	this.add = function(name, func)
	{
		this.events.push(new CEvent(name, func));
	};
	
	this.remove = function(name)
	{
		for(var i = 0; i < this.events.length; i++)
		{
			if(this.events[i].name == name)
			{
				this.events.splice(i, 1);
				i--;
			}
		}
	};
	
	this.fire = function(self, e)
	{
		for(var i = 0; i < this.events.length; i++)
		{
			this.events[i].func(self, e);
		}
	};
}

function CEventManager()
{
	this.events = new Array();
	
	this.events["MOUSE_MOVE"] = new CEvents();
	this.events["MOUSE_LDOWN"] = new CEvents();
	this.events["MOUSE_LUP"] = new CEvents();
	
	this.events["TOUCH_MOVE"] = new CEvents();
	this.events["TOUCH_START"] = new CEvents();
	this.events["TOUCH_END"] = new CEvents();
}

function CTouch(eventManager)
{
	this.canvas = null;
	this.eventManager = eventManager;
		
	this.ontouchstart = function(self, e)
	{	
		this.eventManager.events["TOUCH_START"].fire(self, e);
	};
	
	this.ontouchend = function(self, e)
	{
		this.eventManager.events["TOUCH_END"].fire(self, e);
	};
	
	this.ontouchmove = function(self, e)
	{		
		this.eventManager.events["TOUCH_MOVE"].fire(self, e);
	};
	
	this.init = function()
	{
		var self = this;
		
		this.canvas = gE("canvas");
		this.canvas.ontouchmove = function(e){ e.preventDefault(); sys.input.touch.ontouchmove(self, e); };
		this.canvas.ontouchstart = function(e){ sys.input.touch.ontouchstart(self, e); };
		this.canvas.ontouchend = function(e){ sys.input.touch.ontouchend(self, e); };
	};
}

function CMouse(eventManager)
{
	this.canvas = null;
	this.eventManager = eventManager;
		
	this.onmousedown = function(self, e)
	{
		this.eventManager.events["MOUSE_LDOWN"].fire(self, e);
	};
	
	this.onmouseup = function(self, e)
	{
		this.eventManager.events["MOUSE_LUP"].fire(self, e);
	};
	
	this.onmousemove = function(self, e)
	{
		this.eventManager.events["MOUSE_MOVE"].fire(self, e);
	};
	
	this.init = function()
	{
		var self = this;
		
		this.canvas = gE("canvas");
		this.canvas.onmousemove = function(e){ sys.input.mouse.onmousemove(self, e); };
		this.canvas.onmousedown = function(e){ sys.input.mouse.onmousedown(self, e); };
		this.canvas.onmouseup = function(e){ sys.input.mouse.onmouseup(self, e); };
	};
}

function CInput()
{
	this.eventManager = new CEventManager();
	this.touch = new CTouch(this.eventManager);
	this.mouse = new CMouse(this.eventManager);
	
	this.init = function()
	{
		if(window.Touch) //touch screen devices
		{
			this.touch.init();	
		}
		else
			this.mouse.init();
	};
}

function CTools()
{
	this.browserDetection = browserDetection;
	
	this.init = function()
	{
		this.browserDetection.init();
	};
}

var browserDetection = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

