var IMDTUploadHandlers = {
    
    fileDialogStart : function(){
        //alert('IMDTUploadHandlers::fileDialogStart');
    },

    fileQueued : function(file, swfuInstance){
        //alert('IMDTUploadHandlers::fileQueued');
    },
    
    fileQueuedError : function(file, errorCode, message){
        //alert('IMDTUploadHandlers::fileQueuedError');
    },
    
    fileDialogComplete : function(selectedFilesCount, queuedFilesCount, fileQueueCount, swfuInstance){
        //alert('IMDTUploadHandlers::fileDialogComplete');
        swfuInstance.startUpload();
    },
    
    uploadStart : function(file){
        //alert('IMDTUploadHandlers::uploadStart');
    },
    
    uploadProgress : function(file, uploadedBytes, totalBytes){
        //alert('IMDTUploadHandlers::uploadProgress');
    },
    
    uploadError : function(file, errorCode, message){
        //alert('IMDTUploadHandlers::uploadError');
    },
    
    uploadSuccess : function(file, serverData, response, swfuInstanceCustomProperties){
        var options = $.extend(jQuery.parseJSON(serverData), swfuInstanceCustomProperties);
        options.autoResize = $('input#automaticResize-' + swfuInstanceCustomProperties.widgetId).is(':checked');
        
        appendUploadedImage(file, options);
    },
    
    uploadComplete : function(file){
        //alert('IMDTUploadHandlers::uploadComplete');
    },
    
    queueComplete : function(){
        //alert('IMDTUploadHandlers::queueComplete');
    }
}



/*SEPARAR AQUI*/
iMDTPerformResize = function(fileObj, options){
    
    var largura = options.dimensions.width;
    var altura = options.dimensions.height;
    
    var returnValue = $.ajax({
        url             : '/index2.php/upload/resize/',
        method 		: 'post',
        data		: 'id=' + fileObj.name + '&width=' + largura + '&height=' + altura,
        dataType	: 'json',
        async           : false,
        success	: function(a,b){
            if("success" == b){
                /*var widgetNode  = $('div#' + options.widgetId + '-preview');
                var imgNode =  $(widgetNode).find('img');
                var imgNodeExists = $(imgNode).size() > 0;

                if(! imgNodeExists){
                    $(widgetNode).append('<img/>');
                    imgNode =  $(widgetNode).find('img');
                }				
                $(imgNode).attr('src', '/index2.php/upload/thumb/id/' + a.id);
				
                alert("Imagem redimensionada com sucesso!");
				
                if($('div#imageScale').dialog('isOpen')){
                    $('div#imageScale').dialog('close');
                }*/
                
                return a;
            }
        }
    }).responseText;
    
    var objRetorno = jQuery.parseJSON(returnValue);
    return objRetorno;
}
/*
appendUploadedImage = function(file, options){
    
    var imageUrl = '/index2.php/upload';
    
    if(options.autoResize){
        response = iMDTPerformResize(file, {dimensions:options.dimensions});        
        imageUrl += '/thumb/id/' + response.friendlyFileName;
        options = $.extend(options, response);
    }else{
        imageUrl += '/show/id/' + file.name;
    }
    
    options.imageUrl = imageUrl;
    
    imageContainerNode(options);
}

imageContainerNode = function(options){
    var imageBagTemplate    = '<div class="fileInstance" id="preview{1}"><div class="name">{2}</div><div class="date">{3}</div><div class="imagePreview"><img src="{4}" title="{5}"/></div><input type="hidden" name="' + options.widgetName + '[images][]" value="{6}"/></div>';
    
    var widgetNode          = $('div#' + options.widgetId + '-preview');

    var bagNode             = $(widgetNode).find('div#preview'+options.id);
    var bagExists           = $(bagNode).size() > 0;
    
    var imageUrl            = options.imageUrl;
    
    if(! imageUrl || imageUrl == 'undefined'){
        imageUrl = '/index2.php/upload/show/id/' + options.friendlyFileName;
    }
    
    if(! bagExists){
        bagNode = imageBagTemplate.replace('{1}', options.id);
        bagNode = bagNode.replace('{2}', options.friendlyFileName);
        bagNode = bagNode.replace('{3}', options.uploadedAt);
        bagNode = bagNode.replace('{4}', imageUrl);
        bagNode = bagNode.replace('{5}', options.friendlyFileName);
        bagNode = bagNode.replace('{6}', options.friendlyFileName);
        
        $(widgetNode).append(bagNode);
    }else{
        $(bagNode).find('div.name').html(options.friendlyFileName);
        $(bagNode).find('div.date').html(options.uploadedAt);
        $(bagNode).find('div.imagePreview img').attr('src', imageUrl);
        $(bagNode).find('div.imagePreview img').attr('title', options.friendlyFileName);
        $(bagNode).find('input[hidden]').val(options.friendlyFileName);
    }
}*/

