window.addEvent('domready', function() {
    if ($('fItemUpload')) {
    	var fileswiffy = new FancyUpload2($('fItemUpload'), $('list'), $('browse'), {
    		'url': create_url + '?_ikelk_session_id=' + session_id,
    		'data': {'authenticity_token' : authenticity_token},
    		'limitFiles' : 1,
    		'limitSize' : 83886080,
    		'fieldName': 'content',
    		'path': '/javascripts/Swiff.Uploader.swf',
			target: 'browse',
		
    		'onSelect' : function() {
    		    $('emptyNotice').setStyle('display', 'none');
                $('upload').setStyles({
                    "background-position": 'center bottom' 
                });
                $('upload').tween('color', $('upload').getStyle('color'), '#622D32');
    		},
		
    		'onComplete' : function(file, response) {
                var json = $H(JSON.decode(response, true));
                if (json.get('result') == "success") {
                    new Element('input', {'type' : 'hidden', 'name' : 'permalink', 'value' : json.get('permalink')}).injectInside($('vars'));
                } else {
                    var status = this.currentProgress.element.getParent();
                    status.empty();
                    status.appendText(json.get('error'));
                }
    		},
		
    		'onAllComplete' : function() {
                if ($('vars').getChildren().length == 0) {
                	$('clear').addEvent('click', function() {
                		fileswiffy.removeFile();
                		return false;
                	});
                	$('clear').tween('color', $('clear').getStyle('color'), '#622D32');
                	$('browse').empty();
                	$('browse').appendText('Pasirinkite bylą įkėlimui');
                } else {
                    $('fItemUpload').submit();
                }
    		},
		
    		'fileRemove' : function(file) {
              file.element.fade('out').retrieve('tween').chain(
                    function() {
                        if ($('list').getChildren().length - 1 == 1) {
                            if (Browser.Engine.trident) { // trident == IE!
                                $('emptyNotice').setStyle('display', 'block');
                            } else {
                                $('emptyNotice').setStyle('display', 'table-row');
                            }
                            $('upload').setStyles({
                                "background-position": 'center top' 
                            });
                            $('upload').tween('color', $('upload').getStyle('color'), '#8E8E8E');    
                        }
                        file.element.destroy();
                    }
                ); 
    		},
		
    		'fileCreate' : function(file) {
        		file.element = new Element('tr', {'class': 'file'}).adopt(
        		    new Element('td', {'class' : 'fname', 'html' : ( file.name.length > 30 ) ? file.name.substring(0,30) + '..' : file.name }),
        		    new Element('td', {'class' : 'progress'}).adopt(
        		        new Element('img', {'src' : '/images/bar.gif', 'alt' : 'progresas', 'class' : 'progress'})
        		    ),
        			new Element('td', {'class': 'size', 'html': this.sizeToKB(file.size)}),
        			new Element('td', {'class' : 'delete'}).adopt(
            			new Element('a', {
            			    'class': 'remfile',
            				'href': '#',
            				'html': '&nbsp;',
            				'events': {
            					'click': function() {
            						this.removeFile(file);
            						return false;
            					}.bind(this)
            				}
            			})
            		)
        		);
        		file.element.setStyle('opacity', 0);
        		file.element.inject(this.list);
        		file.element.fade('in');
        		return true;
    		}
    	});
 
    	$('browse').addEvent('click', function() {
    		fileswiffy.browse();
    		return false;
    	});

    	$('clear').addEvent('click', function() {
    		fileswiffy.removeFile();
    		return false;
    	});
 
    	$('upload').addEvent('click', function() {
    	    $('clear').removeEvents('click');
    	    $('browse').removeEvents('click');
    	    $('clear').tween('color', $('clear').getStyle('color'), '#8E8E8E');
    	    $$('a.remfile').each(function(remlink){
    	        remlink.removeEvents('click');
    	    });
    	    $('upload').setStyles({
                "background-position": 'center top' 
            });
            $('upload').tween('color', $('upload').getStyle('color'), '#8E8E8E');
    		fileswiffy.upload();
    		return false;
    	});
    }
    
    if ($('basicinfo')) {
        new Request.JSON(
            { url: '/services/generate_download_code',
              method: 'get', 
            onFailure: function(obj) { 
                alert(JSON.decode(obj.responseText).status); 
            }, 
            onSuccess: function(response, responseText) {
                var json = JSON.decode(responseText, true);
                $('download').highlight('#EAD841');
                $('download').set('href','/services/get-file/'+json.code);
            } 
         }).send('permalink=' + permalink);
         $$('input').each(function(field){
             field.addEvent('focus', function(){
                 field.select();
             });
         });
    }
});