/** 
 * Objeto para la seccion Centro de Ayuda
 */
var privalia_Ayuda = new Class.create({
	initialize: function() {
		this.clrIni =		'#fff';
		this.clrFin =		'#666';
		this.downed =		'';
		this.iniciado =		false;
		this.segIni =		0.5;
		this.segFin =		0.5;
		this.subnivelActual =-1;
		this.Tabs =			null;
	},
	getSubnivelId: function(target) {
		var subnivelId = "";
		if ($(target).hasClassName('subnivel')) {
			subniveles = $w($(target).className);
			if(subniveles.length > 1)
			{
				subnivel = subniveles[1];
				subnivelId = subnivel.split("-")[1];
			}
		}
		return subnivelId;
	},
	fx_marcar: function(newtarget) {
		// color
		new Effect.Morph(newtarget, {
			style: {
				color: this.clrIni
			},
			duration:this.segIni
		});
	},
	fx_desmarcar: function(newtarget) {
		// color
		var clrActual = $(newtarget).getStyle('color');
		if (clrActual != this.clrFin) {
			new Effect.Morph(newtarget, {
				style: {
					color: this.clrFin
				},
				duration: this.segFin
			});
		}
	},
	hideAndShow: function(hide, show) {
		$(hide).hide();
		new Effect.Appear(show);
	},
	init: function() {
		// aplicar tecnica de tabulacion para las preguntas y respuestas
		this.init_tabs();
		this.iniciado = true;
		//this.init_subniveles();
		//this.init_subniveles_roots();
	},
	init_subniveles: function() {
		// ocultar todos los subniveles
		$$('ul.sub').invoke('hide');
		$$('a.subnivel').each(function(s, index){
			$(s.id).hide();
			$(s).setStyle({
					color: '#666'
				});
		});
	},
	init_subniveles_roots: function() {
		$$('a.subnivel-root').each(function(s, index){
			Event.observe(s, 'click', function() {
				$(s).toggleClassName('down');
			});
		});
	},
	init_tabs: function() {
		var othis = this;
		this.Tabs = new Control.Tabs('tabs-preguntas', {
			beforeChange: function(old_container, new_container) {
				if (old_container.id != new_container.id && new_container.id != undefined) {
					if (old_container.id != undefined) {
						var oldtarget = "p" + old_container.id.substring(1);
						othis.fx_desmarcar(oldtarget);
					}
					if (new_container.id != undefined) {
						var newtarget = "p" + new_container.id.substring(1);
						
						// si el tab que esta activo forma parte de un subnivel
						// hay que abrir el subnivel  
						var subnivelId = othis.getSubnivelId(newtarget);
						if(subnivelId != "")
						{
							othis._subnivel(subnivelId, false);
						}
						othis.fx_marcar(newtarget);
					}
				}
			},
			afterChange: function(new_container) {
				// buscar si tiene subnivel y gestionarlo
				othis.subnivel(new_container.id)
				if (othis.iniciado) {
					othis.mostrarContainer(new_container.id);
				}
			}
		});
	},
	marcarMenu:function(target) {
		$('m_' + target).addClassName('active');
	},
	mostrarContainer: function(container) {
		// efecto fade
		$(container).hide({duration:0});
		$(container).appear();
	},
	resetDown: function() {
		if(this.downed != "") {
			$(this.downed).removeClassName('down');
			this.downed = '';
		}
	},
	setDown: function(obj) {
		if(!$(obj).hasClassName('down')) {
			$(obj).addClassName('down');
			this.downed = obj;
		}
	},
	subnivel: function(id) {
		var marcarPrimero = true;
		var num = parseInt(id.substring(1));
		this._subnivel(num,marcarPrimero);
	},
	_subnivel: function(num, marcarPrimero) {
		var numsig = num + 1;
		var actual = 'p' + num;
		var sig = 'p' + (num + 1);
		var target = 'a.subnivel-' + num;
		
		// no hacer nada si navega dentro del subnivel, porque ya esta abierto.
		// si esta fuera, ver si hay que cerrar o abrir subnivel
		if (!$('p' + num).hasClassName('subnivel')) {
			
			if (num != this.subnivelActual) {
				// ocultar todos los subniveles
				this.init_subniveles();
			} else {
				$('p' + num).setStyle({
					color: '#666'
				});
			}			

			// bullet down si es el root de un subnivel
			if($('p' + num).hasClassName('subnivel-root')) {
				this.setDown('p'+num);
			} else {
				this.resetDown();
			}
			
			// mostrar subnivel que toca
			$$('ul.sub-' + num).invoke('show');
			var othis = this;
			$$(target).each(function(s, index){
				Effect.Appear(s.id);
				
				if (index == 0) {
					othis.subnivelActual = num;
					
					// saltar al siguiente, dentro del subnivel
					try {
						if (marcarPrimero) {
							othis.Tabs.next();
						}
					}catch(e){}
				}
			});
		}
	}
});

Event.observe(window,'load',function() {
	// rollover en las preguntas
	$$('#tabs-preguntas a').each(function(s){
		Event.observe(s,'mouseover',function(){
			if (!$(s).hasClassName('active')) {
				$(s).setStyle({
					color: '#fff'
				});
			}
		});
		Event.observe(s,'mouseout',function(){
			if (!$(s).hasClassName('active')) {
				$(s).setStyle({
					color: '#666'
				});
			}
		});
	});
});

var FMsg; 
Event.observe(window,'load',function() {
	FMsg = new FormMessage();
	FMsg.applyAllForms();
})