$(function(){

	// New Blog Posting
    // - used on My Profile and Blogs
	$("#newBlogPosting").click( function(){
		var offset = $(window).scrollTop();
		$("#newBlogPostForm").css("top", 200 + offset);
		return showDialog("newBlogPostForm");
	});
	$("#newBlogPostForm").submit(function(){
        // Remove previous errors
        $("#newBlogPostForm .error").fadeOut("fast").remove();
        // Form-specific validation
        if ($("#blogTitle").val() == "") {
            $("#blogTitle").after(createError("A title is required"));
        }
        if ($("#blogText").val() == "") {
            $("#blogText").after(createError("Post content is required"));
        }
        // Fade in the errors
        $("#newBlogPostForm .error").fadeIn();

        // Only return true if there are no errors in the form
        return ($("#newBlogPostForm .error").length == 0);
	});
   
	// New Conversation
    // - used on My Profile, Conversations, and Circle details
	$("#startConversation").click( function(){
		var offset = $(window).scrollTop();
		$("#startConversationForm").css("top", 200 + offset);
		return showDialog("startConversationForm");
	});
	$("#startConversation2").click( function(){
		var offset = $(window).scrollTop();
		$("#startConversationForm").css("top", 200 + offset);
		return showDialog("startConversationForm");
	});
	$("#startConversationForm").submit(function(){
        // Remove previous errors
        $("#startConversationForm .error").fadeOut("fast").remove();
        // Form-specific validation
        if ($("#convText").val() == "") {
            $("#convText").after(createError("Some conversation text is required"));
        }
        if ($("#convCat").val() == "") {
            $("#convCat").after(createError("Please select a conversation category"));
        }
        // Fade in the errors
        $("#startConversationForm .error").fadeIn();
        // Only return true if there are no errors in the form
        return ($("#startConversationForm .error").length == 0);
	});

	// New Tip
    // - used on My Profile
	$("#createTip").click( function(){
		var offset = $(window).scrollTop();
		$("#createTipForm").css("top", 200 + offset);
		return showDialog("createTipForm");
	});
	$("#createTip2").click( function(){
		var offset = $(window).scrollTop();
		$("#createTipForm").css("top", 200 + offset);
		return showDialog("createTipForm");
	});
	$("#createTipForm").submit(function(){
        // Remove previous errors
        $("#startConversationForm .error").fadeOut("fast").remove();
        // Form-specific validation
        if ($("#newTipContent").val() == "") {
            $("#newTipContent").after(createError("Some tip text is required"));
        }
        // Fade in the errors
        $("#createTipForm .error").fadeIn();
        // Only return true if there are no errors in the form
        return ($("#createTipForm .error").length == 0);
	});

    // View Tips
    // - used on Member Profile
	$("#viewProfileTips").click( function(){
		var offset = $(window).scrollTop();
		$("#viewProfileTipsForm").css("top", 200 + offset);
		return showDialog("viewProfileTipsForm");
	});

	// Manage Tips
    // - used on My Profile
	$("#manageTips").click( function(){
		var offset = $(window).scrollTop();
		$("#manageTipsForm").css("top", 200 + offset);
		return showDialog("manageTipsForm");
	});

	// Manage Favorite Tips
    // - used on My Home
	$("#manageFavTips").click( function(){
		var offset = $(window).scrollTop();
		$("#manageFavTipsForm").css("top", 200 + offset);
		return showDialog("manageFavTipsForm");
	});

	// Manage Bookmarks
    // - used on My Home
	$("#manageBookmarks").click( function(){
		var offset = $(window).scrollTop();
		$("#manageBookmarksForm").css("top", 200 + offset);
		return showDialog("manageBookmarksForm");
	});

	// New Profile Comment
    // - used on Member Profile
	$("#newProfileComment").click( function(){
		var offset = $(window).scrollTop();
		$("#newProfileCommentForm").css("top", 200 + offset);
		return showDialog("newProfileCommentForm");
	});
	$("#newProfileComment2").click( function(){
		var offset = $(window).scrollTop();
		$("#newProfileCommentForm").css("top", 200 + offset);
		return showDialog("newProfileCommentForm");
	});
	$("#newProfileCommentForm").submit(function(){
		// Remove previous errors
		$("#newProfileCommentForm .error").fadeOut("fast").remove();
		// Form-specific validation
		if ($("#commentText").val() == "") {
			$("#commentText").after(createError("Some comment text is required"));
		}
		// Fade in the errors
		$("#newProfileCommentForm .error").fadeIn();
		// Only return true if there are no errors in the form
		return ($("#newProfileCommentForm .error").length == 0);
	});

    // Review Product
    // - used on Product Review
    $("#reviewProduct").click( function(){
		var offset = $(window).scrollTop();
		$("#reviewProductForm").css("top", 200 + offset);
		return showDialog("reviewProductForm");
	});
    $("#reviewProductForm").submit(function(){
        // Remove previous errors
        $("#reviewProductForm .error").fadeOut("fast").remove();
        // Form-specific validation
        if ($("#reviewText").val() == "") {
            $("#reviewText").after(createError("Review content is required"));
        }
        // Fade in the errors
        $("#reviewProductForm .error").fadeIn();
        // Only return true if there are no errors in the form
        return ($("#reviewProductForm .error").length == 0);
   });
   
	// New Circle
    // - used on Circles
	$("#newCircle").click( function(){
		var offset = $(window).scrollTop();
		$("#newCircleForm").css("top", 200 + offset);
		return showDialog("newCircleForm");
	});
	$("#newCircleForm").submit(function(){
        // Remove previous errors
        $("#newCircleForm .error").fadeOut("fast").remove();
        // Form-specific validation
        if ($("#circleTitle").val() == "") {
            $("#circleTitle").after(createError("A circle title is required"));
        }
        if ($("#circleDescr").val() == "") {
            $("#circleDescr").after(createError("A circle description is required"));
        }
        if ($("#circleCategory").val() == "") {
            $("#circleCategory").after(createError("Please select a circle category"));
        }
        // Fade in the errors
        $("#newCircleForm .error").fadeIn();
		// Only return true if there are no errors in the form
		return ($("#newCircleForm .error").length == 0);
	});

});


/*** General pop up dialog utility functions ***/
function showDialog(id) {
    $("#everythingButTheFooter, #footer").css("opacity", "0.5");
    $("#pageWrapper").css("opacity", "0.5").click( function() { return hideDialog(id) } );
    $("#" + id + " .close").click( function() { return hideDialog(id) } );
    $("#" + id).removeClass("hidden");
    return false;
}

function hideDialog(id) {
    $("#everythingButTheFooter, #footer").css("opacity", "1.0");
    $("#pageWrapper").css("opacity", "1.0").unbind("click");
    $("#" + id + " .close").unbind("click");
    $("#" + id).addClass("hidden");
    $("#" + id + " .error").remove();
    return false;
}

function createError(text) {
    return '<div class="hidden error">' + text + '<span class="pointer"></span></div>';
}