// Establish default ajax settings
$.ajaxSetup({
	complete: function(jqXHR, textStatus)
	{
		if(textStatus == 'timeout')
		{
			// display message about timeout?
		}
	},
	cache: false
}); // timeout: 10000,

/**
 * Tracks an event in any implemented analytics services
 *
 * @param string category Event category (ie "Plans")
 * @param string action Event action (ie "Viewed Plan Recommendation")
 * @param string value Event value (ie "252")
 */
function track_event(category, action, value)
{
	// Google Analytics
	if(typeof _gaq != 'undefined')
	{
		_gaq.push(['_trackEvent', category, action, value]);
	}
	
	// KISSmetrics
	if(typeof _kmq != 'undefined')
	{
		_kmq.push(['record', action]);
	}
}

/**
 * Disable a button
 */
function disableButton(element)
{
  if(!element) { return; } // canary

  element.css({ opacity: .6, cursor: 'default' });
  element.unbind('click');
}

/**
 * Confirm button has been used
 */
function confirmButton(element)
{
	element = $(element);
	
  if(!element) { return; } // canary
  
  var check = $('<img src="/images/check.png" />');
  check.css({ position: 'absolute', top: '7px', right: '-25px', 'zIndex': 10 });

  element.append(check);  
  element.css('cursor', 'default');
  element.unbind('click');
}

/**
 *  resize a text area to grow with the text
 *  
 *  @param  object  element the textarea element
 *  @param  integer font_size the font size the textarea uses
 *  @param  integer integer min_height  how small the height the text area can be 
 */
function resizeTextarea(element, font_size, min_height)
{
	element = $(element);
	
  // defaults, for some reason, ie7 reports the font-size as the textarea width...
  if(!font_size || (font_size > 100) || (font_size < 10))
	{ 
		font_size = 16; 
	}
  
  // NOTE: textarea only gets a new size if it isn't empty...
  if(element.val())
	{
  	// pixel width an average character is, lots of w's and m's throw this off though...
	  var font_width = Math.ceil(font_size / 2) + 2;

	  // pixel height an average character is...
	  var font_height = Math.ceil(font_size * 1.5);

	  // get the max width...
	  var max_width = element.offsetWidth;
  
	  // get the actual amount of lines in the textarea form...
	  var real_lines = element.val().split(/\n/);
	  var new_height = real_lines.length * font_height;
  
	  // go through each of the actual lines and find out if they have wrapped...
	  for(var i in real_lines)
		{
	    var line_width = real_lines[i].length * font_width;

			// the line has wrapped atleast once, so let's find out exactly how many times and compensate...
	    if(line_width > max_width)
			{
	    	new_height += ((line_width / max_width) * font_height);
	    }
	  }
  
		// actually set the height...
		if(new_height < min_height)
		{ 
			new_height = min_height; 
		}
		
  	element.css('height', Math.ceil(new_height) + 'px');
  }
	else
	{
  	element.css('height', Math.ceil(min_height) + 'px');
  }
}
