////////////////////////
// FORM INPUT PLUGINS //
////////////////////////

(function($) {
	$(function(){
		// LOAD PLUGINS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
		$("a[rel='external']").attr({"target": "_blank", "class":"external"});
		$("*[class*=useTitleForValue]").useTitleForValue();
		$("input[class*=numberOnly]").numberOnly();
		$("ul[class*=toggleList]").toggleList("h3", ".content", true);
		//$("select[class*=submitOnChange]").submitOnChange();
		
		// FUNCTIONALITY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
		
 	}); 
 })(jQuery); 


// PLUGINS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

jQuery.fn.useTitleForValue = function() { 
	this.each(function(){
		i = $(this);
		if (i.attr("title").length) {
			i.addClass("inactive").attr({
				"value": i.attr("title")
			});
			i.focus(function(){
				if ($(this).val() == $(this).attr("title")) { $(this).removeClass("inactive").removeAttr("value");}
			})
			.blur(function(){
				if ($(this).val() == "") {$(this).addClass("inactive").attr({"value": $(this).attr("title")});}
			})
		}
	})
	return this;
};

jQuery.fn.numberOnly = function() {
	this.each(function(){
		$(this).keypress(function(e){
			var nums = "0123456789";
			var w = e.which;
			var keycode = String.fromCharCode(w);
			
			// don't block L/R, Delete, Backspace, or '+'
			if (w == null || w == 0 || w == 8 || w == 9 || w == 13 || w == 27 || w == 32 || w == 43 || w == 46) {
				return true;
				// limit to numbers
			}else if (nums.indexOf(keycode) > -1) {
				return true;
			}else {
				return false;
			}
		});
	});
	return this;
}

jQuery.fn.submitOnChange = function(){
	this.each(function(){
		$(this).change(function(){$(this).parents("form")[0].submit();});
	});
	return this;
}

jQuery.fn.toggleList = function(title, body, addAllControl){
	this.each(function(){
		i = $(this);
		i.addClass("toggleActive")
		$(body, i).hide();
		$(title, i).css("cursor", "pointer").click(function(){
			$(this).siblings(body).slideToggle("fast");
			$(this).toggleClass("active")
		});
		i.before('<p class="toggle-all"><a href="#">Expand all items in this list</a></p>');
		i.prev(".toggle-all").click(function(){
			$(body+":eq(0):visible", i).size() ? $(body, i).hide() : $(body, i).show();
			$("a", this).text($("a", this).text().indexOf("Expand") == -1 ? "Expand all items in this list" : "Collapse all items in this list");
		});
	});
	return this;
}