/**
 * @author David Sugden
 */

var loginForm = false;
var commentcnt = 0;
var timeout;
var timeoutCount = 0;
showLoginForm = function(currentId){
	if ( ! loginForm ) {
		loginForm = true;
		$.ajax({
			url: "/members/loginForm.html",
			success: function(data){
				$(currentId).append(data);
			}
		});
	}
};
toggleSubmitText = function() {
	if ( $("#loginSubmit").val() == "Logging In" ) {
		$("#loginSubmit").val("Please Wait");
	} else {
		$("#loginSubmit").val("Logging In");
	}
	timeoutCount++;
	if ( timeoutCount > 5 ) {
		window.clearInterval(timeout);
	}
};
validateLogin = function() {
	if ( $("#loginEmail").val() <= ' ' ) {
		$("#message").html('You have not supplied your email address. Please try again.');
		return false;
	} else if ( $("#loginPassword").val() <= ' ' ) {
		$("#message").html('You have not supplied your password. Please try again.');
		return false;
	}
	var filter=/^.+@.+\..{2,4}$/;
	if (filter.test($("#loginEmail").val())) {
		$("#loginEmail").attr("disabled", "true");
		$("#loginPassword").attr("disabled", "true");
		$("#loginSubmit").val("Logging In").attr("disabled", "true");
		timeout = window.setInterval(toggleSubmitText, 2000);
		$.ajax({
			type: "POST",
			url: "/members/login.php",
			dataType: "xml",
			timeout: 4000,
			data: {loginEmail: $("#loginEmail").val(), loginPassword: $("#loginPassword").val(), reqType: "xml"},
			success: function(xmlResponse){
				$("#message").html('You have been signed in. Please post your comment below...');
				window.clearInterval(timeout);
				$("#loginForm").remove();
				$("#post").append('<form method="post" id="commentForm" action="/article/postcomment.php" onsubmit="return validatePost();"><textarea id="yourComment" name="yourComment" rows="5"></textarea><input id="article_id" type="hidden" name="article_id" value="'+article_id+'"><input id="messagesubmit" type="submit" name="submit" value="Submit Your Comments!"></form>');
				$("#members").html('<ul><li><a href="/members/logoff.php">Sign Out</a></li><li><a href="/profile/">Admin</a></li></ul>');												
			},
			error: function(x,y,z){
				$("#message").html('The details you provided were not recognised.');
				$("#loginEmail").removeAttr("disabled");
				$("#loginPassword").removeAttr("disabled");
				$("#loginSubmit").val("Login").removeAttr("disabled");
				window.clearInterval(timeout);
			}
		});
	} else {
		$("#message").html('You have not supplied a valid email address. Please try again.');
		return false;
	}
	return false;
};
validatePost = function() {
	if ( $("#yourComment").val() > ' ' ) {
		commentSubmission();
	} else {
		$("#message").html('You have not provided a comment.');
	}
	return false;
};
commentSubmission = function() { 
	$("#message").html('Posting... please wait...');
	$("#messagesubmit").css({visibility: "hidden"});
	$("#yourComment").attr("disabled", "true");
	$.ajax({
		type: "POST",
		url: "/article/postcomment.php",
		dataType: "xml",
		timeout: 4000,
		data: {article_id: $("#article_id").val(), yourComment: $("#yourComment").val(), reqType: "xml"},
		success: function(xmlResponse){
			var usernameText = '<h3>'+$("resp > username", xmlResponse).text()+' wrote...</h3>'; 
			var commentText = '<p>'+$("resp > comment", xmlResponse).text()+'</p>';
			var postTime = '<div class="commenttime">Posted just now</div>';
			$("#message").html('Thank you, your comment has been posted successfully.');
			$("#nocomment").css({display: "none"});
			$("#messagesubmit").css({visibility: "visible"});
			$("#yourComment").val('').removeAttr("disabled");
			$("#newcomment"+commentcnt).append(usernameText+commentText+postTime).css({backgroundColor: "#FFF9BF"}).animate({backgroundColor: '#FFFFFF'}, 1000);
			commentcnt++;
			$("#post").before('<div id="newcomment'+commentcnt+'"></div>');
		},
		error: function(x,y,z){
			$("#message").html('Sorry, there was an error posting your comment. Please try again later.');
		}
	});
}; 
showPrevious = function() {
	if ( $('#previousLink').html() == 'show' ) {
        $('#previousLink').html('hide');
	} else {
        $('#previousLink').html('show');
	}
	$('#previousGigs').toggle('slow');
};
//Stuff that can be delayed until the page has fully loaded.
$(window).load(function(){
	$("#post").before('<div id="newcomment'+commentcnt+'"></div>');
	$("#postLogin").bind("click", function(event){
		showLoginForm('#post');
		event.preventDefault();
	});
	$('.articlepage a, .articlepage input, .articlepage img').each(function() {
		$(this).Tooltip({track:true,delay:0,showURL:false});
	});
});