var Blog = function () {};

/*
 * Chargement de l'application
 */
Blog.Load = function (parameter)
{
    Dashboard.AddEventAppMenu(Blog.Execute, "", "Blog");
    Dashboard.AddEventWindowsTool("Blog");

    Blog.InitTabBlog();
    Blog.InitTabCategory();
    Blog.InitTabArticle();
    Blog.InitTabExport();
};

/***
 * Tab du forum
 */
Blog.InitTabBlog = function () {
    BlogForm.Init();
};

/**
 * Tab des catgéogrie
 * @returns {undefined}
 */
Blog.InitTabCategory = function () {
    //Catégory
    Event.AddById("btnAddCategory", "click", () => {
        Blog.ShowAddCategory("")
    });
    EntityGrid.Initialise('gdCategory');
};

/**
 * Edit category
 * @param {type} categoryId
 * @returns {undefined}
 */
Blog.EditCategory = function (categoryId) {
    Blog.ShowAddCategory(categoryId);
};

/**
 * Dialogue d'ajout de catégorie
 */
Blog.ShowAddCategory = function (categoryId) {

    Dialog.open('', {"title": Dashboard.GetCode("Blog.AddCategorie"),
        "app": "Blog",
        "class": "DialogAdminBlog",
        "method": "AddCategorie",
        "params": categoryId
    });
};

/***
 * Rafraichit la tab des catégorie
 * @returns {undefined}
 */
Blog.ReloadCategory = function (data) {
    let gdCategory = Dom.GetById("gdCategory");
    gdCategory.parentNode.innerHTML = data.data;
    Forum.InitTabCategory();
};

/***
 * Supprime une catégorie
 * @param {type} categoryId
 * @returns {undefined}
 */
Blog.DeleteCategory = function (categoryId) {

    Animation.Confirm(Language.GetCode("Blog.ConfirmRemoveCategorie"), () => {

        let data = "Class=Blog&Methode=DeleteCategory&App=Blog";
        data += "&categoryId=" + categoryId;

        Request.Post("Ajax.php", data).then(data => {

            data = JSON.parse(data);

            Blog.ReloadCategory(data);
        });
    });
};

/***
 * Translate dialogue
 */
Blog.TranslateCategory = function(categoryId){

    Dialog.open('', {"title": Dashboard.GetCode("Blog.TranslateCategory"),
    "app": "Lang",
    "class": "DialogAdminLang",
    "method": "Translate",
    "params": "Blog-BlogCategory-"+categoryId,
    "type": "full",
});
};

/**
 * Tab des articles
 * @returns {undefined}
 */
Blog.InitTabArticle = function () {
    //Catégory
    Event.AddById("btnAddArticle", "click", () => {
        Blog.ShowAddArticle("");
    });
    EntityGrid.Initialise('gdArticle');
};

/***
 * Init export tab
 */
Blog.InitTabExport =function(){

    Event.AddById("btnExport", "click", Blog.Export);
    Event.AddById("btnImport", "click", Blog.ShowImport);
};

/***
 * Export Category and Article
 */
Blog.Export = function(){
    window.open("Api/Blog/Export", "_blank");
};

/***
 * Import Categorie and Article
 */
Blog.ShowImport = function(){

    Dialog.open('', {"title": Dashboard.GetCode("Blog.Import"),
    "app": "Blog",
    "class": "DialogAdminBlog",
    "method": "ShowImport",
    });
};


/**
 * Edit article
 * @param {type} categoryId
 * @returns {undefined}
 */
Blog.EditArticle = function (articleId) {
    Blog.ShowAddArticle(articleId);
};

/**
 * Dialogue d'ajout d'article
 */
Blog.ShowAddArticle = function (articleId) {

    Dialog.open('', {"title": Dashboard.GetCode("Blog.AddArticle"),
        "app": "Blog",
        "class": "DialogAdminBlog",
        "method": "AddArticle",
        "params": articleId
    });
};

/***
 * Rafraichit la tab des article
 * @returns {undefined}
 */
Blog.ReloadArticle = function (data) {
    let gdArticle = Dom.GetById("gdArticle");
    gdArticle.parentNode.innerHTML = data.data;
    Blog.InitTabArticle();
};

/***
 * Translate dialogue
 */
