﻿jQuery.fn.fixate = function() {
	this.each(function(){
		var that = $(this);
		var wrapper = $("<div>").append(that.children()).appendTo(that);
		var top = that.offset().top - parseFloat(that.css("marginTop").replace(/auto/,0));
		$(window).scroll(function() {
			if($(this).scrollTop()>=top) {
				that.css("height",wrapper.height()+"px");
				wrapper.addClass("fixed").css("width",that.width()+"px");
			} else {
				that.css("height","");
				wrapper.removeClass("fixed").css("width","");
			}
		});
	});
};

jQuery.fn.makeHintedField = function() {
	this.each(function() {
		var input = $(this);
		var label = $("<input>")
			.attr("id", input.attr("id"))
			.val(input.prev("span").text())
			.addClass("hintedLabel").addClass("hinted");
		var blur = function() { if(input.val() != "") return; input.replaceWith(label); label.focus(focus); };
		var focus = function() { label.replaceWith(input); input.blur(blur); setTimeout(function() { input.focus() }, 10) };
		blur();
	});
};

// http://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery/1186309
jQuery.fn.serializeObject = function()	{
	var o = {};
	$.each(this.serializeArray(), function() {
		if(!o[this.name]) {
			o[this.name] = this.value || '';
			return;
		}	
		if (!o[this.name].push) {
			o[this.name] = [o[this.name]];
		}
		o[this.name].push(this.value || '');
	});
	return o;
};

