/**
 * blog.js
 *
 * Methods for handling blogs
 *
 * @author     Fabian C
 * @copyright  2007 Comet Information Systems Ltd, All Rights Reserved.
 * @requires   blog.js
 */


/*
 * Sort blogs by given sortType
 * @param {String} sortType - we will sort the blogs by it
 */
function listBlogsSortedBy(sortType, topicId, tagId) {

    if (!tagId)
        tagId = -1;

    new Ajax.Updater("blogs", "/execute/comp", {
        parameters: "method=listBlogs&params[]=" + topicId + "&params[]=" + tagId + "&params[]=" + sortType,
        method:"get"
    });
}

// todo doc
function sortList(elem) {
    var li = getParentNode(elem);
    var lis = gebi('blogsList').getElementsByTagName('li');

    for (var i = 0; i < lis.length; i++)
        lis[i].className = (lis[i] == li) ? 'selected' : '';
}

function logView(blogId){
    new Ajax.Request('/blog/index/logview/', {
        method:'post',
        parameters: "&blogId=" + blogId,
        onException:generalAJAXFailure,
        onFailure:generalAJAXFailure
    });
}

function logVote(blogId, entityType) {
    var data = Array();
    data.push("&blogId=");
    data.push(blogId);
    data.push("&entityType=");
    data.push(entityType);

    //    new Ajax.Updater("blogs", "/execute/comp", {
    //		method:"get",
    //        parameters: "method=listBlogs&params=" + params.join(','),
    //        onException:generalAJAXFailure,
    //        onFailure:generalAJAXFailure
    //    });


    new Ajax.Request('/blog/index/vote/', {
        method:'post',
        parameters: data.join(''),
        onException:generalAJAXFailure,
        onFailure:generalAJAXFailure,
        onSuccess: function(response) {
            var voted = response.responseText.evalJSON();
            var countEle = gebi("votesCount"+blogId);
            if (voted != "4") {
                countEle.innerHTML = parseInt(countEle.innerHTML) + 1;
            }
        }
    });

    try{
        switch (entityType){
            case 3: //blog
                gebi("blog_scopeit_" + blogId).style.display = "none";
                gebi("blog_scoped_" + blogId).style.display = "inline";
                gebi("votesCount"+blogId).className = "scoped";
                break;
        }
    }catch(e){}
}

function tagCloud(topicId, tagId) {
    if (!tagId) tagId = -1;

    var tagCont = gebi("tagCloudContainer");
    var tagContDiv = gebi("tagCloudDiv");
    var collapseTagBut = gebi("collapseTagBut");
    if (tagCont.innerHTML.indexOf("tagCloudDiv") == -1) {
        new Ajax.Updater("tagCloudContainer", "/blog/topic/tagCloud/", {
            parameters: ["&topic_id=", topicId,"&tag_id=", tagId].join(''),
            method:"get"
        });
        collapseTagBut.className = "topicTagOpen";
        collapseTagBut.innerHTML = "hide cloud";

    } else if (collapseTagBut.className == "topicTagCollapse") {
        //        tagContDiv.style.display = "none";
        collapseTagBut.className = "topicTagOpen";
        tagContDiv.style.display = "block";
        collapseTagBut.innerHTML = "hide cloud";
    } else {
        collapseTagBut.className = "topicTagCollapse";
        tagContDiv.style.display = "none";
        collapseTagBut.innerHTML = "show cloud";
    }
}

