	
	//hello check

	function time_left(size,bytesLoaded,rate) //calculates times left for upload
	{
		var bytes_left = size - bytesLoaded;
		var time_left = bytes_left/(rate);
		return time_left.round();
	}
	
var selectsuccess = false;
	
var SimpleProgress = new Class({
	
	Extends: Swiff.Uploader,
	
	options:{
		multiple:false //only one file at a time
	},
	
	initialize: function(options){
		
		$('fake_file_input').value = null;		
		
		options.target = $('js_file_input'); //this element will be used to open the file browser
		
		this.parent(options); //gets options
				
		this.addEvents({ //adds more event to the class to access below
			'selectSuccess': this.onSelectSuccess,
			'selectFail': this.onSelectFail,
			'fileProgress': this.onFileProgress,
			'fileComplete' : this.onFileComplete,
			'browse' : this.onBrowse,
			'start' : this.onStart,
			'setoptions' : this.setoptions
		});
		
		
	},
	
	onStart: function(){
		//verify shit
		$('uploadDialogue').setStyle('display','none');
		
		
		
		if(selectsuccess==false){return false;}
		
		window.onbeforeunload = function (evt) {
		  var message = 'Are you sure you want to leave?';
		  if (typeof evt == 'undefined') {
		    evt = window.event;
		  }
		  if (evt) {
		    evt.returnValue = message;
		  }
		  return message;
		}	
		
		//etc
	},
	
	onSelectSuccess: function(){ //if a file is selected list it's name and size, set data from text box ready for the request
		$('fake_file_input').value = this.fileList[0].name;
		// $('filesize').innerHTML = (this.fileList[0].size/1048576).round(1)+' mb';
var s =this.fileList[0].name
if(s.substr(-3)=='zip' || s.substr(-3)=='mp3' || s.substr(-3)=='rar' || s.substr(-3)=='wma' || s.substr(-3)=='ogg' || s.substr(-3)=='wav' || s.substr(-3)=='flac')
	{
	 document.getElementById("uploadSubmitButton").style.display='block';
	}
	else
	{
	alert("Not Allowed file type.\nAllowed file types zip,mp3,rar,wma,ogg,wav,flac.\nPlease choose valid file extension to upload the file");	
	document.getElementById("uploadSubmitButton").style.display='none';
	}
		this.setOptions({
			data: {
				loggedin: $('loggedin').value,
				upload_name: $('upload_name').value,
				upload_user_is_artist: $('upload_user_is_artist').checked,
				upload_artist: $('upload_artist').value,
				upload_genre: $('upload_genre').value,
				session_id: Cookie.read('PHPSESSID')
				}
		})
				
		selectsuccess = true;
	},
	
	onSelectFail: function(err){
		if(err[0].validationError=="sizeLimitMax"){alert("Sorry, that file is too big.")}
		else{alert(err[0].validationError)}
	},
	
	onFileProgress: function(){ //if a file is selected list it's name and size, format the units and text.
	
	$('downloadfeedback').setStyle('display','block');
	
	$('filename').innerHTML = this.fileList[0].name
	
		var percentLoaded = this.percentLoaded+"%";
		var rate = (this.rate/1048576).round(2);
		var bytesLoaded = (this.bytesLoaded/1048576).round(2);		
		var size = (this.size/1048576).round(2);		
		
		$('bar_inside').setStyle('width', this.percentLoaded+"%" );
		
		// $('percent_loaded').innerHTML = percentLoaded;
		$('filerate').innerHTML = rate+" mb/s";
		if(bytesLoaded = bytesLoaded.round(1)){bytesLoaded=bytesLoaded+"0"}
		var loaded = bytesLoaded+'/'+size+' mb';
		
		$('filebytesloaded').innerHTML = loaded
		
		document.title = loaded+" - Beatplexity"
		
		var filetimeleft = time_left(size,bytesLoaded,rate);
		
		var minutes = (filetimeleft/60).round();
		if(filetimeleft<minutes){minutes = minutes-1}
		var minutesseconds = filetimeleft - minutes*60;
		minutesseconds = minutesseconds + 30;
		var message
		
		if(filetimeleft == Infinity){ message = "Starting upload."}
		else if (filetimeleft > 0){ message =  minutes + " minutes and " + minutesseconds + " seconds remaining."}	
		else if (filetimeleft == 0){ message =  "Processing..."}	
		$('filetimeleft').innerHTML = message;
	},
	
	onFileComplete: function(arg){ //if the upload completes successfully display the respose from your php script.
		$('bar_inside').setStyle('width', "100%" );
		window.onbeforeunload = null;
		$('filetimeleft').innerHTML = "Upload complete."
		var id = arg.response.text.toInt();
		// alert(arg.response.text);
		
			
		
		
		
		if(id<1){
			alert('Sorry, an error occured, please try again.');
		}else{
			
			
		 /*===========================================================*/
var myRegExp = /Error/;
var string1 = arg.response.text;
var matchPos1 = string1.search(myRegExp);

if(matchPos1 != -1)
{
//alert('Sorry, an error occured, please try again.');
alert('Sorry, an error occured, please try again.! \r\n You Will be directed to the Upload page.');
window.location = "http://beatplexity.com/";
}
 else
{
$('themixpagelink').setStyle('display','block');
$('mixpagelink').innerHTML = "<a href='http://beatplexity.com/mix/?id="+arg.response.text+"' />http://beatplexity.com/mix/?id="+arg.response.text+"</a>";
alert('Beatplexity upload successful! \r\n You will now be directed to the mix page.');
window.location = "http://beatplexity.com/mix/?id="+arg.response.text;
}
 

		 /*===========================================================*/
			
			
			
			
		};
	},
	
	onBrowse: function(){ //reset everything
		this.remove();
		$('fake_file_input').value = "";
		$('filename').innerHTML = "";
		// $('filesize').innerHTML = "";
		//$('percent_loaded').innerHTML  = "";
		$('filerate').innerHTML = "";
		$('filebytesloaded').innerHTML = "";
		$('filetimeleft').innerHTML = "";
		
	},
	
	setoptions: function(){
		this.setOptions({
			data: {
				loggedin: $('loggedin').value,
				upload_name: $('upload_name').value,
				upload_user_is_artist: $('upload_user_is_artist').checked,
				upload_artist: $('upload_artist').value,
				upload_genre: $('upload_genre').value,
				session_id: Cookie.read('PHPSESSID')
				}
		})
	}
	
	
});

