/* utils */

jQuery.fn.ajaxify = function() {
	this.submit(function() {
		var error = false;
		var form = jQuery(this);
		form.find(':input').each(function(index, element) {
			var elementObj = jQuery(element);
			if (elementObj.attr('name').length == 0 || elementObj.attr('id').length == 0) return;

			var selector = '';
			if (elementObj.attr('type') == 'text') {
				selector = 'div.input.text';
			} else if (elementObj.attr('type') == 'textarea') {
				selector = 'div.input.textarea';
			} else if (elementObj.attr('type') == 'select-one') {
				selector = 'div.input.select';
			}

			if ((elementObj.val().length == 0 || elementObj.val() == elementObj.attr('alt')) && elementObj.hasClass('allowEmpty') == false) {
				elementObj.closest(selector).addClass('error');
				error = true;
			} else {
				elementObj.closest(selector).removeClass('error');
			}
		});

		if (error == true) return;

		form.find('button').hide();
		form.find('.loader').show();

		jQuery.ajax({
			type: 'POST',
			url: form.attr('action'),
			data: form.serializeArray(),
			success: ajaxifyCallback,
			dataType: 'json',
			context: form
		});
	});

	return this.each(function(){});
};

function ajaxifyCallback(data, txt) {
	var form = $(this.context);
	if (data.errors == null) {
		form.closest('div').html(form.siblings('.form-replacement').html());
		return;
	}

	form.find('.loader').hide();
	form.find('input[type=submit]').show();

	jQuery.each(data.errors,function(index,value) {
		var inputObj = form.find(":input[name='data[" + index + "]']");

		var selector = '';
		if (inputObj.attr('type') == 'text') {
			selector = 'div.input.text';
		} else if (inputObj.attr('type') == 'textarea') {
			selector = 'div.input.textarea';
		}

		inputObj.closest(selector).addClass('error');
	});
};

$(function() {
	$('ul.sf-menu').superfish({
		animation: {height:'show', opacity:'none'},
		delay: 500
	});
	$('.sf-menu ul ul').children(':last-child').addClass('last');
	$('.sf-menu ul').children(':last-child').addClass('last');
	$('.sf-menu').children(':last-child').addClass('last');
	$('.like-a-news').children(':last-child').addClass('last');
	$('.news-box').children(':last-child').addClass('last');
	$('.clinic').children(':last-child').addClass('last');
	$('.text-box ul.dossier').children(':last-child').addClass('last');
	$('#slides').cycle({
		fx:			'fade',
		random:		1,
		speed:		1000, 
		pause:		1,
		timeout: 15000
	});
	$('.holder').cycle({
		fx:			'zoom',
		random:		0,
		speed:		1000, 
		pause:		1,
		timeout: 5000
	});
	$(".bio").hide();
	$("div.accordion a.read-more").toggle(function() {
		$(this).parent().find(".bio").slideDown("slow");
		$(this).text("zwiń »");
	}, function() {
		$(this).parent().find(".bio").slideUp("slow");
		$(this).text("więcej »");
	});
	equalHeight($(".card"));

	function equalHeight(group) {
		var tallest = 0;
		group.each(function() {
			var thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}

	$("#unit-1").show();
	$("#unit-2").hide();
	$("#unit-3").hide(); 
	
	$(".unit-1-trigger").click(function() {
		$("#unit-1").slideDown(600);
		$("#unit-2").hide(200);
		$("#unit-3").hide(200);
	});
	$(".unit-2-trigger").click(function() {
		$("#unit-1").hide(200);
		$("#unit-2").slideDown(600);
		$("#unit-3").hide(200);
	});
	$(".unit-3-trigger").click(function() {
		$("#unit-1").hide(200);
		$("#unit-2").hide(200);
		$("#unit-3").slideDown(600);
	});
		
	$('#NormalFormNothingToBeSeenHere #FilterFilter').change(function() {
		if (!$(this).val) return;
		if ($(this).val().length != 0) {
			$('#NormalFormNothingToBeSeenHere').attr('action', $('#NormalFormNothingToBeSeenHere').attr('action') + '/' + $(this).val());
		}
		
		$('#NormalFormNothingToBeSeenHere').submit();
	});

	$('#SearchForm').submit(function() {
		$('#SearchForm').attr('action', $('#SearchForm').attr('action') + '/' + $('#SearchQuery').val());
		return true;
	});
	
	$('#SearchBoxForm').submit(function() {
		$('#SearchBoxForm').attr('action', $('#SearchBoxForm').attr('action') + '/' + $('#SearchBoxQuery').val());
		return true;
	});
	
	$('#FullContactForm').ajaxify();
//	$("#FullContactForm button").click(function() {
//		$("#FullContactForm").submit();
//	});
});


