/*
	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($) 
{
	// 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($)
		{
			// App types
			$('.typeof__xcandidatedob').datepicker( {yearRange: '-65:-18'} );

			$('.typeof__xdatepicker').datepicker();
			
			$('.typeof__xdeletebutton').click(function(e) { return confirm('Are you sure you want to delete this item? This cannot be undone!'); } );
			
			$('.typeof__xmodaldialog.dialog_large').dialog({ autoOpen:false, modal:true, width:'550px' });			
			$('.typeof__xmodaldialog.dialog_standard').dialog({ autoOpen:false, modal:true });
			
			$('select.typeof__xautopost').bind('change', function(e) { document.forms[0].submit(); } );
			//
		});
	}
	// End automagic
})(jQuery);

// Helper functions --------------------------------------------------------------------------------------------------------------------------------------
// Post a form, optionally updating fields with values / the contents of others (e.g. event postback hidden fields)
function post(options)
{
	var formselector = null;
	
	for(var el in options)
	{
		if(el == 'formSelector')
			formselector = options[el];
		else
		{
			var valopt = options[el];
			
			$(el).val(valopt.selector ? $(valopt.selector).val() : valopt.value);
		}		
	}
	
	(formselector ? $(formSelector) : document.forms[0]).submit();
}

// Mirrors functionality in serverside tigerlily2_BusinessLogin.Search.inc.js.asp
function SearchDistance(s)
{
	this.toString = function(justid)
	{
		var tcode = { client:'C', candidate:'A', vacancy:'V', clientLocation:'L' }[this.type];
		return tcode + this.id + (!justid ? ':' + this.distance : '');
	}
	
	this.getID = function()
	{
		return this.toString(true);
	}

	this.fromString = function(s)
	{
		if(/^([CAVL])(\d+):(\d+)$/.exec(s))
		{
			this.type = { C:'client', A:'candidate', V:'vacancy', L:'clientLocation' }[RegExp.$1];
			
			this.id = RegExp.$2;
			this.distance = RegExp.$3;
		}
	}
	
	this.getDisplay = function(itemname)
	{
		return (this.distance > 0 ? this.distance : 'Any') + ' mi of ' + itemname;
	}
	
	if(s)
		this.fromString(s);
}


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;
}
