/* Contact */
jQuery.fn.contact = function(settings) {
	return this.each(function() {
		var __e = $(this);
		var options = $.extend({}, settings);

		contact(__e, options);
	});
};


function contact(__e, options) {
	var $background = __e.find('[data-contact-background]'),
		$form = __e.find('[data-contact-form]'),
		$close = __e.find('[data-contact-close]');

	_init();

	function _init() {
		var s = getPageSize();

		__e.hide().addClass('contact');
		$background.addClass('contact-background');
		$form.addClass('contact-form');

		__e.find('textarea').css('resize', 'none').autogrow();
		$background.css({
			'width': s[0],
			'height': s[1]
		});
		$form.css({
			'left': (s[0] / 2) - 270
		});

		$('[href$="/contact"]').removeAttr('href').css('cursor','pointer').unbind('click').click(_show);
		$('[onclick*="/contact"]').removeAttr('onclick').css('cursor','pointer').unbind('click').click(_show);
		$close.removeAttr('onclick').css('cursor','pointer').unbind('click').click(_close);
		$form.removeAttr('onsubmit').removeAttr('action').unbind('submit').submit(_send);
	}
	
	function _close() {
		__e.hide();
	}
	
	function _show() {
		var s = getPageSize();
		
		__e.find('textarea, input[type=text]').val('');
		__e.find('#contact_page').val(window.location.href);
		__e.find('pre.validation').remove();
		__e.find('.invalid').removeClass('invalid');
		
		$form.css({
			'left': (s[0] / 2) - 270,
			'top': $(window).scrollTop() + 110
		});
		
		__e.show();
	}
	
	function _send() {
		var _form = $form.find('form');
		if (validate(_form)) {
			_close();
			
			$.ajax({
				type : "POST",
				url : "/contact",
				data : _form.serialize(),
				error : function() {
					notifyError('Error sending mail.');
				},
				success : function(data, textStatus) {
					if (new Number(data) == 0)data = undefined;
					notifyMail(data);
				}
			});
		}

		return false;
	}
	
}

