﻿/// <reference path="jquery-1.5.2.min.js" />

var postArea, openPostArea, close, getMorePosts, afterPost, context, error, init, currentType, openComment, closeComment;

$(function () {    
    init();
});

init = function () {
    context = $(".column");
    postArea = $("#postArea", context);
    commentForm = $("#commentform", context);
    postArea.click(openPostArea);

    $(".pen:eq(0)", context).unbind().click(openComment);
    $("a", postArea).click(function (e) { e.preventDefault(); });
    $(".textpost", context).addClass("open");
    $("#createTextPost", postArea).click(function () {
        currentType("textpost");
    });
    $("#createImagePost", postArea).click(function () {
        currentType("imagepost");
    });
    $("#createVideoPost", postArea).click(function () {
        currentType("videopost");
    });
    $("#morePosts", context).click(function (e) {
        e.preventDefault();
        getMorePosts();
    });
    $("#morePostsForBlog", context).click(function (e) {
        e.preventDefault();
        var forCurrentBlog = true;
        getMorePosts(forCurrentBlog);
    });    

};

currentType = function (type) {
    $(".postType", postArea).removeClass("open");
    $("." + type, postArea).addClass("open");
    $(".currentType", postArea).val(type);    
};

openComment = function () {
    commentForm.addClass("open");
    $(".pen:eq(0)", context).unbind().click(closeComment);
};

closeComment = function () {
    commentForm.removeClass("open");
    $(".pen:eq(0)", context).unbind().click(openComment);
};

openPostArea = function () {
    postArea.addClass("open");
    $(".teaser", postArea).bind("click", close);
    $(".submit-btn, #errorMsg", postArea).show(400);   
};

close = function (e) {
    postArea.removeClass("open");
    $(".teaser", postArea).unbind();
    e.cancelBubble = true;
    if (e.stopPropagation) { e.stopPropagation(); }
    $(".submit-btn, #errorMsg").hide(400);
};

afterPost = function (updateUrl) {    
    $.ajax({
        url: updateUrl,
        success: function (data) {
            $(".block-post", context).prepend(data);
            $(".block-post .post", context).last().remove();
        }
    });
};

getMorePosts = function (forCurrentBlog) {
    var lastId = $(".block-post .post", context).last().attr("id").replace("post", "");
    $.ajax({
        url: "/forside/GetMorePosts?lastId=" + lastId + "&top=5&forCurrentBlog=" + forCurrentBlog,
        success: function (data) {
            $(".block-post .link-holder", context).before(data);
        }
    });
};

error = function (msg) {
    $("#errorMsg").html(msg);
};
