﻿
var PhotosResult = "";
var Count = 0;
var UploadedFiles = 0;
function photos_fileDialogComplete(numFilesSelected, numFilesQueued) {
    try {
        if (numFilesQueued > 0) {
            PhotosResult = numFilesQueued == '1' ? ' файл' : ' файлов';
            PhotosResult = numFilesQueued + PhotosResult + " загружено";
            Count = parseInt(numFilesQueued);
            $('#AddPhotos').val('загружается...');
            $('#submitStatus')
                .attr('disabled', 'disabled')
                .addClass('disabled');
            this.startUpload();
        }
    } catch (ex) {
    }
}

function photos_uploadProgress(file, bytesLoaded) {
    try {
        var pw = 115;
        var w = Math.ceil(pw * (UploadedFiles / Count + (bytesLoaded / (file.size * Count))));
        $('#Progress').stop().animate({ width: w });
    } 
	catch (ex) {
    }
}
function photos_uploadSuccess(file, serverData) {
    try {
        UploadedFiles++;
    } catch (ex) {

    }
}

function photos_uploadComplete(file) {
    try {
        if (this.getStats().files_queued > 0) {
            this.startUpload();
        } else {
            //$('#UploadPhotos').hide();
			$('#Progress').css('width','0');
			$('#AddPhotos').val('Загрузить еще');
            $('#ext_fileManager_upload_sp').html('<span id="UploadResult" class="images">' + PhotosResult + '</span>');
			PhotosResult = "";
			Count = 0;
			UploadedFiles = 0;
        }
    } catch (ex) {
    }
}
function photos_fileQueueError(file, errorCode, message) {
    try {
        switch (errorCode) {
            case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
                alert('The limit of quantity of the chosen files is exceeded');
                break;
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                break;
        }
    } catch (ex) {
    }

}

function swfuploadLoaded() {
   	 $('#Buttons object').hover(
        function() {
            $(this).next().addClass('hover');
        },
        function() {
            $(this).next().removeClass('hover');
        });

}
var ASPSESSID = "";
var swfuPhotos;
function BindSWFUpload(upload_file) {
    var swfuPhotosSettings = {
        file_dialog_complete_handler: photos_fileDialogComplete,
        upload_progress_handler: photos_uploadProgress,
        upload_success_handler: photos_uploadSuccess,
        upload_complete_handler: photos_uploadComplete,
        swfupload_loaded_handler: swfuploadLoaded,
        file_queue_error_handler: photos_fileQueueError,

        file_size_limit: "10 MB",
        file_types: "*.jpg;*.png",
        file_types_description: "JPG, PNG images",
        file_upload_limit: "100",
        button_placeholder_id: "fAddPhotos"
    }

    var defaultSettings = {
        flash_url: "/exts/ext_fileManager/uploader/swf/swfupload.swf",
        upload_url: upload_file,/*"/exts/ext_fileManager/uploader/upload.php",*/
        post_params: {
            "ASPSESSID": ASPSESSID
        },

        button_width: 115,
        button_height: 32,
        button_image_url: "/exts/ext_fileManager/uploader/images/white50.png",

        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
        button_cursor: SWFUpload.CURSOR.HAND
    }

    swfuPhotos = new SWFUpload($.extend(swfuPhotosSettings, defaultSettings));
}



