//Google load is ascychronous, so need to use the setOnLoadCallback before using $
google.load("jquery", "1");
google.load("jqueryui", "1.8.11");

google.setOnLoadCallback(function() {

$(document).ready(function() {

	
	//LOGIN PAGE 
		$('#signuplink').click(function() {
//				if ($('#logintitle').text() == "Sign Up to Pet Connection")
//					$('#logintitle').text("Log In to Pet Connection");
//				else
//					$('#logintitle').text("Sign Up to Pet Connection");
//				
//				if ($('#signuplink').text() == "Already have an account?")
//					$('#signuplink').text("Sign up now! It's free!");
//				else
//					$('#signuplink').text("Already have an account?");
				$('#logindiv').toggle(300);
				$('#signupdiv').toggle(300);
			}
		);
		$('#loginlink').click(function() {
			$('#logindiv').toggle(300);
			$('#signupdiv').toggle(300);
		});
		
		$('#reloadcaptchaimage').click(function() {
			$('#captcha').attr("src",  './captcha/securimage_show.php?' + Math.random());
			return false;
		});
		
		// ADD bookmark window
		$('#closeaddwindow').click(function() {
			self.close();
			return false;
		});
		
		// Pop open the ADD bookmark window
		$('#addbookmarklink').click(function() {
			window.open('add.php?jump=doclose','lunarmarkaddwin1','location=yes,links=no,scrollbars=no,toolbar=no,width=550,height=550');
		});
		
		//***** BOOKMARK Operations ***********//
		//Delete bookmark link
		$('.deletelink').click(function() {
			var id = $(this).attr('ID').substring(3);
			if (confirm("Are you sure you want to delete this bookmark?"))
			{
				$('#messagepane').show();
				$('#messagepane').removeClass().addClass("neutral").show();
		        $('#waiting').show(100);			       
		        $('#message').hide(0);
		        $.ajax({
		            type: 'POST',
		            url: './systemtools/delete.php',
		            dataType: 'json',
		            data: {
		                ID: id
		            },
		            success: function (data) {
		                $('#waiting').hide(500);
		                $('#messagepane').removeClass().addClass((data.Error === true) ? 'error' : 'status');
						$('#message').text(data.Message).show(500);
		                if (data.Error === false)
		                	$('#bookmarkdiv'+id).hide();
		            },
		            error: function (XMLHttpRequest, textStatus, errorThrown) {
		                $('#waiting').hide(500);
		                $('#messagepane').removeClass().addClass('error');
						$('#message').text('There was an error deleting the bookmark.').show(500);			                
		            }
		        });

		        return false;	
			}
					
		});
		
		//Show details
		$('.detailslink').click(function() {
			$('.details').hide();
			var id = $(this).attr('ID').substring(3);
			$('#details'+id).show();
			$('#details'+id+' > .readonlybookmark').show();
			$('#details'+id+ ' > .editbookmarkdiv').hide();
		});
		//Show EDIT form
		$('.editlink').click(function() {
			$('.details').hide();
			var id = $(this).attr('ID').substring(4);
			$('#details'+id).show();
			$('#details'+id+' > .readonlybookmark').hide();
			$('#details'+id+ ' > .editbookmarkdiv').show();
		});
		
		
		//Verify All links
		$('#verifyLinks').click(function() {
			
			$('#messagepane').show();
			$('#messagepane').removeClass().addClass("neutral").show();
	        $('#waiting').show(100);			       
	        $('#message').hide(0);
			 $.ajax({
		            type: 'POST',
		            url: './systemtools/verifylinks.php',
		            dataType: 'json',
		            
		            success: function (data) {
		                $('#waiting').hide(500);
		                $('#messagepane').removeClass().addClass((data.Error === true) ? 'error' : 'status');
						$('#message').text(data.Message).show(500);
						 $('#refreshlink').show();
		            },
		            error: function (XMLHttpRequest, textStatus, errorThrown) {
		                $('#waiting').hide(500);
		                $('#messagepane').removeClass().addClass('error');
						$('#message').text('There was an error verifying links.').show(500);			                
		            }
		        });
						
			return false;
			
		});

});

});

