
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('p.F=q G({H:p,17:18,3:{I:\'p.F.19\',J:K,L:K,o:8,r:8,s:\'1a\',h:8,t:\'1b\',j:8,M:\'k%\',N:\'k%\',u:8},O:4(c){6(P.Q.R.S<9)5 1c;2.1d(c);7 d=2.3.u||2;6(d.l)2.T(\'l\',d.l);6(!d.v){d.v=4(){5 2.3.o}}7 e={},w=2;[\'v\',\'1e\',\'1f\',\'1g\',\'1h\',\'1i\',\'1j\',\'1k\',\'1l\',\'1m\'].1n(4(a){7 b=d[a]||$1o;e[a]=4(){w.U(a,z,10);5 b.1p(w,z)}});e.l=2.V.1q({1r:10,W:2});2.3.u=e;7 f=2.3.I;6(!f.1s(\'?\'))f+=\'?1t=\'+$1u();2.A(f);7 g=X.1v();2.m=q 1w(\'1x\',{1y:{1z:\'1A\',1B:\'1C\',M:10,N:10,1D:g.y,1E:g.x}});2.Y(2.m);2.m.Y($(2.3.1F)||1G.1H);5 2},V:4(){2.n(\'1I\',2.1J,2.3.J,2.3.L);2.U(\'l\');2.j=$(2.3.j);6(P.Q.R.S>=10&&2.j){2.B();X.T(\'1K\',2.B.W(2))}},B:4(){7 a=2.j.1L(2.m.1M());2.m.1N(a)},Z:4(a){2.3.o=$1O(a,2.3.o);5 2.n(\'Z\')},11:4(a){7 b=2.3;a=$1P({h:b.h,r:b.r,s:b.s,t:b.t},a);6($1Q(a.h)==\'C\')a.h=$(a.h).1R();5 2.n(\'11\',a)},12:4(a){6(a)a={13:a.13,14:a.14};5 2.n(\'12\',a)},15:4(){5 2.n(\'15\')}});D.1S=q G({H:D,3:{i:8,1T:D.1U.1V.1W,1X:\'1Y\'},O:4(a,b){2.C=$(a);2.A(b);2.i=$(2.3.i);2.E(0)},1Z:4(a,b){5 2.A(2.16,(z.20==1)?a.21(0,k):a/b*k)},E:4(a){2.16=a;2.C.22(\'23\',(k-a)+\'% 24\');6(2.i)2.i.E(\'i\',25.26(a)+\'%\');5 2}});',62,131,'||this|options|function|return|if|var|null|||||||||data|text|target|100|onLoad|box|remote|typeFilter|Swiff|new|url|method|fieldName|callBacks|onBrowse|self|||arguments|parent|reposition|element|Fx|set|Uploader|Class|Extends|path|multiple|true|queued|height|width|initialize|Browser|Plugins|Flash|version|addEvent|fireEvent|load|bind|window|inject|browse||upload|removeFile|name|size|getFileList|now|Implements|Events|swf|post|Filedata|false|setOptions|onSelect|onAllSelect|onCancel|onBeforeOpen|onOpen|onProgress|onComplete|onError|onAllComplete|each|empty|apply|create|delay|contains|noCache|time|getScroll|Element|div|styles|position|absolute|overflow|hidden|top|left|container|document|body|register|instance|resize|getCoordinates|getOffsetParent|setStyles|pick|extend|type|toQueryString|ProgressBar|transition|Transitions|Circ|easeOut|link|cancel|start|length|limit|setStyle|backgroundPosition|0px|Math|round'.split('|'),0,{}))

//upload2

