var FancyUpload = new Class({
options: {
url: false,
swf: 'Swiff.Uploader.swf',
multiple: true,
queued: true,
types: {'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'},
limitSize: false,
limitFiles: false,
createReplacement: null,
instantStart: false,
allowDuplicates: false,
optionFxDuration: 250,
container: null,
queueList: 'photoupload-queue',
onComplete: Class.empty,
onNoFiles: Class.empty,
onError: Class.empty,
onCancel: Class.empty,
onProgress: Class.empty,
onUpload: Class.empty,
onAllComplete: Class.empty,
txtBrowse: 'Browse Files',
txtUploading: 'Uploading'
},
initialize: function(el, options){
this.element = $(el);
this.setOptions(options);
this.form = $(this.element.form || null);
this.options.url = this.options.url || (this.form && this.form.action) || location.href;
this.fileList = [];
this.uploader = new Swiff.Uploader({
onOpen: this.onOpen.bind(this),
onProgress: this.onProgress.bind(this),
onComplete: this.onComplete.bind(this),
onError: this.onError.bind(this),
onSelect: this.onSelect.bind(this)
}, this.initializeFlash.bind(this), {
swf: this.options.swf,
types: this.options.types,
multiple: this.options.multiple,
queued: this.options.queued,
target: this.target,
container: this.options.container
});
},
reposition: function() {
var pos = $('btnMeuComputador').getCoordinates();
$('box').setStyles({top:pos.top, left:pos.left, width:pos.width, height:pos.height});
},
repositionParaVideomix: function() {
var pos = $('flashContent').getCoordinates();
var top = parseInt(pos.top);
var left = parseInt(pos.left);
$('box').setStyles({top:(top+102)+'px', left:(left+210)+'px', width:'118px', height:'16px'});
},
initializeFlash: function() {
this.queue = $(this.options.queueList);
if (this.form) this.form.addEvent('submit', this.upload.bindWithEvent(this));
if (this.options.createReplacement) {
this.options.createReplacement(this.element);
} else {
new Element('a', {
id: "btnMeuComputador",
href: "#",
events: {
click: this.browse.bind(this)
}
}).appendText(this.options.txtBrowse).injectBefore(this.element);
this.element.remove();
}
this.reposition();		
window.addEvent('resize', this.reposition.bind(this));
},
browse: function() {
this.uploader.browse();
},
upload: function(e) {
if (e) e.stop();
if(this.fileList.length!=0) {
this.fireEvent('onUpload', this);
this.uploader.send(this.options.url);
} else {
this.fireEvent('onNoFiles', this);
}
},
onSelect: function(name, size) {
if (this.uploadTimer) this.uploadTimer = $clear(this.uploadTimer);
if ((this.options.limitSize && (size > this.options.limitSize))
|| (this.options.limitFiles && (this.fileList.length >= this.options.limitFiles))
|| (!this.options.allowDuplicates && this.findFile(name, size) != -1)) return false;
this.addFile(name, size);
if (this.options.instantStart) this.uploadTimer = this.upload.delay(250, this);
return true;
},
onOpen: function(name, size) {
var index = this.findFile(name, size);
this.fileList[index].status = 1;
if (this.fileList[index].fx) return;
this.fileList[index].fx = new Element('div', {'class': 'queue-subloader'}).injectInside(
new Element('div', {'class': 'queue-loader'}).setHTML(this.options.txtUploading).injectInside(this.fileList[index].element)
).effect('width', {
duration: 200,
wait: false,
unit: '%',
transition: Fx.Transitions.linear
}).set(0);
},
onProgress: function(name, bytes, total, percentage) {
this.uploadStatus(name, total, percentage);
this.fireEvent('onProgress',[name, bytes, total]);
},
onComplete: function(name, size) {
var index = this.uploadStatus(name, size, 100);
this.fileList[index].fx.element.setHTML('Completed');
this.fileList[index].status = 2;
this.highlight(index, 'e1ff80');
this.checkComplete(name, size, 'onComplete');
},
onError: function(name, size, error) {
var msg = "Upload failed (" + error + ")";
switch(error.toInt()) {
case 320: msg = "Not an error, but something the mac think it is!"; onComplete(name, size); break;
default: msg = "Erro!"; this.checkComplete(name, size, 'onError'); break;
}
var index = this.uploadStatus(name, size, 100);
this.fileList[index].fx.element.setStyle('background-color', '#ffd780').setHTML(msg);
this.fileList[index].status = 2;
this.highlight(index, 'ffd780');
this.checkComplete(name, size, 'onError');
},
checkComplete: function(name, size, fire) {
this.fireEvent(fire, [name, size]);
if (this.nextFile() == -1) this.fireEvent('onAllComplete');
},
addFile: function(name, size) {
if (!this.options.multiple && this.fileList.length) this.remove(this.fileList[0].name, this.fileList[0].size);
this.fileList.push({
name: name,
size: size,
status: 0,
percentage: 0,
element: new Element('li').setHTML('<span class="queue-file">'+ name +'</span><span class="queue-size" title="'+ size +' byte">~'+ Math.ceil(size / 1000) +' kb</span>').injectInside(this.queue)
});
new Element('a', {
href: 'javascript:void(0)',
'class': 'input-delete',
title: 'Remove from queue',
events: {
click: this.cancelFile.bindWithEvent(this, [name, size])
}
}).injectBefore(this.fileList.getLast().element.getFirst());
this.highlight(this.fileList.length - 1, 'e1ff80');
},
uploadStatus: function(name, size, percentage) {
var index = this.findFile(name, size);
this.fileList[index].fx.start(percentage).element.setHTML(percentage +'%');
this.fileList[index].percentage = percentage;
return index;
},
uploadOverview: function() {
var l = this.fileList.length, i = -1, percentage = 0;
while (++i < l) percentage += this.fileList[i].percentage;
return Math.ceil(percentage / l);
},
highlight: function(index, color) {
return this.fileList[index].element.effect('background-color', {duration: this.options.optionFxDuration}).start(color, 'fff');
},
cancelFile: function(e, name, size) {
e.stop();
this.remove(name, size);
},
remove: function(name, size, index) {
if (name) index = this.findFile(name, size);
if (index == -1) return;
if (this.fileList[index].status < 2) {
this.uploader.remove(name, size);
this.checkComplete(name, size, 'onCancel');
}
this.fileList[index].element.effect('opacity', {duration: this.options.optionFxDuration}).start(1, 0).chain(Element.remove.pass([this.fileList[index].element], Element));
this.fileList.splice(index, 1);
return;
},
findFile: function(name, size) {
var l = this.fileList.length, i = -1;
while (++i < l) if (this.fileList[i].name == name && this.fileList[i].size == size) return i;
return -1;
},
nextFile: function() {
var l = this.fileList.length, i = -1;
while (++i < l) if (this.fileList[i].status != 2) return i;
return -1;
},
clearList: function(complete) {
var i = -1;
while (++i < this.fileList.length) if (complete || this.fileList[i].status == 2) this.remove(0, 0, 0, i--);
}
});
FancyUpload.implement(new Events, new Options);