var obj = null;
$(document).ready(function()
{	

$('#login > li').click(function() {
		if (obj && !clicked) {
			obj.find('ul').fadeIn(0);
			obj = null;
		} //if
		
		$(this).find('ul').fadeIn(0);
	}, function() {
		obj = $(this);
	});
$("#username").focus(function() {
                $(this).val('');
      	  	});	
$("#password").focus(function() {
                $(this).val('');
      	  	});

$("#comm").submit(function()
	  {
		//remove all the class add the messagebox classes and start fading
		$("#msgbox2").removeClass().addClass('messagebox').text('Please Wait...').fadeIn(1000);
		//check the username exists or not from ajax
		$gid = $('#game-id').val();
		$.post("services/users/ajax_comment.php",{ user_name:$('#post-uname').val(), post_id:$('#post-id').val(), code_name:$('#code-name').val(),  code_desc:$('#code-desc').val(), user_agree:$('#user-agree').attr("checked")} ,function(data)
        
		{
		  if(data=='yes') //if correct logout details...
		  {
		  	$("#msgbox2").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('The world can see your comment!').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
			  	 document.location="http://www.yellowdoggames.com/game.php?gid="+$gid;

			  });
			  
			});
		  }
		  else 
		  {
		  	$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Please fix the errors:' + data).addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});

$("#reg_form").submit(function()
	  {
		//remove all the class add the messagebox classes and start fading
		$("#msgbox2").removeClass().addClass('messagebox').text('Please Wait...').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("services/users/ajax_reg.php",{ user_name:$('#user-username').val(), password:$('#user-password').val(), repassword:$('#user-repassword').val(),email:$('#user-email').val(), reemail:$('#user-reemail').val(), fname:$('#user-fname').val(),lname:$('#user-lname').val(), news:$('#user-news').attr("checked"),agree:$('#user-agree').attr("checked") } ,function(data)
        
		{
		  if(data=='yes') //if correct logout details...
		  {
		  	$("#msgbox2").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('You are officially registered!').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				 document.location='http://www.yellowdoggames.com/';
			  });
			  
			});
		  }
		  else 
		  {
		  	$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Please fix the errors.').addClass('messageboxerror').fadeTo(900,1);
			  $("#errorbox").html('<div class="my_heading">Errors:</div>'+data);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
$("#logout_form").submit(function()
	  {
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Please Wait...').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("services/users/ajax_logout.php",function(data)
        {
		  if(data=='yes') //if correct logout details...
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Logging out.....').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				 document.location='http://www.yellowdoggames.com/';
			  });
			  
			});
		  }
		  else 
		  {
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Was unable to log out. . . try again?').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
$("#login_form").submit(function()
	  {
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1400);
		//check the username exists or not from ajax
		$.post("services/users/ajax_login.php",{ user_name:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {
		  if(data=='yes') //if correct login detail
		  {
		  	$("#msgbox").fadeTo(400,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				document.location=document.location;
			  });
			  
			});
		  }
		  else 
		  {
		  	$("#msgbox").fadeTo(400,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('You must have put in the wrong stuff...' + data).addClass('messageboxerror').fadeTo(900,1, function()
			  {
			  	$('#login > li').find('ul').fadeOut(1000);
				$("#username").val('');
				$("#password").val('');
				$(this).fadeTo(1000,0);
			  });
			});	
			
          }
				
        });
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#password").blur(function()
	{
		$("#login_form").trigger('submit');
	});
});



