var contact = {
	init: function() {
		$.get( "/feedback", function(data){
			$(data).modal({
				closeClass: 'modalClose',
				position: ["10%"],
				dataId: 'contact-data',
				overlayId: 'contact-overlay',
				overlayCss: {
					height:"100%",
					left:["50%"],
					opacity:"0.5",
					position:"fixed",
					top:0,
					width:"100%",
					"z-index":100000
				},
				containerId: 'contact-container',
				containerCss: {
					height:490,
					padding:0,
					position:"fixed",
					width:560,
					"z-index":100001
				},
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
	},
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#contact-container .contact-button').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if ($.browser.safari) {
			$('#contact-container .contact-input').css({
				'font-size': '.9em'
			});
		}

		// dynamically determine height
		var h = 280;
		if ($('#contact-subject').length) {
			h += 26;
		}
		if ($('#contact-cc').length) {
			h += 22;
		}

		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#contact-container .contact-content').animate({
						height: h
					}, function () {
						$('#contact-container form').fadeIn(200);
					});
				});
			});
		});
	},
	show: function (dialog) {
		$('#contact-container #submit-btn').click(function (e) {
			e.preventDefault();
			// validate form
			if ( contact.validate() ) {
				$.ajax({
					url: '/feedback/submit',
					data: $('#contact-container form').serialize() + '&action=send',
					type: 'post',
					cache: false,
					dataType: 'html',
					success: function (data) {
						$.modal.close();
					},
					error: contact.error
				});
			}
			else {
				if ($('#contact-container .contact-message:visible').length > 0) {
					var msg = $('#contact-container .contact-message');
					msg.fadeOut(200, function () {
						msg.empty();
						contact.showError();
						msg.fadeIn(200);
					});
				}
				else {
					$('#contact-container .contact-message').animate({
						height: '30px'
					}, contact.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		dialog.data.fadeOut('fast', function () {
			dialog.container.hide('fast', function () {
				dialog.overlay.slideUp('fast', function () {
					$.modal.close();
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		contact.message = '';

		var email = $('#contact-container #email').val();
		if (!email) {
			contact.message += 'Email girmediniz. ';
		}
		else {
			if (!contact.validateEmail(email)) {
				contact.message += 'Hatalı email. ';
			}
		}

		if (!$('#contact-container #message').val()) {
			contact.message += 'Mesaj girmediniz.';
		}

		if (contact.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	validateEmail: function (email) {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		
		if ( ! reg.test( $('#contact-data #email').val() ) ) {
			return false;
		}
		
		return true;
	},
	showError: function () {
		$('#contact-container .contact-message')
			.html($('<div class="contact-error"></div>').append(contact.message))
			.fadeIn(200);
	}
};