Blog.TranslateArticle = function(articleId){

    Dialog.open('', {"title": Dashboard.GetCode("Blog.TranslateArticle"),
    "app": "Lang",
    "class": "DialogAdminLang",
    "method": "Translate",
    "params": "Blog-BlogArticle-"+articleId,
    "type": "full",
});
};

/***
 * Supprime un article
 * @param {type} categoryId
 * @returns {undefined}
 */
Blog.DeleteArticle = function (articleId) {

    Animation.Confirm(Language.GetCode("Blog.ConfirmRemoveArticle"), () => {

        let data = "Class=Blog&Methode=DeleteArticle&App=Blog";
        data += "&articleId=" + articleId;

        Request.Post("Ajax.php", data).then(data => {

            data = JSON.parse(data);

            Blog.ReloadArticle(data);
        });
    });
};


/**
 * Dialogue dédition d'article
 */
Blog.WriteArticle = function (articleId) {

    Dialog.open('', {"title": Dashboard.GetCode("Blog.WriteArticle"),
        "app": "Blog",
        "class": "DialogAdminBlog",
        "method": "WriteArticle",
        "params": articleId,
        "type": "full",
    });
};

/***
 * Patage de l'article selon le Workflow
 */
Blog.ShareArticle = function(articleId){

    let http = new Http("Blog", "ShareArticle");
    http.Add("articleId", articleId);
    http.Post((data)=>{
        console.log(data);
    });
};


/***
 * Refresh 
 * @returns {undefined}
 */
Blog.RefreshPlugin = function () {
    Dashboard.StartApp("", "Blog", "");
};

/*
 * Chargement des �venements
 */
Blog.LoadEvent = function ()
{
    Dashboard.AddEventAppMenu(Blog.Execute, "", "Blog");
    Dashboard.AddEventWindowsTool("Blog");
};

/*
 * Execute une fonction
 */
Blog.Execute = function (e)
{
    //Appel de la fonction
    Dashboard.Execute(this, e, "Blog");
    return false;
};

/*
 *	Affichage de commentaire
 */
Blog.Comment = function ()
{
    Dashboard.Comment("Blog", "1");
};

/*
 *	Affichage de a propos
 */
Blog.About = function ()
{
    Dashboard.About("Blog");
};

/*
 *	Affichage de l'aide
 */
Blog.Help = function ()
{
    Dashboard.OpenBrowser("Blog", "{$BaseUrl}/Help-App-Blog.html");
};

/*
 *	Affichage de report de bug
 */
Blog.ReportBug = function ()
{
    Dashboard.ReportBug("Blog");
};

/*
 * Fermeture
 */
Blog.Quit = function ()
{
    Dashboard.CloseApp("", "Blog");
};

/*
 * Evenement
 */
BlogAction = function () {};

/**
 * Pop in d'ajout de blog
 * @returns {undefined}
 */
BlogAction.ShowAddBlog = function ()
{
    var param = Array();
    param['App'] = 'Blog';
    param['Title'] = 'Blog.ShowAddBlog';

    Dashboard.OpenPopUp('Blog', 'ShowAddBlog', '', '', '', 'BlogAction.LoadMyBlog()', serialization.Encode(param));
};

/**
 * Chage les blogs de l'utilisateur
 * @returns {undefined}
 */
BlogAction.LoadMyBlog = function ()
{
    var data = "Class=Blog&Methode=LoadMyBlog&App=Blog";
    Dashboard.LoadControl("dvDesktop", data, "", "div", "Blog");

    EntityGrid.Initialise('gdBlog');
    //setTimeout("EntityGrid.Init('gdBlog')", 1000);
};

/**
 * Charge un blog
 * @param {type} blogId
 * @returns {undefined}
 */
BlogAction.LoadBlog = function (blogId)
{
    var data = "Class=Blog&Methode=LoadBlog&App=Blog";
    data += "&blogId=" + blogId;
    Dashboard.LoadControl("dvDesktop", data, "", "div", "Blog");

    BlogAction.Tab = new Array();

    //Memorisation du blog
    BlogAction.BlogId = blogId;

    //Charge les image du blog
    BlogAction.ReloadImage();

    EntityGrid.Initialise('gdBlogCategory');
    EntityGrid.Initialise('gdLecteur');
    EntityGrid.Initialise('gdArticle');

};

BlogAction.EditCategory = function (categoryId)
{
    BlogAction.ShowAddCategory("", categoryId);
};

/**
 * Pop in d'ajout de catégorie
 */
