$(".Controla input").live("click", function()
{
	MiraSiHayQueMostrar()
});

function MiraSiHayQueMostrar()
{
	var IDSS = new Array();
	var IDSO = new Array();
	$(".Controla input:checked[type=checkbox]").each(function(i){
		IDSS[IDSS.length] = this.id;
	});	
	$(".Controla input:checked[type=radio]").each(function(i){
		if ($(this).closest(".RadioButtonList"))
		{
			var Valor = $(this).val();
			var Nombre = $(this).attr("name");
			IDSO[IDSO.length] = Nombre + '_' + Valor;
		}
		else
			IDSS[IDSS.length] = this.id;
	});	
	$(".Controla select option:selected").each(function(i){
		var ElID = this.value;
		var ParentID = $(this).parent().get(0).id;
		IDSO[IDSO.length] = ParentID + '_' + ElID;
	});
   	Cuestionarios.TWFCuestionario.GetMostrar(IDE, IDP, IDSS, IDSO, CallBackGetMostrar);
}

function CallBackGetMostrar(Resultado)
{
	if (Resultado.value) 
	{
		IDS = Resultado.value.Visibles;
		$.each(IDS, function(i){
			$(".P" + this + ".Controla").find("select").each(function(i)
			{
				$(this).change(function()
				{
					MiraSiHayQueMostrar()
				});
			});
			if ($(".P" + this).is(".Canalizada"))
			{
				if ($(".P" + this).is(".Filas")) 
				{
					$(".P" + this).find(".DescripcionFila").each(function () {
						Texto = $(this).attr("Descr");
						if (Texto)
						{
							ArrayResult = Texto.match(/\[%\[(.*?)\]%\]/g);
							if (ArrayResult)
							{
								for (var i = 0; i < ArrayResult.length; i++) 
								{
									ElID = ArrayResult[i].substring(3, ArrayResult[i].indexOf("]%]"));
									if ($("#" + ElID).is(":checked"))
									{
										TextoOpc = $("label[for=" + $("#" + ElID).attr("id") + "]").text()
										Texto = Texto.replace(ArrayResult[i], TextoOpc);
										$(this).find("span").text(Texto);
										$(this).parent().show();
									}
									else
										$(this).parent().hide();
								}
							}
						}
					});
					$(".P" + this).find(".TablaMatriz > tbody > tr:visible:odd").removeClass().addClass("FilaMatrizP")
					$(".P" + this).find(".TablaMatriz > tbody > tr:visible:even").removeClass().addClass("FilaMatrizI")
				}
				else
				{
					Texto = $(".P" + this).find(".DescripcionPregunta").attr("Descr");
					if (Texto) 
					{
						ArrayResult = Texto.match(/\[%\[(.*?)\]%\]/g);
						if (ArrayResult) 
						{
							for (var i = 0; i < ArrayResult.length; i++) 
							{
								ElID = ArrayResult[i].substring(3, ArrayResult[i].indexOf("]%]"));
								if ($("#" + ElID).is(".PregOrdenar")) 
									TextoOpc = $("#OR" + ElID).find("li").first().text()
								else 
									TextoOpc = $("label[for=" + $("#" + ElID).find(":checked").first().attr("id") + "]").text();
								Texto = Texto.replace(ArrayResult[i], TextoOpc);
							}
						}
						$(".P" + this).find(".DescripcionPregunta").text(Texto);
					}
				}
			}
			$(".P" + this).show();
		});
		IDS = Resultado.value.Ocultas;
		var Ocultado = false;
		$.each(IDS, function(i){
			if ($(".P" + this + ":hidden").length == 0) 
			{
				$(".P" + this).find("input:checked").each(function(i)
				{
					this.checked = false;
				});
				$(".P" + this).hide();
				Ocultado = true;
			}
		});
		if (Ocultado)
		  MiraSiHayQueMostrar();
	}	
}

function PreparaSelects()
{
	if ($(".TablaRespuesta select").length > 0)
	{
		$(".TablaRespuesta select").change(function()
		{
			MiraSiHayQueMostrar()
		});
	}
}

function EvaluaOrden()
{
	$(".PregOrdenar").each(function(index){
		var IDControl = "UL" + $(this).attr("id");
		var Nombre = "ORD" + $(this).attr("id");
		var Opc = new Array();
		$("#" + IDControl).find("li").each(function(index){
			Opc[index] = $(this).attr("id").substr(2);
		});
		$("#" + Nombre).attr("value", ArrayToString(Opc));		
	});  		
}

function RefrescaPiping(event, ui)
{
	if ($(ui.item).parents(".Controla").length > 0)
	{
		$(".Canalizada").each(function () {
			Texto = $(this).find(".DescripcionPregunta").attr("Descr");
			if (Texto) 
			{
				ArrayResult = Texto.match(/\[%\[(.*?)\]%\]/g);
				if (ArrayResult) 
				{
					for (var i = 0; i < ArrayResult.length; i++) 
					{
						ElID = ArrayResult[i].substring(3, ArrayResult[i].indexOf("]%]"));
						if ($("#" + ElID).is(".PregOrdenar")) 
							TextoOpc = $("#OR" + ElID).find("li").first().text()
						else 
							TextoOpc = $("label[for=" + $("#" + ElID).find(":checked").first().attr("id") + "]").text();
						Texto = Texto.replace(ArrayResult[i], TextoOpc);
					}
				}
				$(this).find(".DescripcionPregunta").text(Texto);
			}
		});				
	}
}

function PreparaHover()
{
	$("#Cuestionario .Ordenable > li").live("mouseover mouseout", function(event) {
		if ( event.type == "mouseover" ) {
		    $(this).addClass("Hover");
		} else {
		    $(this).removeClass("Hover");
		}
	});
}

$(document).ready(function()
{
	$("td .Dinamica").each(function () {
		$(this).hide(); 
	});
	$("td .Canalizada").each(function () {
		if ($(this).is(".Filas"))
		{
			$(this).find(".DescripcionFila").each(function () {
				if ($(this).text().trim() != '')
					$(this).attr("Descr", $(this).text()); 
			});
		}
		else
			$(this).find(".DescripcionPregunta").attr("Descr", $(this).find(".DescripcionPregunta").text()); 
	});
	MiraSiHayQueMostrar();
	PreparaSelects();
	PreparaHover();
});

