


/*
 * Customer Area
 */
 
$(function() {

	
	var texts = {
		success: "Thank you for registering with Leap Power Solutions, LLC. Your customer account will be activated shortly. In the mean time, please contact info@leap-power.com or 1-469-757-1787 for any urgent request."
	},
	userInfo = {
		start: function() {
			$.get("process/check.php", function(data) {
				$("#footer").after('<div id="response" style="display: none;">' + data + '</div>');
			});
		},
		loggedIn: function() {
			var result = ($("#response").text() === "true") ? true : false;
			return result;
		} // End userInfo
	},
	customerAreas = {
		/********************************
		* Customer Box Reference
		********************************/
		box: $("#customer-area"),
		/********************************
		* Main Links Area
		********************************/
		main: function() {
			
			// Link Functionality
			var links = {
				documentation: function() {
					$("#documentation").click(function() {
						customerAreas.documentation();
						return false;
					});
				},
				tcoAnalysis: function() {
					$("#tcoAnalysis").click(function() {
						customerAreas.tcoAnalysis();
						return false;
					});
				},
				rmaStatus: function() {
					$("#rmaStatus").click(function() {
						
						return false;	
					});
				},
				poStatus: function() {
					$("#poStatus").click(function() {
						
						return false;	
					});
				},
				carbonCredit: function() {
					$("#carbonCredit").click(function() {
						
						return false;	
					});
				},
				taxCredit: function() {
					$("#taxCredit").click(function() {
						
						return false;	
					});
				},
				login: function() {
					$("#login").click(function() {
						customerAreas.login();
						return false;
					});
				},
				registration: function() {
					$("#registration").click(function() {
						customerAreas.registration();
						return false;
					});
				},
				logout: function() {
					$("#logout").click(function() {
						customerAreas.logout();
						return false;
					});
				}
			} // End Links
			
			// Start Loading...
			customerAreas.addOns.loadingAnim(true);
			customerAreas.box.load("inc/customerLinks.php", function(data) {
				customerAreas.box.html(data);
				customerAreas.addOns.closeButton();
				customerAreas.addOns.adjustHeight();
				// Add functionality to the links
				links.login();
				links.registration();
				links.logout();
				links.documentation();
				links.tcoAnalysis();
				// End Loading...
				customerAreas.addOns.loadingAnim(false);
				
			}); // End Main Customer Link Area
			
		}, // End Main
		/********************************
		* Registration
		********************************/
		registration: function() {
			// Start Loading...
			customerAreas.addOns.loadingAnim(true);
			
			customerAreas.box.load("inc/registration.php", function(data) {
				customerAreas.box.html(data);
				// Addons
				customerAreas.addOns.closeButton();
				customerAreas.addOns.adjustHeight();
				customerAreas.addOns.backToCustomer("backToMain");
				
				// End Loading...
				customerAreas.addOns.loadingAnim(false);
				
				$("#registerForm").submit(function() {
					
					// Start Loading...
					customerAreas.addOns.loadingAnim(true);
					
					// Serialize the registration data
					var formData = $(this).serialize();
					
					// Post the Data
					$.post("process/regprocess.php", formData, function(data) {
						
						if(data !== null) {
							$("#errors").html(data);
						} else {
							$(this).fadeOut(400);
							$(this).append('<div id="success" style="display: none;"></div>');
							// Add Success Text
							$("#success")
								.text(texts.success)
								.fadeIn(350);
								customerAreas.addOns.closeButton();
								customerAreas.addOns.adjustHeight();
								customerAreas.addOns.backToCustomer("backToMain");
						}
						customerAreas.addOns.adjustHeight();
						
						// End Loading...
						customerAreas.addOns.loadingAnim(false);
					});
					return false;
				});
				
			});
			
		}, // End Registration
		/********************************
		* Login
		********************************/
		login: function() {
		
			// Start Loading...
			customerAreas.addOns.loadingAnim(true);
		
			customerAreas.box.load("inc/login.php", function(data) {
			
				// End Loading
				customerAreas.addOns.loadingAnim(false);
			
				customerAreas.box.html(data);
				customerAreas.addOns.closeButton();
				customerAreas.addOns.adjustHeight();
				customerAreas.addOns.backToCustomer("backToMain");
				
				$("#loginForm").submit(function() {
				
					// Start Loading...
					customerAreas.addOns.loadingAnim(true);
					
					var loginData = $(this).serialize();
					
					$.post("process/loginprocess.php", loginData, function(data) {
					
						// End Loading...
						customerAreas.addOns.loadingAnim(false);
						
						$("#status").html(data);
						if($("#status").text() === "") {
							customerAreas.main();
						}
					});
					return false;
				});
				
			});
		}, // End Login
		/********************************
		* Documentation
		********************************/
		documentation: function() {
			// Start the loading...
			customerAreas.addOns.loadingAnim(true);
			// Load the documentation
			customerAreas.box.load("customerAreas/documentation.php", function(data) {
				// Cache the status
				$status = $("#status");
				
				// Check if customer is logged in
				if($status !== undefined && $status.text() === "false") {
					customerAreas.main();
				} else {
					// Edit customerArea css
					customerAreas.box.css({
						'overflow':'auto'
					});
					// Grab the innerhtml of the data
					var documentationArea = $("#documentation-area").html();
					
					// End the loading...
					customerAreas.addOns.loadingAnim(false);
					
					// Insert the documentationArea html into the box
					customerAreas.box.html(documentationArea);
					
					// Addons
					customerAreas.addOns.closeButton();
					customerAreas.addOns.adjustHeight();
					customerAreas.addOns.backToCustomer("backToMain");
				}
			});
		}, // End Documentation
		/********************************
		* tcoAnalysis
		********************************/
		tcoAnalysis: function() {
			// Start the loading...
			customerAreas.addOns.loadingAnim(true);
			// Load the documentation
			customerAreas.box.load("customerAreas/tcoanalysis.php", function(data) {
				// Cache the status
				$status = $("#status");
				
				// Check if customer is logged in
				if($status !== undefined && $status.text() === "false") {
					customerAreas.main();
				} else {
					// Edit customerArea css
					customerAreas.box.css({
						'overflow':'auto'
					});
					// Grab the innerhtml of the data
					var tcoanalysisArea = $("#tcoanalysis-area").html();
					
					// End the loading...
					customerAreas.addOns.loadingAnim(false);
					
					// Insert the documentationArea html into the box
					customerAreas.box.html(tcoanalysisArea);
					
					// Addons
					customerAreas.addOns.closeButton();
					customerAreas.addOns.adjustHeight();
					customerAreas.addOns.backToCustomer("backToMain");
				}
			});
		},
		/********************************
		* Logout
		********************************/
		logout: function() {
			// Start Loading...
			customerAreas.addOns.loadingAnim(true);
			
			customerAreas.box.load("process/logout.php", function(data) {
				if(data === "finished") {
					customerAreas.main();
				}
			});
		},
		/********************************
		* Addons
		********************************/
		addOns: {
			adjustHeight: function() {
				customerAreas.box.css({
					'marginTop': '-' + customerAreas.box.height() / 2 + 'px'
				});
			}, // End adjustHeight
			// * Adds a close button feature to the customer area * //
			closeButton: function() {
				customerAreas.box
				// Attach the close button to the customerBox
				.prepend('<a href="#" id="windowCloseBtn"><img src="images/closebtn.gif" alt="Close" /></a>')
				.find("#windowCloseBtn")
				// When clicked...
				.click(function() {
					// ...Close the Customer Window and Page Wrap
					$("#page-wrap").fadeOut(300);
					customerAreas.box.fadeOut(300);
					// Disable default action
					return false;
				});
			}, // End CloseButton Func
			// * Provides functionality to the back button	* //
			backToCustomer: function(id) {
				$("#" + id).click(function() {
					// Clear away any existing html
					customerAreas.box.html("");
					// Get the mainArea
					customerAreas.main();
					
					return false;
				});
			}, // End backToCustomer Function
			// * Loading Animation for Getting and Posting * //
			loadingAnim: function(startStop) {
				// If startStop has not been set
				if(startStop === null) {
					// Set it true
					startStop = true;
				}
				// If startStop is true
				if(startStop) {
					// Insert the loading animation
					customerAreas.box
						.prepend('<img id="loadingAnim" src="images/loadingalt.gif" alt="Loading..." title="Loading..." />')
						.find("#loadingAnim")
						.hide()
						.fadeIn(500);
				}
				else {
					// Remove the loading animation
					$("#loadingAnim")
						.fadeOut(500)
						.delay(500)
						.remove();
				}
			} // End loadingAnime func
		} // End Addons
	} // End customerAreas
	
	
	$("#customer-link").click(function() {
		$("#page-wrap").fadeIn(350);
		$("#customer-area").fadeIn(350);
		customerAreas.main(function() {
			$("#login").click(function() {
				// Login Area
				customerAreas.login();
				return false;
			});
		});
		return false;
	});
		
});

