var Bericht = Class.create();
Bericht.prototype = {
    initialize: function(){
        this.filterTypeSet = false;
        this.filterCatSet = false;
        this.filterLikeSet = false;
        this.singleFilter = false;
        this.hop = 1;
        this.buildFilterMenu();
        this.obj = {};
        this.tagsToMessage = [];
        this.previous = false;
        this.previousMenu = "menu_2";
        this.boxId = 0;
        this.Boxdrop = 0;
        this.boxTypeId = 1;
        this.sliders = [];
        this.d = new Element("div", {
            id: "dragDiv"
        }).addClassName("sleepIcoon").hide();
        document.body.appendChild(this.d);
        var func1 = this.sleepIcoon.bind(this);
        var func2 = this.verbergIcoon.bind(this);
        this.dragDiv = new Draggable("dragDiv", {
            revert: false,
            onStart: func1,
            onEnd: func2
        });
        this.objQuery = document.location.search.toQueryParams();
    },
    loadFirstMessages: function(method, id){
        this.lastMethod = 'retrieveMyMessages';
        var func = this.buildMessages.bind(this);
        var obj = {};
        obj.newType = 'Bericht';
        obj.newMethod = 'retrieveMyMessages';
        ARRM.jsonRequest(obj, func);
    },
    buildFilterMenu: function(){
        if ($("filterWrap")) {
            var option = new Element("option", {
                value: "0"
            }).update("alle categorie&euml;n");
            var func = this.filterCat.bind(this);
            $('selectCat').insert(option).observe('change', func, false).setStyle("vertical-align: middle;");
            option = new Element("option", {
                value: ""
            }).update("alle typen");
            func = this.filterType.bind(this);
            $('selectType').update(option).observe('change', func, false).setStyle("vertical-align: middle;");
            var funcTwo = this.searchLike.bind(this);
            var input = new Element("input", {
                id: "inputTerm",
                type: "text",
                value: ""
            }).setStyle("width: 150px; vertical-align: middle;").observe('keypress', funcTwo, true);
            $("filterWrap").insert(input);
            funcTwo = this.searchLikeButton.bind(this);
            var img = new Element("img", {
                src: "/img/btns/btnOKXS.gif"
            }).setStyle("border: none; vertical-align: middle;");
            var a = new Element("a", {
                href: "javascript:void(0);"
            }).update(img).observe('click', funcTwo, true).setStyle("vertical-align: middle;");
            $("filterWrap").insert(a);
        }
    },
    searchLikeButton: function(event){
        this.filterLikeWord();
    },
    searchLike: function(event){
        if (event.keyCode == 13) {
            this.filterLikeWord();
        }
    },
    retrieveFilterOptions: function(){
        var func = this.buildFilterOptions.bind(this);
        var obj = {};
        obj.newType = 'Bericht';
        obj.newMethod = 'haalFilterOpties';
        ARRM.jsonRequest(obj, func);
    },
    buildFilterOptions: function(obj){
        if (obj.categorie) {
            var option = new Element("option", {
                value: "0"
            }).update("alle categorie&euml;n");
            $('selectCat').update(option);
            for (var x = 0, len = obj.categorie.length; x < len; ++x) {
                option = new Element("option", {
                    value: obj.categorie[x].id
                }).update(obj.categorie[x].naam);
                $('selectCat').insert(option);
            }
        }
        if (obj.type) {
            option = new Element("option", {
                value: ""
            }).update("alle typen");
            $('selectType').update(option);
            for (x = 0, len = obj.type.length; x < len; ++x) {
                option = new Element("option", {
                    value: obj.type[x].id
                }).update(obj.type[x].naam);
                $('selectType').insert(option);
            }
        }
        $("inputTerm").value = "";
        this.filterObj = obj;
    },
    filterLikeWord: function(){
        this.filterLikeSet = true;
        if (this.singleFilter) {
            this.filterCatSet = false;
            this.filterTypeSet = false;
        }
        this.filterLike = $F('inputTerm').stripScripts().stripTags();
        this.retrieveMessages(this.boxTypeId, this.changeId);
    },
    filterCat: function(){
        this.filterCatSet = true;
        if (this.singleFilter) {
            this.filterTypeSet = false;
            this.filterLikeSet = false;
        }
        this.filterCat = $F('selectCat');
        this.retrieveMessages(this.boxTypeId, this.changeId);
    },
    filterType: function(){
        this.filterTypeSet = true;
        if (this.singleFilter) {
            this.filterCatSet = false;
            this.filterLikeSet = false;
        }
        this.filterType = $F('selectType');
        this.retrieveMessages(this.boxTypeId, this.changeId);
    },
    firstLoadHome: function(template_id, categorie_id, targetdiv, sort_type, id, obj){
        this.changeMenuState(id);
        this.boxId = 0;
        this.sort_type = sort_type;
        this.targetdiv = targetdiv;
        this.lastMethod = 'retrieveHomeMessages';
        if (!template_id) {
            this.buildMessages(obj);
            template_id = 0;
        }
        else {
            this.buildHomeEvents(obj);
        }
    },
    retrieveHomeMessages: function(template_id, categorie_id, targetdiv, sort_type, id){
        this.changeMenuState(id);
        this.boxId = 0;
        this.sort_type = sort_type;
        this.targetdiv = targetdiv;
        this.lastMethod = 'retrieveHomeMessages';
        if (!template_id) {
            var func = this.buildMessages.bind(this);
            template_id = 0;
        }
        else {
            func = this.buildHomeEvents.bind(this);
        }
        var obj = {};
        obj.newType = 'Bericht';
        obj.newMethod = 'retrieveHomeMessages';
        obj.sort_type = sort_type;
        obj.template_id = template_id;
        obj.categorie_id = categorie_id;
        obj.targetdiv = targetdiv;
        ARRM.jsonRequest(obj, func);
    },
    updateMessageCount: function(val) {
        obj = document.getElementById("priveberichtenteller");
        obj.innerHTML = "(" + val + ")";
        if (val > 0) {
            obj = document.getElementById("priveberichtenlink");
            obj.style.fontWeight = "bold";
        }
    },
    retrieveMessageCount: function(box_type_id){
        var obj = {};
        var func = this.updateMessageCount.bind(this);
        obj.newType = 'Bericht';
        obj.newMethod = 'retrieveMessageCount';
        obj.box_type_id = box_type_id;        
        ARRM.jsonRequest(obj, func);
    },
    retrieveMessages: function(box_type_id, id, page_id){
        var obj = {};
        var x;
        if (this.changeId != id) {
            this.buildFilterOptions(this.filterObj);
        }
        else {
            if (this.filterCatSet) {
                if (this.filterCat !== "") {
                    obj.filterCat = this.filterCat;
                    x = 1;
                }
            }
            if (this.filterTypeSet) {
                if (this.filterType !== "") {
                    obj.filterType = this.filterType;
                    x = 1;
                }
            }
            if (this.filterLikeSet) {
                if (this.filterLike !== "") {
                    obj.filterLike = $F("inputTerm");
                    x = 1;
                }
            }
            if (MC.boxActive && box_type_id > 4) {
                obj.box_id = MC.boxActive;
            }
        }
        this.changeId = id;
        if (x > 0) {
        }
        else {
            this.changeMenuState(id);
        }
        this.boxId = 0;
        this.boxTypeId = box_type_id;
        this.lastMethod = 'retrieveMyMessages';
        var func = this.buildMessages.bind(this);
        obj.newType = 'Bericht';
        obj.newMethod = 'retrieveMyMessages';
        obj.box_type_id = box_type_id;
        obj.targetdiv = 0;
        ARRM.jsonRequest(obj, func);
    },
    retrievePage: function(page_id){
        var func = this.buildMessages.bind(this);
        var obj = {};
        if (this.filterCatSet) {
            if (this.filterCat !== "") {
                obj.filterCat = this.filterCat;
            }
        }
        if (this.filterTypeSet) {
            if (this.filterType !== "") {
                obj.filterType = this.filterType;
            }
        }
        if (this.filterLikeSet) {
            if (this.filterLike !== "") {
                obj.filterLike = $F("inputTerm");
            }
        }
        obj.newType = 'Bericht';
        obj.newMethod = this.lastMethod;
        obj.page_id = page_id;
        obj.box_id = this.boxId;
        obj.box_type_id = this.boxTypeId;
        ARRM.jsonRequest(obj, func);
    },
    observeClick: function(id, targetdiv){
        if (this.hop == 1) {

            this.sliders[targetdiv].slide(targetdiv + "_bericht_" + id + "", id);
        }
        else 
            if (this.hop == 2) {
            }
    },
    rateBericht: function(objBericht, doAction){
        this.changehop();
        if (this.userstatus == 1) {
            this.floatDiv = new Div(tempY, tempX, true);
            $('floatDiv').innerHTML = "<div class=\"popup\"><div class=\"popupHeader\"><a href=\"javascript:void(0);\" onclick=\"MC.message.floatDiv.close();\"><img src=\"/img/btnPopupClose.gif\" alt=\"Sluiten\" /></a><h3>Niet ingelogd</h3></div><div class=\"popupBody\">U bent niet ingelogd, log in om dit bericht te kunnen raten.<div class=\"spacer\"></div></div></div>";
        }
        else {
            var func = this.maakBerichtPopup.bind(this);
            var obj = {};
            obj.newType = 'Bericht';
            obj.newMethod = 'RateBericht';
            obj.id = objBericht.id;
            obj.doAction = doAction;
            ARRM.jsonRequest(obj, func);
        }
    },
    addUpRating: function(obj){
        if ($("rating-" + this.ratingId)) {
            this.enablehop();
            $("rating-" + this.ratingId).update(this.nieuweRating);
            var func = this.floatDiv.close.bind(this);
            obj = $("rateBerichtForm").serialize(true);
            obj.newType = 'Bericht';
            obj.newMethod = 'RateBerichtTags';
            ARRM.jsonFormRequest(obj, func);
        }
        else {
            this.floatDiv.close();
            this.enablehop();
        }
    },
    rateBerichtDetail: function(id, doAction){
        this.enablehop();
        var func = this.maakBerichtPopup.bind(this);
        var obj = {};
        obj.newType = 'Bericht';
        obj.newMethod = 'RateBericht';
        obj.id = id;
        obj.doAction = doAction;
        ARRM.jsonRequest(obj, func);
    },
    rateBerichtTags: function(formObj){
        var func = this.sluitPopup.bind(this);
        $("rating-" + this.ratingId).update(this.nieuweRating);
        var obj = {};
        obj = $("rateBerichtForm").serialize(true);
        obj.newType = 'Bericht';
        obj.newMethod = 'RateBerichtTags';
        ARRM.jsonFormRequest(obj, func);
    },
    sluitPopup: function(obj){
        this.enablehop();
        this.floatDiv.close();
        MC.buildTagCloud(obj);
    },
    maakBerichtPopup: function(obj){
        this.floatDivs = 1;
        this.ratingId = obj.id;
        this.nieuweRating = obj.nieuweRating;
        this.floatDiv = new Div(tempY, tempX, true);
        var img = new Element("img", {
            src: "/img/btnPopupClose.gif"
        });
        var func = this.addUpRating.bind(this);
        var a = new Element("a", {
            href: "javascript:void(0);"
        }).insert(img).observe('click', func, true);
        var div = new Element("div").addClassName("popupHeader").insert(a);
        var h = new Element("h3").insert(obj.titel);
        div.insert(h);
        this.rateDiv = new Element("div").addClassName("popup").insert(div);
        if (obj.status === 1) {
            div = new Element("div").addClassName("popupBody").update("<form id=\"rateBerichtForm\" onsubmit=\"MC.message.rateBerichtTags(this); return false;\">" + obj.nietIngelogd + "</form><div class=\"spacer\"></div>");
        }
        else {
            div = new Element("div").addClassName("popupBody").update("<form id=\"rateBerichtForm\" onsubmit=\"MC.message.rateBerichtTags(this); return false;\">" + obj.output + "</form><div class=\"spacer\"></div>");
        }
        this.rateDiv.insert(div);
        $('floatDiv').update(this.rateDiv);
    },
    buildMessages: function(obj){
        this.userstatus = obj.userStatus;
        if (!obj.targetdiv) {
            obj.targetdiv = 'messageWrap';
        }
        this.targetDiv = new Element("div");
        this.sliders[obj.targetdiv] = new Accordion("", 0.3);
        this.sliders[obj.targetdiv].accord = [];
        if (obj.total > 0) {
            for (var x = 0, len = obj.parameters.length; x < len; ++x) {
                var maindiv = new Element("div", {
                    id: obj.parameters[x].id
                }).addClassName('bericht');
                if (obj.parameters[x].gelezen == 1) {
                    maindiv.addClassName("gelezen");
                }
                if (obj.parameters[x].commercieel == 1) {
                    maindiv.addClassName("ad");
                }
                var func = this.observeClick.bind(this, obj.parameters[x].id, obj.targetdiv);
                var div = new Element("div");
                maindiv.observe('mouseup', func, false);
                if (obj.userStatus === 0) {
                    if (obj.pageType == "home") {
                    }
                    else {
                        var img = new Element("img", {
                            src: "/img/btnVerwijderen.gif"
                        }).addClassName('berichtVerwijder');
                        func = this.showConfirm.bind(this, obj.parameters[x].id, this.boxTypeId, this.boxId);
                        var a = new Element('a', {
                            href: "javascript: void(0);"
                        }).insert(img).observe('mousedown', func, false);
                        div.insert(a);
                    }
                }
		//jon hier avatar er bij zetten?
                if (obj.parameters[x].image !== null) {
                    var imgDiv = new Element("div").addClassName('berichtImage').setStyle("background: url(/images/" + obj.parameters[x].image + "-t);background-position: center center;");
                    div.insert(imgDiv);
                } else {
                    var imgDiv = new Element("div").addClassName('berichtImage').setStyle("background: url(" + obj.parameters[x].avatar + ");background-position: center center;");
                    div.insert(imgDiv);
		}
                if (obj.parameters[x].categorie_id >= 0) {
                    var newDiv = new Element("div").addClassName('berichtIcoon');
                    newDiv.addClassName('cat-' + obj.parameters[x].categorie_id);
                    div.insert(newDiv);
                }
                if (obj.pageType == "home") {
                }
                else {
                    maindiv.insert(div).observe('mousedown', this.initIcoon.bind(this, newDiv, obj.parameters[x].id), false);
                }
                div.insert("<strong>" + obj.parameters[x].templatenaam + ":</strong> " + obj.parameters[x].datum + " erbij gezet door <a href=\"" + obj.parameters[x].profiellink + "\" title=\"" + obj.parameters[x].gebruikersnaam + "\" class=\"profile\">" + obj.parameters[x].gebruikersnaam + "</a>");
                if (obj.parameters[x].persoonlijk) {
                    div.insert(obj.parameters[x].persoonlijk);
                }
                div.insert("<h2>" + obj.parameters[x].titel + "</h2>");
                maindiv.insert(div);
                div = new Element("div", {
                    id: obj.targetdiv + "_bericht_" + obj.parameters[x].id
                }).addClassName("berichtBody");
                img = new Element("img", {
                    src: "/img/ratingMin.gif"
                }).observe('mousedown', this.rateBericht.bind(this, maindiv, 2), false);
                newDiv = new Element("div").insert("<div class=\"ratingScore\" id=\"rating-" + obj.parameters[x].id + "\">" + obj.parameters[x].rating + "</div>").insert(img);
                img = new Element("img", {
                    src: "/img/ratingPlus.gif"
                }).observe('mousedown', this.rateBericht.bind(this, maindiv, 1), false);
                newDiv.insert(img).addClassName("rating");
                div.insert(newDiv);
                div.insert(obj.parameters[x].inhoud);
                div.insert("<ul class=\"meta\"><li>bekeken: " + obj.parameters[x].bekeken + "</li><li>reacties: " + obj.parameters[x].reactiecount + "</li></ul>");
                div.setStyle("display: none;");
                maindiv.insert(div);
                div = new Element("div", {
                    id: "leesverder_" + obj.parameters[x].id + ""
                }).setStyle({
                    display: "none"
                });
                maindiv.insert(div);
                div = new Element("div").addClassName("spacer");
                maindiv.insert(div);
                maindiv.onselectstart = function(){
                    return (false);
                };
                this.targetDiv.insert(maindiv);
                func = this.changehop.bind(this);
                var hop = this.enablehop.bind(this);
                this.sliders[obj.targetdiv].accord.push(obj.targetdiv +'_bericht_' + obj.parameters[x].id);
            }
            $(obj.targetdiv).update(this.targetDiv);
            if (obj.totalpages > 1) {
                div = new Element("div").addClassName("spacer");
                maindiv = new Element("div").addClassName('paginering').insert(div);
                if (obj.page_id > 1) {
                    img = new Element("img", {
                        src: "/img/paginasBegin.jpg"
                    });
                    a = new Element('a', {
                        href: "javascript: void(0);"
                    }).insert(img);
                    func = this.retrievePage.bind(this, 1);
                    a.observe('click', func, false);
                    maindiv.insert(a);
                    img = new Element("img", {
                        src: "/img/paginasVorige.jpg"
                    });
                    a = new Element('a', {
                        href: "javascript: void(0);"
                    }).insert(img);
                    func = this.retrievePage.bind(this, Number(obj.page_id) - 1);
                    a.observe('click', func, false);
                    maindiv.insert(a);
                }
                var pageCount = 1;
                if (obj.totalpages > 11) {
                    if (obj.page_id > 5) {
                        if (obj.page_id > (obj.totalpages - 6)) {
                            pageCount = obj.totalpages - 10;
                        }
                        else {
                            pageCount = obj.page_id - 5;
                        }
                    }
                }
                if (pageCount > 1) {
                    maindiv.insert(' ...');
                }
                if (pageCount + 10 > obj.totalpages) {
                    pagecountEnd = obj.totalpages;
                }
                else {
                    pagecountEnd = pageCount + 10;
                }
                for (i = pageCount; i <= pagecountEnd; i++) {
                    if (i == obj.page_id) {
                        maindiv.insert('<span>' + i + '</span>');
                    }
                    else {
                        a = new Element('a', {
                            href: "javascript: void(0);"
                        }).insert(i).addClassName("page");
                        func = this.retrievePage.bind(this, i);
                        a.observe('click', func, false);
                        maindiv.insert(a);
                    }
                }
                if (pageCount + 10 < obj.totalpages) {
                    maindiv.insert('... ');
                }
                if (obj.page_id < obj.totalpages) {
                    img = new Element("img", {
                        src: "/img/paginasVolgende.jpg"
                    });
                    a = new Element('a', {
                        href: "javascript: void(0);"
                    }).insert(img);
                    func = this.retrievePage.bind(this, Number(obj.page_id) + 1);
                    a.observe('click', func, false);
                    maindiv.insert(a);
                    img = new Element("img", {
                        src: "/img/paginasEind.jpg"
                    });
                    a = new Element('a', {
                        href: "javascript: void(0);"
                    }).insert(img);
                    func = this.retrievePage.bind(this, obj.totalpages);
                    a.observe('click', func, false);
                    maindiv.insert(a);
                }
                $(obj.targetdiv).insert(maindiv);
            }
        }
        else {
            if (obj.pageType == "home") {
                $(obj.targetdiv).update("<div class=\"padded\">" + NF.notificatieObj[124] + "</div>");
            }
            else {
                $(obj.targetdiv).update("<div class=\"padded\">" + NF.notificatieObj[60] + "</div>");
            }
        }
    },
    initIcoon: function(obj, id){
        $("dragDiv").update(obj.cloneNode(true));
        $("dragDiv").name = id;
        Draggables.activate(this.dragDiv);
    },
    sleepIcoon: function(){
        this.changehop();
        var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
        var dsoctop = document.all ? iebody.scrollTop : pageYOffset;
        if (dsoctop > 285) {
            ss.smoothScroll();
        }
        $("dragDiv").show();
        this.dragDiv.offset = [33, 25];
        $("dragDiv").setStyle("z-index: 201; position: absolute; top: " + tempY + "px; left: " + tempX + "px;");
        Draggables.activate(this.dragDiv);
    },
    verbergIcoon: function(obj){
        this.enablehop();
        $("dragDiv").hide();
    },
    buildHomeEvents: function(obj){
        for (var x = 0, len = obj.parameters.length; x < len; ++x) {
            var maindiv = new Element("div");
            maindiv.addClassName("box");
            maindiv.addClassName("agenda");
            var header = new Element("div").addClassName("header");
            var door = new Element("div").insert('door: ' + obj.parameters[x].gebruikersnaam);
            var body = new Element("div").addClassName("body").insert(door);
            body.insert('<h4>' + obj.parameters[x].titel + '</h4>' + obj.parameters[x].inhoud);
            var date = new Element("div").addClassName("date");
            var month = new Element("span").insert(obj.parameters[x].monthAgenda);
            var day = new Element("div").insert(obj.parameters[x].dayAgenda);
            date.insert(month);
            date.insert(day);
            var footer = new Element("div").addClassName("footer");
            header.insert(body);
            header.insert(date);
            header.insert(footer);
            maindiv.insert(header);
            $("agenda").insert(maindiv);
        }
    },
    changehop: function(){
        this.hop = 2;
    },
    enablehop: function(){
        this.hop = 1;
    },
    saveMessage: function(){
        var obj = {};
        obj = $('add_message').serialize(true);
        var inst = tinyMCE.getInstanceById('inhoud');
        obj.inhoud = inst.getHTML();
        inst = tinyMCE.getInstanceById('ingredienten');
        obj.ingredienten = inst.getHTML();
        inst = tinyMCE.getInstanceById('bereidingswijze');
        obj.bereidingswijze = inst.getHTML();
        inst = tinyMCE.getInstanceById('bijzonderheden');
        obj.bijzonderheden = inst.getHTML();
        obj.type = 'Request';
        obj.method = 'SaveMessage';
        obj.returnType = 'check';
        obj.returnMethod = 'berichtOpgeslagen';
        ARRM.jsonRequest(obj);
    },
    berichtOpgeslagen: function(){
        document.location.href = "/mijn/";
    },
    showConfirm: function(id, boxTypeId, boxId){
        this.changehop();
        var locX = tempX - 200;
        this.floatDiv = new Div(tempY, locX, true);
        var popup = new Element("div").addClassName("popup");
        var popupHeader = new Element("div").addClassName("popupHeader");
        var imgHolder = new Element("img", {
            src: "/img/btnPopupClose.gif",
            alt: "Sluiten"
        });
        var func = this.floatDiv.close.bind(this);
        var a = new Element("a", {
            href: "javascript:void(0);"
        }).observe('click', func, false).insert(imgHolder);
        popupHeader.insert(a);
        popupHeader.insert("<h3>Item Verwijderen</h3>");
        popup.insert(popupHeader);
        popupBody = new Element("div").insert(NF.notificatieObj[59]).addClassName('popupBody');
        func = this.confirmRemove.bind(this, id, boxTypeId, boxId, true);
        var inp = new Element("input", {
            type: "image",
            src: "/img/btns/btnJaS.gif",
            name: "sbmt"
        }).addClassName("imgBtn1").observe('click', func, false);
        func = this.floatDiv.close.bind(this);
        var inpNo = new Element("input", {
            type: "image",
            src: "/img/btns/btnNeeS.gif",
            name: "sbmtNo"
        }).addClassName("imgBtn3").observe('click', func, false);
        popupBody.insert(inp);
        popupBody.insert(inpNo);
        var divReturn = new Element("div", {
            id: "returnM"
        });
        popup.insert(popupBody);
        popup.insert(divReturn);
        $('floatDiv').update(popup);
        this.enablehop();
    },
    confirmRemove: function(id, boxTypeId, boxId, response){
        if (response) {
            this.verwijderBericht(id, boxTypeId, boxId);
            this.floatDiv.close();
        }
    },
    verwijderBericht: function(id, boxTypeId, boxId){
        var func = this.buildNaVerwijder.bind(this);
        var sendObj = {};
        if (this.trash === 1) {
            sendObj.no_trash = 1;
            this.trash = 2;
        }
        sendObj.box_id = boxId;
        sendObj.type_id = boxTypeId;
        sendObj.bericht_id = id;
        sendObj.box_type_id = boxTypeId;
        sendObj.newType = 'Box';
        sendObj.newMethod = 'VerwijderBericht';
        ARRM.jsonRequest(sendObj, func);
    },
    buildNaVerwijder: function(obj){
        this.buildMessages(obj);
        MC.buildBox(obj);
    },
    verwijderBerichtNaDrop: function(id, boxTypeId, boxId, trash){
        if (this.floatDivs === 1) {
            this.floatDiv.close();
            this.floatDivs = 2;
        }
        this.MessageId = id;
        this.BoxTypeId = boxTypeId;
        this.BoxId = boxId;
        this.trash = trash;
        this.verwijderBericht(id, boxTypeId, boxId);
    },
    changeActiveState: function(id){
        if (this.previous > 0) {
            if ($(this.previous)) {
                $(this.previous).removeClassName("active");
            }
        }
        if (this.previous != id) {
            if ($(id)) {
                $(id).addClassName("active");
            }
            this.previous = id;
        }
        else {
            this.previous = 0;
        }
    },
    changeMenuState: function(id){
        if (id) {
            $(id).ancestors()[0].descendants().each(function(item){
                if (item.hasClassName('active')) {
                    item.className = "inactive";
                }
            });
            if (id != "menu_5") {
                if ($('menu_1')) {
                    $('menu_1').replace("<li id=\"menu_1\">&gt; <a href=\"javascript:void(0);\" onclick=\"MC.message.retrieveMessages('1', 'menu_1');\" title=\"Inbox\">inbox</a></li>");
                }
                if ($('menu_3')) {
                    $('menu_3').replace("<li id=\"menu_3\">&gt; <a href=\"javascript:void(0);\" onclick=\"MC.message.retrieveMessages('3', 'menu_3');\" title=\"Verzonden\">verzonden</a></li>");
                }
                $(id).className = "active";
            }
        }
    },
    updateBox: function(berichtId){
        reqObj = {};
        reqObj.type = 'Request';
        reqObj.method = 'addMessage';
        reqObj.returnType = 'check';
        reqObj.returnMethod = 'buildMessages';
        reqObj.berichtId = berichtId;
        ARRM.jsonRequest(reqObj);
    },
    thumbs: function(berichtId, motion){
        this.tagsToMessage = [];
        if (motion == "up") {
            this.motion = 1;
        }
        else 
            if (motion == "down") {
                this.motion = 2;
            }
            else {
                this.motion = 3;
                alert('niet cheaten!');
            }
        var obj = {};
        obj.returnType = 'check';
        obj.returnMethod = 'none';
        obj.berichtId = berichtId;
        obj.motion = this.motion;
        obj.newType = 'Bericht';
        obj.newMethod = 'setThumbs';
        obj.message = "update geslaagd";
        ARRM.jsonRequest(obj);
        this.getRelatedMessageTags(berichtId);
    },
    getRelatedMessageTags: function(berichtId){
        $('tagsMessage').update().show();
        for (var x = 0; x < 5; x++) {
            var div = new Element("div", {
                onclick: 'check.addTagsToMessage(this.id);',
                id: "tagsMessage_" + x + ""
            }).insert("Value" + x + "");
            $('tagsMessage').insert(div);
        }
        var a = new Element("a", {
            href: "javascript: void(0);",
            onclick: "check.savePersonalisation();"
        }).insert('Wijzigingen opslaan');
        div = new Element("div", {
            id: "savePersonalisation"
        }).insert(a).setStyle("margin-top: 10px; width: 400px; float: left; cursor:pointer;");
        $('tagsMessage').insert(div);
    },
    addTagsToMessage: function(id){
        id = id.replace(/tagsMessage_/, "");
        if (this.itemExists(id)) {
            this.tagsToMessage.push(id);
        }
        else {
            alert('item al aanwezig');
        }
    },
    savePersonalisation: function(){
        alert(this.tagsToMessage);
    },
    itemExists: function(id){
        for (var i = 0, len = this.tagsToMessage.length; i < len; ++i) {
            if (this.tagsToMessage[i] == id) {
                return false;
            }
        }
        return true;
    }
};
