$(document).ready(function() {
			
	/*** data grid ***/
	
	var padding = 0;
	var maxWidth = 1500;
	
	if ($('.chl2_w_dataGrid').length != 0) {
		
		if ($('.chl2_w_dataGrid_titles').length != 0) {
			
			// set initial column widths (not applicable to media grid)
			$('.chl2_w_dataGrid_titles th').children().each(function() {
				// allow for multiple data grids
				var parent = $(this).parents('.chl2_w_dataGrid');
				var column = $(this).attr('class');
				var titleColumnWidth = $(this).width();
				var columnWidth = $('.chl2_w_dataGrid_results .' + column, parent).width();

				// set width to widest column (either title or data column)
				if (columnWidth < titleColumnWidth) {
					width = titleColumnWidth + padding;
				} else {
					width = columnWidth + padding;
				}
				
				// maximum width of 1500
				if (width > maxWidth) {
					width = maxWidth;
				}
				
				// set width of data column
				$('.chl2_w_dataGrid_results .' + column, parent).css({'width' : width});
								
				// set width of title column
				$(this).css({'width' : width});
			});
		
			
			// check if datagrid is larger than the window, if so resize it so it initialises without horizontal scrolling
			$('.chl2_w_dataGrid_results').each(function() {
			
				var parent = $(this).parents('.chl2_w_dataGrid');
				var gridWidth = $(this).width();
				var windowWidth = $(this).parents('.chl2_body').width(); 
				
				if (gridWidth > windowWidth) {
					// find widest column
					var widest = 0;
					var column = '';
					
					$('.chl2_w_dataGrid_results tr:first').children().each(function() {
					
						if ($(this).width() > widest) {
							column = $('div', this).attr('class');
							widest = $(this).width();
						}
					});
					
					// get new width (minus 1 for border)
					var newWidth = (windowWidth - (gridWidth - widest)) - 1;
					
					if (newWidth > 100) {
						
						$('.' + column, parent).css({'width' : newWidth});
					}
				}		
			});
		
			// resize vertically
			$('.chl2_w_dataGrid_results').each(function() {
				$(this).parents('.chl2_w_dataGrid_resultsWrapper').height($(this).height());
			});			
		
			// resize grid horizontally (not applicable to media grid)
			$('.chl2_w_dataGrid_titles .chl2_resizable').resizable({ 
				resize: function(event, ui) {
					// get new width of column
					var width = $(this).width();
					// set width on parent th
					$(this).parent().css({'width' : width});
					// get class to determine if we are changing col1, col2, etc
					var column = $(this).parent().attr('class');		
					// get parent data grid
					var dataGrid = $(this).parents('.chl2_w_dataGrid');
					
					$('.' + column, dataGrid).css({'width' : width});
										
					// ie needs this to reposition the titles, other browsers automatically call .scroll					
					$('.chl2_w_dataGrid_titles', dataGrid).css({'right' : $('.chl2_w_dataGrid_resultsWrapper', dataGrid).scrollLeft()});
					
					// make sure columns are always synchronized (ie needs this)
					if ($('.' + column, dataGrid).parents('td').width() != $(this).parents('th').width()) {
						
						// find the widest column
						if ($('.' + column, dataGrid).parents('td').width() > $(this).parents('th').width()) {
							width = $('.' + column, dataGrid).parents('td').width();
						}
						else {
							width = $(this).parents('th').width();
						}
						// set all resizable elements to the same width
						$(this).parent().css({'width' : width});
						$('.' + column, dataGrid).css({'width' : width});
						$(this).css({'width' : width});
					}
				},
				handles: 'e',
				minWidth: 40
			});
		}		
		
		// resize grid vertically
		$('.chl2_w_dataGrid_resize').resizable({
			resize: function(event, ui) {
				// get new width of column
				var height = $(this).height();
				// set height of body
				$('.chl2_w_dataGrid_resultsWrapper', this).css({'height' : height});
			},
			handles: 's'
		});		
		
		// scroll titles when results table is scrolled
		$('.chl2_w_dataGrid_resultsWrapper').scroll(function() {
			var dataGrid = $(this).parents('.chl2_w_dataGrid');
			$('.chl2_w_dataGrid_titles', dataGrid).css({'right' : $(this).scrollLeft()});
		
		});
	}
	
});
