
jQuery.extend({
	blinkOk: function(obj){
		if(typeof(obj) == "string"){
			obj = jQuery(obj);
		}
		var currentBkg = obj.css("backgroundColor");
		obj.css({backgroundColor: "#cfc"});
		setTimeout(function(){
			obj.css({background: currentBkg});
		}, 300);		
	},

	blinkError: function(obj){
		if(typeof(obj) == "string"){
			obj = jQuery(obj);
		}
		var currentBkg = obj.css("backgroundColor");
		obj.css({backgroundColor: "#fcc"});
		setTimeout(function(){
			obj.css({background: currentBkg});
		}, 300);		
	},
	
	ft_bindAll: function(){
		/**
		 * Adds the "pressed" states to the input and div-based pills, and makes the div-based pills 
		 * follow the href inside even when clicking outside the <a> tag, like at the edge of the buttons.
		 */
		jQuery(".pill-70, .pill-100, .pill-130").bind("mousedown", function(){
			jQuery(this).addClass("pressed");
		}).bind("mouseup", function(){
			jQuery(this).removeClass("pressed");
		}).bind("click", jQuery._ft.pillBindClick);
		
		/**
		 * Fancy checkboxes
		 */
		jQuery(".inputCheckbox").each(function(){
			if(jQuery(this).css("display") != "none") {
				jQuery(this).hide().after('<div id="' + jQuery(this).attr("id") + '_img" class="inputCheckboxImg'
					+ (jQuery(this).attr("disabled") ? " disabled" : "")
					+ (jQuery(this).attr("checked") ? " checked" : "")
					+ '"></div>');
			}
		});

		jQuery(".inputCheckboxImg:not(.disabled)").bind("click", jQuery._ft.checkboxBindImageClick).css({cursor: "pointer"});
		
		jQuery(".inputCheckbox").bind("change", jQuery._ft.checkboxBindInputChange);
	},

	/**
	 * "Private" functions used by the FormTools code, encapsulated within the _ft object
	 */
	_ft: {
		/**
		 * Bind to the "click" event of the checkbox images, and trigger the change
		 * event on the input boxes
		 */
		checkboxBindImageClick: function(){
			// find the hidden input checkbox's ID by removing the "_img" from the end of $(this)'s
			var inputID = jQuery(this).attr("id").split("_");
			inputID.pop();
			ele = jQuery("#" + inputID.join("_"));
			
			if (ele.attr("checked")) {
				ele.attr("checked", "").change();
			}
			else {
				ele.attr("checked", "checked").change();
			}
		},
		
		/**
		 * Bind to the "change" event of the actual input boxes and set the "checked" class on the image
		 */
		checkboxBindInputChange: function(){
			if(jQuery(this).attr("checked"))
				jQuery("#" + jQuery(this).attr("id") + "_img").addClass("checked");
			else
				jQuery("#" + jQuery(this).attr("id") + "_img").removeClass("checked");
		},
		
		/**
		 * For href pills, make a click anywhere on the button follow the a href url
		 */
		pillBindClick: function(){
			if(jQuery(this).is("div") && jQuery(this).find("a").length > 0)
				window.location = jQuery(this).find("a").attr("href");
		}
	}
});

;(function($){
	/**
	 * Return the value of a Form_Tools_Element
	 */
	$.fn.ft_val = function(val){
		/**
		 * Checkboxes
		 */
		if(this.attr("type") == "checkbox") {
			if (typeof val == "undefined") {
				/* Return the checkbox value */
				return (this.attr("checked") ? this.val() : 0);
			}else{
				/* Set the checkbox value */
				this.attr("checked", val ? "checked" : "").change();
			}
		}
		return null;
	};

	$.fn.ft_toggle = function(){
		/**
		 * Checkboxes
		 */
		if(this.attr("type") == "checkbox") {
			$("#" + this.attr("id") + "_img").click();
		}
	};
	
})(jQuery);

jQuery(function(){
	jQuery.ft_bindAll();
});
