var ContentManager = function (id) {

    var that, init, clearContent, hideContent,
        node = document.getElementById(id);

    init = function () {
            
    };
    
    clearContent = function () {
        while (node.hasChildNodes()) {
            node.removeChild(node.firstChild);
        }
    };
    
    var callCallback = function (callback) {
        callback();
        node.removeEventListener( 'webkitTransitionEnd', 
            function () { 
                callCallback(callback);
            },
            false);
    };
    
    var setOpacity = function (value, callback) {
    
        $(node).fadeTo(300, value, callback);

    };
    
    var loadTextById = function (id) {
        var article = document.getElementById("articleId" + id),
            p = document.createElement("p"),
            moreTag = article.getElementsByTagName("a")[0],
            lessTag = document.createElement("a"),
            text = rpc.getTextById(id);
                    
                
        p.innerHTML = text;
        moreTag.setAttribute("style", "display:none;"); 
        article.appendChild(p);
        p.setAttribute("class", "fadeOut");
        p.setAttribute("id", "articleId" + id + "fulltext");
        lessTag.innerHTML = "weniger";
        article.appendChild(lessTag);

        var hideFct = function (id, p, lessTag, moreTag) {
            p.setAttribute("style", "opacity:0;");
            setTimeout(function() {
              p.parentNode.removeChild(p);
                lessTag.parentNode.removeChild(lessTag);
                moreTag.removeAttribute("style");
              }, 300);
        };

        $(lessTag).click(function () { hideFct(id, p, lessTag, moreTag); });
                
        var doTheRest = function () { p.removeAttribute("class"); };
                
        setTimeout(function () { doTheRest(); }, 100);
               
     };

    
    var addContent = function (content, showPreview, showTitle, showAuthor) {
        var articleTag = document.createElement("article"),
            headerTag = document.createElement("header"),
            hTag = document.createElement("h1"),
            imgTag = document.createElement("img"),
            pTag = document.createElement("p"),
            cTag = document.createElement("p"),
            moreTag = document.createElement("a"),
            sourceTag = document.createElement("p"),
            dateArray = content.date.split("-"),
            date = dateArray[2] + "." + dateArray[1] + "." + dateArray[0];
                            
        
        articleTag.setAttribute("id", "articleId" + content.id);                         
        articleTag.appendChild(headerTag);
        
        if (showTitle) {
            hTag.setAttribute("style", "margin-bottom:0.5em;");
            hTag.innerHTML = content.title;
            headerTag.appendChild(hTag);
        }
        
        if (showAuthor) {
            sourceTag.innerHTML = "Geschrieben am " + date + " von " + content.author;
            sourceTag.setAttribute("style", "color:gray;padding-top:0; margin-top:0;");        
            headerTag.appendChild(sourceTag);
        }
        
        if (content.image) {
            imgTag.setAttribute("src", content.image);
            headerTag.appendChild(imgTag);
        }

        pTag.setAttribute("id", "articleId" + content.id + "Preview");
        pTag.innerHTML = content.preview;
        headerTag.appendChild(pTag);
        
        if (showPreview) {
            moreTag.innerHTML = "mehr";             
            $(moreTag).click(function () { loadTextById(content.id); });
            headerTag.appendChild(moreTag);
        }
        if (!showPreview && content.text) {
            cTag.innerHTML = content.text;
            headerTag.appendChild(cTag);
        }
                     
        node.appendChild(articleTag);
        
    };
    
    init();
    
    that = {
        clear : function () {
            clearContent();
        },
        hide : function (callback) {
            setOpacity(0, (callback) ? callback.response : null);
        },
        show : function (callback) {
            setOpacity(1, (callback) ? callback.response : null);
        },
        addContent : function (content, showPreview) {
            addContent(content, (showPreview) ? showPreview : true, true, true);
        },
        addStatic : function (content) {
            addContent(content, false, false, false);
        }
    };
    
    return that;

};



