function Load_filter {
    param ($root)

    #0=Office;1=Bilder;2=Videos;3=Audios;4=Unterordner einbeziehen
    $Script:filtercheck=@($false,$false,$false,$false,$false)

    # Lädt das Fenster
    [xml]$Form = Get-Content "$PSScriptRoot\filter.xaml" -Encoding utf8
    $NR=(New-Object System.Xml.XmlNodeReader $Form)
    $window=[Windows.Markup.XamlReader]::Load($NR)

    # Objekt - Variablen Verknüpfung
    $Office=$window.FindName("imgoffice")
    $Bilder=$window.FindName("imgbilder")
    $Videos=$window.FindName("imgvideos")
    $Audios=$window.FindName("imgaudios")
    $Script:Filteroffice = $window.FindName("btnoffice")
    $Script:Filterbilder = $window.FindName("btnbilder")
    $Script:Filtervideos = $window.FindName("btnvideos")
    $Script:Filteraudios = $window.FindName("btnaudios")
    $Script:filterok = $window.FindName("btnsetzen")
    $Script:filterreset = $window.FindName("btnreset")

    # Verknüpft feste Elemente
    $window.Title = $Script:Version
    $window.icon="$root/Source/Logo.png"
    $Office.Source = "$root/Source/Office.png"
    $Bilder.Source = "$root/Source/Bild.png"
    $Videos.Source = "$root/Source/Video.png"
    $Audios.Source = "$root/Source/Audio.png"
    
    return $window
}

function Reset_Filter {
    #0=Office;1=Bilder;2=Videos;3=Audios;4=Unterordner einbeziehen
    $Script:filtercheck=@($false,$false,$false,$false,$false)
    $Filteroffice.background = "#444444"
    $Filterbilder.background = "#444444"
    $Filtervideos.background = "#444444"
    $Filteraudios.background = "#444444"
    $mainimgswitch.Source = "$ScriptRoot/Source/swfalse.png"
}

function Filter_data {
    param($tag)
    $Script:data = @()
    main_loading $true
    Filter_deep $tag
    main_loading $false
    $item = [System.Windows.Data.ListCollectionView]$Script:data
    $mainbrowser.Remove
    $mainbrowser.ItemsSource = $item
}

function Filter_deep {
    param ($tag)
    #Log "### Main: Filter deep ###" -clear
    $folder = $Script:folderdb[$tag]
    for ($i = 5; $i -lt $folder.count; $i++) {
        if ($folder[$i] -match "f") {
            if ($Script:filtercheck[4]) {
                $string=$folder[$i].Substring(1)
                $int = [int]$string
                if (!$Script:folderdb[$int][4]) {
                    Filter_deep $int
                }
            }
        }else {
            if (!$Script:filedb[$folder[$i]][4]) {
                for ($t = 0; $t -lt 3; $t++) {
                    if ($Script:filtercheck[$t]) {
                        for ($ti = 0; $ti -lt $Script:types[$t].Count; $ti++) {
                            if ($Script:types[$t][$ti] -eq $Script:filedb[$folder[$i]][5]) {
                                if ($Script:filedb[$folder[$i]][3]) {
                                    $cp = "$Script:ScriptRoot\Source\checked.png"
                                }else {
                                    $cp = "$Script:ScriptRoot\Source\unchecked.png"
                                }
                                $Length = calc_Size $Script:filedb[$folder[$i]][1]
                                $icon = $Script:icontypes[$Script:filedb[$folder[$i]][5]]
                                $Script:data += New-Object PSObject -prop @{Source="$Script:ScriptRoot\icons\$icon.ico";Name=$Script:filedb[$folder[$i]][0];Size="$Length";Tag=$folder[$i];SourceChecked="$cp"}
                                Write-Host $Script:filedb[$folder[$i]][0]
                            }
                            [System.Windows.Forms.Application]::DoEvents()
                        }
                    }
                    [System.Windows.Forms.Application]::DoEvents()
                }
            }
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    return $data
}

function Update_Filter {
    $ok = @($true,$true,$true,$true)
    $btnname = @("Office","Bilder","Videos","Audios")
    $Script:mainchoosen1.Content = $null
    $Script:mainchoosen2.Content = $null
    $Script:mainchoosen3.Content = $null
    $Script:mainchoosen4.Content = $null
    for ($i = 0; $i -lt 4; $i++) {
        if ($Script:filtercheck[$i]) {
            if ($ok[0]) {
                $Script:mainchoosen1.Content = "- "+$btnname[$i]
                $ok[0] = $false
                #Log "### Filter "+$btnname[$i]+" gesetzt ###" -clear
            }elseif ($ok[1]) {
                $Script:mainchoosen2.Content = "- "+$btnname[$i]
                $ok[1] = $false
                #Log "### Filter "+$btnname[$i]+" gesetzt ###" -clear
            }elseif ($ok[2]) {
                $Script:mainchoosen3.Content = "- "+$btnname[$i]
                $ok[2] = $false
                #Log "### Filter "+$btnname[$i]+" gesetzt ###" -clear
            }elseif ($ok[3]) {
                $Script:mainchoosen4.Content = "- "+$btnname[$i]
                $ok[3] = $false
                #Log "### Filter "+$btnname[$i]+" gesetzt ###" -clear
            }
        }
        [System.Windows.Forms.Application]::DoEvents()
    } 
}

#Log "### Filter geladen ###" -clear