var _uacct = "UA-187442-11";
var work_item_count = 0;
var swf_url = sisu_base_url+'javascript/Swiff.Uploader.swf';
var uploaders = new Array();
var upload_url = null;

window.addEvent('domready', function() { // wait for the content
    CreateUploaders();
    CreateWorkItem();
    
    attachUploaderCompleteEvents();
});

var attachUploaderCompleteEvents = function() {
    if ($('participation_form'))
        $('participation_form').addEvent('submit', function() {
    		uploaders.each(function(uploader) {
    		    uploader.start();
    		});
    		return false;
    	});
};

var CreateUploaders = function() {
    if (upload_url) {
        // our uploader instance
    	var portaitUploader = new FancyUpload2($('portrait-status'), $('portrait-list'), {
    		verbose: false, // we console.log infos, remove that in production!!
    		
    		url: upload_url + '&section=portrait',
    		path: swf_url,
    		typeFilter: {
    			'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
    		},
    		limitFiles: 1,
    		instantStart: false,
    		target: 'person_portrait_file',
    		
    		onLoad: function() {
    			this.target.addEvents({
    				click: function() {
    					return false;
    				},
    				mouseenter: function() {
    					this.addClass('hover');
    				},
    				mouseleave: function() {
    					this.removeClass('hover');
    					this.blur();
    				},
    				mousedown: function() {
    					this.focus();
    				}
    			});
    		},
    		
    		onFail: function(error) {
    			switch (error) {
    				case 'hidden': // works after enabling the movie and clicking refresh
    					alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
    					break;
    				case 'blocked': // This no *full* fail, it works after the user clicks the button
    					alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
    					break;
    				case 'empty': // Oh oh, wrong path
    					alert('A required file was not found, please be patient and we fix this.');
    					break;
    				case 'flash': // no flash 9+ :(
    					alert('To enable the embedded uploader, install the latest Adobe Flash plugin.');
    			}
    		},
    		
    		onComplete: function() {
        		this.status.removeClass('file-uploading');
        		if (this.size) {
        			this.overallProgress.start(100);
        		} else {
        			this.overallProgress.set(0);
        			this.currentProgress.set(0);
        		}
        		submitForm(1);
        	},
        	
        	onStart: function() {
        		$('portrait-status').setStyle('display', 'block');
        	}
    	});
    	
    	uploaders.push(portaitUploader);
    	
    	
    	
    	
    	var cvUploader = new FancyUpload2($('cv-status'), $('cv-list'), {
    		verbose: false, // we console.log infos, remove that in production!!
    		
    		url: upload_url + '&section=cv',
    		path: swf_url,
    		typeFilter: {
    			'Documents (*.docx, *.doc, *.txt, *.rtf)': '*.docx; *.doc; *.txt; *.rtf;'
    		},
    		limitFiles: 1,
    		target: 'person_cv_file',
    		
    		onLoad: function() {
    			this.target.addEvents({
    				click: function() {
    					return false;
    				},
    				mouseenter: function() {
    					this.addClass('hover');
    				},
    				mouseleave: function() {
    					this.removeClass('hover');
    					this.blur();
    				},
    				mousedown: function() {
    					this.focus();
    				}
    			});
    		},
    		
    		onFail: function(error) {
    			switch (error) {
    				case 'hidden': // works after enabling the movie and clicking refresh
    					alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
    					break;
    				case 'blocked': // This no *full* fail, it works after the user clicks the button
    					alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
    					break;
    				case 'empty': // Oh oh, wrong path
    					alert('A required file was not found, please be patient and we fix this.');
    					break;
    				case 'flash': // no flash 9+ :(
    					alert('To enable the embedded uploader, install the latest Adobe Flash plugin.');
    			}
    		},
    		
    		onComplete: function() {
        		this.status.removeClass('file-uploading');
        		if (this.size) {
        			this.overallProgress.start(100);
        		} else {
        			this.overallProgress.set(0);
        			this.currentProgress.set(0);
        		}
        		submitForm(2);
        	},
        	
        	onStart: function() {
        		$('cv-status').setStyle('display', 'block');
        	}
    	});
    	
    	uploaders.push(cvUploader);
	}
};

var submitForm = function(num) {
    var allDone = true;
    uploaders.each(function(uploader) {
	    if (uploader.fileList.length && uploader.percentLoaded < 100) {
	        allDone = false;
	    }
	});
	
	if (allDone) {
        var participation_form = $('participation_form');
        if (participation_form)
            participation_form.submit();
    }
};

function CreateWorkItem() {
	var work_item = $('work_item');
	if(work_item) {
		var new_work_item = work_item.clone()
		.setProperty('id', 'work_item_'+work_item_count)
		.setStyle('opacity', 0)
		.injectInside('portfolio');
		
		new_work_item.getElements('.work_image').each(function(input) {
		    input.id = 'person_work_file_' + work_item_count;
		});
		new_work_item.getElements('.work-status-container').each(function(el) {
		    el.id = 'work-status-' + work_item_count;
		});
		new_work_item.getElements('.work-list-container').each(function(el) {
		    el.id = 'work-list-' + work_item_count;
		});
		
		new_work_item.getElements('.work_image').each(function(input) {
		    
		    var counter = work_item_count;
            
		    var workUploader = new FancyUpload2($('work-status-'+counter), $('work-list-'+counter), {
        		verbose: false, // we console.log infos, remove that in production!!
        		
        		url: upload_url + '&section=work&work_number='+counter,
        		path: swf_url,
        		typeFilter: {
        			'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
        		},
        		instantStart: false,
        		target: 'person_work_file_'+counter,
        		appendCookieData: true,
        		
        		onLoad: function() {
        			
        			// We relay the interactions with the overlayed flash to the link
        			this.target.addEvents({
        				click: function() {
        					return false;
        				},
        				mouseenter: function() {
        					this.addClass('hover');
        				},
        				mouseleave: function() {
        					this.removeClass('hover');
        					this.blur();
        				},
        				mousedown: function() {
        					this.focus();
        				}
        			});
        		},
        		
        		onFail: function(error) {
        			switch (error) {
        				case 'hidden': // works after enabling the movie and clicking refresh
        					alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
        					break;
        				case 'blocked': // This no *full* fail, it works after the user clicks the button
        					alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
        					break;
        				case 'empty': // Oh oh, wrong path
        					alert('A required file was not found, please be patient and we fix this.');
        					break;
        				case 'flash': // no flash 9+ :(
        					alert('To enable the embedded uploader, install the latest Adobe Flash plugin.');
        			}
        		},
        		
        		onComplete: function() {
            		this.status.removeClass('file-uploading');
            		if (this.size) {
            			this.overallProgress.start(100);
            		} else {
            			this.overallProgress.set(0);
            			this.currentProgress.set(0);
            		}
            		submitForm();
            	},
            	
            	onStart: function() {
            		$('work-status-' + counter).setStyle('display', 'block');
            	}
        	});
        	
        	uploaders.push(workUploader);
		    
        });
        
        work_item_count++;
        new_work_item.setStyle('opacity', 1);
        
        attachUploaderCompleteEvents();
    }
}