// version 0.2


uploadPhoto = Class.create ();

uploadPhoto.prototype = {

	initialize : function (total_assets, content_type, model, counter, view_path) {
		this.form_upload_id = 'photo_upload_form';
		if (counter >= total_assets)
			this.upload_exists = false;
		else this.upload_exists = true;
		
		if (total_assets != false)
			this.total_assets = total_assets - 1;
		else this.total_assets = total_assets;
		
		if (BASE_URL.substring (BASE_URL.length - 6, BASE_URL.length) == '/admin')
			this.base_url = BASE_URL.substring (0, BASE_URL.length - 6);
		else
			this.base_url = BASE_URL;
		
		this.current_upload = counter;
		this.content_type = content_type;
		this.model = model;
		this.counter = counter;
		this.view_path = view_path;
	},
	
	// envia el formulario a traves del iframe oculto
	send: function () {
		//alert ("ajax_msg_" + this.content_type + '_' + this.current_upload);
		$("ajax_msg_" + this.content_type + '_' + this.current_upload).style.display = "inline";  // icono de ajax
		
		//creacion de un nuevo form
		var _form = $(this.form_upload_id).cloneNode(true);
		_form.id = _form.id + this.current_upload;
		_form.target = 'upload_target_' + this.content_type + '_' + this.current_upload;
		_form.action +=  this.content_type + '/' + this.current_upload; // cambia la url del envio

		// anadido de los valores al form
		var fileInput = $( 'photo_input_' + this.content_type + '_' + this.current_upload).cloneNode( false);

		fileInput = $( 'photo_input_' + this.content_type + '_' + this.current_upload);

		_form.appendChild( fileInput);
		document.body.appendChild( _form);
		this.upload_exists = false;
		this.counter++;
		if (this.counter <= this.total_assets || this.total_assets === false)	
			this.createUpload ();

		_form.submit();
	},
	
	createUpload : function (remove) {
		this.current_upload++;
		if (!remove) remove = false;
		url = this.base_url + '/assets/create_upload/' + (this.current_upload) + '/' + this.content_type + '/' + this.view_path;
		var myAjax = new Ajax.Request(
	      url,
	      {
					method: 'post', 
					onComplete: this.appendUpload.bindAsEventListener (this, remove),
					onFailure: function(t) {
					        alert('Error ' + t.status + ' -- ' + t.statusText);
					    }
				});
	},
	
	appendUpload : function (e, remove) {		
		new_el = document.createElement ('div');
		new_el.innerHTML = e.responseText;
		$('assets_form_' + this.content_type).appendChild (new_el);
		this.upload_exists = true;
		if (remove)
			Element.remove (remove);
	},
	
	createAsset : function (asset_id, counter) {
		url = this.base_url + '/assets/show/' + asset_id + '/' + this.content_type + '/' + this.model + '/' + "false" + '/' + counter+ '/' + this.view_path ;
		var myAjax = new Ajax.Request(
	      url,
	      {
					method: 'post', 
					onComplete: this.appendAsset.bindAsEventListener (this, counter)
				});
	},
	
	appendAsset : function (e, counter) {
		new_el = document.createElement ('div');
		new_el.innerHTML = e.responseText;
		$('assets_' + this.content_type).appendChild (new_el);
		Element.remove ('photo_upload_' + this.content_type + '_' + counter);
	},

	error : function (error_message, counter) {
		html = '<div class="error">';
		html += error_message;
		html +=	'</div>';
		new_el = document.createElement ('div');
		new_el.innerHTML = html;
		$('assets_' + this.content_type).appendChild (new_el);
		
		this.counter--;
		// Si hay un error y se nos han acabado los uploads, crea uno nuevo
		if (this.counter <= this.total_assets && this.total_assets !== false && this.upload_exists === false)
    	this.createUpload ('photo_upload_' + this.content_type + '_' + counter);
		else
			Element.remove ('photo_upload_' + this.content_type + '_' + counter);
		
	
	},

	deleteButton : function (ids) {
    el_a = $(ids[0]);
    el_div = $(ids[1]);
    el_a.div = ids[1];
    Event.observe (el_a, 'click', this.delete_block.bindAsEventListener (this), true);
  },
           
  delete_block: function (e) {
  	a	 = confirm ('¿Está seguro de quiere borrar?');
    el_a = Event.element (e);
    if (a) 
    { 
      Element.remove (el_a.div);
			this.counter--;
			if (this.counter <= this.total_assets && this.total_assets !== false && this.upload_exists === false)
				this.createUpload ();
    }
	}
	
};