﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />
var commentForm;

j$(function () {
	j$("#name").val(get_cookie("name"));
	j$("#email").val(get_cookie("email"));
	j$("#city").val(get_cookie("city"));
	j$("#subject").val("Re: " + document.title);
	arrangeReplies();
	if (j$(".comment").length == 0) {
		j$("#commentsPlaceHolder").html('<div class="noComments">No comments posted. Be the first to comment!</div>');
	}
	commentForm = j$("#commentbox");
	commentForm.data("replyid", 0);
	sizeCommentControls();
});

function getCommentLocation() {
	var winloc = window.location.toString();
	winloc = winloc.replace("#usercomment", "");
	winloc += "#usercomment";
	return winloc;
}

function saveComment() {
	var commentBox = commentForm;
	if (!validateCommentForm()) return;
	var name = j$(commentBox).find("#name").val().replace(/'/g, "\\'");
	var subject = "";
	var email = j$(commentBox).find("#email").val().replace(/'/g, "\\'"); ;
	var city = j$(commentBox).find("#city").val().replace(/'/g, "\\'"); ;
	var comment = j$(commentBox).find("#comment").val().replace(/'/g, "\\'");
	var commentid = j$(commentBox).data("replyid");
	var source = j$(commentBox).find("input:radio:checked").val();
	var notifyReplies = j$(commentBox).find("#notifyreplies")[0].checked ? 1 : 0;
	if (!source) source = 0; else source = parseInt(source);
	if (!commentid) commentid = 0;
	j$(commentBox).find("#submitComment").hide();
	j$(commentBox).find("#submitWait").show();
	j$.ajax({
		type: "POST",
		url: webserviceURL,
		data: "{'Name':'" + name + "', Subject:'" + subject + "', 'Email':'" + email + "', 'Location':'" + city + "', 'Comment':'" + comment + "', 'PostID':" + postid + ", 'CommentID':" + commentid + ", 'NotifyReplies':" + notifyReplies + ", 'Source':" + source + "}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function (msg) { if (msg.d.toString().substring(0, 3) == "ERR") alert(msg.d); else commentSuccess(msg); },
		error: function (xhr) {
			alert(xhr.responseText);
		},
		complete: function () {
			j$(commentBox).find("#submitWait").hide();
			j$(commentBox).find("#submitComment").show();
		}
	});
}

function commentSuccess(msg) {
	var comm = j$('<div class="comment"><div class="commentHeader"><img src="/images/user.gif"/><b>' + j$("#name").val() + '</b> from ' + j$("#city").val() + '</div><div class="commentBody"><div class="commentText">' + j$("#comment").val() + '</div></div></div>');
	var replyid = commentForm.data("replyid");
	if (replyid == 0)
		j$("#commentsPlaceHolder").append(comm);
	else {
		j$("#" + replyid).after(comm);
		comm.css("margin-left", parseInt(j$("#" + replyid).css("margin-left")) + 20);
	}
	moveCommentBoxToOriginalPosition();
	comm.css("background-color", "lightyellow");
	alert("Comment Posted Successfully. Thank You!");
	document.cookie = "name=" + j$("#name").val();
	document.cookie = "email=" + j$("#email").val();
	document.cookie = "city=" + j$("#city").val();
	j$("#comment").val("");
}

function showReplyForm(obj) {
	var replyPH = j$(obj).findParentByClassName("comment").find("#replyPlaceHolder");
	if (j$(obj).text().trim() == "Reply") {
		if (commentForm.data("replyid") != 0) {	//first remove the form from any other reply location
			commentForm.data("replybutton").html("Reply");
			commentForm.data("replybutton").css("color", "black").css("font-weight", 400);
		}
		replyPH.append(commentForm);
		commentForm.data("replyid", j$(replyPH).attr("replyid"));
		commentForm.data("replybutton", j$(obj));
		replyPH.show();
		replyPH.find("#comment").focus();
		sizeCommentControls();
		commentForm.find("#ReplyHeader").html("&nbsp;Reply to " + j$(replyPH).attr("replyto"));
		j$(obj).animate({ opacity: 'toggle' }, function () {
			j$(obj).html("Cancel");
			j$(obj).css("color", "red").css("font-weight", 700);
			j$(obj).animate({ opacity: 'toggle' });
		});
	}
	else { //if text was cancel, when clicked
		j$(obj).html("Reply");
		j$(obj).css("color", "black").css("font-weight", 400);
		moveCommentBoxToOriginalPosition();
	}
}

function moveCommentBoxToOriginalPosition() {
	if (commentForm.data("replybutton")) commentForm.data("replybutton").html("Reply");
	if (commentForm.data("replybutton")) commentForm.data("replybutton").css("color", "black").css("font-weight", 400);
	j$("#commentboxholder").append(commentForm);
	commentForm.data("replyid", 0);
	commentForm.data("replybutton", null);
	commentForm.find("#ReplyHeader").html("Post your comment");
}

function arrangeReplies() {
	j$(".comment").each(function () {
		j$(this).css("margin-left", (parseInt(j$(this).attr("level")) - 1) * 20);
	});
}

function validateCommentForm() {
	commentBox = commentForm;
	j$(commentBox).find("#name").val(j$.trim(j$(commentBox).find("#name").val()));
	j$(commentBox).find("#email").val(j$.trim(j$(commentBox).find("#email").val()));
	j$(commentBox).find("#city").val(j$.trim(j$(commentBox).find("#city").val()));
	j$(commentBox).find("#comment").val(j$.trim(j$(commentBox).find("#comment").val()));
	if (j$(commentBox).find("#name").val() == "") { alert("Name must be entered"); j$(commentBox).find("#name").focus(); return (false); }
	if (j$(commentBox).find("#email").val() == "") { alert("Email must be entered"); j$(commentBox).find("#email").focus(); return (false); }
	if (j$(commentBox).find("#comment").val() == "") { alert("Comment must be entered"); j$(commentBox).find("#comment").focus(); return (false); }
	if (!isValidEmail(j$(commentBox).find("#email").val())) { alert("Invalid Email"); j$(commentBox).find("#email").focus(); return (false); }
	return (true);
}

var fbUserName;
function fb_logout() {
	j$(".FBLoginButton").show();
	j$(".PostUsingFB").hide();
	FB.XFBML.Host.parseDomTree();
	userLoggedIn = false;
	showAnonymousProfile();
}

function fb_login() {
	FB.getLoginStatus(function (response) {
		if (response.session) {
			j$(".FBProfile").show();
			j$(".PostUsingFB").show();
			j$(".FBLoginButton").hide();
			FB.api('/me', function (response) {
				fbUserName = response.name;
				showFBProfile();
			});
			j$(".cbPostFacebook")[0].checked = true;
		} else {
			fb_logout();
		}
	});
}

j$(function () {
	commentForm = j$("#commentbox");
	var cbAnonymous = j$(".cbPostAnonymous");
	if (cbAnonymous.length > 0) cbAnonymous[0].checked = true;

	showAnonymousProfile();
	j$(".cbPostFacebook").click(function () {
		showFBProfile();
	});

	j$(".cbPostAnonymous").click(function () {
		showAnonymousProfile();
	});

	j$(".cbPostGFC").click(function () {
		showGFCProfile();
	});

	//FB.login(function (response) { if (response.session) fb_login(); else fb_logout(); });

	j$(".replyButton").click(function () {
		showReplyForm(this);
	});
	j$("#submitComment").click(function () {
		saveComment();
	});
});

function hideProfiles() {
	j$(".FBProfile").hide();
	j$(".GFCProfile").hide();
}

function showFBProfile() {
	hideProfiles();
	j$(".FBProfile").show();
	j$(".username").each(function () { this.disabled = true; });
	j$(".username").val(fbUserName);
	j$(".userlabel").html("Logged in via Facebook");
	j$(".cbPostFacebook").each(function () { this.checked = true; });
	sizeCommentControls();
}

function showGFCProfile() {
	hideProfiles();
	j$(".GFCProfile").show();
	j$(".username").each(function () { this.disabled = true; });
	j$(".username").val(gfcUserName);
	j$(".userlabel").html("Google Friend Connect");
	j$(".cbPostGFC").each(function () { this.checked = true; });
	sizeCommentControls();
}

function showAnonymousProfile() {
	hideProfiles();
	j$(".username").each(function () { this.disabled = false; });
	j$(".userlabel").html("Name (Required)");
	sizeCommentControls();
}

function sizeCommentControls() {
	j$(".commentbox").each(function () {
		var boxwidth = j$(this).width();
		j$(this).find("#comment").width(boxwidth - 10);
		if (j$(this).find("input:radio:checked").val() != 0) boxwidth -= 55;
		j$(this).find("#name, #email, #city").width(boxwidth / 3 - 8);
	});
}