BlogAction.ShowAddCategory = function (blogId, categoryId)
{
    var param = Array();
    param['App'] = 'Blog';
    param['Title'] = 'Blog.ShowAddCategory';
    param['blogId'] = blogId;

    if (categoryId != undefined)
        param['idEntite'] = categoryId;

    Dashboard.OpenPopUp('Blog', 'ShowAddCategory', '', '', '', 'BlogAction.RefreshCategory(' + blogId + ')', serialization.Encode(param));
};

/**
 * Recharge les catégories du blog
 **/
BlogAction.RefreshCategory = function (blogId)
{
    var data = "Class=Blog&Methode=GetTabCategory&App=Blog";
    data += "&blogId=" + blogId;
    data += "&showAll=true";

    Dashboard.LoadControl("tab_1Blog", data, "", "div", "Blog");

    EntityGrid.Initialise('gdBlogCategory');
};

/**
 * Popin de creation d'article
 * @returns {undefined}
 */
BlogAction.ShowAddArticle = function (blogId)
{
    var param = Array();
    param['App'] = 'Blog';
    param['Title'] = 'Blog.ShowAddArticle';
    param['blogId'] = blogId;

    Dashboard.OpenPopUp('Blog', 'ShowAddArticle', '', '', '', 'BlogAction.LoadArticle(' + blogId + ')', serialization.Encode(param));
};


/**
 * Rafraichit la liste des articles
 * @param {type} blogId
 * @returns {undefined}
 */
BlogAction.LoadArticle = function (blogId)
{
    var data = "Class=Blog&Methode=GetTabArticle&App=Blog";
    data += "&blogId=" + blogId;

    Dashboard.LoadControl("gdArticle", data, "", "table", "Blog");

    EntityGrid.Initialise('gdArticle');
};

/**
 * Edite les propriete d'un blog
 * @type type
 */
BlogAction.EditPropertyArticle = function (articleId, blogId)
{
    BlogAction.ArticleId = articleId;

    var param = Array();
    param['App'] = 'Blog';
    param['Title'] = 'Blog.ShowAddArticle';
    param['articleId'] = articleId;

    Dashboard.OpenPopUp('Blog', 'ShowAddArticle', '', '', '', 'BlogAction.LoadArticle(' + blogId + ')', serialization.Encode(param));
};

/**
 * Edite un article
 * @param {type} articleId
 * @returns {undefined}
 */
BlogAction.EditArticle = function (articleId, name)
{
    //ouvre l'article dans une pop up
    if (typeof (BlogAction.Tab) == "undefined")
    {
        var param = Array();
        param['App'] = 'Blog';
        param['Title'] = 'Blog.OPenArticle';
        param['articleId'] = articleId;
        param['Top'] = "100px";

        Dashboard.OpenPopUp('Blog', 'OpenArticle', '', '800', '600', '', serialization.Encode(param));
    } else
    {
        //Verification que l'on ne la pas déjà ajouter
        for (i = 0; i < BlogAction.Tab.length; i++)
        {
            if (BlogAction.Tab[i] == name)
            {
                return;
            }
        }

        //Recuperation du tabStrip
        var tsFile = document.getElementById("tbBlog");
        var th = tsFile.getElementsByTagName("th");

        //Recuperation du dernier onglet
        var lastTab = th[th.length - 1];

        var idTab = lastTab.id.split("_");
        var id = idTab[1];
        id++;

        var newTab = document.createElement("th");

        newTab.innerHTML = name;
        newTab.name = name;
        newTab.id = "index_" + id;
        newTab.className = "TabStripDisabled";
        newTab.title = name;
        var click = function () {
            TabStrip.ShowTab(this, 'tab_' + id + '', id);
        };

        Dashboard.AddEvent(newTab, "click", click);

        //Ajout de l'onglet
        (th[0]).parentNode.appendChild(newTab);

        //Ajout de la div correspondante
        var div = document.createElement("div");
        div.id = "tab_" + id + "";
        div.style.overflow = 'hidden';
        div.style.display = 'none';

        div.innerHTML = "Loading ...";
        var JAjax = new ajax();
        JAjax.data = "App=Blog&Methode=EditArticle";
        JAjax.data += "&ArticleId=" + articleId;

        var contenu = JAjax.GetRequest("Ajax.php");
        div.innerHTML = BlogAction.FormatDataForClient(contenu);

        tsFile.appendChild(div);

        TabStrip.ShowTab(newTab, 'tab_' + id + '', 10, "Blog");

        BlogAction.Tab.push(name);


    }

    //Initialisation dez l'editeur
    //   CKEDITOR.disableAutoInline = true;
    var editor = CKEDITOR.replace('tbContentArticle_' + articleId);

    CKEDITOR.setApp("BlogAction");
};

