/* WALKER'S AMAZING JAVASCRIPT CODE */

$(document).ready(function () {	
	// Load first screen
	$('#screen').load('pages/initial-setup.php');
	
	// Home screen reset
	$('#home-screen-btn').live('click', function() {
		$.get("php/logout.php");
		location.reload(true);
	});

	// Operate Nav Bar 
	$('.nav-bar li').live('click', function() {
		if ($(this).hasClass('selected')) {
		}
		else
		{
			$('#screen').load('pages/'+ $(this).attr('id')+'.php');
			$(this).siblings().removeClass('selected');
			$(this).addClass('selected');
		}
	});
	
	// Turn switches on/off on click 
	$('.switch').live('click', function() {
		if ($(this).hasClass('off')) {
			$(this).removeClass('off');
		}
		else
		{
			$(this).addClass('off');
		}
	});
	
	// Hide password field if switch is turned off.
	$('#password-hider').live('click', function() {
		if ($(this).hasClass('off')) {
			$('#password-input-line').slideUp(200);
		}
		else
		{
			$('#password-input-line').slideDown(200);
		}
	});
	
	// Click get started
	$('#complete-setup').live('click', function() {
		if ($('#password-protect-input').val() == '' && $('#password-hider').hasClass('off') == false) {
			alert("Please input a password or disable password locking before continuing.");
		}
		else
		{
			$('#screen').load('pages/home.php');
		}

	});
	
});

/////////////////////////////
// OTHER GENERAL FUNCTIONS //
/////////////////////////////

// Activate child - 1st parameter = parent id, 2nd = child id
function activateID(id) {
	$('#'+id).focus();
}

// Back button baby
function link(target) {
	$('#screen').load('pages/'+target+'.php');
}

// Login functionality
$("#submit-login").live('click', function() {
	$.post("php/ajax-login.php",{ username:$('#username-field').val(),password:$('#password-field').val(),rand:Math.random() } ,function(data) {
		if(data=='yes')	{
			// Load home-logged-in.php
			$('#screen').load('pages/home.php');
			}
		else {
			//  If the login failed...
			alert(data);
			}
	});
});

// Create account functionality
$("#submit-account-creation").live('click', function() {
	$.post("php/ajax-account-creation.php",{ username:$('#username-field').val(),password:$('#password-field').val(), email:$('#email-field').val(),picture:$('#picture-field').val(),rand:Math.random() } ,function(data) {
		if(data=='success')	{
			// Load home-logged-in.php
			$('#screen').load('pages/home.php');
			}
		else {
			if(data=='usernametaken') {
				alert("The username '"+$('#username-field').val()+"' has already been taken.  Please choose another username.");
				}
			if(data=='emailtaken') {
				alert("The email address '"+$('#email-field').val()+"' has already been registered.  Please login to your account on the 'Home' tab.");
				}
			if(data=='nousername') {
				alert("Please enter a username.");
				}
			if(data=='nopassword') {
				alert("Please enter a password.");
				}
			if(data=='bademail') {
				alert("Please enter a valid email address.");
				}
			}
	});
});

// XML
function loadXMLString(txt) 
{
if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(txt); 
  }
return xmlDoc;
}



