(function($) {


$.fn.prompt = function(textContainer, labelContainer) {
	return $(this).each(function() {
	
		var root = $(this);
		root.css({"position": "relative"}); // -matti
		var input = root.find(textContainer);
		var label = root.find(labelContainer);
		var prompt =
			$("<span />")
			 .addClass("prompt")
			 .insertAfter(label);
		label.show().appendTo(prompt);
		
		var pos = input.position();
		prompt.css('left', pos.left).css('top', pos.top);
		input.bind('click focus', function() {
			prompt.hide();
			if ($(this).val() == label.text()) $(this).val('');

		}).bind('blur', function() {
			if ($(this).val() == label.text() || $(this).val() == '') {
				prompt.show();
				$(this).val('');
			}
		});
		prompt.click(function() {
			$(this).hide();
			input.focus();
		});

		$(function() {
			if (input.val() != '') prompt.hide();
		});

	});
};
	
	
	$.fn.Faq = function() {
		return $(this).each(function() {
			var root = $(this);
			var question = root.find('.Question');
			question.click(function() {
				$(this).next().toggle('fast');				
			});
		});
	};
$.fn.submitForm = function(args) {
	return $(this).each(function() {
	
		var root = $(this);
		
		 var conf = $.extend({
			submit: '.AvenlaFormSubmit input', 
			url: window.location,
			inputs: 'input,textarea, select',
			requiredInputs: 'input:text:visible, select:visible, textarea:visible',
			successTarget: '.FormSuccess',
			clearFields: this.inputs,
			errorContainer: $('<div></div>').text('There was an error sending the form'),
			target: '.AvenlaForm'
		}, args);	
		
		root.find(conf.submit).click(function(e) {
			e.preventDefault();
			
			var readyForSubmit = true;
			root.find(conf.requiredInputs).each(function() {
				if(!$(this).val()) {
					$(this).parent().parent().addClass('error');	
					readyForSubmit  = false;
				}
			});					
			
			if(readyForSubmit) {
				var params = $.param(root.find(conf.inputs));								
				$.post(conf.url, params, function(data) {
					var temp = $(data);										
					if(temp.find(conf.successTarget).length) {					
						var box = temp.find(conf.target);
					} else {
						var box = $(conf.errorContainer);
					}
					if(box.length) {
						box.appendTo($('#Form')).hide();
						box.css({
							top: root.find(conf.submit).offset().top - box.outerHeight() - 20,
							left: root.find(conf.submit).offset().left - 50,
							position: 'absolute'
						}).show();
						
						if(window.Cufon) {
							Cufon.replace(box.find('.Heading span'));
						}
						
						box.find('.close').click(function(e) {
							root.find(conf.inputs).val("");
							root.find('.prompt').show();
							e.preventDefault();
							box.remove();
						});
					}
				});
			}
		});
		
		root.find(conf.inputs).keydown(function() {
			if(root.val()) {
				root.parent().removeClass('error');
			}
		});			
	});
};
})(jQuery);
