/*
	JQuery AJAX functions
	Written by Simon Baxter
	Copyright 2011 Simbax.com Ltd
*/

$(document).ready(function(){ 
	
	$("form#signin_form").submit(function(){
		$.ajax({  
			type: "POST", 
			cache: false, 
			url: "ajax.php",  
			dataType: "text",
			data: ({
				'action':'login',
				'email': $('#email').val(),
				'password':$('#password').val()
			}),  
			success: function(result)
				{ 
					if(result === '1') {
						if(window.location.pathname == '/logout.php') {
							window.location.replace("http://www.s2kuk.com/account.php");
						} else {
							location.reload();
						}
					} else {
						alert(result);
					}
				}  
		});
		return false;
	});
	
	/*
		Function to delete a members photo
	------------------------------------------------------------------------------------------------------*/
	$('.delete_car_photo').live('click', function(e) {
		var id = $(this).attr('id');
		if (confirm('Are you sure you want to delete this photo? This cannot be undone')) {
			$.ajax({  
				type: "POST",  
				url: "ajax.php",  
				dataType: "text",
				data: ({
					'action':'deletephoto',
					'id': id
				}),  
				success: function(result)
					{ 
						if(result=='1') {
							$(e.target).closest('div').fadeOut(300, function() {
							//$(e.target).fadeOut(300, function() {
								$(this).remove();
								}
							);
						} else {
							alert(result);
						}
					}  
			});
		}
	});

	
	/*
		Change the input field style on focus.  Revert back on blur
	------------------------------------------------------------------------------------------------------*/
	$('.textfield').focus(function() {
		$(this).addClass("current_textfield");
	});
	$('.textfield').blur(function() {
		$(this).removeClass("current_textfield");
	});
	
	/*
		Hide PayPal form if different pay metod chosen
	------------------------------------------------------------------------------------------------------*/
	$('#pay_method').change(function() {
		if($(this).val() == 'Card') {
			$('#paypal_fields').slideDown();
		} else {
			$('#paypal_fields').slideUp();
		}
	});
	
	/*
		Count how many calendar photos a user has checked for voting
	------------------------------------------------------------------------------------------------------*/
	$('.vote_checkbox').click(function(e) {		
		if($(":checkbox:checked").length >= 4) {
			alert('You can only choose 3 of your favourite photos.');
			$(this).attr('checked', false);
		} else {
			if ($(this).is(':checked')) {
				$(e.target).closest('div').addClass('vote_highlight');
			} else {
				$(e.target).closest('div').removeClass('vote_highlight');
			}
		}
	});
	
});

function getMembersPhotos(mid)
{
	$.ajax({  
		type: "POST", 
		cache: false, 
		url: "ajax.php",  
		dataType: "text",
		data: ({
			'action':'getmembersphotos',
			'id': mid
		}),  
		success: function(result)
			{ 
				$('#car_photos').hide().html(result).fadeIn();
				$(".colorbox").colorbox();
			}  
	});
	return false;
}
