/*
thanks to everyone @ http://swfupload.org/ | http://code.google.com/p/swfupload/  :)
*/




var __server_available				=	1;
var __upload_id						=	'2171a79ea10cfbd8209371a8f7342779';
var __main_server_online			=	0;
var __upload_url 					= 	"http://ul1.s21.ifile.it/upload?2171a79ea10cfbd8209371a8f7342779";
var __success_url 					= 	"http://mihd.net/upload:result?uuid=2171a79ea10cfbd8209371a8f7342779,upload_server=1,server_id=21";
var __uploader						=	null;
var __file							=	null;
var	__success						=	false;



var __settings						= 	{
	flash_url: 								"http://static.mihd.net/default/libraries/swfupload_2.1.0/swfupload_f9.swf",
	upload_url: 							__upload_url,
	file_size_limit: 						"100 MB",
	file_types: 							"*.*",
	file_types_description: 				"All Files",
	file_upload_limit: 						1,
	debug: 									false,
	file_queue_error_handler: 				__fileQueueErrorEvent,
	file_dialog_complete_handler : 			__fileDialogCompleteEvent,
	upload_progress_handler : 				__uploadProgressEvent,
	upload_error_handler : 					__uploadErrorEvent,
	upload_success_handler : 				__uploadSuccessEvent,
	upload_complete_handler : 				__uploadCompleteEvent
};





var upload = {



	//start attaching events
	init:function(){
	
		if(  __server_available == 1 ){
		

			$("#uploadFormPanel").empty().append( '<br /><input type="button" name="selectBtn" id="selectBtn" value="Browse" />' );
			
			var selectBtn 	= new YAHOO.widget.Button("selectBtn"); 
			YAHOO.util.Event.addListener("selectBtn", "click", upload.selectFile ); 
			
			var uploadBtn 	= new YAHOO.widget.Button("uploadBtn"); 
			var cancelBtn 	= new YAHOO.widget.Button("cancelBtn");
		}
		else{
		
			$("#uploadNotificationPanel").css("color","red");
			$("#uploadNotificationPanel").empty().append( "No upload servers currently available" );
			/*$("#uploadNotificationPanel").empty().append( "<a style='color:orange;' href='http://blog.ifile.it/2008/03/maintenance-notice.html'>maintenance in progress, new uploads have been temporarily disabled</a>" );*/

		}
	},
	
	
	
	
	//select single file
	selectFile:function(){
	
		__uploader.cancelUpload(); //clear the queue just in case
		__uploader.selectFile();
	},
	
	
	
	
	
	
	
	//cancel upload
	failure:function(){
	
		$("#uploadNotificationPanel").empty().append( 'File Upload failed...' );
		window.location = '';
	},
	
	
	
	
	
	
	
	
	//once a file is selected go here
	selected:function(){
	
		__file	=	__uploader.getFile(); //get info about the file
		
		
		$("#uploadFormPanel").empty().append( 
'<input type="button" name="changeBtn" id="changeBtn" value="Change" />' + 
'<br /><br /><strong>' + this.getFileName() + '</strong>&nbsp;(' + upload.convertToMegaBytes( this.getFileSize() ) + ')' 
		);

		var changeBtn 	= new YAHOO.widget.Button("changeBtn"); 
		
		
		$("#uploadSubmitPanel").css("visibility","visible").css("display","block");  
		$("#uploadNotificationPanel").empty().append( 'Maximum filesize: 100MB' );
		
			
		YAHOO.util.Event.addListener("changeBtn", "click", upload.selectFile ); 
		YAHOO.util.Event.addListener("cancelBtn", "click", upload.cancel ); 
		YAHOO.util.Event.addListener("uploadBtn", "click", upload.start ); 
	},
	
	
	
	
	//gets the filename
	getFileName:function(){
	
		if( __file != null ){
		
			return __file.name;
		}
		else{
		
			return '';
		}
	},
	
	
	
	
	//gets the filesize
	getFileSize:function(){
	
		if( __file != null ){
		
			return __file.size;
		}
		else{
		
			return 0;
		}
	},
	
	
	
	//starts the upload
	start:function(){
	
		$("#uploadNotificationPanel").empty().append( 'File upload in progress, please be patient...' );
		$("#uploadSubmitPanel").css("visibility","hidden").css("display","none"); 
		$("#uploadCancelPanel").css("visibility","visible").css("display","block"); 
		$("#uploadFormPanel").css("visibility","hidden").css("display","none"); 
		$("#uploadProgressPanel").css("visibility","visible").css("display","block"); 
		$("#uploadProgressBarField").css("display","block");
		
		
		__uploader.startUpload();
	},
	
	
	//cancel upload
	cancel:function(){
	
		__uploader.cancelUpload(); //clear the queue just in case
		
		$("#uploadNotificationPanel").empty().append( 'File Upload cancelled...' );
		window.location = '';
	},
	

	
	
	
	//converts bytes to megabytes
	convertToMegaBytes:function( bytes ) {
			
		var mb		=	Math.round((bytes/[Math.pow(1024,2)])*1000000)/1000000;
		var mbStr 	= 	String(mb);
		var mbArr	=	mbStr.split('.');
		var	rhs		=	mbArr[1];
		
		if( rhs ){
			
			mbStr		=	mbArr[0] + '.' + rhs.substr(0,2) + 'MB';
		}
		else{
			
			mbStr		=	mbArr[0] + 'MB';
		}
		
		return mbStr;
	},
	
	
	//animates the background color as the upload progresses
	calcBackgroundColor:function( percentage ){
		
		//calculate bg color
		var new_color	=	'#e0f4fc';
		if( percentage < 10){
			
			new_color	=	'#d7eefb';
		}
		else if( percentage < 20 ){
			
			new_color	=	'#cfe8fa';
		}
		else if( percentage < 30 ){
			
			new_color	=	'#c6e2f9';
		}
		else if( percentage < 40 ){
			
			new_color	=	'#bddcf8';
		}
		else if( percentage < 50 ){
			
			new_color	=	'#b5d6f7';
		}
		else if( percentage < 60 ){
			
			new_color	=	'#acd0f6';
		}
		else if( percentage < 70 ){
			
			new_color	=	'#a3caf5';
		}
		else if( percentage < 80 ){
			
			new_color	=	'#9bc4f4';
		}
		else if( percentage < 90 ){
			
			new_color	=	'#92bef3';
		}
		else{
			
			new_color	=	'#8ab9f2';
		}
		
		return new_color;
	}
};
		






