/*
	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 
 *   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  */
// 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)
{
	// Add jQuery ready handler
	jQuery(function($)
	{
		$('.typeof__xcandidatedob').datepicker( {yearRange: '-100:-18'} );			
		
		$('.typeof__xdeletebutton').click(function(e) { return confirm('Are you sure you want to delete this item? This cannot be undone!'); } );
		
		$('.typeof__xmodaldialog').dialog({ autoOpen:false, modal:true });
		
		if(typeof($.highlightFade) != 'undefined')
			$('.breadcrumb').highlightFade({speed:1000, end:'#ffffff'});

		// MAGIC DIALOGS - these are simple client-side AJAX dialogs for adding handy user interface functionality to many pages.
		// Anything more complicated should use the normal control model and be included on a page-by-page basis			
		// Interactions
		$('.magic_quickCallBack').click(function(e)
			{
				// Clear up from last time
				$('#quickCallBackName').val('');
				$('#quickCallBackPhone').val('');
				$('#quickCallBackEmail').val('');
				$('#quickCallBackDetails_error').hide();
				//
				
				//
				$('#quickCallBack').dialog('open').data('callbackOptions', optionsArrayToMap(this.href));
			
				e.preventDefault();
			}
		);
		$('#quickCallBackSubmitButton').click(function(e)
			{
				var options = $('#quickCallBack').data('callbackOptions');

				var name = $('#quickCallBackName').val();
				var phone = $('#quickCallBackPhone').val();
				var email = $('#quickCallBackEmail').val();				
				
				var url = $APPROOT + 'publicajax.asp?control=callBackHandler&name=' + escape(name) + '&phone=' + escape(phone) + '&email=' + escape(email);									
				
				if(name && (phone || email))
				{
					$.ajax({
						url:url,
						type:'POST',
						success:function(data, textStatus, jqXHR)
						{
							$('#quickCallBack').dialog('close');
						},
						error:function(jqXHR, textStatus, errorThrown)
						{
							alert('Sorry! A problem occurred sending your callback request; please try again later.');
						}
					});						
				}
				else
					$('#quickCallBackDetails_error').show();
			}
		);
		$('.magic_quickMap').click(function(e)
			{
				var options = optionsArrayToMap(this.href);
				
				
				$('#quickMap').data('quickMapOptions', options).dialog('option', 'title', 'Location map for ' + options.title).dialog('open');
				
				
				e.preventDefault();
			}
		);
	});
//
}

function optionsArrayToMap(s)
{
	var res = {};
	
	if(/\[(.+)\]/.exec(s))
	{
		var a = RegExp.$1.split(/,/);
		
		for(var i = 0; i < a.length - 1; i+=2)
			res[a[i]] = a[i+1];
	}
	
	return res;
}
