/**************************************************

Filename:		home.js
Date Modified:	October 24, 2011
Modified By:	David Lackey

Dependencies:	jquery.js

Description:	javascript for homepage

**************************************************/

// Create namespace and specify some meta-information
Home = {};
Home.NAME = "Home";
Home.VERSION = 1.0;

/*************************************************

Declare constants

**************************************************/

Home.HIDE_SPEED = 200;
Home.SHOW_SPEED = 400;
Home.ACT_FETCH_INTERVAL = 5000;
Home.ACT_CLASS_1 = 'odd';
Home.ACT_CLASS_2 = 'even';

/*************************************************

Initialize listeners

**************************************************/

$(function() {
	// Listener for planned date removal
	$('.plannedDateRemove').bind('click', function() {
		// Set vars
		var id = parseInt($(this).attr('name'));
		var container = $(this).closest('.plan');
		// Call ajax
		$.ajax({
			url: './ajax/homeAjax.php',
			type: 'post',
			data: {
				action: 'plannedDateRemove',
				plannedDateID: id
			},
			dataType: 'html',
			success: function(response) {
				if (parseInt(response) > 0)
				{
					// Hide the removed date
					container.hide(Home.HIDE_SPEED);	
					// Check to see if there are any planned dates left
					var plans = $('.plans .plan').filter(':visible');
					
					if ((plans.length - 1) == 0 || !plans.length) {
						$('#emptyPlannedDates').show();
					}
				}
			}
		});
		// Disable the link
		return false;
	});
	
	// Listener for dismissal from Message Center
	$('#msgCenter .dismiss span').bind('click', function() {
		// Immediately hide the message
		$('#msgCenter').hide(Home.HIDE_SPEED);
		// Define ajax params
		var params = {
			type: $(this).attr('id')	
		};
		// Call ajax
		$.ajax({
			url: 'ajax/msgCenterAjax.php',
			type: 'POST',
			data: {
				action: 'dismiss',
				params: JSON.stringify(params)
			},
			dataType: 'html',
			success: function(response) {
				// Message Center should already be hidden
			}
		});
	});
	
	// Listener to dismiss tutorial video
	$('#tutorialVideoDismiss').bind('click', function() {
		$('#tutorialVideo').slideUp(250);
		// Call ajax
		$.ajax({
			url: 'ajax/homeAjax.php',
			type: 'GET',
			data: {
				action: 'tutorialVideoDismiss'
			},
			dataType: 'html',
			success: function(response) {
				// Test
				console.log(response);
			
				// Tutorial video should already be hidden
			}
		});
		// Disable link
		return false;
	});
});

/*************************************************

Initialize tutorial video

**************************************************/

$(function() {
	$('#tutorialVideo').slideDown(250);
});

/*************************************************

Initialize activities

**************************************************/

$(function() {
	// Apply the correct classes to the activities
	var acts = $('#activityPane .activity');
	var odd = acts.filter(':odd');
	odd.addClass(Home.ACT_CLASS_1);
	var even = acts.filter(':even');
	even.addClass(Home.ACT_CLASS_2);
	
	// Interval to fetch activities (commented out for now)
	//setInterval('Home.fetchActivity()', Home.ACT_FETCH_INTERVAL);
});

/*************************************************

Fetch activity

**************************************************/

Home.fetchActivity = function() {
	// Get last activity
	var act = $('#activityPane .activity:first');
	// Get last fetched activity id
	var actID = parseInt(act.attr('name'));
	// Set the class to add to the new element
	var newClass = '';
	if (act.hasClass(Home.ACT_CLASS_1))
	{
		newClass = Home.ACT_CLASS_2;
	}
	else
	{
		newClass = Home.ACT_CLASS_1;
	}
	
	// Call ajax
	$.ajax({
	    url: 'ajax/homeAjax.php',
	    type: 'GET',
	    data: {
	    	action: 'fetchActivity',
	    	lastActID: actID
	    },
	    dataType: 'html',
	    success: function(response) {
	    	// Construct a jQuery object
	    	response = $(response);
	    	if (response.hasClass('activity'))
	    	{
	    		// Hide and style the response
	    		response.hide().addClass(newClass);
	    		// Insert and slide down the response
				response.insertBefore('#activityPane .activity:first');
				response.slideDown(Home.SHOW_SPEED);
				// Hide the last activity
				var lastAct = $('#activityPane .activity:last').remove();
			}
	    }
	});
}



/*************************************************

Change date for the date

**************************************************/
var savDateID;
var savCtrl;
$(document).ready(function() {
		//this function saves the dateID for use in the ajax call in datepicker
		$('.chgUpcomingDates a').click(function(){ 
			$id = $(this).attr('name'); 
			savDateID = $id;
			savCtrl = this;
		});
  
	 	$('.chgUpcomingDates a').fancybox ({ 
 			'autoDimensions'		: false,
			'width'         		: 255,
			'height'        		: 235
		}); 
				   
				
		$(function() {
			try {
				$( "#datepicker" ).datepicker({  
				   changeMonth: true,
				   changeYear: true,  
				   minDate: new Date(),
				   onSelect: function(dateText, inst) {
				   	//alert("Selected date: " + dateText  + "; input's current value: " + this.value ); 
				   	var dt = new Date(dateText);
				   	var dtJSON = JSON.stringify(Home.getWhen(dt)); 
				    	$.ajax({ 
				   		url: 'ajax/homeAjax.php',
				   		type: 'get',
				   		data: {
				   			action: 'plannedDateChange',
				   			dateID: savDateID,
				   			dt:	dtJSON
				   		},
				   		dataType: 'html',
				   		success: function($response) {
				   			// So many attributes need to change that we need to just reload the page
				   			window.location.reload();
				   			return;
				   		
				   			//alert("Date set to: "+$response); 
				   			$(savCtrl).text($response)	 
				   			parent.$.fancybox.close();
				   			return false;
				   		} 
				   	});     
				   }  
				});
			}
			catch (e) {
				// Silently dismiss
			} 
		});
}); 
 
/*************************************************

Get when array

**************************************************/

Home.getWhen = function(dt)
{
	// Return null if N/A
	if (!dt)
		return null;
	// set when var
	var when = {}; 
	when.day = dt.getDate();
	when.month = dt.getMonth()+1;
	when.year = dt.getFullYear();
	return when;
}

/**************************************************

Filename:		msgCenter.js
Date Modified:	November 16, 2011
Modified By:	David Lackey

Description:	js for message center

**************************************************/

// Create namespace and specify some meta-information
MsgCenter = {};
MsgCenter.NAME = "MsgCenter";
MsgCenter.VERSION = 1.0;

/*************************************************

Declare constants

**************************************************/

MsgCenter.speed = 200;

/*************************************************

Dismiss

**************************************************/

$(function() {
	$('#msgDismiss').one('click', function() {
		$(this).parents('.msg').slideUp(MsgCenter.speed);
		var type = $(this).attr('name');
		$.ajax({
		    url: 'ajax/msgCenterAjax.php',
		  	type: 'POST',
		  	data: {
		  		action: 'dismiss',
		  		type: type
		  	},
		  	dataType: 'html',
			success: function(response) {
			}
		});
	});
});

