function Load_del {
    param ($root)

     # Speichert ob gelöscht oder verschoben werden soll
    $Script:whattodo=""

    # Arraydefinition
    ## 0=Name,1=Größe,2=checked
    $Script:sortdel=@("up","up",$false)

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

    # Objekt - Variablen Verknüpfung
    $pb=$window.findName("pb")
    $Script:Delbrowser = $window.findName("browser")
    $Script:Delback = $window.findName("btnback")
    $Script:Deldel = $window.findName("btnDeleteAll")
    $Script:DelMove = $window.findName("btnMoveAll")
    $Script:Delwarning = $window.findName("warning_delete")
    $Script:Movewarning = $window.findName("warning_move")
    $Script:Delmarked = $window.findName("marked_delete")
    $Script:Movemarked = $window.findName("marked_move")
    $Script:Delstats = $window.findName("stats")
    $Script:Delsortname = $window.findName("btnsortname")
    $Script:Delsortsize = $window.findName("btnsortsize")
    $Script:Delcheckall = $window.findName("btncheckbox")

    # Verknüpft feste Elemente
    $window.icon="$root/Source/Logo.png"

    ### GIF Sanduhr
    $file = (get-item "$root/Source/hourglas.gif")
    $img = [System.Drawing.Image]::Fromfile($file);
    $pb.Image = $img

    return $window
}

