/* CLIQUE */
/* Form Enhancements Pattern Javascript
----------------------------------------------------------------------------- */


	(function($) {


	$.fn.cliqueForms = function(parameters) {
	

		var defaults = {
		
		};

		var parameters = $.extend(defaults, parameters);


/* PRESERVE CHAINING
----------------------------------------------------------------------------- */

	
	return this.each(function(index) {

		var $this = $(this);


/* WIDGETS - DROPDOWN REPLACEMENTS
----------------------------------------------------------------------------- */


	if ($this.find('option').length > 0 && $this.hasClass('replace') || $this.hasClass('toggle')) {

		function replaceWithField() {

			var $label = $(this).parent();
			var selection = $(this).children('option:selected').attr('class');
	
			if (selection && selection.match('replace')) {
			
				var name = $(this).attr('name');
				var id = $(this).attr('name');
				var size = $(this).children('option:selected').val();
	
				if (size.length > 0) {
		
					$('<input type="text" id="' + id + '" name="' + name + '" size="' + size + '" maxlength="' + size + '" />').insertAfter(this);
	
				} else {
				
					$('<input type="text" id="' + id + '" name="' + name + '" />').insertAfter(this);
				
				}
	
				$(this).remove();
				$label.children('input').focus();
	
			}
			
			return false;
		
		}
		
		if ($this.find('option.replace').length == 0) {
		
			var characters = $this.find('option:last-child').val().length;
			$this.append('<option class="replace" value="' + characters + '">Other</option>');

		}
		
		$this.each(replaceWithField);
		$this.change(replaceWithField);

	}


/* WIDGETS - TOGGLES
----------------------------------------------------------------------------- */


	if ($this.hasClass('toggle')) {

		$this.find('input[type=radio]').hide();

		$this.children('li').click(function() {
		
			$(this).children('label').children('input').attr('checked','checked');
			
			$this.find('label strong').each(function() {
			
				var label = $(this).text();
				$('<em>' + label + '</em>').insertAfter(this);
				$(this).remove();
			
			});
			
			var $selection = $(this).children('label').children('em');
			var label = $selection.text();
			
			$('<strong>' + label + '</strong>').insertAfter($selection);
			$selection.remove();
		
		});

	}


/* ADVICE LOOKUP
----------------------------------------------------------------------------- */


	if ($this.hasClass('lookup')) {

		$this.blur(function() {

			var lookup = $this.attr('id');
			var site = window.location.toString().split('/');
			var check = 'http://' + site[2] + '/' + lookup + '/check.html?' + lookup + '=' + $this.val();
			
			$.get(check, function(result) {
			
				$this.next().remove();
				$this.after('<small>' + result + '</small>');
			
			});

		});
		
	}


/* INPUT PLACEHOLDERS
----------------------------------------------------------------------------- */


	if (parameters.placeholder) {

		if (!$this.val() && !$this.parent().hasClass('error')) {

			if (navigator.userAgent.match('WebKit')) {
			
				$this.attr('placeholder', parameters.placeholder);
			
			} else {
			
				$this.val(parameters.placeholder).addClass('placeholder_text');
							
				$this.focus(function() {
				
					if ($this.val() == parameters.placeholder) $this.val('').removeClass('placeholder_text');
				
				});
				
				$this.blur(function() {
				
					if (!$this.val()) $this.val(parameters.placeholder).addClass('placeholder_text');
				
				});
			
			}
		
		}

	}
	
	
/* SEARCH INPUTS
----------------------------------------------------------------------------- */

	
	if ($this.attr('id') == "query" && $this.parent().parent().hasClass('search') && navigator.userAgent.match('WebKit')) {
	
		var search_id = $this.attr('id');
		var search_name = $this.attr('name');
		var search_placeholder = $this.attr('placeholder');
		if (!search_placeholder) var search_placeholder = "Search";
		$this.replaceWith('<input type="search" id="' + search_id + '" name="' + search_name + '" results="10" placeholder="' + search_placeholder + '" />');

	}


/* END PLUG-IN
----------------------------------------------------------------------------- */


			});

		};

	})(jQuery);
