/**
 * @fileoverview jQuery scripts for Lexummedica
 * @author Bartosz Białas
 * @copyright Copyright (c) Fresh Solutions Sp. z o.o.
 * @version 1.0
 */
var root_path = '/';
$(function(){
	/**
	 * Cupon animation
	 */
	$('#cupon').hover(function() {
		$(this).animate({
			bottom: -10
		}, 200);
	}, function() {
		$(this).animate({
			bottom: 0
		}, 200);
	});
	
	/**
	 * Download animation
	 */
	$('#download').hover(function() {
		$(this).animate({
			bottom: -10
		}, 200);
	}, function() {
		$(this).animate({
			bottom: 0
		}, 200);
	});
	
	/**
	 * Colorbox - pdf open on layer
	 */
	$('#download').colorbox({iframe: true, width: 930, height: 700, onOpen: function() {
			_gaq.push(['_trackEvent', 'button', 'press', 'pobrano_ulotke']);
	}});
	
	/**
	 * Cupon print
	 */
	$('#cupon').click(function() {
		window.open(this.href, null, 'width=750,height=400,scrollbars=yes');
		return false;
	});
	$('#print-form').submit(function(){
		_gaq.push(['_trackEvent', 'button', 'press', 'wydrukowano_kupon']);
		return false;
	});
	
	/**
	 * Form validation
	 */
	$('#article form').submit(function() {
		mailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		$form = $(this);
		$form.css('height', $form.height()).find('.warning').removeClass('warning');
		if ($.trim($('#user_phone').val()) == '')
			$('label[for=user_phone]').parent('div').addClass('warning');
		if (!$('#accept_terms:checked').size()) {
			$('label[for=accept_terms]').addClass('warning');
			alert('Musisz wyrazić zgodę na przetwarzanie danych osobowych, aby wysłać formularz.');
			return false;
		}
		if ($('.warning').size()){
			alert('Podaj swój numer telefonu.');
			$('.warning:first label').next().focus();
			return false;
		}
		$form.addClass('processing').find('#unique_id').val((Math.random()) * 10);
		$.post(root_path+'users/create.php', $form.find('input, textarea').serialize(), function(){
			$form.removeClass('processing').html('<div class="success"><p><strong>Dziękujemy, formularz został wysłany.</strong></p><p><strong>Wkrótce oddzwonimy na podany numer.</strong></p></div>');
			_gaq.push(['_trackEvent', 'button', 'press', 'wyslano_formularz']);
		});
		
		return false;
	});
});
