theranos.login = { };					// Set up Theranos Login Singleton

theranos.login.Init = function() {
	// import sha1.js
	$.getScript("../global/scripts/lib/sha1.js");
	
	// attach events to things
		// attach open drawer to log in tab
		$('#tab').click(function(){
			$('#pane').animate(
				{top: '0px'}, 
				750, 
				"easeOutBounce", 
				function() {}
			);
		});
		$('#tab').keypress(function(e){		
	        if (e.which == 13) {
				$('#tab').click();
	        }	
		})
		// attach close drawer to cancel button
		$('#btn-cancel')
			.click(function(){
				$('#pane')
					.animate(
						{top: '-92px'}, 
						500, 
						"easeInQuad", 
						function() {
							$('#overlay').remove();
						}
					)
					// clear input values
					.find('input').val("");
			});
		$('input','#pane').keypress	(function(e){		
	        if (e.which == 13) {
				$('#btn-submit').click();
	        } else if (e.which == 27) {
				$('#btn-cancel').click();
			}
		})
		// attach mousedown events to buttons
		$('#btn-cancel,#btn-submit')
			.mousedown(function(){
				$.log('Pressed');
				$(this).addClass('active');
			})	
			.mouseup(function(){
				$.log('Released');
				$(this).removeClass('active');
			});
		$('#btn-submit')
			.click(function(){
				if ($('#userID').val()) {
				// submit info to servlet
					try {
					$.ajax({
						type: "GET",
						url: '../userurl.cgi',
						data: "user="+$('#userID').val()+'&sha1='+hex_sha1( $('#userID').val() + $('#userPass').val() ),
						success: function(msg){
							$.log( "Server Response: " + msg );
							if ((!!msg == true) && (msg.indexOf('http') > -1)) {
								$.log( "Redirecting. msg.indexOf('http') = "+msg.indexOf('http'));
								// if it's valid, continue to destination
								// now we're going to post username and pass to that msg
								$('#login').attr('action',msg).submit()
								//document.location.href = msg + "?userID=" + $('#userID').val() + "&userPass=" + $('#userPass').val();
							} else if ((!!msg == false) || (!msg) || (msg.indexOf('http') < 0)) {
									$.log( "Shaking");
								// if it's not valid, shake it	
								$('#pane')
									.animate(
										{left: '5px'}, 
										10, 
										"linear", 
										function() {
											$(this)
												.animate(
													{left: '-5px'}, 
													10, 
													"linear", 
													function() {
														$(this)
															.animate(
																{left: '5px'}, 
																10, 
																"linear", 
																function() {
																	$(this)
																		.animate(
																			{left: '-5px'}, 
																			10, 
																			"linear", 
																			function() {
																				$(this)
																					.animate(
																						{left: '0px'}, 
																						50, 
																						"linear"
																					);
																			}
																		);
																}
															);
													}
												);
										}
									);
							}
					   },
						error: function(){
						}
					 });					
				}
					catch(e) {
					function closeIt() {
						$('#btn-cancel').trigger('click');
					}
					$.log(e);
					$('#pane').append($('<div id="overlay"/>')
						.html("<p>Oh no! There's something wrong.  Please try again in a few minutes.</p>")
						.css('opacity',0)
						.append(
							$('<div id="btn-ok"><span>OK</span></div>')
								.click(function(){ closeIt(); return false;	})
							)
						);
					$('#overlay').fadeTo(500,1,function(){
						$.log('Faded');
					});
				}
				}
				return false;
			});
};

// INIT
$().ready(function(){theranos.login.Init();});


























