﻿
/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("TraderMagazine");

TraderMagazine.Blog = function() {
    TraderMagazine.Blog.initializeBase(this);


    this.blogId = 0;
    this.page = 0;
    this.parameters = ["0", "0", "", ""];

}

TraderMagazine.Blog.prototype = {
    initialize: function() {
    TraderMagazine.Blog.callBaseMethod(this, 'initialize');
    this.init();
    },
    /// <summary>
    /// get content
    /// </summary>
    getContent: function() {
       
        TradersMag.WebService.BlogService.GetBlogContent(this.page, this.parameters, this.insertBlogContent, GLOBALS.callError);





    },
    /// <summary>
    /// get content
    /// </summary>
    init: function() {
        this.page = 0;
        this.parameters = ["0", "0", "", ""];




        if (this.blogId > 0)
            this.showArticle(this.blogId, false);
        else
            this.getContent();



    },
    /// <summary>
    /// get content
    /// </summary>
    initCategory: function(id) {
        this.page = 0;
        PAGE.parameters = [id.toString(), "0", "", ""];
        PAGE.getContent();
    },
    /// <summary>
    /// get content
    /// </summary>
    initMonth: function(id) {

        this.page = 0;
        PAGE.parameters = ["0", id.toString(), "", ""];
        PAGE.getContent();
    },
    /// <summary>
    /// get content
    /// </summary>
    initSearch: function(s) {
        this.page = 0;
        PAGE.parameters = ["0", "0", s, ""];
        PAGE.getContent();
    },
    /// <summary>
    /// get content
    /// </summary>
    initUser: function(id) {
        this.page = 0;
        PAGE.parameters = ["0", "0", "", id.toString()];
        PAGE.getContent();
    },

    /// <summary>
    /// get content
    /// </summary>
    showBlogger: function(bloggerId) {
        COMP.createLoader($get("Content"), 400);
        TradersMag.WebService.BlogService.GetAboutBlogger(bloggerId, this.insertBlogContent, GLOBALS.callError);



    },
    /// <summary>
    /// insert blog content
    /// </summary>
    insertBlogContent: function(html) {
        $get("Content").innerHTML = html;
        if (GLOBALS.PageParameter > 0) {
            PAGE.blogId = GLOBALS.PageParameter;
            GLOBALS.PageParameter = 0;
            PAGE.showArticle(PAGE.blogId, true);
        }

        GLOBALS.adjustContentHeight();
    },
    /// <summary>
    /// get blog article
    /// </summary>
    showArticle: function(blogId, comment) {
        TradersMag.WebService.BlogService.GetBlogArticle(blogId, comment, this.displayBlogArticle, GLOBALS.callError);
    },
    /// <summary>
    /// display blog article
    /// </summary>
    displayBlogArticle: function(html) {

        $get("BlogTable_0_0").innerHTML = html;

    },
    /// <summary>
    /// turn page
    /// </summary>
    switchBlogArticlePage: function(inc) {
        this.page += inc;
        TradersMag.WebService.BlogService.GetBlogContent(this.page, this.parameters, this.insertBlogContent, GLOBALS.callError);
    },
    /// <summary>
    /// create editor page
    /// </summary>
    createEditorPanel: function(blogId) {

        $get("Content").innerHTML = "";
        if (blogId == "0") {
            TradersMag.WebService.BlogService.GetBlogEditor(blogId, PAGE.insertBlogEditorCreate, GLOBALS.callError);

        } else {
            PAGE.blogId = blogId;
            TradersMag.WebService.BlogService.GetBlogEditor(blogId, PAGE.insertBlogEditorEdit, GLOBALS.callError);
        }








        /*
        var div = document.createElement("DIV");
        div.style.textAlign = "left";
        var src = "Editor/BlogEditor.aspx?Id=" + blogId;
        var c = "<iframe id='BlogEditor' frameborder='0' width='775' height='1600' scrolling='no' src='" + src + "' ></iframe>";

        div.innerHTML = c;
        $get("Content").appendChild(div);
        
        */
    },

    /// <summary>
    /// insert blog content
    /// </summary>
    insertBlogEditorCreate: function(html) {
        $get("Content").innerHTML = html;

    },
    /// <summary>
    /// create editor page
    /// </summary>
    saveBlogComment: function(blogId) {
        var comment = $get("CommentText").value;
        if (ERROR.checkField(comment, "CommentTextError", "multiline", 5, Text.errors.e33))
            TradersMag.WebService.BlogService.SaveBlogComment(blogId, escape(comment), PAGE.displayBlogArticle, GLOBALS.callError);
    },
    /// <summary>
    /// create editor page
    /// </summary>
    updateBlogComment: function(commentId) {
        var comment = $get("CommentText").value;
        if (ERROR.checkField(comment, "CommentTextError", "multiline", 5, Text.errors.e33))
            TradersMag.WebService.BlogService.UpdateBlogComment(commentId, escape(comment), PAGE.reloadBlog, GLOBALS.callError);
    },
    /// <summary>
    /// create editor page
    /// </summary>
    deleteBlogComment: function(commentId) {
        var comment = $get("CommentText").value;
        if (ERROR.checkField(comment, "CommentTextError", "multiline", 5, Text.errors.e33))
            TradersMag.WebService.BlogService.DeleteBlogComment(commentId, PAGE.reloadBlog, GLOBALS.callError);
    },
    /// <summary>
    /// create editor page
    /// </summary>
    editBlogComment: function(commentId, comment) {
        $get("CommentText").value = unescape(comment);
        $get("CommentButtonList").innerHTML = "<a onclick='PAGE.updateBlogComment(" + commentId + ");' class='button_orange'>" + Text.forum.change_comment + "</a>";
        $get("CommentButtonList").innerHTML += "&nbsp;|&nbsp;<a onclick='PAGE.deleteBlogComment(" + commentId + ");' class='button_orange'>" + Text.forum.delete_comment + "</a>";
        $get("CommentButtonList").innerHTML += "&nbsp;|&nbsp;<a onclick='PAGE.getContent();' class='button_orange'>" + Text.cancel + "</a>";

    },
    /// <summary>
    /// create editor page
    /// </summary>
    reloadBlog: function(success) {
        if (success)
            PAGE.getContent();
    },
    /// <summary>
    /// create editor page
    /// </summary>
    createNewArticle: function() {

        ERROR.hideError("BlogArticleTitleError");
        ERROR.hideError("CategoryDropDownError");
        var blogTitle = escape($get("BlogArticleTitleBox").value);
        var blogCategory = $get("CategoryDropDown").value;

        var success = true;
        if (!ERROR.checkField(blogTitle, "BlogArticleTitleError", "multiline", 2, Text.errors.e26)) success = false;
        if (blogCategory == "0") {
            success = false;
            ERROR.showError("CategoryDropDownError", Text.errors.e25);
        }
        if (success) {
            TradersMag.WebService.BlogService.CreateNewArticle(blogCategory, blogTitle, PAGE.createEditorPanel, GLOBALS.callError);
        }
    },
    /// <summary>
    /// create editor page
    /// </summary>
    articleCreated: function(result) {
        $get("Content").innerHTML = result;

    },


    /// <summary>
    /// create editor page
    /// </summary>
    insertUploadedFile: function(filePath) {
        var html = "<br/><div style='width:500px;overflow:hidden;'><img src='" + unescape(filePath) + "'></div><br/>";
        window.frames["BlogEditor"].frames["Editor1_ctl02_ctl00"].document.getElementsByTagName("body")[0].innerHTML += html;
    },
    /// <summary>
    /// insert blog content
    /// </summary>
    insertBlogEditorEdit: function(html) {
        $get("Content").innerHTML = html;


        var src = "HtmlEditor.aspx?blogId=" + PAGE.blogId;
        var c = "<iframe id='BlogEditor' name='BlogEditor' frameborder='0' width='500' height='415' scrolling='no' src='" + src + "' ></iframe>";


        $get("BlogArticleTextEditor").innerHTML = c;


    },
    /// <summary>
    /// delete article
    /// </summary>
    deleteBlogArticle: function(blogId) {
        if (confirm(Text.blog.confirm_delete_article)) {
            TradersMag.WebService.BlogService.DeleteBlogArticle(blogId, PAGE.reloadBlog, GLOBALS.callError);
        }
    },
    /// <summary>
    /// delete article
    /// </summary>
    reloadBlog: function(result) {
        if (result) {
            COMP.createLoader($get("Content"), 400);
            TradersMag.WebService.BlogService.GetBlogContent(PAGE.page, PAGE.parameters, PAGE.insertBlogContent, GLOBALS.callError);
        }
    },

    /// <summary>
    /// create editor page
    /// </summary>
    updateBlogArticle: function(blogId) {
        var html = window.frames["BlogEditor"].frames["Editor1_ctl02_ctl00"].document.getElementsByTagName("body")[0].innerHTML;


        ERROR.hideError("BlogArticleTitleError");
        ERROR.hideError("CategoryDropDownError");
        ERROR.hideError("BlogArticleTagsError");
        // ERROR.hideError("BlogArticleTextError");

        var blogTitle = escape($get("BlogArticleTitleBox").value);
        var categoryId = $get("CategoryDropDown").value;
        var blogTags = escape($get("BlogArticleTagsBox").value);
        var blogArticle = escape(html);

        var success = true;
        if (!ERROR.checkField(blogTitle, "BlogArticleTitleError", "multiline", 2, Text.errors.e26)) success = false;
        if (!ERROR.checkField(blogTags, "BlogArticleTagsError", "multiline", 2, Text.errors.e33)) success = false;
        if (blogArticle == "") {
            success = false;
            ERROR.showError("BlogArticleTextError", Text.errors.e33);
        }
        if (categoryId == "0") {
            success = false;
            ERROR.showError("CategoryDropDownError", Text.errors.e25);
        }

        if (success) {
            TradersMag.WebService.BlogService.UpdateBlogArticle(blogId, categoryId, blogTitle, blogArticle, blogTags, PAGE.insertBlogEditorEdit, GLOBALS.callError);
        }


    },
    /// <summary>
    // editor function : edit text
    /// </summary>
    editBloggerInfo: function(bloggerId) {
        var html = $get("AboutBloggerContent").innerHTML;
        var c = "<TEXTAREA style='height:500px' id='AboutBloggerEditor' class='ContactTextBox' name='AboutBloggerEditor'>" + html + "</TEXTAREA>";
        c += "<br><br><a onclick='PAGE.saveBloggerInfo(\"" + bloggerId + "\")'>Speichern</a>";
        $get("AboutBloggerContent").innerHTML = c;
    },
    /// <summary>
    // editor function : edit text
    /// </summary>
    saveBloggerInfo: function(bloggerId) {
        var c = $get("AboutBloggerEditor").value;
        TradersMag.WebService.PageService.SaveBloggerInfo(c,bloggerId, PAGE.insertAboutBloggerContent, GLOBALS.callError);
    },

    /// <summary>
    // editor function : edit text
    /// </summary>
    insertAboutBloggerContent: function(html) {
          $get("AboutBloggerContent").innerHTML = html;
    },
    dispose: function() {
        //Add custom dispose actions here
        TraderMagazine.Blog.callBaseMethod(this, 'dispose');
    }
}
TraderMagazine.Blog.registerClass('TraderMagazine.Blog', Sys.Component);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