window.addEvent('domready', function() {  //add an event once the page is ready
	//alert('hello world');
	//return;
	$('js_file_input').setStyle('display','block');
	$('html_file_input').setStyle('display','none');
			
	var uploader = new SimpleProgress({
		path: '/upload/assets/scripts/fancyupload/Swiff.Uploader.swf', //location of flash file
		url: '/upload/upload.php', //location of php script to process uploaded file
		/*typeFilter: {
			'Files (*.mp3,*.zip)': '*.mp3,*.zip'
		},*/
		fileSizeMax: 0,
		appendCookieData: true,
		timeLimit: 0,
		verbose: false, //for debuggin
		debug: false // enable logs, uses console.log
	});
	
		$each($$('input[type=submit]'),function(el) {
			el.addEvent('click',function(e) {
				
				uploader.setoptions();
				
				
				if(!$('upload_agree').checked){alert('You have to agree to the terms and conditions in to be able to upload.'); return false;}
				if($('fake_file_input').value == ""){alert('Please select a file to upload.'); return false;}
				if($('upload_name').value == ""){alert('Please enter a name to continue.'); return false;}
				if($('upload_artist').value == ""){
					if($('upload_user_is_artist').checked && $('loggedin').value == "1")
					{}
					else
					{
					alert('Please enter a artist name to continue.'); return false;
					}
				}
				
				this.disabled = 1;
				this.set('value','Processing....');
				uploader.start();
				return false;
			}.bind(el));
		});
		
		$('uploadSubmitButton').disabled = 0;
		
	// $('cancel_upload').addEvent('click', function() {
	// 	uploader.stop(); // stop upload
	// 	return false;
	// });
});