function Browser_del {
    $data = @()
    for ($i = 0; $i -lt $Script:folderdb.Count; $i++) {
        if ($Script:folderdb[$i][3]) {
            if (!$Script:folderdb[$i][4]) {
                $Length = calc_Size $Script:folderdb[$i][1]
                $data += New-Object PSObject -prop @{Source="$Script:ScriptRoot\Source\Folder.png";Name=$Script:folderdb[$i][0];Size="$Length";Tag="f$i";SourceChecked="$Script:ScriptRoot\Source\checked.png";Path=$Script:folderdb[$i][2]}
                #$logcontent = "Del: Browser f" + $i
                #Log $logcontent -clear
            }
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    for ($i = 0; $i -lt $filedb.Count; $i++) {
        if ($Script:filedb[$i][3]) {
            if (!$Script:filedb[$i][4]) {
                $Length = calc_Size $Script:filedb[$i][1]
                $icon = $Script:icontypes[$Script:filedb[$i][5]]
                $data += New-Object PSObject -prop @{Source="$Script:ScriptRoot\icons\$icon.ico";Name=$Script:filedb[$i][0];Size="$Length";Tag=$i;SourceChecked="$Script:ScriptRoot\Source\checked.png";Path=$Script:filedb[$i][2]}
                #$logcontent = "Del: Browser " + $i
                #Log $logcontent -clear
            }
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    $item = [System.Windows.Data.ListCollectionView]$data
    $Script:Delbrowser.Remove
    $Script:Delbrowser.ItemsSource = $item
}

function Check_Del {
    param ($tag)
    $data = @()
    for ($i = 0; $i -lt $delbrowser.Items.Count; $i++) {
        if (($delbrowser.Items.Tag[$i] -eq $tag) -or ($delbrowser.Items.Count -eq 1)) {
            $item=$delbrowser.Items[$i]
            if ($tag -match "f") {
                $string=$tag.Substring(1)
                $int = [int]$string
                if ($Script:folderdb[$int][3]) {
                    $Script:folderdb[$int][3]=$false
                    count_element $false $Script:folderdb[$int][1]
                    $data += New-Object PSObject -prop @{Source=$item.Source;Name=$item.Name;Size=$item.Size;Tag=$item.Tag;SourceChecked="$Script:ScriptRoot\Source\unchecked.png";Path=$item.Path}
                }else {
                    $Script:folderdb[$int][3]=$true
                    count_element $true $Script:folderdb[$int][1]
                    $data += New-Object PSObject -prop @{Source=$item.Source;Name=$item.Name;Size=$item.Size;Tag=$item.Tag;SourceChecked="$Script:ScriptRoot\Source\checked.png";Path=$item.Path}
                }
            }else {
                if ($Script:filedb[$tag][3]) {
                    $Script:filedb[$tag][3]=$false
                    count_element $false $Script:filedb[$tag][1]
                    $data += New-Object PSObject -prop @{Source=$item.Source;Name=$item.Name;Size=$item.Size;Tag=$item.Tag;SourceChecked="$Script:ScriptRoot\Source\unchecked.png";Path=$item.Path}
                }else {
                    $Script:filedb[$tag][3]=$true
                    count_element $true $Script:filedb[$tag][1]
                    $data += New-Object PSObject -prop @{Source=$item.Source;Name=$item.Name;Size=$item.Size;Tag=$item.Tag;SourceChecked="$Script:ScriptRoot\Source\checked.png";Path=$item.Path}
                }
            }
        }else {
            $data += $delbrowser.Items[$i]
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    $item = [System.Windows.Data.ListCollectionView]$data
    $delbrowser.Remove
    $delbrowser.ItemsSource = $item
}

function del_sortname{
    #Log "### Del: sort name ###" -clear
    $data= @()
    for ($i = 0; $i -lt $delbrowser.Items.count; $i++) {
        $item = $delbrowser.Items[$i]
        $data += New-Object PSObject -prop @{Name=$item.Name;Source=$item.Source;Size=$item.Size;Checked=$item.Checked;Tag=$item.Tag;SourceChecked=$Item.SourceChecked;Path=$item.Path}
        [System.Windows.Forms.Application]::DoEvents()
    }
    if ($sortdel[0] -eq "up") {
        $data = $data | Sort-Object -Property Name
        $sortdel[0]="down"
    }else {
        $data = $data | Sort-Object -Property Name -Descending
        $sortdel[0]="up"
    }
    $ItemList = [System.Windows.Data.ListCollectionView]$data
    $delbrowser.ItemsSource = $ItemList
}

function del_sortsize {
    #Log "### Del: sort size ###" -clear
    $data=@()
    $array=@()
    for ($i = 0; $i -lt $delbrowser.Items.Count; $i++) {
        $item = $delbrowser.Items[$i]
        if ($item.Size.EndsWith("GB")) {
            $stringtrim = $item.Size.TrimEnd(" GB")
        }else {
            $stringtrim = $item.Size.TrimEnd(" MB")
        }
        $int = [int]$stringtrim
        $array += New-Object PSObject -prop @{Tag=$item.Tag;Size=$int}
        [System.Windows.Forms.Application]::DoEvents()
    }
    if ($sortdel[1] -eq "up") {
        $array = $array | Sort-Object -Property Size 
        $sortdel[1]="down"
    }else {
        $array = $array | Sort-Object -Property Size -Descending
        $sortdel[1]="up"
    }
    for ($i = 0; $i -lt $array.Count; $i++) {
        for ($e = 0; $e -lt $delbrowser.items.Count; $e++) {
            if ($array.Tag[$i] -eq $delbrowser.Items.Tag[$e]) {
                $item=$delbrowser.Items[$e]
                $data += New-Object PSObject -prop @{Name=$item.Name;Source=$item.Source;Size=$item.Size;Checked=$item.Checked;Tag=$item.Tag;SourceChecked=$Item.SourceChecked;Path=$item.Path}
                $e=$delbrowser.items.Count               
            }
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    $ItemList = [System.Windows.Data.ListCollectionView]$data
    $delbrowser.Remove
    $delbrowser.ItemsSource = $ItemList
}

function del_checkall {
    #Log "### Del: Check all ###" -clear
    $data = @()
    $check = 0
    if ($sortdel[2]) {
        $sortdel[2]=$false
        for ($i = 0; $i -lt $delbrowser.Items.Count; $i++) {
            $item = $delbrowser.Items[$i]
            $tag=$item.Tag
            if ($tag -match "f") {
                $string=$tag.Substring(1)
                $int = [int]$string
                if ($folderdb[$int][3]) {
                    $folderdb[$int][3]=$false
                    count_element $false $Script:folderdb[$int][1]
                    $data += New-Object PSObject -prop @{Name=$item.Name;Source=$item.Source;Size=$item.Size;Checked=$false;Tag=$item.Tag;SourceChecked="$Script:ScriptRoot\Source\unchecked.png"}
                    $check++
                }else {
                    $data += $item
                }
            }else {
                if ($filedb[$tag][3]) {
                    $filedb[$tag][3]=$false
                    count_element $false $Script:filedb[$tag][1]
                    $data += New-Object PSObject -prop @{Name=$item.Name;Source=$item.Source;Size=$item.Size;Checked=$false;Tag=$item.Tag;SourceChecked="$Script:ScriptRoot\Source\unchecked.png"}
                    $check++
                }else {
                    $data += $item
                }
            }
            [System.Windows.Forms.Application]::DoEvents()
        }
    }else {
        $sortdel[2]=$true
        for ($i = 0; $i -lt $delbrowser.Items.Count; $i++) {
            $item = $delbrowser.Items[$i]
            $tag=$item.Tag
            if ($tag -match "f") {
                $string=$tag.Substring(1)
                $int = [int]$string
                if (!$folderdb[$int][3]) {
                    $folderdb[$int][3]=$true
                    count_element $true $Script:folderdb[$int][1]
                    $data += New-Object PSObject -prop @{Name=$item.Name;Source=$item.Source;Size=$item.Size;Checked=$true;Tag=$item.Tag;SourceChecked="$Script:ScriptRoot\Source\checked.png"}
                    $check++
                }else {
                    $data += $item
                }
            }else {
                if (!$filedb[$tag][3]) {
                    $filedb[$tag][3]=$true
                    count_element $true $Script:filedb[$tag][1]
                    $data += New-Object PSObject -prop @{Name=$item.Name;Source=$item.Source;Size=$item.Size;Checked=$true;Tag=$item.Tag;SourceChecked="$Script:ScriptRoot\Source\checked.png"}
                    $check++
                }else {
                    $data += $item
                }
            }
            [System.Windows.Forms.Application]::DoEvents()
        }
    }
    if ($check -eq 0) {
        del_checkall
    }else{
        $ItemList = [System.Windows.Data.ListCollectionView]$data
        $delbrowser.ItemsSource = $ItemList
    }
}

function final_action {
    if ($script:whattodo -eq "move") {
        for ($i = 0; $i -lt $filedb.Count; $i++) {
            if ($filedb[$i][3]) {
                if (!$filedb[$i][4]) {
                    Move-Item -Path $filedb[$i][2] -Destination $Script:movelocation
                    $filedb[$i][4] = $true
                    #$logcontent = "Move: " + $filedb[$i][2] + " To: " + $Script:movelocation
                    #Log $logcontent -clear
                }
            }
            [System.Windows.Forms.Application]::DoEvents()
        }
        for ($i = 0; $i -lt $folderdb.Count; $i++) {
            if ($folderdb[$i][3]) {
                if (!$folderdb[$i][4]) {
                    Move-Item -Path $folderdb[$i][2] -Destination $Script:movelocation
                    $folderdb[$i][4] = $true
                    #$logcontent = "Move: " + $folderdb[$i][2] + " To: " + $Script:movelocation
                    #Log $logcontent -clear
                }
            }
            [System.Windows.Forms.Application]::DoEvents()
        }
    }elseif ($Script:whattodo -eq "delete") {
        for ($i = 0; $i -lt $filedb.Count; $i++) {
            if ($filedb[$i][3]) {
                if (!$filedb[$i][4]) {
                    Remove-Item $filedb[$i][2]
                    $filedb[$i][4] = $true
                    #$logcontent = "Removed: " + $filedb[$i][2]
                    #Log $logcontent -clear
                }
            }
            [System.Windows.Forms.Application]::DoEvents()
        }
        for ($i = 0; $i -lt $folderdb.Count; $i++) {
            if ($folderdb[$i][3]) {
                if (!$folderdb[$i][4]) {
                    Remove-Item $folderdb[$i][2]
                    $folderdb[$i][4] = $true
                    #$logcontent = "Removed: " + $folderdb[$i][2]
                    #Log $logcontent -clear
                }
            }
            [System.Windows.Forms.Application]::DoEvents()
        }
    } 
}

#Log "### Del geladen ###" -clear