/* ------------------------------------------------------------
 * PROJECT        : FHSC Interface Standard
 * FILENAME       : jqform2col.js
 * ------------------------------------------------------------
 * DATE CREATED   : 12 Nov 2007
 * LAST UPDATED   : 12 Nov 2007
 * ------------------------------------------------------------
 * AUTHOR(S)      : Kevin Scholl  (KSScholl@cvty.com)
 *                : Barbi Sanders (BRSanders@cvty.com)
 * ------------------------------------------------------------ */

/* ------------------------------------------------------------
 * CHARACTER COUNT PLUGIN
 * ------------------------------------------------------------ */

// attaches a character counter to each textarea element in the jQuery object
// usage: $("#elementID").charCounter(max, settings);
// copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
jQuery.fn.charCounter = function(max, settings) {
	max = max || 255;
	settings = $.extend({
		container: "<span style=\"display: block\">",
		classname: "example",
		format: "(%1 / " + max + " characters left)",
		pulse: true
		}, settings);
	var p;
	
	function count(el, container) {
		el = $(el);
		if (el.val().length > max) {
			el.val(el.val().substring(0, max));
			if (settings.pulse && !p) {
				pulse(container, true);
				};
			};
		container.html(settings.format.replace(/%1/, (max - el.val().length)));
		};
	
	function pulse(el, again) {
		if (p) {
			window.clearTimeout(p);
			p = null;
			};
		el.animate({ opacity: 0.1 }, 100, function() {
			$(this).animate({ opacity: 1.0 }, 100);
			});
		if (again) {
			p = window.setTimeout(function() { pulse(el) }, 200);
			};
		};
	
	return this.each(function() {
		var container = (!settings.container.match(/^<.+>$/)) 
			? $(settings.container) 
			: $(settings.container)
				.insertAfter(this)
				.addClass(settings.classname);
		$(this)
			.bind("keydown", function() { count(this, container); })
			.bind("keypress", function() { count(this, container); })
			.bind("keyup", function() { count(this, container); })
			.bind("focus", function() { count(this, container); })
			.bind("mouseover", function() { count(this, container); })
			.bind("mouseout", function() { count(this, container); })
			.bind("paste", function() { 
				var me = this;
				setTimeout(function() { count(me, container); }, 10);
				});
		if (this.addEventListener) {
			this.addEventListener('input', function() { count(this, container); }, false);
			};
		count(this, container);
		});
	
	};

/* ------------------------------------------------------------
 * JQUERY PSEUDO-ONLOAD
 * ------------------------------------------------------------ */

$(document).ready(function(){
													 
	// position radio buttons and checkboxes
	$("input[type='radio']").addClass("radioBtn");
	$("input[type='checkbox']").addClass("checkBox");

	// define the border for input elements
	$("fieldset ol li, table.dataTable, table.infoTable").each(function(){
  	$("input[type='text'], input[type='password'], textarea")
		  .css("border","1px solid #7F9DB9");
  	$("select")
		  .css({border: "1px solid #7F9DB9", padding: "1px"});
		});

	}); // end ready function

// toggle user option links
function toggleEditSave(thisLink) {
	$(thisLink)
	  .html($(thisLink).html() == "Edit Info" ? "Save App" : "Edit Info")
	  .attr("class",$(thisLink).attr("class") == "linkEdit" ? "linkSave" : "linkEdit");
	}
function toggleOptionalFields(thisLink) {
	$(thisLink)
	  .html($(thisLink).html() == "Hide Optional Fields" ? "Show Optional Fields" : "Hide Optional Fields")
		.parents("fieldset")
		  .find("ol:not(.actionBtns)")
			.find("li:not(:has(label.required))").toggle();
	}

/* ------------------------------------------------------------
 * JQUERY ACTIVE FIELD PLUGIN
 * ------------------------------------------------------------ */

// toggle default value and coloring on form field focus/blur
// usage: $("#elementID").toggleActive(settings);
jQuery.fn.toggleActive = function(settings) {
	settings = jQuery.extend({
		focusBG: "#F6F6F6",
		focusFG: "#000",
		blurBG:  "#FFF",
		blurFG:  "#333"
		}, settings);
	this.each(function() {
		$(this)
			.focus(function() {
				$(this).css({ backgroundColor: settings.focusBG, color: settings.focusFG });
				if (this.value == this.defaultValue && this.value != "http://") {
					this.value = "";
					}
				})
			.blur(function() {
				$(this).css({ backgroundColor: settings.blurBG, color: settings.blurFG });
				if (this.value == "") {
					this.value = this.defaultValue;
					}
				});
		});
	};