function __fileQueueErrorEvent(file, errorCode, message) {

	switch (errorCode) {
	case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
		alert("Files can not exceed 100MB.");
		break;
	case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		alert("Can not upload zero byte files.");
		break;
	default:
		upload.failure();
		break;
	}
}



function __fileDialogCompleteEvent(numFilesSelected, numFilesQueued) {

	//let the upload object handle this
	upload.selected();
}

		
function __uploadSuccessEvent(file, serverData) {
	
	__success	=	true;
}

function __uploadErrorEvent(file, errorCode, message) {
	
	__success	=	false;

	switch (errorCode) {
	case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
		alert("Upload Failed [#" + message + "]" );
		break;
	case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
		alert("Upload Failed [#1]");
		break;
	case SWFUpload.UPLOAD_ERROR.IO_ERROR:
		alert("Upload Failed [#2]");
		break;
	case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
		alert("Upload Failed [#3]");
		break;
	case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
		alert("Upload Failed [#4]");
		break;
	case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
		alert("Upload Failed [#5]");
		break;
	default:
		break;
	}

}


function __uploadCompleteEvent(file) {
	
	if( __success == true ){
	
		$("#uploadProgressPanel").css("visibility","hidden").css("display","none"); 
		$("#uploadCancelPanel").css("visibility","hidden").css("display","none"); 
		$("#uploadNotificationPanel").empty().append( '<a href="' + __success_url + '">File uploaded, redirecting...</a>' );
		
		window.location = __success_url;
	}
	else{
	
		upload.cancel();
	}
}



function __uploadProgressEvent(file, bytesLoaded, bytesTotal) {
	

	var percentage = Math.ceil((bytesLoaded / bytesTotal) * 100);

	//get the width of the upload box from the stylesheet
	var style_width = parseInt( 
		YAHOO.util.Dom.getStyle('uploadProgressPanel', 'width') 
	);

	//calculate new width
	w = style_width * bytesLoaded / bytesTotal;
     	

	//calculate new background color
	var new_color	=	upload.calcBackgroundColor( percentage );
		
		
	//animate progress bar
	var myAnim = new YAHOO.util.ColorAnim('uploadProgressBarField', {   
		width: { to: w },
		backgroundColor: { to: new_color }      
	}, 1, YAHOO.util.Easing.easeOut);   
	myAnim.animate();  
		
		
	
		
 	//show progress in megabytes
 	$("#uploadProgressBarField").empty().append( 

'<strong>' + percentage + 
'%</strong>&nbsp;&nbsp;&nbsp;' + upload.convertToMegaBytes( bytesLoaded ) + 
'&nbsp;/&nbsp;' + upload.convertToMegaBytes( bytesTotal )
	);
}





		
function init() {  

	__uploader = new SWFUpload( __settings );
	
	upload.init();
			
	$("#loading_marker").css("visibility","hidden"); 
}
YAHOO.util.Event.onDOMReady( init );  