namespace eval ::chooseImage {
    source [file join [app get shareDir]\
            script/cm/serverCmds/wizards/lib/wizardController.tcl]


    proc handleInitPage {buttonName} {
        return [renderChoosePage]
    }


    proc handleChooseCancelPageAction {} {
        return ""
    }


    proc handleChooseOkPageAction {} {
        variable objIds

        set objId [lindex $objIds 0]
        catch {obj withId $objId edit}
        catch {obj withId $objId take}
        set linkId [getLinkId $objId]
        set newDestUrl [obj withId [getParam currentImage] get path]
        if {$linkId eq ""} {
            obj withId $objId editedContent addLinkTo attribute ip_valueAddImage destinationUrl $newDestUrl
        } else {
            link withId $linkId set destinationUrl $newDestUrl
        }
        addNpsRefresh $objId
        return ""
    }


    proc handleChoosePage {buttonName} {
        return [renderChoosePage]
    }


    proc renderChoosePage {} {
        variable objIds

        set objId [lindex $objIds 0]
        set oldImage [getParam oldImage]
        if {$oldImage eq ""} {
            set linkId [getLinkId $objId]
            if {$linkId ne ""} {
                set oldImage [link withId $linkId get destination]
            }
        }
        if {$oldImage eq ""} {
            set oldImage [obj withPath /internet/playland/ressourcen/images/layout/standardBaer get id]
        }
        addHiddenValue oldImage $oldImage
        set currentImage [getParam currentImage]
        if {$currentImage eq ""} {
            set currentImage $oldImage
        }
        set content "<div style='float: left; margin-right: 2em; width: 15em;'>"
        append content [::layout::fieldSet [localize oldImage]\
                [imagePreviewHtml $oldImage style "width: 12.3em; margin-left: 1em; margin-right: 1em;"]]
        append content "</div><div style='width: 15em;'>"
        append content [::layout::fieldSet [localize newImage] "<div style='width: 14.3em; text-align: center;'>"\
                [imagePreviewHtml $currentImage style "width: 12.3em; margin-left: 1em; margin-right: 1em;"]\
                "<br/><br/><npsSelectObject selectMessage='[localize title]' name='currentImage'\
                objectTypes='image' startPath='/internet/playland/ressourcen/images'/></div>"]
        append content "</div>"
        set buttons [list]
        if {$currentImage ne $oldImage} {
            lappend buttons ok
        }
        lappend buttons {cancel}
        return [npsPage -title [localize title] -subtitle [localize subtitle] -affectedNodes $objIds\
                -buttons $buttons choose $content]
    }


    proc getLinkId {objId} {
        set contentId [obj withId $objId get editedContentId]
        if {$contentId eq ""} {
            set contentId [obj withId $objId get releasedContentId]
        }
        return [lindex [content withId [getContentId $objId] get ip_valueAddImage] 0]
    }


    initLocalizer de {
        title "Mehrwertbild ausw\u00e4hlen"
        subtitle "Bitte w\u00e4hlen Sie ein Bild für die Mehrwertspalte aus."
        oldImage "Aktuelles Bild"
        newImage "Neues Bild"
    }

    initLocalizer en {
        title "Choose value add image"
        subtitle "Please choose an image to display in the value add column."
        oldImage "Current image"
        newImage "New image"
    }
}
