var flashvars = {};
var params = { menu: "false" };
var attributes = { wmode: "transparent" };

swfobject.embedSWF("piecemaker.swf", "spotlightMovie", "940", "380", "10.0.0", "expressInstall.swf", flashvars, params, attributes);

var postUrl = '/php/mail.php';

$(document).ready(function() {

	$('a.faq').click(function(){
		var qid = $(this).attr('href');
		var question = $(qid);
		if (question.is(':visible')){
			question.slideUp();
		} else {
			question.slideDown();
		}
		return false;
	});
			
	$("a.make_an_appointment").fancybox({
		modal: true,
		onStart: function(){
			$.browser.msie && $('#spotlightMovie').hide();
		},
		onClosed: function(){
			$.browser.msie && $('#spotlightMovie').show();
		}
	});

	var errors = {
		name: '* UZUPEŁNIJ WSZYSTKIE POLA',
		surname: '* UZUPEŁNIJ WSZYSTKIE POLA',
		phone: '* UZUPEŁNIJ WSZYSTKIE POLA',
		email: '* WPISZ POPRAWNY ADRES EMAIL'
	};
	
	var defaults = {
		name: 'Imię',
		surname: 'Nazwisko',
		phone: 'Numer telefonu',
		email: 'Adres e-mail'
	};
	
	function setError(val) {
		
		$('#error').html(val);
		
	};

	function reset() {
		
		$('#doradca form input[type=text]').each(function(){
			
			this.value = defaults[this.name];
			
		})
		
	};
	
	function postError() {
		
		$.fancybox({content: '<div style="padding: 10px;background: white;">Niestety nie udało się wysłać wiadomości. Prosimy spróbować ponownie, a jeśli błąd będzie się powtarzał skontaktuj się z nami wysyłając e-mail na: <a href="mailto:kontakt@financialtrust.pl"><b><u>kontakt@financialtrust.pl</u></b></a></div>'});
		
	};
	
	function postSuccess(data) {
	
		reset();
		
		$.fancybox({content: '<div style="padding: 10px;background: white;">Dziękujemy za wysłanie wiadomości. Nasz doradca wkrótce się z Tobą skontaktuje</div>'});
	
	};
	
	function postData(data) {
		
		$.ajax({
			url: postUrl,
			success: postSuccess,
			error: postError,
			data: data,
			type: 'post'
		});
		
	};

	$('#doradca form').submit(function(){
	
		var post = {};
	
		// walidacja
		$(this).find('input[type=text], select').each(function(){
			
			if (this.name) post[this.name] = $(this).val();
			
		});
		
		// imie
		if (post.name === '' || post.name === 'Imię') {
			
			setError(errors.name);
			
			return false;
			
		};
		
		if (post.surname === '' || post.surname === 'Nazwisko') {
		
			setError(errors.surname);
			
			return false;
		
		};
		
		if (post.phone === '' || post.phone === 'Numer telefonu') {
		
			setError(errors.phone);
			
			return false;
		
		};
		
		if (!post.email.match(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/)) {
		
			setError(errors.email);
			
			return false;
		
		};
		
		postData(post);
		
		return false;
	
	}).find('input[type=text]').each(function(){
		
		this.value = defaults[this.name];
		
	}).focus(function(){
		
		if (this.value = defaults[this.name]) {
			
			this.value = '';
			
		};
		
	}).blur(function(){
		
		if (this.value === '') {
			
			this.value = defaults[this.name];
			
		};
		
	});
	
	$('#doradca .pSubmit a').click(function(){
		
		$.fancybox.close();
		
		return false;
		
	});

});
