$(document).ready(function(){
	
	// -------------------------
	// CSS
	
	$("ul.Faq p, #SearchAlt, .Datepicker, .Calendar").hide();
	$("tr.Information").attr({ style: "display: none;" });
	
	//var pageWidth = $(document).width();
	//$("#Sitemap").width(pageWidth);
	
	$(".Equal").equalHeights();

	
	// -------------------------
	
	$('ul.Faq h3').click(function() {
         var faqDt = $(this).next();
         if ($(this).parent().hasClass('Active')) {
			$("ul.Faq li.Active").removeClass("Active");
			$(this).next().hide("fast");
			$("ul.Faq h3").next().hide("fast");
         } else {
			$("ul.Faq li.Active").removeClass("Active");
			$("ul.Faq h3").next().hide("fast");
            $(this).next().show("fast");
            $(this).parent().addClass("Active")
         }
     });
     
     $('.ToggleInformation').click(function() {
         if ($(this).parent().parent().next().hasClass('InformationVisible')) {
         	$(this).removeClass('ToggleInformationActive');
			$(this).parent().parent().next().removeClass("InformationVisible");
			$(this).parent().parent().next().attr({ style: "display: none;" });
         } else {
         	$(this).addClass('ToggleInformationActive');
			$(this).parent().parent().next().addClass("InformationVisible");
			$(this).parent().parent().next().attr({ style: "" });
         }
     });
     
     $('.TopToggle h2').click(function() {
         if ($(this).hasClass('Active')) {
         	$(this).removeClass('Active');
			$(this).parent().next().slideUp();
         } else {
         	$(this).addClass('Active');
			$(this).parent().next().slideDown();
         }
     });
     
     $('#cbToggle').change(function() {
     	$('.Datepicker').slideToggle();
     });
     
     $('.Datepicker span.Date').click(function() {
			$('.Calendar').slideToggle();
		}, function () {
		
     });
	 
	 /* -------------------------------------------------------------------------------
	 		XFORMS
		------------------------------------------------------------------------------- */	
	
	var $currItem;
	
	$('#id_matrix tr').each(function(i){		
		var $TDs = $(this).children();
		
		// We are in a loop of TR elements. Each TR is assumed to contain 2 TD elements.
		// If the second TD element is empty, remove it and set colSpan = 2, on the first TD element.
		// READ IF CASE BELOW: if lenght is == 0 OR if ( lenght == 1 AND the html content is == one blank space )
		if($($TDs[1]).html().length == 0 || ($($TDs[1]).html().length == 1 && $($TDs[1]).html() == " ")) // if true --> the TD is empty
		{
			$($TDs[0]).attr('colSpan','2');
			$($TDs[1]).remove();
		}
		else
		{
			$($TDs[1]).addClass('Choices');
		}
	});
	
	$('#id_matrix .Choices .radio').click(function(){
		$(this).removeAttr('disabled').removeClass('disabled');
		disableRow(this);
	});
	
	function disableRow(handle)
	{
		$(handle).parent().children('.radio').each(function(i){
			if(handle == this)
			{
				if($currItem && !$currItem.attr('checked'))
					$currItem.attr('disabled','disabled').addClass('disabled');
				$currItem = $(handle);
				disableRowNum(i, handle);
			}
		});
	}
	
	function disableRowNum(num, clickedElem)
	{
		$('#id_matrix .Choices fieldset').each(function(i){
			$(this).children('.radio').each(function(n){
				if(n == num)
				{
					if(clickedElem != this)
					{
						if(!$(this).attr('checked'))
						{
							$(this).attr('disabled','disabled').addClass('disabled');
						}
						else
						{
							$(this).attr('checked',false);
							$(this).attr('disabled','disabled').addClass('disabled');
						}
					}
				}
			});
		});
	}
	
	// On hover enable radiobuttons in current fieldset,
	// and on out disable unchecked radiobuttons
	$('.Choices fieldset').hover(function(){
		$(this).children('input.radio').removeAttr('disabled');
	},function(){		
		$(this).children('input.radio').each(function(i){	
			
			if($(this).hasClass('disabled'))
			{
				//console.log('disable ' + i);
				$(this).attr('disabled','disabled');
			}
		});
	});
	
});