/**
 * Retrouve l'ongelt actif
 * @returns {undefined}
 */
BlogAction.findTabSelected = function ()
{
    var tb = document.getElementById('tbBlog');

    if (tb != "")
    {
        dv = tb.getElementsByTagName("div");

        for (i = 0; i < dv.length; i++)
        {
            if (dv[i].id.indexOf("tab") > -1 && dv[i].style.display == "block")
            {
                return dv[i];
            }
        }
    } else
    {
        dvArticle = document.getElementById("dvArticle");

        return dvArticle;
    }
};

/**
 * Sauvegarde le contenu
 * @returns {undefined}
 */
BlogAction.saveContent = function ()
{
    //Recuyeration de l'onglet selectionné
    dvContent = BlogAction.findTabSelected();

    //Recuêration du contenu
    txtContent = dvContent.getElementsByTagName("textarea");
    content = eval("CKEDITOR.instances." + txtContent[0].id + ".getData()");

    //Recuperation de l'identifiant dez l'article
    controls = dvContent.getElementsByTagName("input");

    for (i = 0; i < controls.length; i++)
    {
        if (controls[i].type == "hidden")
        {

            var articleId = controls[i].value;
        }
    }

    //Sauvegarde
    var JAjax = new ajax();
    JAjax.data = "App=Blog&Methode=UpdateContent";
    JAjax.data += "&ArticleId=" + articleId;
    JAjax.data += "&content=" + BlogAction.FormatDataForServeur(content);

    alert(JAjax.GetRequest("Ajax.php"));
};

/**
 *
 * @returns {String}Obitent le bouton de telechargement de fichier
 */
BlogAction.getUploadButton = function ()
{
    $html = "<div>";

    $html += "<input type='file' id='fileUpload' name='fileUpload' />";
    $html += "<input type='button' value='Envoyer' onclick='upload.doUpload(this)' /> ";

    $html += "<input type='hidden' id='hdApp' name = 'hdApp'  value='Blog'  /> ";
    $html += "<input type='hidden' id='hdIdElement' name='hdIdElement' value='" + BlogAction.BlogId + "' /> ";
    $html += "<input type='hidden' id='hdCallBack' name='hdCallBack' value='BlogAction.ReloadImage();' /> ";
    $html += "<input type='hidden' id='hdAction' name='hdAction' value='UploadImageBlog' /> ";
    $html += "<input type='hidden' id='hdIdUpload'  name='hdIdUpload' value='' /> ";

    //Frame From Upload
    if (1 == 2)
    {
        $html += "<iframe id='frUpload' src='upload' style='display:block' >";
    } else
    {
        $html += "<iframe id='frUpload' src='upload' style='display:none' >";
    }

    $html += "</iframe>";
    $html += "</div>";

    return $html;

};

/**
 *
 * @returns {undefined}enoit l'image sur le serveur
 */
BlogAction.upload = function ()
{
    UploadAjaxFile.Upload('BlogAction.ReloadImage()', 'fileToUpload');

    ;
};

/**
 * Recharge la bibliothèque d'image
 * @returns {undefined}
 */
BlogAction.ReloadImage = function ()
{
    var JAjax = new ajax();
    JAjax.data = "App=Blog&Methode=GetImages";
    JAjax.data += "&BlogId=" + BlogAction.BlogId;

    lstImage = JAjax.GetRequest("Ajax.php").split(";");

    var vTypeOf = typeof (CKEDITOR);
    if (vTypeOf == 'object')
    {
        CKEDITOR.config.tabbedimages = lstImage[0].split(",");
        CKEDITOR.config.tabbedthumbimages = lstImage[1].split(",");
        if (typeof ReplaceDSbrowserHTML == 'function') {
            ReplaceDSbrowserHTML();
        }
    }
};

/**
 * Remplace les caractères spéciaux
 */
BlogAction.FormatDataForServeur = function (data)
{
    var myRegEx = new RegExp("&", "gm");
    data = data.replace(myRegEx, "!et!");

    return data;
};

/**
 * Remplace les caractères speciaux
 */
