// JavaScript Document
jQuery(document).ready(function($) {
								
//rewrite URL
//TEMP: replace 'nbc/' with '~newporv8/'
//TEMP: replace 'http://bamboodev.com' with ''
//$('a').html('milk');
$('a').each(function() 
{
str = $(this).attr('href');
if(str!='')
{
	
	// str = str.replace("nbc/", "/");
	// str = str.replace("bamboodev.com/", "newportbay.org/");
	// //console.log(str);
	// //.replace("nbc/", "~newporv8/");
	// $(this).attr('href', str)
	// 
	// //$('a').attr("href", str)	
	
}

});



//clear form field on focus
var clearMePrevious = '';
// clear input on focus
jQuery('.clearMeFocus').focus(function()
{
if(jQuery(this).val()==jQuery(this).attr('title'))
{
clearMePrevious = jQuery(this).val();
jQuery(this).val('');
}
});
// if field is empty afterward, add text again
jQuery('.clearMeFocus').blur(function()
{
if(jQuery(this).val()=='')
{
jQuery(this).val(clearMePrevious);
}
});

//swap password fields on focus, to get around IE bug
$('#fakepassword').focus(function(){	
		$('#fakepassword').hide();
		$('#loginPassword').show();
		$('#loginPassword').focus();
});

$('#loginPassword').blur(function(){	
	if ($('#loginPassword').attr('value') == '') {
		$('#loginPassword').hide();
		$('#fakepassword').show();
	}
});

//show loginFormWrapper when login is clicked
$('a#loginLink[href=/home/transactions/login/]').click(
	function(){	
	//var loginText = 
	if($("#loginLink").text()=='Log In'){$('a#loginLink').text('Cancel');}else{$('a#loginLink').text('Log In');}
	$('#loginFormWrapper').toggle('slow');
	//if(("#loginLink").val()=='Log In'){("#loginLink").html('Cancel');}
	//("#loginLink").html('Cancel');	
	return false;
			  }
					  );

$("#loginForm").validate({
submitHandler: function(form) {
//$("#membershipLogin .ajaxLoader").toggle();
$.getJSON("/ajax/curl.php", {action: 'login', username: $("#loginUsername").val(), password: $("#loginPassword").val()  },  function(data){																			  																  
	if (data.response=="200")  
		{
			//console.log('success');
			$('#loginFormWrapper').toggle('slow');
			$('#loginUserGreetingWrapper a').html('Welcome, '+data.firstname+'!');
			$('#loginUserGreetingWrapper').toggle('slow');
			//to do: fix logOut link after login
			$('#loginLinkWrapper').toggle('slow');
			//$('a#loginLink').text('Log Out');
//			$('a#loginLink').attr("href", "http://bamboodev.com/wp-content/themes/newportbay/_inc/logout.php?a=logout");
//currentURL
window.location.replace(window.location);
		}
		else
		{
			//console.log('fail');
			$('#loginFormWrapper ul').append('<li><strong><a href="#">Sorry...please try again</a></strong></li>');
			return false;
		}
  });
		return true;
 },
 rules: {
			//username: {
//				required: true,
//				email: true
//			},
//			password: "required"
		},
 messages: {
			//username: "Please enter your email address",
//			password: "Please enter your password"
		}
});

//function actionGetProfile(id)
//{
//console.log("uID: "+uID);
//console.log("action: "+doAction);
//}

//if (window.doAction !== undefined) alert("doAction is undefined/undeclared"); 




if(window.doAction !== undefined && window.doAction==='getProfile'){
$.getJSON("/ajax/curl.php", {action: 'getProfile', sID: sID  },  function(data){																			  																  
if (data.response=="200")  
{
	//console.log('getProfile success');
	$('#memberShipName').val(data[0].firstname + ' ' + data[0].lastname);
}
else
{
	//console.log('getProfile fail');
	//return false;
}
});	

}
								



}); //END jQuery(document)
