$(document).ready(function(){
	bind_actions();
	init_fade();

	$('#contact_form').submit(function(event){
		event.preventDefault();

		$.ajax({
			type: 'post',
		    url: $(this).attr('action'),
		    data: $(this).serialize(),
		    dataType: 'json',

		    success: function(results) {
		    	$('#contact_form').html('Запитването беше изпратено успешно!').hide().fadeIn(1800);
		    }
		});
	});
});

function bind_actions()
{
	$('#btn_old').click(function(e){
		e.preventDefault();

		var type = $(this).attr('rel');
		var page = $('#news').attr('rel');

		load_news(page, type);
	});

	$('#btn_new').click(function(e){
		e.preventDefault();

		var type = $(this).attr('rel');
		var page = $('#news').attr('rel');

		load_news(page, type);
	});
}

function load_news(page, type)
{
	$.ajax({
		type: 'get',
	    url: url_for_ajax,
	    data: 'page=' + page + '&type=' + type,
	    dataType: 'html',

	    success: function(results) {
			// Render the resulting data, via template.
			$('#news').after(results).remove();
			bind_actions();
			init_fade();
	    }
	});
}

function init_fade()
{
	$('#news li a').each(function(i){$(this).fadeTo('slow', 1 - i/7);});
}