var App = {
	pageFlash: $('#page-flash'),

	blockUILoadingMessage: '<span style="font-weight: bold; font-size: 20px; line-height: 24px; color: #fff;">Se incarca...</span>',
	blockUIStyle: {
		greenfield: {
			css: {
				width: '471px',
				backgroundColor: 'transparent',
				border: 'none',
				top: '100px',
				left: '50%',
				marginLeft: '-235px',
				cursor: 'default'
			},
			overlay: {
				cursor: 'wait'
			}
		},
		iPhoto: {
			css: {
				border: 'none',
				padding: '15px',
				backgroundColor: '#000',
				'-webkit-border-radius': '10px',
				'-moz-border-radius': '10px',
				'border-radius': '10px',
				opacity: .5,
				color: '#fff'
			}
		}
	},

	init: function() {
		var $that;
		$('a.pop').click(function() {
			$that = $(this);
			$.ajax({
				type: 'get',
				url: $that.attr('href'),
				success: function(server_response, status) {
					$.blockUI(jQuery.extend(
						{ message: server_response },
						App.blockUIStyle.greenfield
					));
					pageTracker._trackPageview($that.attr('href'));
					$('.blockOverlay').click($.unblockUI);
				},
				beforeSend: function(xhr) {
					$.blockUI(
						jQuery.extend({ message: App.blockUILoadingMessage }, App.blockUIStyle.iPhoto)
					);
					$('.blockOverlay').click($.unblockUI);
				},
				global: false,
				dataType: 'html'
			});

			return false;
		});

		$('body').delegate('a.link-button-close-popup', 'click', function() {
			$.unblockUI();
		});

		$('.tooltip').tooltip({
			bodyHandler: function() {
				return $(this).attr("data-tooltip");
			},
			positionLeft: true
		});
	},

	initFormRecomanda: function() {
		var $form_wrapper = $('#form-recomanda'),
			$form = $form_wrapper.find('form');

		$form.submit(function() {
			$.ajax({
				type: $form.attr('method'),
				url: $form.attr('action'),
				data: $form.serialize(),
				success: function(server_response, status) {
					var response = JSON.parse(server_response);
					if (response.status == 0) {
						$message = $('<div class="state-highlight">Datele au fost trimise cu succes</div>');
						App.pageFlash.html($message);
						$form.find('.text input').val('');
						$form_wrapper.oneTime('3s', 'hide', function() {
							$message.hide();
						});
					}
					else {
						$message = $('<div class="state-error">'+ response.error +'</div>');
						App.pageFlash.html($message);
					}
				},
				beforeSend: function(xhr) {
					$.blockUI(
						jQuery.extend({ message: App.blockUILoadingMessage }, App.blockUIStyle.iPhoto)
					);
				},
				complete: function(xhr, status) {
					$.unblockUI();
				},
				global: false,
				dataType: 'text'
			});

			return false;
		});
	}
};

