// JavaScript Document

$(document).ready(function()
{
  $('#tbxUser').autocomplete('?do=user-search', {
	scroll: true,
	matchContains: true,
	delay: 50,
	formatResult: function(data)
	{
	  if (data == '...') {
		return $('#tbxUser').val();
	  } else {
		return data;
	  }
	}
  });
  
  	$('#password_hint').click(function()
	{
		var _u = $('#tbxUser').val();
		if (_u == '')
		{
			$('.info-error, .info-notice, .temp').remove();
			$('#tbxUserContainer').append('<div class="clearer temp"></div><div class="info-error"><p>Required<br/><br/></p></div>');
		}
		else
		{
			$('#loaderBg').centerInClient().show(); 
	
			$.ajax({type: 'POST', url: '?do=get-hint', data: 'u='+_u, dataType: 'json', success: function(_data) 
			{
				$('#loaderBg').hide();
				
				$('.info-error, .info-notice').remove();

				if (_data.ok == 1)
				{
					$('#tbxPasswordContainer').append('<div class="clearer temp"></div><div class="info-error"><p>Your password reminder is: '+_data.response+'<br/><br /><a id="reset_password" title="Reset Password" href="#">Reset Password?</a><br/></p></div>');
				}
				else
				{
					$('#tbxPasswordContainer').append('<div class="clearer temp"></div><div class="info-error"><p>'+_data.response+'<br/><br/></p></div>');
				}
			}
			});
			return false;
		}
		return false;
	});
	
	$('#reset_password, #forgotPassword').live('click', function()
	{
		var _u = $('#tbxUser').val();
		if (_u != '')
		{
			$('#loaderBg').centerInClient().show(); 
	
			$.ajax({type: 'POST', url: '?do=reset-password', data: 'u='+_u, dataType: 'json', success: function(_data) 
			{
				$('#loaderBg').hide();
				
				$('.info-error, .info-notice').remove();

				if (_data.ok == 1)
				{
					$('#tbxPasswordContainer').append('<div class="info-notice"><div class="clearer"/><p>You have been emailed a link to enable you to reset your password</p></div>');
				}
				else
				{
					$('#tbxPasswordContainer').append('<div class="info-error"><div class="clearer"/><p>We could not reset your password, plesase try again later</p></div>');
				}
			}});
		}
		else
		{
			$('.info-error, .info-notice').remove();
			$('#tbxUserContainer').append('<div class="info-error"><div class="clearer"/><p>Required<br/><br/></p></div>');
		}
		return false;
	});

});

$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}

var sortExtraction = function(_node)  
{  
    // extract data from markup and return it  
	if (_node.className == 'tableDate')
	{
		var _date = _node.innerHTML;
		var _date_array = _date.split('/',3);
		if (_date_array.length == 3)
		{
			return (parseInt(_date_array[2]) * 10000 + parseInt(_date_array[1]) * 100+parseInt(_date_array[0]))+'';
		}
	}
	
	return _node.innerHTML; 
} 