var FancyUpload2 = new Class({

	Extends: Swiff.Uploader,

	options: {
		limitSize: true,
		limitFiles: 1,
		instantStart: false,
		allowDuplicates: false,
		validateFile: $lambda(true), // provide a function that returns true for valid and false for invalid files.
		debug: false,

		fileInvalid: null, // called for invalid files with error stack as 2nd argument
		fileCreate: null, // creates file element after select
		fileUpload: null, // called when file is opened for upload, allows to modify the upload options (2nd argument) for every upload
		fileComplete: null, // updates the file element to completed state and gets the response (2nd argument)
		fileRemove: null // removes the element
		/**
		 * Events:
		 * onBrowse, onSelect, onAllSelect, onCancel, onBeforeOpen, onOpen, onProgress, onComplete, onError, onAllComplete
		 */
	},

	initialize: function(status, list, options) {
		this.status = $(status);
		this.list = $(list);

		this.files = [];

		if (options.callBacks) {
			this.addEvents(options.callBacks);
			options.callBacks = null;
		}

		this.parent(options);
		this.render();
		$('demo-upload').disabled=true;
	},

	render: function() {
		//this.overallTitle = this.status.getElement('.overall-title');
		this.currentTitle = this.status.getElement('.current-title');
		this.currentText = this.status.getElement('.current-text');

		/*var progress = this.status.getElement('.overall-progress');
		this.overallProgress = new Fx.ProgressBar(progress, {
			text: new Element('span', {'class': 'progress-text'}).inject(progress, 'after')
		});*/
		var progress = this.status.getElement('.current-progress');
		this.currentProgress = new Fx.ProgressBar(progress, {
			text: new Element('span', {'class': 'progress-text'}).inject(progress, 'after')
		});
	},

	onLoad: function() {
		this.log('Uploader ready!');
	},

	onBeforeOpen: function(file, options) {
		this.log('Initialize upload for "{name}".', file);
		var fn = this.options.fileUpload;
		var obj = (fn) ? fn.call(this, this.getFile(file), options) : options;
		return obj;
	},

	onOpen: function(thefile, overall) {
		this.log('Starting upload "{name}".', thefile);
		thefile = this.getFile(thefile);
		thefile.element.addClass('file-uploading');
		thefile.removelink.destroy();
		this.currentProgress.cancel().set(0);
		this.currentTitle.set('html', 'Uploading {name}...'.substitute(thefile) );
	},

	onProgress: function(file, current, overall) {
		//this.overallProgress.start(overall.bytesLoaded, overall.bytesTotal);
		this.currentText.set('html', 'Uploading at {rate}/s. Time left: ~{timeLeft}'.substitute({
			rate: (current.rate) ? this.sizeToKB(current.rate) : '- B',
			timeLeft: Date.fancyDuration(current.timeLeft || 0)
		}));
		this.currentProgress.start(current.bytesLoaded, current.bytesTotal);
	},

	onSelect: function(file, index, length) {
		var errors = [];
		if (this.options.limitSize && (file.size > this.options.limitSize)) errors.push('size');
		if (this.options.limitFiles && (this.countFiles() >= this.options.limitFiles)) errors.push('length');
		if (!this.options.allowDuplicates && this.getFile(file)) errors.push('duplicate');
		if (!this.options.validateFile.call(this, file, errors)) errors.push('custom');
		if (errors.length) {
			var fn = this.options.fileInvalid;
			if (fn) fn.call(this, file, errors);
			return false;
		}
		(this.options.fileCreate || this.fileCreate).call(this, file);
		this.files.push(file);
		if (this.countFiles() > 0)
			$('demo-upload').disabled=false;
		else
			$('demo-upload').disabled=true;
		return true;
	},

	onAllSelect: function(files, current, overall) {
		this.log('Added ' + files.length + ' files, now we have (' + current.bytesTotal + ' bytes).', arguments);
		//this.updateOverall(current.bytesTotal);
		this.status.removeClass('status-browsing');
		if (this.files.length && this.options.instantStart) this.upload.delay(10, this);
	},

	onComplete: function(file, response) {
		this.log('Completed upload "' + file.name + '".', arguments);
		this.currentProgress.start(100);
		(this.options.fileComplete || this.fileComplete).call(this, this.finishFile(file), response);
	},

	onError: function(file, error, info) {
		this.log('Upload "' + file.name + '" failed. "{1}": "{2}".', arguments);
		(this.options.fileError || this.fileError).call(this, this.finishFile(file), error, info);
	},

	onCancel: function() {
		this.log('Filebrowser canceled.', arguments);
		this.status.removeClass('file-browsing');
	},

	onAllComplete: function(current) {
		this.log('Completed all files, ' + current.bytesTotal + ' bytes.', arguments);
		$('uploading_indicator').innerHTML='Upload Complete!';
		this.currentText.set('html', '<a href="/myfiles">View Files</a> - <a href="javascript:window.location.reload()">Upload again</a>');
		this.currentTitle.set('html', '');
		//this.updateOverall(current.bytesTotal);
		//this.overallProgress.start(100);
		this.status.removeClass('file-uploading');
	},

/*
	browse: function(fileList) {
		var ret = this.parent(fileList);
		if (ret !== true){
			if (ret) this.log('An error occured: ' + ret);
			else {
				this.log('Browse in progress.');
			}
		} else {
			this.log('Browse started.');
			this.status.addClass('file-browsing');
		}
	},
*/
	upload: function(options) {
		var ret = this.parent(options);
		if (ret !== true) {
			this.log('Upload in progress or nothing to upload.');
			//if (ret) alert(ret);
		} else {
			this.log('Upload started.');
			this.status.addClass('file-uploading');
			//this.overallProgress.set(0);
			if (this.countFiles() > 1) {
				//$('overall-progress').setProperty('style', 'display:block');
			}
		}
	},

	removeFile: function(file) {
		var remove = this.options.fileRemove || this.fileRemove;
		if (!file) {
			this.files.each(remove, this);
			this.files.empty();
			//this.updateOverall(0);
		} else {
			if (!file.element) file = this.getFile(file);
			this.files.erase(file);
			remove.call(this, file);
			//this.updateOverall(this.bytesTotal - file.size);
		}
		this.parent(file);
		
		if (this.countFiles() > 0)
			$('demo-upload').disabled=false;
		else
			$('demo-upload').disabled=true;
	},

	getFile: function(file) {
		var ret = null;
		this.files.some(function(value) {
			if ((value.name != file.name) || (value.size != file.size)) return false;
			ret = value;
			return true;
		});
		return ret;
	},

	countFiles: function() {
		var ret = 0;
		for (var i = 0, j = this.files.length; i < j; i++) {
			if (!this.files[i].finished) ret++;
		}
		return ret;
	},

	updateOverall: function(bytesTotal) {
		//this.bytesTotal = bytesTotal;
		//this.overallTitle.set('html', 'Overall Progress (' + this.sizeToKB(bytesTotal) + ')');
	},

	finishFile: function(file) {
		file = this.getFile(file);
		file.element.removeClass('file-uploading');
		file.finished = true;
		return file;
	},

	fileCreate: function(thefile) {
		thefile.info = new Element('span', {'class': 'file-info'});
		thefile.removelink = new Element('a', {
				'class': 'file-remove',
				'href': '#',
				'html': 'Remove',
				'events': {
					'click': function() {
						this.removeFile(thefile);
						return false;
					}.bind(this)
				}
			});
		thefile.element = new Element('li', {'class': 'file'}).adopt(
			new Element('span', {'class': 'file-size', 'html': this.sizeToKB(thefile.size)}),
			thefile.removelink, 
			new Element('span', {'class': 'file-name', 'html': thefile.name}),
			thefile.info
		).inject(this.list);
	},

	fileComplete: function(thefile, response) {
		thefile.info.innerHTML = response;
		if (stristr(response, 'error')) {
			thefile.element.addClass('file-failed');
		} else {
			thefile.element.addClass('file-success');
		}
	},

	fileError: function(file, error, info) {
		file.element.addClass('file-failed');
		file.info.set('html', '<strong>' + error + '</strong><br />' + info);
	},

	fileRemove: function(file) {
		//file.element.fade('out').retrieve('tween').chain(Element.destroy.bind(Element, file.element));
		file.element.destroy();
	},

	sizeToKB: function(size) {
		var unit = 'B';
		if ((size / 1048576) > 1) {
			unit = 'MB';
			size /= 1048576;
		} else if ((size / 1024) > 1) {
			unit = 'kB';
			size /= 1024;
		}
		return size.round(1) + ' ' + unit;
	},

	log: function(text, args) {
		if (this.options.debug && window.console) console.log(text.substitute(args || {}));
	}

});

