(function($) {
    $.fn.contactForm = function(options) {
		
        var $c = this,
            $o = $('<div id="contactOverlay"></div>').appendTo(document.body),
            $close = $(options.closeSelector),
            $toggle = $(options.toggleSelector),
            $message = $(options.messageSelector);

		var placeholderSupport = "placeholder" in document.createElement("input");
		if (!placeholderSupport) {
			$c.find('input[type=text]').each(function(){
				$(this).data('placeholder',$(this).attr('placeholder'));
				$(this).addClass('placeholder');
			}).focus(function(){
				if ($(this).val() == $(this).data('placeholder')) {
					$(this).val('');
					$(this).removeClass('placeholder');
				}
			}).blur(function(){
				if ($(this).val() == '') {
					$(this).val($(this).data('placeholder'));
					$(this).addClass('placeholder');
				}
			});
		}

        $c.css({
            'top': '50%',
            'left': '50%',
            'margin-top': -$c.outerHeight() / 2,
            'margin-left': -$c.outerWidth() / 2
        });

        $o.add($close).add($toggle).click(function(e) {
            e.preventDefault();
            if ($c.is(':visible')) {
                $c.fadeOut(300, function(){
					$c.find('input[type=text]').val('');
					$message.hide()	
				});
                $o.fadeOut(300);
            } else {
                $c.fadeIn(300);
                $o.fadeIn(300);
            }
			
			if (!placeholderSupport) {
				$c.find('input[type=text]').each(function(){
					$(this).val($(this).data('placeholder'));	
					$(this).addClass('placeholder');
				});
			}
			
        });
		$toggle.each(function(i){
			$(this).click(function(){
				var title = $(this).attr('title');
				if (title != '') {
					$c.find('h3').html(title);
				}
				if ($(this).data('product') !== undefined) {
					$c.find('input[name="form_product"]').val(title);
				}
			});
		});

        $c.find('form').submit(function(e) {
            e.preventDefault();
            var $this = $(this);
            
            $.ajax({
                type: 'post',
                url: $this.attr('action'),
				dataType: 'json',
                data: $this.serialize(),
                error: function() {
                    $message.hide().show(300).html('Connection error, please try again.').removeClass('error success').addClass('error');
                },
                success: function(data) {
                    $message.hide().show(300).html(data.message).removeClass('error success');
					if (data.status) {
	                    $message.addClass('success');
						$this.find('input[type=text]').val('');
						$this.find('input[type=submit]')[0].focus();
						
						if (!placeholderSupport) {
							$c.find('input[type=text]').each(function(){
								$(this).val($(this).data('placeholder'));	
								$(this).addClass('placeholder');
							});
						}
						
					} else {
						$message.addClass('error');
					}
                }
            });

        });

        return this;
    };
})(jQuery);
