// Scripts for index.php

var startingPost = 0; // Post to start with (reverse chrono)
var numberOfPosts = 0; // Total number of posts in this category

function loadPosts(startingPost) {
	
	var activeCategory = $(".active_category_button").text(); // Get the active category
	if (activeCategory == 'All categories') {activeCategory = '';} // If it's all, make it blank
	
	$("#content").load("includes/ajax_scripts/display_posts.php", {starting_post: startingPost, category: activeCategory}); // Load the posts based on starting post and category
	
	$.post("includes/ajax_scripts/return_number_of_posts.php", {category: activeCategory}, function(data) { // Get number of total posts in this category, and with that information decide what to do with the older and newer buttons
																				 
		numberOfPosts = data.replace(/\n/, '');
		
		// Newer button
		
		if (startingPost == 0)
		{
			$("#newer_button").addClass('disabled_chrono_button').removeClass('chrono_button');
		}
		
		else
		{
			$("#newer_button").addClass('chrono_button').removeClass('disabled_chrono_button');
		}
		
		// Older button
		
		if ((numberOfPosts - startingPost) <= 10) // If number of posts minus starting post is less than 10, category or not
		{	
			$("#older_button").addClass('disabled_chrono_button').removeClass('chrono_button');
		}
		
		else
		{
			$("#older_button").addClass('chrono_button').removeClass('disabled_chrono_button');	
		}
	
	});
	
};

function loadOlderPosts()
{
	if ((numberOfPosts - startingPost) > 10)
	{
		startingPost = startingPost + 10;		
		loadPosts(startingPost);	
	}
}

function loadNewerPosts()
{
	if (startingPost > 0)
	{
		startingPost = startingPost - 10;	
		loadPosts(startingPost);	
	}
}

$( function() {	  
		  
	loadPosts(startingPost);
	$("#older_button").bind("click", function () {loadOlderPosts();});
	$("#newer_button").bind("click", function () {loadNewerPosts();}); 
	
	$(".category_button").click( function () {				    
		var category = $(this).text();
		if (category == 'All categories') {category = '';}
		$(".category_button").addClass('inactive_category_button').removeClass('active_category_button');
		$(this).addClass('active_category_button').removeClass('inactive_category_button');
	 	loadPosts(0, category);
	});
	
	$("#blast_registration_link").live('click', function(event){
							
		$("#wrapper").append(popUpMarkup);
		$("#popup_content").load("includes/ajax_scripts/blast_registration.php");
		$("#popup_box").css({'height': '410px', 'width': '900px'});
		centerPopup();loadPopup();	
		$("#popup_box").draggable();	
		event.preventDefault();
											   
	 });
	
	$("#email_my_camper_link").live('click', function(event){
							
		$("#wrapper").append(popUpMarkup);
		$("#popup_content").load("includes/ajax_scripts/email_my_camper.php");
		$("#popup_box").css({'height': '500px', 'width': '750px'});
		centerPopup();loadPopup();	
		$("#popup_box").draggable();	
		event.preventDefault();
											   
	 });
	
	// Blast registration

	$("#register").live('click', function() {
	
		var email = $("#email").val();
		var cellNumber = $("#cell_number").val();
		var cellProviderId = $("#cell_provider_id").val();
		
		if (email == '' && cellNumber == '')
		{
			alert("Please enter an e-mail address or a cell phone number.");
		}
		
		else if ((cellNumber != '' && cellProviderId == '') || (cellProviderId != '' && cellNumber == ''))
		{
			alert("Please enter your cell phone number AND select your provider, if you wish to receive text message blasts. If you do not, please clear both fields.");
		}
		
		else
		{
			var proceed = true;
			
			if (cellNumber != '')
			{
				var confirmSMS = confirm("I have read the text message service disclaimer, and acknowledge the potential costs associated with receiving text messages.")
				
				if (!confirmSMS)
				{
					proceed = false;
				}
			}
			
			if (proceed)
			{
				$("#registration_area").load("includes/ajax_scripts/process_blast_registration.php", { email: email, cell_number: cellNumber, cell_provider_id: cellProviderId});
			}
		}
	
	});
	
	$(".unsubscribe_link").live('click', function(){
	
		$("#popup_content").load("includes/ajax_scripts/blast_unregistration.php");
	
	});
	
	$("#unsubscribe").live('click',  function() {
	
		var email = $("#email").val();
		var cellNumber = $("#cell_number").val();
		
		if (email == '' && cellNumber == '')
		{
			alert("Please enter either an e-mail address, a cell phone number, or both to unsubscribe.");
		}
		
		else
		{
			$("#unregistration_area").load("includes/ajax_scripts/process_blast_unregistration.php", {email: email, cell_number: cellNumber});
		}
		
	});
	
	// E-mail my camper
	
	$(".submit_email_button").live('click', function() {
		
		var firstName = $("#first_name").val();
		var lastName = $("#last_name").val();
		var bunk = $("#bunk").val();
		var message = $("#message").val();
		var fromName = $("#from_name").val();
		
		if (firstName == '')
		{
			alert("Please enter the camper's first name.");
		}
		
		else if (lastName == '')
		{
			alert("Please enter the camper's last name.");
		}
		
		else if (bunk == '')
		{
			alert("Please select the camper's bunk. If you don't know the camper's bunk, select 'Unknown'");
		}
		
		else if (message == '')
		{
			alert("Please enter a message.");
		}
		
		else if (fromName == '')
		{
			alert("Please indicate who this message is from.");
		}
		
		else
		{
			$("#email_form_container").load("includes/ajax_scripts/process_submit_email.php", 
			
				{
					first_name: firstName,
					last_name: lastName,
					bunk: bunk,
					message: message,
					from_name: fromName
				}
			);
		}
		
	});
	
	$("#message").live('keyup', function() {
		
		var message = $("#message").val();
		var messageLength = message.length;
		var maxMessageLength = 8000;
		var warningLength = 7800;
		
		if (messageLength == warningLength)
		{
			alert("Note: You have about 200 characters left before you reach the limit.");
		}
		
		if (messageLength >= maxMessageLength)
		{
			alert("You've reached the maximum length of a single message.");
			var newMessage = message.substring(message, maxMessageLength);
			$("#message").val(newMessage);
		}
		
	});
	
});