/**
 * @todo Clean-up, into Date.js
 */
Date.parseDuration = function(sec) {
	var units = {}, conv = Date.durations;
	for (var unit in conv) {
		var value = Math.floor(sec / conv[unit]);
		if (value) {
			units[unit] = value;
			if (!(sec -= value * conv[unit])) break;
		}
	}
	return units;
};

Date.fancyDuration = function(sec) {
	var ret = [], units = Date.parseDuration(sec);
	for (var unit in units) ret.push(units[unit] + Date.durationsAbbr[unit]);
	return ret.join(', ');
};

Date.durations = {years: 31556926, months: 2629743.83, days: 86400, hours: 3600, minutes: 60, seconds: 1, milliseconds: 0.001};
Date.durationsAbbr = {
	years: 'j',
	months: 'm',
	days: 'd',
	hours: 'h',
	minutes: 'min',
	seconds: 'sec',
	milliseconds: 'ms'
};

window.addEvent('domready',function(){var swiffy=new FancyUpload2($('demo-status'),$('demo-list'),{url:$('form-demo').action + '&swf=true',fieldName:'userfile',path:'/swf/Swiff.Uploader.swf',limitSize:50*1024*1024,onLoad:function(){$('demo-status').removeClass('hide');if($('demo-fallback')){$('demo-fallback').destroy()}},debug:false,target:'demo-browse'});$('demo-browse').addEvent('click',function(){swiffy.browse();return false});$('demo-upload').addEvent('click',function(){$('uploading_indicator').style.display='block';$('after_upload_starts').style.display='block';$('hide_after_start').style.display='none';$('demo-upload').style.display='none';swiffy.upload();return false})});