/*
	Flexible Modular Core Ultimate by Alesandro Ubriaco
	Object Manipulation
*/

var fc = {};

fc.Controllers = new Array();

fc.o = function(param, parentObj, c)
{
	if(!c)
	{
		for(var n = 0; n < fc.Controllers.length; n++)
		{
			if(fc.Controllers[n].Parent == parentObj && fc.Controllers[n].Param == param)
				return fc.Controllers[n];
		}
		c = new fc.ObjectController();
		c.Param = param;
		c.Parent = parentObj;
	}
	if(param.substr(0,1) == '#')
	{
		c.add(fc.get(param.substr(1)));
	}
	else if(param.indexOf('.') > -1)
	{
		var p = param.split('.');
		var objs = null;
		if(parentObj)
			objs = parentObj.getElementsByTagName(p[0]);
		else
			objs = document.getElementsByTagName(p[0]);
		for(var n = 0; n < objs.length; n++)
		{
			if(objs[n].className == p[1])
				c.add(objs[n]);
		}
	}
	else
	{
		var objs = null;
		if(parentObj)
			objs = parentObj.getElementsByTagName(param);
		else
			objs = document.getElementsByTagName(param);
		for(var n = 0; n < objs.length; n++)
		{
			c.add(objs[n]);
		}
	}
	fc.Controllers[fc.Controllers.length] = c;
	return c;
};

fc.ObjectController = function()
{
	var _self = this;
	this.Param = false;
	this.Parent = null;
	this.Objects = new Array();
	
	this.add = function(obj)
	{
		this.Objects[this.Objects.length] = obj;
		this.update();
	};
	
	this.appendChild = function(obj)
	{
		for(var n = 0; n < this.Objects.length; n++)
		{
			this.Objects[n].appendChild(obj);
		}
	};
	
	this.set = function(name, value)
	{
		for(var n = 0; n < this.Objects.length; n++)
		{
			this.Objects[n].setAttribute(name, value);
		}
	};
	
	this.get = function(name, index)
	{
		if(!index)
			index = 0;
		
		return this.Objects[index].getAttribute(name);
	};
	
	this.getContent = function(index)
	{
		if(!index)
			index = 0;
		return this.Objects[index].innerHTML;
	};
	
	this.getValue = function(index)
	{
		if(!index)
			index = 0;
			
		return this.Objects[index].value;
	};
	
	this.setValue = function(v)
	{
		for(var n = 0; n < this.Objects.length; n++)
		{
			this.Objects[n].value = v;
		}
	};
		
	this.onchange = function() { return true; };
	this.onfocus = function() { return true; };
	this.onblur = function() { return true; };
	this.onmouseover = function() { return true; };
	this.onmouseout = function() { return true; };
	this.onkeydown = function() { return true; };
	this.onkeyup = function() { return true; };
	this.onkeypress = function() { return true; };
	this.onclick = function() { return true; };
	this.onsubmit = function() { return true; };
	
	this.update = function()
	{
		for(var n = 0; n < this.Objects.length; n++)
		{
			this.Objects[n].onchange = function() { return _self.onchange(); };
			this.Objects[n].onfocus = function() { return _self.onfocus(); };
			this.Objects[n].onblur = function() { return _self.onblur(); };
			this.Objects[n].onmouseover = function() { return _self.onmouseover(); };
			this.Objects[n].onmouseout = function() { return _self.onmouseout(); };
			this.Objects[n].onkeydown = function() { return _self.onkeydown(); };
			this.Objects[n].onkeyup = function() { return _self.onkeyup(); };
			this.Objects[n].onkeypress = function() { return _self.onkeypress(); };
			this.Objects[n].onclick = function() { return _self.onkeypress(); };
			this.Objects[n].onsubmit = function() { return _self.onsubmit(); };
		}
	};
	
	this.setContent = function(c)
	{
		for(var n = 0; n < this.Objects.length; n++)
		{
			this.Objects[n].innerHTML = c;
		}
	};
	
	this.o = function(param)
	{
		var c = new fc.ObjectController();
		for(var n = 0; n < this.Objects.length; n++)
		{
			c = fc.o(param, this.Objects[n], c);
		}
		return c;
	};
	
};

fc.request = function(url, callback, method, data)
{
	if(!method)
		method = 'GET';
	if(!callback)
		callback = function(r) { };
	
	var objXMLHttp = null;
	try
	{
		objXMLHttp=new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				return false;	
			}
		}
	}
	
	objXMLHttp.open(method, url, true);
	objXMLHttp.onreadystatechange = function()
	{
		if(objXMLHttp.readyState == 4 || objXMLHttp.readyState == 'complete')
			callback(objXMLHttp.responseText);
	};
	
	if(method == 'POST')
	{
		objXMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		objXMLHttp.setRequestHeader("Content-length", data.length);
		objXMLHttp.setRequestHeader("Connection", "close");
		objXMLHttp.send(data);
	}
	else
		objXMLHttp.send(null);
	return true;
}

fc.get = function(param)
{
	var obj = document.getElementById(param);
	if(!obj)
	{
		obj = document.createElement('DIV');
		obj.id = param;
	}
	return obj;
};

fc.create = function(type)
{
	var obj = document.createElement(type);
	var c = new fc.ObjectController();
	c.add(obj);
	return c;
};


