/*
 * Namespaces. It's a good thing.
 */
CSPH = {};
CSPH.Reporting = {};
CSPH.FileCabinet = {};
CSPH.HR = {};


/*
 * Setup the message box component's image locations.
 */
apMessageBox.errorImage = "/images/error-32x32.png";
apMessageBox.informationImage = "/images/information-32x32.png";


/**
 * Convience method for logging stuff to the console.
 */
CSPH.log = function(msg) {
	if ("console" in window) {
		console.log(msg);
	}
};


/**
 * Handles the login page for the CSPH application. Login is processed
 * via an AJAX call.
 * @author Adam Presley
 */
CSPH.Login = function(config) {
	var __init = function() {
		$("#userId").focus();
		$("#btnLogin").button().click(__processLogin);
		$(":input").keyup(function(e) {
			if (e.keyCode === 13) {
				$("#btnLogin").click();
			}
		})
	};
	
	var __processLogin = function(e) {
		$.ajax({
			url: "/login/processLogin",
			data: {
				userId: $("#userId").val(),
				password: $("#password").val()
			},
			dataType: "json",
			type: "POST",
			error: function() {
				apMessageBox.error({ title: "Error", message: "An error occured while processing your login." });
			},
			success: function(data) {
				CSPH.log(data);
				if (!data.success) {
					apMessageBox.error({ title: "Error", message: data.message });
				}
				else {
					/*window.location = $.sprintf("/%s", (data.userBean.homePage != null) ? data.userBean.homePage : "");*/
					window.location = "/";
				}
			}
		});
	};
	
	var __this = this;
	var __config = $.extend(config, {});
	__init();
};


/**
 * Support for the reporting home. This is actually the home landing
 * page for most (if not all) users.
 * @author Adam Presley
 */
CSPH.Reporting.Home = function(config) {
	var __init = function() {
		$("#frmReports").stepy();
		$("#txtStartDate").datepicker({ showButtonPanel: true, dateFormat: "m/d/yy", showAnim: "blind" });
		$("#txtEndDate").datepicker({ showButtonPanel: true, dateFormat: "m/d/yy", showAnim: "blind" });
		
		$("#ddlDates").change(__dateRangeDropdownHandler);
		$("#ddlReportGroup").change(__reportGroupDropdownHandler);
	};

	var __dateRangeDropdownHandler = function(e) {
		var today = new Date();
		var selection = $(this).val();
		var start, end;
		var dayUnit = 86400000;
		
		var theDay = today.getDay();
		var theMonth = today.getMonth();
		var theYear = today.getFullYear();
		
		var diff = 0;
		
		CSPH.log($.sprintf("today = %s, selection = %s", today, selection));
		
		switch (selection) {
			case "Yesterday":
				start = new Date(today - dayUnit);
				end = new Date(today - dayUnit);
				break;
				
			case "Week To Date":
				diff = theDay - 1;
				start = new Date(today - (diff * dayUnit));
				end = new Date(today);
				
				CSPH.log($.sprintf("start = %s, end = %s", start, end));
				break;
				
			case "Last Week":
				diff = (theDay - 1) + 7;
				start = new Date(today - (diff * dayUnit));
				end = new Date(start.getTime() + (6 * dayUnit));
				
				CSPH.log($.sprintf("start = %s, end = %s", start, end));
				break;
				
			case "Period To Date":
				// Breaking in current site.
				break;
				
			case "Last Period":
				// Breaking in current site.
				break;
				
			case "Month To Date":
				diff = today.getDate() - 1;
				start = new Date(today - (diff * dayUnit));
				end = new Date(today);
				
				CSPH.log($.sprintf("start = %s, end = %s", start, end));
				break;
				
			case "Last Month":
				break;
				
			case "Period Year To Date":
				diff = today.getDate() - 1;
				start = new Date(theYear, theMonth - 1, 1);
				end = new Date((today - (diff * dayUnit)) - dayUnit);
				
				CSPH.log($.sprintf("start = %s, end = %s", start, end));
				break;
				
			case "Calendar":
				break;
		}
		
		$("#txtStartDate").val($.format.date(start, "MM/dd/yyyy"));
		$("#txtEndDate").val($.format.date(end, "MM/dd/yyyy"));
	};
	
	var __reportGroupDropdownHandler = function(e) {
		var group = $("#ddlReportGroup").val();
		var newOptions = "";
		
		$("#ddlReport").html("");
		
		switch (group) {
			case "audit":
				newOptions = [
					"<option value=\"BadOrders\">Bad Orders</option>"
				].join(" ");
				break;
				
			case "keys":
				newOptions = [
					"<option value=\"DailyKeys\">Daily Keys</option>",
					"<option value=\"PeriodNDLCStore\">Period N/D/L/C (Store)</option>",
					"<option value=\"PeriodNDLCArea\">Period N/D/L/C (Area)</option>",
					"<option value=\"PeriodNDLCCSPH\">Period N/D/L/C (CSPH)</option>",
				].join(" ");
				break;

			case "service":
				newOptions = [
					"<option value=\"IDLog\">ID Log</option>",
					"<option value=\"IDLogLunch\">ID Log (Lunch)</option>",
					"<option value=\"IDLogDinner\">ID Log (Dinner)</option>",
				].join(" ");
				break;
		}
		
		$("#ddlReport").html(newOptions);
	};
	
	var __this = this;
	var __config = $.extend(config, {});
	__init();
};

/* REMOVED FROM KEYS FOR NOW
	"<option value=\"Ranking\">Ranking</option>",
	"<option value=\"ReportCard\">Report Card</option>",
	"<option value=\"DailyReportCard\">Daily Report Card</option>",
	"<option value=\"MVPs\">MVP's</option>"
*/

/**
 * Provides grid functionality to the File Cabinet application.
 * @author Adam Presley
 */
CSPH.FileCabinet.Home = function(config) {
	var __init = function() {
		$("#fileTable").dataTable({ bJQueryUI: true, aaSorting: [ [1, "asc"] ] });
	};
	
	var __this = this;
	var __config = $.extend(config, {});
	__init();
};


/**
 * Handles the Pay Raise Request form application.
 * @author Adam Presley
 */
CSPH.HR.PayRaiseRequest = function(config) {
	var __init = function() {
		$("#btnSubmit").button();
		$("#employeeName").focus();
	};
	
	var __this = this;
	var __config = $.extend(config, {});
	__init();
};


/**
 * Handles the Store Transfer Request form application.
 * @author Adam Presley
 */
CSPH.HR.TransferRequest = function(config) {
	var __init = function() {
		$("#btnSubmit").button();
		$("#employeeName").focus();
	};
	
	var __this = this;
	var __config = $.extend(config, {});
	__init();
};


