(function(){

var ajaxOn = window.ajaxOn = {};

ajaxOn.presets = {
	link: {

	},
	form: {

	}
}

ajaxOn.fn = {
	openModal: function() {
		alert('modal');
	}
}

ajaxOn.fn.after = function(settings) {	
	$(settings.runtime.w).each(function() {
		$('form,a', this).ajaxOn({
			after: ajaxOn.fn.after,
			runtime: settings.runtime,
			when: settings.when
		});
	});
}

ajaxOn.fn.upload = function() {
    var _action = $(this).parents("form").attr("action");

		var _model = $(this).parents("form").find('input[name=_model]').val();

    var _name = this.name;

    _nameId = _name.replace(/[\[\]]/g, '');

    var _buttonPlaceholder = _nameId+'__bph';
    var _fileProgressContainer= _nameId+'__fpc';
    var _loadingContent = _nameId+'__lc';
    var _longLoading = _nameId+'__ll';
    var _alternateContent = _nameId+'__ac';
    var _status = _nameId+'__s';
    var _cancelButton = _nameId+'__cb';

    var _minimum_flash_version = "9.0.28";

    var _this = this;

    if ($(_this).hasClass('doc')) {
      var _file_types = "*.pdf;*.doc;*.txt;*.odt;*.rtf";
    } else {
      var _file_types = "*.jpg;*.png;*.jpeg;*.swf;*.gif;*.bmp";
    }

		var _multiple = $(this).hasClass('many');

    var el = $(this)
    //.after('<div id="'+_nameId+'"></div>')
    .after('<span id="'+_buttonPlaceholder+'">button</span>')
    .after('<div id="'+_loadingContent+'"></div>')
    .after('<div id="'+_longLoading+'"></div>')
    .after('<div style="display: none" id="'+_alternateContent+'">(flash &gt; '+_minimum_flash_version+' not available)</div>')
    .after('<div id="'+_status+'"></div>')
    .after('<input type="button" id="'+_cancelButton+'" value="Cancel" disabled="true" style="display: none"/>')
    .after('<div id="'+_fileProgressContainer+'"></div>')
		.remove();

    var swfu;
    swfu = new SWFUpload({
        // Backend Settings
        upload_url: _action,	// Relative to the SWF file or absolute
        file_post_name: _name,

        post_params: {
            "CAKEPHP": "",
            'from': 'swfupload220b5',
						'_model': _model
        },

        // Flash Settings
        flash_url : "/js/swfupload/swfupload.swf",

        // File Upload Settings
        file_size_limit : "2 MB",	// 2MB
        file_types : _file_types,
        file_types_description : "Archivos",
        file_upload_limit : "0",
        file_queue_limit : 0,

        custom_settings : {
            field: _this,
						multiple: _multiple,
            upload_target : _nameId,
            loading_content: _loadingContent,
            long_loading: _longLoading,
            alternate_content: _alternateContent,
            status: _status,
            cancelButtonId: _cancelButton,
            progressTarget: _fileProgressContainer
        },
        // Debug Settings
        debug: false,

        // Event Handler Settings - these functions as defined in Handlers.js
        //  The handlers are not part of SWFUpload but are part of my website and control how
        //  my website reacts to the SWFUpload events.

        // The event handler functions are defined in handlers.js
        swfupload_loaded_handler : swfUploadLoaded,
        file_queued_handler : fileQueued,
        file_queue_error_handler : fileQueueError,
        file_dialog_complete_handler : fileDialogComplete,
        upload_start_handler : uploadStart,
        upload_progress_handler : uploadProgress,
        upload_error_handler : uploadError,
        upload_success_handler : uploadSuccess,
        upload_complete_handler : uploadComplete,
        queue_complete_handler : queueComplete,	// Queue plugin event

        // Button Settings
        button_image_url : "/img/SmallSpyGlassWithTransperancy_17x18.png",	// Relative to the SWF file
        button_placeholder_id : _buttonPlaceholder,
        button_width: 180,
        button_height: 18,
        button_text : '<span class="button">Seleccionar ficheros</span>',
        button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 10pt; }',
        button_text_top_padding: 0,
        button_text_left_padding: 18,
        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
        button_cursor: SWFUpload.CURSOR.HAND,

        // SWFObject settings
        minimum_flash_version : _minimum_flash_version,
        swfupload_pre_load_handler : swfUploadPreLoad,
        swfupload_load_failed_handler : swfUploadLoadFailed
    });
}

})();
