/**
 * Add global scripts that are only required on the front-end here
 */



/**
 * Add animation to flash success messages to fade out after a period of time
 */
var init_flash_message_fade = function () {
	$('.ui-state-success').click(function () { $(this).css('cursor', 'pointer'); $(this).hide(); });
	$('.ui-state-success').each(function () {
		$(this).css('left', '25%').css('top', '25%').css('width', '50%').css('position', 'absolute').css('z-index', 1000);				
		$(this).delay(2000).fadeOut(2000);		
	});
};




function init_inFieldLabels() {	
	if ($.InFieldLabels != null) {
		// In field labels loaded
		
		$('.in-field-labels label').each(function() {		
			if ($(this).attr('for') != '' && $(this).attr('for') != null) {		
				var input_type = $('#'+$(this).attr('for')).attr('type');
				if (input_type == 'text' || input_type == 'password' || $('#'+$(this).attr('for')).get(0).tagName == 'TEXTAREA') {
					$(this).addClass("in-field-label");				
					
					if ($(this).parent().get(0).tagName == 'DT') {
						$(this).parent().hide();
						$(this).parent().next('dd').append($(this).parent().html());
						$(this).parent().html('');
					}		
					
					if (($('#'+$(this).attr('for')).attr('type') == 'text' && $('#'+$(this).attr('for')).val() != '') || ($('#'+$(this).attr('for')).get(0).tagName == 'TEXTAREA' && $('#'+$(this).attr('for')).html() != '')) {
						$(this).hide();
					}
					else {
						$(this).show();
					}
				}
			}				
		});
		
		$('label.in-field-label').inFieldLabels();
		
		$('form.in-field-labels').bind('reset', function() {					
			$(this).find('label').not('.error').css('opacity', '').show();
			$('label.in-field-label').inFieldLabels();
		});
	}
}

/**
 * Function to remove empty columns from marked tables
 * @return
 */
function init_trim_tables() {
	
	$('.trim-table').each(function() {
		var table = $(this);
		var rows = $(this).find('tbody tr:not(.ignore)');		
		
		var valCounts = [];		
		if (rows.length > 0) {				
			// get a count of how many empty values there are in each column
			rows.each(function (i) {
				var columns = $(this).find('td');
				columns.each(function (j) {													
					if (valCounts[j] == null) valCounts[j] = 0;
					
					if ($(this).text() == '') {
						valCounts[j]++; 
					}							
				});
			});			
		}
		
		var removed_coumns = 0;
		//console.log(table.attr('summary'));
		//console.log(valCounts);
		
		$.each(valCounts, function(i, val) {
			// check if all values in this column are empty			
			//console.log(rows.length+' '+val);
			if (rows.length == val) {				
				// remove the column from the table				
				//table.find('colgroup').first().find('col:nth-child('+(i+1-removed_coumns)+')').remove();
				table.find('thead').first().find('tr th:nth-child('+(i+1-removed_coumns)+')').html(' ');
				table.find('tbody').first().find('tr td:nth-child('+(i+1-removed_coumns)+')').html(' ');
				//removed_coumns++; // required offset to adjust which column to target after removing columns
			}
		});		
	});
	
}



function init_forms() {
	$('form').bind('reset', function() {
		$(this).find('label.error').remove();
	});
}

$(document).ready(function () {
	
	init_flash_message_fade();
	init_inFieldLabels();
	init_trim_tables();
	init_forms();
});

