$(document).ready(function () {
	
	var $aTags = $('ul.project_paging a');
	var $divAjax = $('div#ajax_content');
	var $divPreloader = $('div#preloader_media');
	
	$aTags.click(function(event) {
		
		event.preventDefault();
		
		var $aTag = $(this);
		var $href = $(this).attr('href') + '/format/html';
		
		$.ajax({
			type: 'GET',
			url: $href,
			timeout : 10000,
			dataType: 'html',
			
			beforeSend: function() 
			{
				// remove content
				$divAjax.html('');
			
				// show preloader
				$divPreloader.show();
			
				// remove class active from any other tags that have it
				$aTags.each(function () {
					
					$thisTag = $(this);
					
					if ($thisTag.hasClass('active')) {
						$thisTag.removeClass('active');
					}
							
			    });

				// set class active to the clicked link
				$aTag.addClass('active');
						
			},
			
			error: function() 
			{
				// hide preloader
				$divPreloader.hide();
				
				$divAjax.html("<p>An error occoured! Please try again later!</p>");
			},
			
			success: function(data, textStatus)
			{
				// hide preloader
				$divPreloader.hide();
												
				$divAjax.html(data);
			}
			
		});
	});
});
