YAHOO.namespace("kibet.container");

function initFilter () {
    var spMenu = new YAHOO.widget.Menu("spMenu");
    var tmMenu = new YAHOO.widget.Menu("tmMenu");
    var yaMenu = new YAHOO.widget.Menu("yaMenu");
	
    spMenu.subscribe("itemAdded", function (p_sType, p_aArgs) {
        var spMenuItem = p_aArgs[0];
        spMenuItem.subscribe("mouseover", attOver, 1);
        spMenuItem.subscribe("mouseout", attOut, 1);
        spMenuItem.subscribe("click", attClick, 0);
        spMenuItem.subscribe("click", insertElement, 1);
    });
	
    tmMenu.subscribe("itemAdded", function (p_sType, p_aArgs) {
        var tmMenuItem = p_aArgs[0];
        tmMenuItem.subscribe("mouseover", attOver, 1);
        tmMenuItem.subscribe("mouseout", attOut, 1);
        tmMenuItem.subscribe("click", attClick, 0);
        tmMenuItem.subscribe("click", insertElement, 1);
    });
	
    yaMenu.subscribe("itemAdded", function (p_sType, p_aArgs) {
        var yaMenuItem = p_aArgs[0];
        yaMenuItem.subscribe("click", attClick, 0);
        yaMenuItem.subscribe("click", insertElement, 1);
    });

    spMenu.addItems([
    {
        id: "sp44",
        text: "Englisch",
        value: "sp44"
    },

    {
        id: "sp42",
        text: "Franz&ouml;sisch",
        value: "sp42"
    },

    {
        id: "sp43",
        text: "Spanisch",
        value: "sp43"
    },

    {
        id: "sp45",
        text: "T&uuml;rkisch",
        value: "sp45"
    },

    {
        id: "sp47",
        text: "Sprachf&ouml;rderung",
        value: "sp47"
    },

    {
        id: "sp46",
        text: "Fr&uuml;hkindliche Bildung",
        value: "sp46"
    },
    ]);

    tmMenu.addItems([
    {
        id: "tm22",
        text: "Ganztagsbetreuung",
        value: "tm22"
    },

    {
        id: "tm27",
        text: "ganztags mit Mittagspause",
        value: "tm27"
    },

    {
        id: "tm25",
        text: "Kernzeitbetreuung",
        value: "tm25"
    },

    {
        id: "tm24",
        text: "verl&auml;ngerter Vormittag",
        value: "tm24"
    },

    {
        id: "tm23",
        text: "Vormittag",
        value: "tm23"
    },

    {
        id: "tm29",
        text: "stundenweise",
        value: "tm29"
    },
    ]);

    yaMenu.addItems([
    {
        id: "ya00",
        text: "0",
        value: "ya0"
    },

{
        id: "ya01",
        text: "1",
        value: "ya1"
    },

    {
        id: "ya02",
        text: "2",
        value: "ya2"
    },

    {
        id: "ya03",
        text: "3",
        value: "ya3"
    },

    {
        id: "ya04",
        text: "4",
        value: "ya4"
    },

    {
        id: "ya05",
        text: "5",
        value: "ya5"
    },

    {
        id: "ya06",
        text: "6",
        value: "ya6"
    },

    {
        id: "ya07",
        text: "7",
        value: "ya7"
    },

    {
        id: "ya08",
        text: "8",
        value: "ya8"
    },

    {
        id: "ya09",
        text: "9",
        value: "ya9"
    },

    {
        id: "ya10",
        text: "10",
        value: "ya10"
    },

    {
        id: "ya11",
        text: "11",
        value: "ya11"
    },

    {
        id: "ya12",
        text: "12",
        value: "ya12"
    },

    {
        id: "ya13",
        text: "13",
        value: "ya13"
    },

    {
        id: "ya14",
        text: "14",
        value: "ya14"
    },

    {
        id: "ya15",
        text: "15",
        value: "ya15"
    },

    {
        id: "ya16",
        text: "16",
        value: "ya16"
    },

    {
        id: "ya17",
        text: "17",
        value: "ya17"
    },

    {
        id: "ya18",
        text: "18",
        value: "ya18"
    },
    ]);

    spMenu.render("spContainer");
    tmMenu.render("tmContainer");
    yaMenu.render("yaContainer");

    function insertElement(p_sType, p_aArgs) {
        var oDOMEvent = p_aArgs[0];

        if(this.id != "sp0" && this.id != "tm0" && this.id != "ya0"){
            myCat = this.id.substr(0,2);
			
            if(selected[myCat].length <= 1 || ($('yaTarget').childElements().length <= 1 && myCat == "ya")) {
                str_spacer = '<div id="' + myCat + 'Spacer">&nbsp;</div>';
                $(myCat + 'Target').insert({
                    top: str_spacer
                });
            }
			
            if($('yaTarget').childElements().length >= 2 && myCat == "ya") {
                lastLink = $('yaTarget').childElements()[1].id.substr(0,4);
                $(lastLink+'Link').remove();
                $(lastLink).show();
            }

            linkText = $(this.id).firstDescendant().innerHTML;

            str_a = '<a id="' + this.id + 'Link" class="navitem" href="javascript:void(0);">' + linkText + '</a>';
            $(myCat + 'Target').insert({
                bottom: str_a
            });
			
            if(myCat != "ya") {
                Event.observe(this.id + 'Link', 'mouseover', attOver.bindAsEventListener(this));
                Event.observe(this.id + 'Link', 'mouseout', attOut.bindAsEventListener(this));
            }
            Event.observe(this.id + 'Link', 'click', attClick.bindAsEventListener(this));
            Event.observe(this.id + 'Link', 'click', removeElement.bindAsEventListener(this));
			
            $(this.id).hide();
        }
    }

    function removeElement(){
        myCat = this.id.substr(0,2);
        selected["ya"] = "";

        if($(myCat+'Target').childElements().length <= 2) {
            $(myCat+'Spacer').remove();
        }

        $(this.id+'Link').remove();
        $(this.id).show();
        $('selectionInstitution_legend').hide();
    }

    var spMenuButton = new YAHOO.widget.Button( {
        type: "menu",
        label: "Bitte w&auml;hlen",
        name: "spButton",
        menu: spMenu,
        container: "spContainer",
        title: "",
        id  : "yui-sp-button"
    });
    var tmMenuButton = new YAHOO.widget.Button( {
        type: "menu",
        label: "Bitte w&auml;hlen",
        name: "tmButton",
        menu: tmMenu,
        container: "tmContainer",
        title: "",
        id  : "yui-tm-button"
    });
    var yaMenuButton = new YAHOO.widget.Button( {
        type: "menu",
        label: "Bitte w&auml;hlen",
        name: "yaButton",
        menu: yaMenu,
        container: "yaContainer",
        title: "",
        id  : "yui-ya-button"
    });

    YAHOO.kibet.container.menumodule = new YAHOO.widget.Module("spModule", {
        visible:true
    } );
    YAHOO.kibet.container.menumodule = new YAHOO.widget.Module("tmModule", {
        visible:true
    } );
    YAHOO.kibet.container.menumodule = new YAHOO.widget.Module("yaModule", {
        visible:true
    } );
}

function clearFilter(){
    catArr = new Array('sp','tm','ya');
    for(var j=0;j<catArr.length;j++){
        var cat = catArr[j];
        var eArr = $(cat+'Target').childElements();
        for(var i=0;i<eArr.length;i++){
            e = eArr[i].id;
            if(e.indexOf("Spacer") > 0) {
                $(e).remove();
            } else {
                $(e).remove();
                $(e.replace(/Link/,'')).show();
            }
        }
        selected[cat] = new Array();
    }
    process();
}

