/*
	Set up jQuery defaults for general Helmstone usage
	(c) 2010 Rob Watkins
	This code is free to use for any purpose, provided the copyright notice and credit to original author is maintained.
	The code is provided AS IS. No guarantee is made on the fitness of this code for any purpose. Use at your own risk.
*/
/*   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 
 *   DEPENDENCIES
 *
 *   jQuery (based on 1.4)
 *   tiny 
 *   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  */

(function($) 
{ 
	// Set up templating defaults
	if($.fn.hTemplate)
	{
		$.fn.hTemplate.defaults.ajax.getDataArray = function(data /* Response as text */) { return (data.charAt(0) == '(' ? eval(data) :eval('(' + data + ')')).ResultSet.Items; };
		$.fn.hTemplate.defaults.ajax.contentType = 'text/xml';
	}	
	
	// UI DatePicker defaults
	if($.datepicker)
	{
		$.datepicker.setDefaults(
			{
				changeMonth: true,
				changeYear: true,
				dateFormat: 'dd/mm/yy', // Default to UK date - !!!WARNING!!! if this is changed, the ensure all database code is expecting the new date format!
				constrainInput: true
			}
		);
	}
	//
	
	// AUTOMAGIC!!! These can cause performace problems on pages with many elements; set STOP_THE_MAGIC to true before this file is included to 
	// stop these events being done. You can still assign them yourself on an element by element basis.
	if(typeof(STOP_THE_MAGIC) == 'undefined' || !STOP_THE_MAGIC)
	{
		$(function($)
		{
			// MAGIC VALIDATED EDITS
			if($.fn.validated)
			{
				// Numeric
				$('.typeof__float').validated({type:'float'});
				$('.typeof__positivefloat').validated({type:'float', allowNegatives:false});
				$('.typeof__int').validated({type:'int'});
				$('.typeof__positiveint').validated({type:'int', allowNegatives:false});
				
				// Date / time
				$('.typeof__date').validated({type:'date', format:'gb'});
				$('.typeof__time').validated({type:'time'});
			}
			
			// MAGIC UI
			if($.datepicker)
			{
				$('.typeof__datepicker').datepicker();
			}
		});
	}
	// End automagic
	
	// Add ready event to page
	if(typeof(HUI) != 'undefined')
	{
		HUI.attachPageToFramework = function(pageinstance)
		{		
			if(typeof pageinstance.ready == 'function')
				$(function($) { pageinstance.ready($); }); // Closure ensures .ready() is called in context of page object like the other events
		};
	}
	//
})(jQuery);
