// Sends proper headers for AJAX requests
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

// A link to remote function using jQuery


// A link as form submit button using jQuery
jQuery.fn.linkAsSubmit = function(request) {
	this.click(function() {
		var $form = $(this).closest("form");
		if ( request === undefined ) {
		  	$form.attr("action", $(this).attr("href")).submit();
		} else {
			if (request == "delete") {
				var answer = confirm("Are you sure?");
				if (answer) { 
					$form.attr("action", $(this).attr("href"));
					$("input[name='_method']", $form) ? $("input[name='_method']", $form).attr("value", request) : $form.prepend('<input type="hidden" name="_method" value="' + request + '" />')
					$form.submit();
				}
			}
		}
		return false;
	});
	return this;
}

// Delay function used for delaying animations
jQuery.fn.delay = function(time, func) {
	this.each(function() {
		setTimeout(func, time);
	});
	return this;
}

// Vertical Align
jQuery.fn.vAlign = function() {
	return this.each(function(i){
	var oh = $(this).outerHeight();
	var mt = oh / 2;	
	$(this).css("margin-top", "-" + mt + "px");	
	$(this).css("top", "50%");
	$(this).css("position", "absolute");	
	});	
}

// Horizontal Align
jQuery.fn.hAlign = function() {
	return this.each(function(i){
	var ow = $(this).outerWidth();	
	var ml = ow / 2;	
	$(this).css("margin-left", "-" + ml + "px");
	$(this).css("left", "50%");
	$(this).css("position", "absolute");
	});
}

jQuery.fn.embedded_labels = function() {
	$(":input[class!='no_clear']", this).each(function() {
	  $(this).data("txt", $.trim($(this).val()));
	}).focus(function() {
	  if ($.trim($(this).val()) === $(this).data("txt")) { 
			$(this).val("").css("color", "#2d2d2d");
		}
	}).blur(function() {
	  if ( $.trim($(this).val()) === "" && !$(this).hasClass("once") ) { 
			$(this).val($(this).data("txt")).css("color", "#8795A2");
		}
	});
	return this;
}