//console.log('js loaded');
$(function() {

	//console.log('dom loaded');
	$('#loginLink').click(function() {
		$('#loginLink').unbind('click').click(function() { return false; });

		
		//console.log('click fired');
	
		$('#loginBox').animate({height: "105px"}, 100).append('<label id="usernameLabel">User:<input type="text" id="usernameBox" value="" /></label><label id="passwordLabel">Pass:<input type="password" id="passwordBox" value="" /></label><input type="button" id="aLoginButton" value="Login" /><a id="forgotLink" href="/login-help.php">Forgot your password?</a>');

		
		//console.log($('#aLoginButton'));
		//*
		var ajlogin = function () {		
			$('#loginNotice').hide();
			$.post("/ajax_login.php", { 'u': $('#usernameBox').val(), 'p':$('#passwordBox').val() },
				function (data) {
					//console.log(data);
					if (data.valid) {
						//console.log(data);
						window.location.href='/members.php';
					} else {
					//console.log(data);
						$('#loginBox').animate({height: "125px"}, 100).append('<span id="loginNotice">' + data.message + '</span>');
					}
				}, "json");


			
			return false;
			
		}
		//*/
		
		$('#aLoginButton').bind('click', function() { ajlogin(); return false; });
		$('#aLoginButton').add('#passwordBox').keyup(function(e) {
			if(e.keyCode == 13) { ajlogin(); return false; }
			return false;
		});
		
		
		return false;
	});
	



	$('#logoutLink').click(function(e) {

		e.preventDefault();
		$.post("/ajax_logout.php", {},
			function (data) {
				//console.log(data);
				if (data.done) {
					//console.log(data);
					window.location.href='/';
				}
			}, "json");
			return false;
	
	});















	
	/*
	
	
	$('#registerLink').click(function() {
		//$('#registerLink').unbind('click').click(function() { return false; });
		$('#registerLink').attr('href', '#');
		console.log('hit');
		if ($('#modalWindow').size()==0) {
		$('body').append('<div id="modalWindow" class="jqmWindow"><div id="jqmTitle"><button class="jqmClose">	Close X</button><span id="jqmTitleText">Title of modal window</span></div><iframe id="jqmContent" src=""></iframe></div>');
		}
		
		

  
  // notice that you can pass an element as the target 
  //  in addition to a string selector.
  var t = $('#modalWindow');
  
  $('#ex4').jqm({
    trigger: '#registerLink',
    ajax: '/register.php', /* Extract ajax URL from the 'href' attribute of triggering element * /
    target: t,
    modal: true, /* FORCE FOCUS * /
    onHide: function(h) { 
      t.html('Please Wait...');  // Clear Content HTML on Hide.
      h.o.remove(); // remove overlay
      h.w.fadeOut(888); // hide window
      
    },
    overlay: 0});
  t.jqmShow(); 
   
  // Close Button Highlighting Javascript provided in ex3a.
  /*
  // Work around for IE's lack of :focus CSS selector
  if($.browser.msie)
    $('input')
      .focus(function(){$(this).addClass('iefocus');})
      .blur(function(){$(this).removeClass('iefocus');});
	  //* /
  
		
		
		return false;
		
		
		
	
		
	});
	//*/
	
	
	
	
	
	
	
	
	
	
	
	
	
	

});
