﻿$(document).ready(function() {		
	
	// Add events to row_selector checkboxes
	$('input.row_selector').click(function() {
		$(this).parent('td').parent('tr').children('td').toggleClass('row_selected');					
	});  
	
	// Add event to list_selector checkbox
	$('input.list_selector').click(function() {
		if ($(this).attr('checked'))
		{
			// Set all rows as selected	(that aren't already)			
			
			// We can't just use the .click() method on the checkboxes because that fires the click event before the DOM is updated (annoying)
			// That then breaks the code used on the ViewEdit page to disable the reorder radio button.						
			// This alternative method ensures that the DOM is updated before the click event is triggered.		
							
			$(this).parents('tr').siblings('tr').children('td').children('.row_selector[checked!="true"]').each(function(i) {
				$(this).attr('checked', true);
				$(this).triggerHandler('click');				
			});		
		} 
		else 
		{
			// Set all rows as not selected (that aren't already)
			$(this).parents('tr').siblings('tr').children('td').children('.row_selector[checked="true"]').each(function(i) {
				$(this).attr('checked', false);
				$(this).triggerHandler('click');				
			});
		}
	});
	
	// Apply a special style to all textboxes
	$('input[type="text"], input[type="password"]').addClass('input_textbox');
		
	// Apply a special style to all input buttons
	$('input[type="submit"], input[type="button"]').addClass('input_button');
	
	// Add error glyphs to fields (if required)
	$('*[show-error="true"]').after('<span class="error_glyph">*</span>');

	// Add rounded corners to forms
	$('table.standard_form').children('tbody').children('tr:first-child').children('th:first-child').addClass('standard_form_tl');
	$('table.standard_form').children('tbody').children('tr:first-child').children('td:last-child').addClass('standard_form_tr');
	$('table.standard_form').children('tbody').children('tr:last-child').children('th:first-child').addClass('standard_form_bl');
	$('table.standard_form').children('tbody').children('tr:last-child').children('td:last-child').addClass('standard_form_br');
	
	// Add rounded corners to lists
	$('table.standard_list').children('tbody').children('tr:last-child').children('td:first-child').addClass('standard_list_bl');
	$('table.standard_list').children('tbody').children('tr:last-child').children('td:last-child').addClass('standard_list_br');	
	
	// Apply image hovering effect for any image with a 'hover' attribute - hover attribute should contain URL of alternative image
	$('img[hover]').hover(function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
    }, function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
    });	
});
