var Common =
	{
		baseUrl: null
		, setBaseUrl: function(url)
		{
		Common.baseUrl = url || jQuery('script[src$=js/common.js]')
	    	.attr('src')
	    	.replace(/js\/common.js$/,'');
          }
		, url: function(url)
		{
			return this.baseUrl+url.replace(/^\/+/, '');
	  	}
	}
	Common.setBaseUrl();

jQuery(document).ready(function($){

	/**
	 * Make ajax spinner show on ajax calls
	 */
	$(document).ready(function(){
		$(document)
			.ajaxStart(function(){
				$('#busy').show();
				$('.busy').show();
			})
			.ajaxStop(function(){
				$('#busy').hide();
				$('.busy').hide();
			});
	});

	/**
	 * Alternate row color on tables
	 */
	$(document).ready(function(){
		$("tr:nth-child(odd)").addClass("odd");
	});

	/**
	 * Add Tooltips from title attribute to form elements.
	 * Requires Interface and tooltips plugins:
	 * <?=$javascript->link('jquery/interface/iutil')?>
	 * <?=$javascript->link('jquery/interface/itooltip')?>
	 *
	$('input').ToolTip(
		{
			className: 'inputTooltip',
			position: 'bottom',
			delay: 200
		}
	);
	*/


	/**
	 * Links and input boxes to create items on the fly.
	 * For example, see adding a new expense
	 **/
	$('.new-fly a').click(function(){
		$(this).siblings('.new-fly-add').toggle();
		return false;
	});


	/**
	 * Help message and hide links
	 */

	$('.help p.close a').click(function(){
		var id = $(this).parent().parent('.help').attr('id');
		$('#' + id).hide();
		$.cookie(id, 'hidden', {expires: 365});
		$('#help-show').show();
		return false;
	});

	$('.help p.close a').each(function(){
		var id = $(this).parent().parent('.help').attr('id');
		var hidden = $.cookie(id);
		if (hidden == 'hidden')
		{
			$('#lnk-'+id).click(); // hide help box
			// Show the link to unhide the help
			$('#help-show').show();
		}
	});

	// Show all help messages on page and clear the cookies that hide them
	$('#help-show').click(function(){
		$('.help').each(function(){
			var id = $(this).attr('id');
			$('#'+id).show('fast');
			$.cookie(id, null); // undo auto-hide
			$('#help-show').hide();
		});
		return false;
	});

	if ($('.help').length == 0)
		$('#help-show').hide(); // no help messages on this page

	//append asterisk to required labels
	$(function() { $('.required label').append(' *'); });
});



/** Quick search - ajax search at the top **/
var qsearch_term = '';
jQuery(document).ready(function($){
    $('#txt_search').autocomplete(Common.url('/users/quick_search'), {
       delay: 40,
       minChars: 2,
       matchSubset: 1,
       matchContains: 1,
       cacheLength: 100,
       width: 250,
	   onItemSelect: selectItem,
	   onFindValue: findValue,
       formatItem: formatItem,
       autoFill: false
   });

   function findValue(li) {
        if( li == null )
            return alert("No match!");

        if( !!li.extra ) {
            var userId = li.extra[1];
            window.location = Common.url('/users/edit/'+userId);
        }
    }

    function selectItem(li) {
         findValue(li);
    }

    function formatItem(row, i, num) {
        return "<ul><li class='format-item-li'>" + row[0] + "<span class='format-item-span'>" + row[1] + "</span></li></ul>";
    }

    $('#search_user').submit(function() {
        return false;
    });
});

/**
 * Sets button text to "Saving.." or "Save" and disables it when clicked
 * @param {Object} elem - element to change (<button>)
 * @param {Object} inProcess - bool (true is saving, false if not)
 * @param string label - custom text for the button
 */
function changeButtonStatus(elem, inProcess, label)
{
	if (inProcess == true)
	{
		if (!label)
			label = 'Saving...';
		if (elem.is('button'))
			elem.text(label);
		else if (elem.is('input'))
			elem.val(label);
		elem.attr('disabled', 'true');
	}
	else
	{
		if (!label)
			label = 'Save';
		if (elem.is('button'))
			elem.text(label);
		else if (elem.is('input'))
			elem.val(label);
		elem.removeAttr('disabled');
	}
}

