//
// $Id: CMSFileUpload.js,v 1.1 2010/07/10 16:48:25 steve Exp $
//
var CMSFileUpload = function() {

    this.url  = new Url()

    this.init = function() {
    
        var self = this

        self.type      = self.url.clickedUrlArg('Type')
        self.targetDir = self.url.clickedUrlArg('TargetDir')
        self.id        = self.url.clickedUrlArg('Id')

        self.uploadVar = 'U' + ('' + Math.random()).substr(2)

        var url = 'Lib/action.php?Action=getUploadForm'
                + '&ReturnFormat=Text&Type=' + self.type
                + '&TargetDir=' + self.targetDir
                + '&Type=' + self.type
                + '&Id=' + self.id
                + '&UploadVar=' + self.uploadVar

        $('#uploadFileDiv iframe').attr('src', url)
        $('#addFileClose').click( function() { self.uploadDone() })
    }

    this.uploadDone = function() {

        var self = this

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=uploadDone&Type=' + self.type
                                                      + '&TargetDir=' + self.targetDir
                                                      + '&Type=' + self.type
                                                      + '&Id=' + self.id
                                                      + '&UploadVar=' + self.uploadVar,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                    location.reload()
                }
        })
        tb_remove()
    }
}