﻿function BindTagMethods(id) {


        if ($(".tags").hasClass("active") == false){
            $(".tags").css("display", "none");
        }
        $(".newtag").css("display", "none");


              
        $(".hovereffect").hover(function(){
            $(this).addClass("bg_hover");
          },function(){
            $(this).removeClass("bg_hover");
        });
        
        $(".tag_click").hover(function(){
            $(this).children().addClass("tag_hover");
            $(this).children().children().addClass("tag_right_hover");
        },function(){
            $(this).find(".tag").removeClass("tag_hover");
            $(this).find(".tag_right").removeClass("tag_right_hover");
        });


        $(".tag_click").click(function() {

            var TagName = $(this).find(".tag").text();

            if (!FilterTagInArray(TagName)) {
                $(".tags").css("display", "block");
                $(".tags").addClass("active");
                $(".newtag").css("display", "block");
                widthcontent = $(this).find(".tag").outerWidth();
                $(".tag_transfer").css("width", widthcontent);
            
                
                if (AllowSmallAnimations == 1){
                    $(this).find(".tag").effect("transfer", { to: $(".tag_transfer") }, 500,
                        function() {
                            //Animatie callback
                            addFilterTag(TagName);
                            rebindInfoContainer(id);
                        });
                }
                else{
                     addFilterTag(TagName);
                     rebindInfoContainer(id);                     
                }

                
                if ($(".autosuggest_container").hasClass("active") == true ){
                    $(".autosuggest_container").removeClass("active"); 
                    $(".autosuggest_container").animate({
                        height: "0px",
                        paddingTop: "0px",
                        paddingBottom: "0px"         
                     }, 500, function(){$(".autosuggest_container").css("display", "none")});
                }
                var iswat = "Tag";
                setTimeout('MakeTag(\'' + TagName + '\', \'' + id + '\', \'' + iswat + '\')', 500);

                //alert("toevoegen: " + GetHtml);

                //Tag toevoegen aan array


                //alert("Array is nu: " + FilterTagInArray());
            }
            
            //Om te voorkomen dat de rowclick erachter vuurt. 
            return false;
        });
        

    }

    function MakeTag(Content, id, from) {
        MakeTag(Content, id, from, true);
    }
    
    function MakeTag(Content, id, from, animation) {
        if (AllowSmallAnimations == 0 || !animation){
            $(".tags").css("display", "block");
            $(".tags").addClass("active");
            
        }
        
        //alert(Content);
        GetNieuwTag = $(".tags").html();
        //alert(GetNieuwTag);
        MakeHTML = '' + GetNieuwTag + '<div class="fade needtofade '+ from + '" style="display:none"><div class="tag">' + Content + '<div class="tag_right_cross"></div></div></div>';
        $(".tags").html(MakeHTML);
        if (AllowFades == 1){
            $(".needtofade").fadeIn(1000);
        }
        else{
             $(".needtofade").css("display", "block");
        }        
        $(".needtofade").removeClass("needtofade");

        $(".tag_right_cross").click(function() {
            $(this).parent().parent().addClass("removed");

            var TagName = $(this).parent().parent().find(".tag").text();
             if ($(this).parent().parent().hasClass("Tag") == true){
                removeFilterTag(TagName);
            }
            if ($(this).parent().parent().hasClass("Keyword") == true){
                removeFilterKeyword(TagName);
            }

            rebindInfoContainer(id);
            if (AllowFades == 1 && animation){
                $(this).parent().parent().fadeOut(1000, function() {
   
                var aantalActives = $(".tags > .fade").size()
                var aantalRemoved = $(".tags > .removed").size()
                
                
                if (aantalActives == aantalRemoved) {                    
                        $(".tags").fadeOut();
                        $(".tags").removeClass("active");
                    //$(".tags").css("display", "none");
                }


                //if ($(".tags").find(".fade").text() == ""){
                //    $(".tags").css("display", "none");
                // }

                //alert("Verwijderen: " + GetHtml);
                });
            }
            else{
                $(this).parent().parent().css("display", "none");
                
                var aantalActives = $(".tags > .fade").size()
                var aantalRemoved = $(".tags > .removed").size()

                if (aantalActives == aantalRemoved) {
                    if (AllowFades == 1 && animation){                                     
                        $(".tags").fadeOut();
                        $(".tags").removeClass("active");
                    }
                    else{                    
                        $(".tags").css("display", "none");
                        $(".tags").removeClass("active");
                    }
                }
            }

            //alert("nieuwe tagfilter: " + getFilterTags());

            //$(this).closest(".blok_container").find(".header").effect("highlight", {color: "#a9a9a9"}, 3000);
        });
    }

    var FilterTags = new Array();


    function FilterTagInArray(tagid) {
        var found = 0;

        //Kijken of de key al in de collection zit
        for (var i = 0; i < FilterTags.length; i++) {

            //alert("key: " + FilterTags[i]);
            if (FilterTags[i] == escape(trim(tagid))) {

                return true;
            }
        }

        //Tag niet in collection
        return false;
        
    }

    function addFilterTag(tagid) {
        var found = 0;

       // alert("add: " + tagid);
    
        //Kijken of de key al in de collection zit
        for (var i = 0; i < FilterTags.length; i++) {

            //alert("key: " + FilterTags[i]);
            if (FilterTags[i] == escape(trim(tagid))) {
                
                found = 1;
            }
        }

       // alert("found: " + found);

        if (found == 0) {
            //Key zit nog niet in collection
            //alert("Count: " + FilterTags.length);


            //alert("Tag to add: " + tagid);
            FilterTags.push(escape(trim(tagid)));

            $.cookie("FilterTags", FilterTags);
            return true;
            //alert("new add: " + FilterTags[FilterTags.length]);
        }


        $.cookie("FilterTags", FilterTags);
        return false;
    }

    function removeFilterTag(tagid) {

        //var index = 0;

        //Kijken of de key al in de collection zit
        for (var i = 0; i < FilterTags.length; i++) {

            //alert(FilterTags[i] + ", " + escape(tagid));
            //alert("key: " + FilterTags[i]);
            if (FilterTags[i] == escape(trim(tagid))) {
                FilterTags.splice(i, 1);
                
            }
        }
        
        $.cookie("FilterTags", FilterTags);
    }

    function getFilterTags() {

        return FilterTags;

    }