BlogAction.FormatDataForClient = function (data)
{
    var myRegEx = new RegExp("!et!", "gm");
    data = data.replace(myRegEx, "&");

    return data;
};

/**
 * Rafraichit l'image de l'article
 * @returns {undefined}
 */
BlogAction.RefreshImageArticle = function ()
{
    var dvImageArticle = document.getElementById("dvImageArticle");

    var JAjax = new ajax();
    JAjax.data = "App=Blog&Methode=GetImageArticle";
    JAjax.data += "&ArticleId=" + BlogAction.ArticleId;

    dvImageArticle.innerHTML = JAjax.GetRequest("Ajax.php");
};

/*
 * Affiche les commentaires sur un article
 *
 */
BlogAction.ShowComment = function (articleId)
{
    var param = Array();
    param['App'] = 'Blog';
    param['Title'] = 'Blog.ShowComment';
    param['articleId'] = articleId;

    Dashboard.OpenPopUp('Blog', 'ShowComment', '', '', '', '', serialization.Encode(param));
};

/**
 * Publie/Depublie un commentaire
 * @param {type} commentId
 * @param {type} state
 * @returns {undefined}
 */
BlogAction.Publish = function (control, commentId, state)
{
    //Nouvau boutton
    var container = control.parentNode;
    var newButton = "<input type='button' ";
    newButton += " class='btn btn-info' ";

    container.removeChild(control);

    //Sauvagarde ajax
    var JAjax = new ajax();
    JAjax.data = "App=Blog&Methode=PublishComment";
    JAjax.data += "&commentId=" + commentId;
    JAjax.data += "&state=" + state;

    JAjax.GetRequest("Ajax.php");

    if (state == 1)
    {
        newButton += "value='unpublish' ";
        newButton += " onclick ='BlogAction.Publish(this, " + commentId + ", 0)' >";
    } else
    {
        newButton += "value='publish' ";
        newButton += " onclick ='BlogAction.Publish(this, " + commentId + ", 1)' >";
    }

    container.innerHTML += newButton;
};

/*
 * Synchronise le blog sur le site distant
 * Pour l'instant les images
 * TODO Gérer un site complet externe
 */
BlogAction.Synchronise = function (blogId)
{
    var jbBlog = document.getElementById("jbBlog");

    var JAjax = new ajax();
    JAjax.data = "App=Blog&Methode=Synchronise";
    JAjax.data += "&BlogId=" + blogId;

    jbBlog.innerHTML += JAjax.GetRequest("Ajax.php");
};

/*
 Ajoute le commentaire
 */
BlogAction.AddComment = function (code)
{
    var dvComment = document.getElementById("dvAddComment");

    var tbEmail = document.getElementById("tbEmail");
    var tbName = document.getElementById("tbName");
    var tbComment = document.getElementById("tbComment");

    if (tbComment.value != "")
    {

        var JAjax = new ajax();
        JAjax.data = "App=Blog&Methode=AddComment";
        JAjax.data += "&code=" + code;
        JAjax.data += "&tbEmail=" + tbEmail.value;
        JAjax.data += "&tbName=" + tbName.value;
        JAjax.data += "&tbComment=" + tbComment.value;

        dvComment.innerHTML = JAjax.GetRequest(Dashboard.GetPath("Ajax.php"));
    } else
    {
        alert(Dashboard.GetCode("Blog.MustAddMessage"))
    }
};

/*
 * Inscription newsLetter
 */
Blog.AddEmailNews = function (blogId)
{
    var dvNewsLetter = document.getElementById("dvNewsLetter");
    var tbEmailNews = document.getElementById("tbEmailNews");

    if (tbEmailNews.value != "")
    {
        var JAjax = new ajax();
        JAjax.data = "App=Blog&Methode=AddEmailNews";
        JAjax.data += "&BlogId=" + blogId;
        JAjax.data += "&tbEmailNews=" + tbEmailNews.value;

        dvNewsLetter.innerHTML = JAjax.GetRequest(Dashboard.GetPath("Ajax.php"));
    } else
    {
        alert(Dashboard.GetCode("Blog.MustAddEmailNews"))
    }
};

/***
 * Init thre blog front page
 */
Blog.Init = function (app, method) {
    if(app == "Blog"){
        UserNewsLetterForm.Init();
 
        switch (method){
            case "Article" :
            break;
            default :
            break;   
        }
    }
};


Blog.InitAdminWidget = function(){
    
};

