/*
 * jQuery Niceforms 1.0
 *
 * Copyright (c) 2008 NavigationArts (www.navigationarts.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2008-06-03 11:13:00 -0500 (Thu, 3 July 2008) $
 * $Rev: 0 $
 */

(function($) {
	//
	// plugin definition
	//
	$.fn.niceforms = function(obj) {
		// iterate and reformat each matched element
		return this.each(
			function(i) {
				var tag  = $.fn.niceforms.capitalize($(this).get(0).tagName.toLowerCase());
				
				if (tag == "Input") {
					var type = $.fn.niceforms.capitalize($(this).attr("type").toLowerCase());
					
					if (type == "Text" || type == "Password" || type == "Button" || type == "Submit") {
						$(this).wrap('<div class="js' + type + 'WrapperLeft"><div class="js' + type + 'WrapperRight"></div></div>');
					}
					
					else if (type == "Checkbox") {
						$(this).hide();
						$(this).wrap('<div class="jsCheckboxWrapper"></div>');
						$(this).parent().click(function(){
							$.fn.niceforms.checkboxWrapperClicked(this);
						});
						$("label[for=" + $(this).attr('id') + "]").click(function(){
							$.fn.niceforms.checkboxLabelClicked(this);
						});
						
						if ($(this).attr("checked") == "1") {
							$(this).parent().addClass("checked");
						}
					}
					
					else if (type == "Radio") {
						$(this).hide();
						$(this).wrap('<div class="jsRadioWrapper"></div>');
						$(this).parent().click(function(){
							$.fn.niceforms.radioWrapperClicked(this);
						});
						$("label[for=" + $(this).attr('id') + "]").click(function(){
							$.fn.niceforms.radioLabelClicked(this);
						});
						
						if ($(this).attr("checked") == "1") {
							$(this).parent().addClass("checked");
						}
					}
				}
				
				else if (tag == "Select") {
					if ($(this).attr("id")) {}
					else {
						$(this).attr("id", "jsSelect" + i);
					}
					var thisID = $(this).attr("id");
					
					var defaultText = $(this).get(0).options[$(this).get(0).selectedIndex].text;
					
					
					//create and build div structure
					$(this).before('<div class="jsSelectWrapper" id="jsSelectWrapper' + i + '"><div class="jsSelectWrapperLeft"></div><div class="jsSelectWrapperRight"><a href="javascript:jQuery.fn.niceforms.selectOptionsToggle(' + i + ');"></a></div><div class="jsSelectWrapperCenter" onclick="jQuery.fn.niceforms.selectOptionsToggle(' + i + ');">' + defaultText + '</div></div>');
					
					//hide the select field
					$(this).hide();
				
					$(this).prev(".jsSelectWrapper").append('<div class="jsSelectWrapperOptions"><ul></ul></div>');
					
					$("option", this).each(
						function(j) {
							$("#jsSelectWrapper" + i + " ul").append('<li><a href="javascript:jQuery.fn.niceforms.selectOptionsToggle(' + i + '); jQuery.fn.niceforms.selectOptionClicked(\'' + thisID + '\',' + j + ',' + i + ');">' + $(this).html() + '</a></li>');
						}
					);
				}
				
				else if (tag == "Textarea") {
					$(this).wrap('<div class="js' + tag + 'WrapperLeft"><div class="js' + tag + 'WrapperRight"></div></div>');
				}
			}
		);
	};
 
	//
	// define and expose our format function
	//
 
	$.fn.niceforms.capitalize = function(str) {
	    return str.replace(/\w+/g, function(a){
	        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
	    });
	};
	
	// checkbox
	
		$.fn.niceforms.checkboxWrapperClicked = function(obj) {
			var input = $("input", obj);
			$.fn.niceforms.checkboxToggle($(input));
		};
		
		$.fn.niceforms.checkboxLabelClicked = function(obj) {
			var input = $("#" + $(obj).attr("for"));
			$.fn.niceforms.checkboxToggle($(input));
		};
		
		$.fn.niceforms.checkboxToggle = function(obj) {
			if($(obj).attr("checked") === true) {
				$(obj).attr("checked", false);
			}
			else {
				$(obj).attr("checked", true);
			}
			$.fn.niceforms.checkboxWrapperToggle($(obj));
			$(obj).blur();
		};
		
		$.fn.niceforms.checkboxWrapperToggle = function(obj) {
			if($(obj).attr("checked") === true) {
				$(obj).parent().addClass("checked");
			}
			else {
				$(obj).parent().removeClass("checked");
			}
		};
	
	// radio
	
		$.fn.niceforms.radioWrapperClicked = function(obj) {
			var input = $("input", obj);
			$.fn.niceforms.radioToggle($(input));
		};
		
		$.fn.niceforms.radioLabelClicked = function(obj) {
			var input = $("#" + $(obj).attr("for"));
			$.fn.niceforms.radioToggle($(input));
		};
		
		$.fn.niceforms.radioToggle = function(obj) {
			if($(obj).attr("checked") === true) {
				//$(obj).attr("checked", false);
			}
			else {
				$("input[name=" + $(obj).attr("name") + "]").attr("checked", false);
				$(obj).attr("checked", true);
			}
			$.fn.niceforms.radioWrapperToggle($(obj));
			$(obj).blur();
		};
		
		$.fn.niceforms.radioWrapperToggle = function(obj) {
			$("input[name=" + $(obj).attr("name") + "]").each(function(){
				if($(this).attr("checked") === true) {
					$(this).parent().addClass("checked");
				}
				else {
					$(this).parent().removeClass("checked");
				}
			});
		};
	
	// select
	
		$.fn.niceforms.selectOptionsToggle = function(i) {
			var obj = $("#jsSelectWrapper" + i + " div.jsSelectWrapperOptions");
			if ($(obj).css("display") == "block") {
				$(obj).hide();
			}
			else {
				$("div.jsSelectWrapperOptions:visible").hide(); // hide any others
				$(obj).show();

					$(obj).parent().click(function(e) {
						e.stopPropagation(); // Stops the following click function from being executed
					});
					$(document).one("click", function(f) {
						$(obj).hide();
					});
			}
		};
		
		$.fn.niceforms.selectOptionClicked = function(selectFieldId, linkNo, selectNo) {
			//feed selected option to the actual select field
			var obj = $("#" + selectFieldId);
			$("option", $(obj)).each(
				function(i) {
					if(i == linkNo) {
						$(this).attr("selected", "selected");
					}
					else {
						$(this).attr("selected", "");
					}
				}
			);
			
			var newText = $("option", $(obj)).slice(linkNo).html();
			$("#jsSelectWrapper" + selectNo + " .jsSelectWrapperCenter").html(newText);
			
			$("#jsSelectWrapper" + selectNo + " .jsSelectWrapperOptions a.selected").removeClass("selected");
			$("#jsSelectWrapper" + selectNo + " .jsSelectWrapperOptions a").slice(linkNo, linkNo+1).addClass("selected");
			$(obj).change();
		};
//
// end of closure
//
})(jQuery);






