;(function($){
 
	// Create a name space for ColdFusion related functionality.
	jQuery.coldfusion = {};
 

	// Create a function that will iterate over each row of the
	// serialized JSON query. This iteration can handle three
	// types of query structure:
	//
	// - Default from SerializeJSON()
	// - WDDX Compatible from SerializeJSON()
	// - Array of structs.
	jQuery.coldfusion.eachRow = function( query, callback ){
		// Check to see which type of iterator we are going to
		// use when looping over this query.
		
		if ($.isArray( query )){
 
			// This is an array of structs.
			jQuery.coldfusion.eachRow.arrayIterator(
				query,
				callback
			);
 
		} else if ("ROWCOUNT" in query){
 
			// This is the WDDX-compatible format.
			jQuery.coldfusion.eachRow.wddxIterator(
				query,
				callback
			);
 
		} else {
 
			// This is the default format.
			jQuery.coldfusion.eachRow.defaultIterator(
				query,
				callback
			);
 
		}
 
		// Return the jQuery library.
		return( this );
	};
 
 
	// Define a cfquery loop iteration method that can handle the
	// default SerializeJSON() method.
	jQuery.coldfusion.eachRow.defaultIterator = function( query, callback ){
		var i = 0;
 
		// Loop over the data array.
		for (var i = 0 ; i < query.DATA.length ; i++){
 
			(function( rowIndex ){
				var row = {};
 
				// Loop over the column names to create the data
				// collection as column-value pairs.
				$.each(
					query.COLUMNS,
					function( index, column ){
						row[ column ] = query.DATA[ rowIndex ][ index ];
					}
				);
 
				// Execute the callback method in the context of
				// the row data.
				callback.call( row, rowIndex, row );
 
			})( i );
 
		}
	};
 
 
	// Define a cfquery loop iteration method that can handle the
	// SerializeJSON() method that returns WDDX-compatible data.
	jQuery.coldfusion.eachRow.wddxIterator = function( query, callback ){
		var i = 0;
 
		// Loop over the records.
		for (var i = 0 ; i < query.ROWCOUNT ; i++){
 
			(function( rowIndex ){
				var row = {};
 
				// Loop over the column names to create the data
				// collection as column-value pairs.
				$.each(
					query.DATA,
					function( column, values ){
						row[ column.toUpperCase() ] = values[ rowIndex ];
					}
				);
 
				// Execute the callback method in the context of
				// the row data.
				callback.call( row, rowIndex, row );
 
			})( i );
 
		}
	};
 
 
	// Define a cfquery loop iteration method that can handle the
	// query as an array of structs.
	jQuery.coldfusion.eachRow.arrayIterator = function( query, callback ){
		var i = 0;
 
		// Loop over the records.
		for (var i = 0 ; i < query.length ; i++){
 
			// Execute the callback method in the context of
			// the row data.
			callback.call( query[ i ], i, query[ i ] );
 
		}
	};
 
})( jQuery );
