Tabs = Class.create();
Object.extend(Tabs, {
	intialized: null,
	Kind: null,
	subKind: null,
	Kindparent: null,
	tabover: null,
	stack: null,
	work: null,
	

	initialize: function () {
		if(!this.intialized){
			this.intialized = true;
			
			this.Kind = $("Secciones");
			this.subKind = $("SubSecciones");
			this.Kindparent = $("SECCCONTAINER");
			this.tabover = null;
			this.stack = null;
			this.work = true;
			
			if(this.findRel("On")){
				this.Actual = this.getSecc(this.findRel("On"));
				this.Cambia(this.Actual);
			}else{
				this.work = false;
			}
			
			
		}
	},

	// Getters
	getRels: function(obj) {
		var rv = new Array();
		rv = [null,null];
		var str = obj.className.toString();
		// Codigo de busqueda sobre string

		var pos;
		if((pos = str.indexOf("r_")) != -1)	{
			var temp = str.substring(pos);
			rv[0] = temp.substring(0, temp.indexOf(" "));
		}

		if((pos = str.indexOf("t_")) != -1) {
			var temp = str.substring(pos);
			rv[1] = temp.substring(0, temp.indexOf(" "));
		}
		// Codigo de busqueda sobre string
		
		return rv;
	},

	getTab: function(obj) {
		return this.getRels(obj)[0]; //Devuelve el classname prefijado con r_ (tab0x)
	},
	
	getSecc: function(obj) {
		return this.getRels(obj)[1]; //Devuelve el classname prefijado con t_ (noticias, flash, etc)
	},

	getTabOver: function(){
		return this.tabover;
	},

	// Setters
	setActual: function(id) {
		this.Actual = id;
	},
	
	setTabOver: function(o){
		this.tabover = o;
	},
	
	setKind: function(m){
		if(typeof(m) == "string")
			this.Kind = $(m);
		else if(typeof(m) == "object" && m.nodeType == ELEMENT_NODE)
			this.Kind = m;	
	},

	setsubKind: function(m){
		if(typeof(m) == "string")
			this.subKind = $(m);
		else if(typeof(m) == "object" && m.nodeType == ELEMENT_NODE)
			this.subKind = m;	
	},

	setKindparent: function(m){
		if(typeof(m) == "string")
			this.Kindparent = $(m);
		else if(typeof(m) == "object" && m.nodeType == ELEMENT_NODE)
			this.Kindparent = m;	
	},
	// Metodos Funcionales
	
	offAll: function(overs)
	{
		var list = this.Kind.childNodes;
		
		for (var i=0;i<list.length;i++){
			if(ELEMENT_NODE == list[i].nodeType){
				var Rel = this.getTab(list[i]);
				if(overs){
					list[i].className = list[i].className.toString().replace("OOn", "Off");
					list[i].className = list[i].className.toString().replace("IAon", "Off");
				}
				else{
					list[i].className = list[i].className.toString().replaceAll("OOn", "Off");
					list[i].className = list[i].className.toString().replaceAll("IAon", "Off");
					list[i].className = list[i].className.toString().replaceAll("On", "Off");
					list[i].className = list[i].className.toString().replaceAll("Ion", "Off");
					
				}
				
					if(this.stack == Rel){
						list[i].className = list[i].className.replace("Off", "Ion");
					}
			}
		}
	},
	
	offAllSubSecc: function(){
		var list = this.subKind.childNodes;
		
		for (var i=0;i<list.length;i++){
			if(ELEMENT_NODE == list[i].nodeType){
				if(list[i].className.indexOf("Hidden") == -1)
					list[i].className += " Hidden";
			}
		}
	},

	findRel: function(val, sSecc) //sSecc determina si se busca sobre Secciones(false) o SubSecciones(true)
	{
		if (!sSecc)		var _elems = this.Kind.getElementsByTagName("li");
		else				var _elems = this.subKind.childNodes;
		
		for(var i=0;i<_elems.length;i++){
			if (_elems[i].nodeType == ELEMENT_NODE && _elems[i].className && _elems[i].className.indexOf(val) != -1)
				return _elems[i];
		}
		
		return null;
	},
	
	// Metodos de Interfaz Secciones

	Cambia: function(obj)
	{
		if(!this.intialized) this.initialize();
	
		if(typeof(obj) == "string"){ 
			obj = this.findRel(obj, false); // Si se le envio un Rel busca ese rel en el Reino de tabs de Secciones
		}
		
		this.offAll();

		this.setActual(this.getSecc(obj));

		var Rel = this.getTab(obj);
		var Num = Rel.substr(Rel.length - 2, 2);
		var TabI = this.findRel("r_tab"+((Num-1 < 10 ? "0" : "") + "" + (Num-1).toString()), false);
		if(TabI)
			TabI.className = TabI.className.toString().replace("Off", "Ion");
		obj.className = obj.className.toString().replace("Off", "On");
		this.subActive(this.getSecc(obj));
	},
	
	over: function(obj){
		if(!this.intialized) this.initialize();
		
		this.offAll(true);
		
		this.setTabOver(obj);
		var Rel = this.getTab(obj);
		var Num = Rel.substr(Rel.length - 2, 2);
		var TabI = this.findRel("r_tab"+((Num-1 < 10 ? "0" : "") + "" + (Num-1).toString()), false);
		if(TabI)
			TabI.className = TabI.className.toString().replace("Off", "IAon");		
		
		if(obj.className.toString().indexOf("Ion") != -1){
			obj.className = obj.className.toString().replace("Ion", "OOn");
			this.stack = Rel;
		}
		else{
			obj.className = obj.className.replace("Off", "OOn");
			this.stack = null;
		}
		
		this.subActive(this.getSecc(obj));
	},

	out: function(obj){
		if(!this.intialized) this.initialize();
		
		if (!obj)
			obj = this.getTabOver();
		
		if(obj){
			var Rel = this.getTab(obj);
			var Num = Rel.substr(Rel.length - 2, 2);
			var TabI = this.findRel("r_tab"+((Num-1 < 10 ? "0" : "") + "" + (Num-1).toString()), false);
			if(TabI)
				TabI.className = TabI.className.toString().replace("IAon", "Off");		
	
			if(this.stack == Rel)
				obj.className = obj.className.replace("OOn", "Ion");
			else
				obj.className = obj.className.replace("OOn", "Off");
		}
	},

	// Metodos de Interfaz Sub Secciones
	
	subActive: function(id){
		if(!this.intialized) this.initialize();
		
		if(!offset)
			offset = 0;
		
		//Codigo ON de Subseccion
		if (typeof(id) != "string")
			id = this.getSecc(id);
		this.offAllSubSecc();
		var c = this.findRel(id); // Tab
		id = id.replace("t_","s_");
		var o = this.findRel(id, true); //SubMenu
		o.className = id;
		
		
		Position.prepare();
		var offset = 0; var Wi = 0;
		var rt = Position.positionedOffset(c);
		var rtp = Position.positionedOffset(c.parentNode);
		rt[0] = rt[0] - rtp[0];
		
		var child = o.getElementsByTagName("li");
		for(var i=0;i<child.length;i++){
			Wi += child[i].offsetWidth;
		}
		o.style.width = Wi+"px";
		
		offset = Math.round(( rt[0] + ( c.offsetWidth / 2 ) ) - (Wi / 2));
		o.style.paddingLeft = (offset < 0 ? 0 : offset )+ "px";
		
		me = this;
		this.Kindparent.onmouseout = function(e) { me.subDeactive(e); };
	},
	
	subDeactive: function(e) {
		if(!this.intialized) this.initialize();
		
		Position.prepare();
		var _a = Mouse.FindMousePosition(e);
		if (!Position.withinIncludingScrolloffsets(this.Kindparent, _a[0], _a[1])){
			if(this.work){
				this.subActive(this.Actual);
			}else{
				this.offAll(true);
				this.offAllSubSecc();
			}
			this.out();
		}
	}
	
});

function debug(msg)
{
	$('DEBUG').value += msg;
}