appendUploadedImage = function(file, options){
    var resolutions = options.resolutions;
    var files       = new Array();
        
    $(resolutions).each(function(i, curr){
        files[files.length] = iMDTPerformResize(file, {dimensions:curr});
    });
    
    $(files).each(function(i, curr){
        imageContainerNode($.extend(options, curr));
    });
}

imageContainerNode = function(options){
    //var imageBagTemplate    = '<div class="fileInstance" id="preview{1}"><div class="name">{2}</div><div class="date">{3}</div><div class="imagePreview"><img src="{4}" title="{5}"/></div><input type="hidden" name="' + options.widgetName + '[images][{7}][fileName]" value="{6}"/><input type="hidden" name="' + options.widgetName + '[images][{7}][width]" value="{8}"/><input type="hidden" name="' + options.widgetName + '[images][{7}][height]" value="{9}"/></div>';
    
    var imageBagTemplate = '<div class="fileInstance" id="preview{1}"><div class="name">{2}</div>';
    imageBagTemplate += '<div class="date">{3}</div><div class="imagePreview"><img src="{4}" title="{2}"/></div>';
    imageBagTemplate += '<input type="hidden" name="' + options.widgetName + '[images][{5}][fileName]" value="{5}"/>';
    imageBagTemplate += '<input type="hidden" name="' + options.widgetName + '[images][{5}][childImages][{2}][fileName]" value="{2}"/>';
    imageBagTemplate += '<input type="hidden" name="' + options.widgetName + '[images][{5}][childImages][{2}][width]" value="{6}"/>';
    imageBagTemplate += '<input type="hidden" name="' + options.widgetName + '[images][{5}][childImages][{2}][height]" value="{7}"/>';
    imageBagTemplate += '</div>';
    
    var widgetNode          = $('div#' + options.widgetId + '-preview');

    var bagNode             = $(widgetNode).find('div#preview'+options.id);
    var bagExists           = $(bagNode).size() > 0;
    
    var imageUrl            = options.imageUrl;
    
    if(! imageUrl || imageUrl == 'undefined'){
        imageUrl = '/index2.php/upload/show/id/' + options.friendlyFileName;
    }
    
    if(! bagExists){
        bagNode = imageBagTemplate.replace(/\{1\}/gi, options.id);
        bagNode = bagNode.replace(/\{2\}/gi, options.friendlyFileName);
        bagNode = bagNode.replace(/\{3\}/gi, options.uploadedAt);
        bagNode = bagNode.replace(/\{4\}/gi, imageUrl);
        bagNode = bagNode.replace(/\{5\}/gi, options.originalFileName);
        bagNode = bagNode.replace(/\{6\}/gi, options.resolution.width);
        bagNode = bagNode.replace(/\{7\}/gi, options.resolution.height);
        
        $(widgetNode).append(bagNode);
    }else{
        $(bagNode).find('div.name').html(options.friendlyFileName);
        $(bagNode).find('div.date').html(options.uploadedAt);
        $(bagNode).find('div.imagePreview img').attr('src', imageUrl);
        $(bagNode).find('div.imagePreview img').attr('title', options.friendlyFileName);
        $(bagNode).find('input[hidden]').val(options.friendlyFileName);
    }
}
