$(document).ready(function() {
	
	// Preload Images
	$.preloadCssImages();
	
	
	//Ajax page loading for projects pagination
	$('.pageNavLink').live('click', function() {
	  	$('#proposalsListing').load($(this).attr("href"), function(){ 
			$('a.details').each(function() {
				$(this).smfbox();
				$(window).scrollTop(0);
			});
		});
		return false;
	});
	
	$('a.details').each(function() {
		$(this).smfbox();
	});
	
	//If the page is reloaded and the boxes are checked, add the selected class
	$('#filterForm input:checkbox').each(function(){
		if($(this).attr('checked')){
			$(this).closest('label').addClass('selected');
		}
	});
	$('#filterForm input:radio').each(function(){
		if($(this).attr('checked')){
			$(this).closest('label').addClass('selected');
		}
	});
	
	//Add the selected class on click
	$('#filterForm input:checkbox').click(function(){
		$(this).closest('label').toggleClass('selected');
		$('#filterForm').submit();
	});
	$('#filterForm input:radio').click(function(){
		var selectedOne = $(this);
		$('#filterForm input:radio').closest('label').removeClass('selected');
		$(this).closest('label').addClass('selected');
		
		$('#filterForm').submit();
	});  
	
	//Submit the filter form
	$("#filterForm").submit(function(){
		$.get( 
			"projects.php",
			$("#filterForm").serialize(),
			function(data){ 
				$('#proposalsListing').html(data);
			}
		); 
		return false;
	});
	
	//Remove filters
	$('.removeFilter').live('click', function() {
		var filterToRemove = $(this).attr("name");
		var typeToRemove = $('#filterForm input[name='+filterToRemove+']').attr('type');
		if(typeToRemove == 'checkbox' || typeToRemove == 'radio') {	
			$('#filterForm [name='+filterToRemove+']').closest('label').removeClass('selected');
			$('#filterForm [name='+filterToRemove+']').removeAttr('checked');
		}
		else {
			$('#filterForm [name='+filterToRemove+']').val('');
		}	
		
		$('#filterForm').submit();
		return false;
	});
	
	//Clear filter fields
	$('.clear').click(function(){
		var toClear = $(this).attr('name');
		var typeToRemove = $('#filterForm .'+toClear).attr("type");
		if(typeToRemove == 'checkbox' || typeToRemove == 'radio') {			
			$('#filterForm .'+toClear).removeAttr('checked');
			$('#filterForm .'+toClear).closest('label').removeClass('selected');
		}
		else {
			$('#filterForm .'+toClear).val('');
		}
		
		$('#filterForm').submit();
		return false;
	});
	
	//Donate Now form
	$(".donateNow form").submit(function() {
		var donationURL = $(this).attr('action');
		var donationParam = $(this).serialize();
		window.open(donationURL+'&'+donationParam);	
		$('[name=donationAmount]').val('');	
		return false;
	});
	
	//Recommended Projects
	$('.recommended-more').click(function(){
		var projectNum = $(this).attr('name');
		$(this).hide();
		$('#overview-'+projectNum).hide();
		$('#details-'+projectNum).show(150);
		$('#recommended-less-'+projectNum).show(150);
	});
	
	$('.recommended-less').click(function(){
		var projectNum = $(this).attr('name');
		$(this).hide();
		$('#details-'+projectNum).hide();
		$('#overview-'+projectNum).show(150);
		$('#recommended-more-'+projectNum).show(150);
	});
	
	//Photo gallery
	$(function() {
	    $(".photoGallery").jCarouselLite({
	        btnNext: ".photos-next",
	        btnPrev: ".photos-prev",
			circular: true,
			visible: 1
	
	    });
	});
});


