/**
 * @author User
 */

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

var Element = function(){}

Extend_Filhos = function(obj, yy){
	Object.extend(obj, Element.prototype);
	//alert(obj.id)
	for(a in obj.childNodes){
		if (typeof obj.childNodes[a] == "object"){
			if (obj.childNodes[a].id){
				//alert(obj.childNodes[a].id)
				obj[obj.childNodes[a].id] = obj.childNodes[a] 
			}else{
				//extende os metodos aos nodes qui nao tem id
				//sendo qui no IE não se pode extender para os nodes do tipo text 
				/*if (yy == "1"){
					alert("node:" + obj.childNodes[a].nodeValue)
				}*/
				if (!(obj.childNodes[a].nodeType == "3" && document.all)){
					//alert("entrou: " + obj.childNodes[a].nodeValue)
					Object.extend(obj.childNodes[a], Element.prototype);
				}
			}
			if (obj.childNodes[a].childNodes.length > 0){Extend_Filhos(obj.childNodes[a], "1")}
		}
	}	
}

Object.extend = function(objDestination, objSource) {
	if(typeof objSource == "undefined") {
		objSource = objDestination;
		objDestination = this;
		//alert("id :" + this.id)
	}
	//alert("extend :" + objDestination.innerHTML)
	for(var property in objSource){
		//alert(property)
		objDestination[property] = objSource[property];
	}
	
	//Extend_Filhos(objDestination)
	return objDestination;
}

/*Element.extend = function(objSource) {
	//alert("2")
	return Object.extend(Element.prototype, objSource);
}*/

// Adaptado da Mootools
var $ = function(el) {
	if (typeof el == "string"){el = document.getElementById(el)};
	//el.extend = Object.extend;
	el.Extend_Filhos = Extend_Filhos
	//alert(Object.extend)
	el.Extend_Filhos(el);
	return el;
}
//document.removeChild
//document.insertBefore()
/*Element.extend({
  	"metodo1": function() { alert(this.innerHTML + " 1111"); },
	"metodo2": function() { alert(this.innerHTML + "2222"); },
	"metodo3": function() { alert(this.id); }
});*/

var FG = {
	events: function() {
		if (!FG._eventsFactory) throw "FG Events module isn't loaded";
		return FG._eventsFactory
		//event = FG.events().fix(ev) ex de como usar
	}
}

add_El = function(Elm, Conteudo, Atributos, Pai){
	//if (typeof Pai != "object"){Pai = this}

	Elm_temp =  document.createElement(Elm)
	if (Conteudo != ""){
		Text_node = document.createTextNode(Conteudo)
		Elm_temp.appendChild(Text_node)
	}
	
	for (obj in Atributos){
		//alert(Atributos[obj][0] + "  " +  Atributos[obj][1])
		Elm_temp.setAttribute(Atributos[obj][0], Atributos[obj][1])
	}
	if (typeof Pai != "undefined"){ 
		return Pai.appendChild(Elm_temp)
	}else{
		return Elm_temp
	}
	
	//var tags = [['href', 'yaaaaaaaa'], ['class', 'Link']]
	//FG.CriaElm('A', 'clique aqui', tags, gE('menu_tree_')) // ex de como usar
}

insert_El_before = function(Elm, Conteudo, Atributos, RefNode, aa){
	Elm_temp =  document.createElement(Elm)
	if (Conteudo != ""){
		Text_node = document.createTextNode(Conteudo)
		Elm_temp.appendChild(Text_node)
	}
	
	for (obj in Atributos){
		//alert(Atributos[obj][0] + "  " +  Atributos[obj][1])
		Elm_temp.setAttribute(Atributos[obj][0], Atributos[obj][1])
	}

	return this.insertBefore(Elm_temp, RefNode)
	
	//var tags = [['href', 'yaaaaaaaa'], ['class', 'Link']]
	//FG.CriaElm('A', 'clique aqui', tags, gE('menu_tree_')) // ex de como usar
}

remove_El = function(num_node){
	if (typeof num_node == "undefined"){
		return this.parentNode.removeChild(this)
	}else{
		return this.removeChild(this.childNodes[num_node])
	}
}

replace_El = function(newEl, num_ou_El_replace){
	if (typeof num_ou_El_replace != "object"){
		old_chid_temp = this.childNodes[num_ou_El_replace]
	}else{
		old_chid_temp = num_ou_El_replace
	}
	old_chid = this.replaceChild(newEl, old_chid_temp)
	return {
		new_child:newEl,
		old_child:old_chid
	}
}

$A = function(pseudoArray) {
    var results = [];
    for (i=0;i<pseudoArray.length;i++)
		//alert(i)
      	results.push(pseudoArray[i]);
    return results;
}

function Teste(ev, oo, pp){
	alert(ev)
	alert(oo)
	alert(document.documentElement.yy())	
	//alert(this.documentElement.innerHTML)
}

Function.prototype.AplicarPara = function(object){
	var _method = this 
	var args = $A(arguments)
	alert(args.length)
	args.shift()
	//args.push("errrrr")						
	//for (i=0;i<args.length;i++){
		//alert(args[i])
	//}
	alert(args.length)
	return function(ev){
		//alert(ev)
		//alert(args[1])
		//return "ssss"
		//return _method.apply(object)
		return _method.apply(object, [( ev || window.event)].concat(args))
	}
}
if (document.addEventListener){
	//document.addEventListener("click", Teste.AplicarPara(document, "www", "popoo"), false)
}else{
	//document.attachEvent("onclick", function (){alert(this.documentElement.innerHTML)})
	//document.attachEvent("onclick", Teste.AplicarPara(document, "www", "popoo"))
}

function add_Ev_Elm(el, evento, funct, params){
	if (document.addEventListener) {
        el.addEventListener (evento, function(ev) {funct.apply(el, [ev].concat(params))}, false);
	} else if (document.attachEvent) {
		if (params != undefined){
			el.attachEvent ("on"+evento, function() {funct.apply(el, [window.event].concat(params));});
   		}else{
            el.attachEvent ("on"+evento, function() {funct.apply(el, [window.event]);});
        }
	}
}

//add_Event_Elm(document, "click", Teste.AplicarPara(document))
//document.addEventListener (evento, function() {funct.apply(el, params);}, false);
//document.addEventListener ("click", function() {alert(arguments[0])}, false);

Object.extend(Element.prototype, 
	{add_El: function(Elm, Conteudo, Atributos){return add_El(Elm, Conteudo, Atributos, this)},
	add_Ev_Elm: function(evento, funct, params){add_Ev_Elm(this, evento, funct, params)},
	insert_El_before: insert_El_before,
	remove_El: remove_El,
	replace_El: replace_El
	}
)

if (!window.Event) {
  var Event = new Object();
}

Object.extend(Event, {
	KEY_BACKSPACE: 8,
	KEY_TAB:       9,
	KEY_RETURN:   13,
	KEY_ESC:      27,
	KEY_LEFT:     37,
	KEY_UP:       38,
	KEY_RIGHT:    39,
	KEY_DOWN:     40,
	KEY_DELETE:   46,
	KEY_HOME:     36,
	KEY_END:      35,
	KEY_PAGEUP:   33,
	KEY_PAGEDOWN: 34,
	KEY_ENTER:    13
	}
)
/*document.prototype = {
	CriaElm: function(){
		return FG.CriaElm
	}
}*/