
/*<![CDATA[*/

jQuery.download = function(url, data, method){
	//url and data options required
	if( url && data ){ 
		//data can be string of parameters or array/object
		data = typeof data == 'string' ? data : jQuery.param(data);
		//split params into form inputs
		var inputs = '';
		jQuery.each(data.split('&'), function(){ 
			var pair = this.split('=');
			inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />'; 
		});
		//send request
		jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
		.appendTo('body').submit().remove();
	};
};


$(document).ready(function(){

	/* example 1 */
	var button = $('#button1'), interval;
	new AjaxUpload(button,{
		//action: 'upload-test.php', // I disabled uploads in this example for security reasons
		action: 'upload.php', 
		name: 'myfile',
		onSubmit : function(file, ext){
		        if (! (ext && /^(mp3)$/.test(ext))){
		            // extension is not allowed
		            alert('Error: invalid file extension');
		            // cancel upload
		            return false;
		        }
			// change button text, when user selects file			
			button.text('Uploading');
			
			// If you want to allow uploading only 1 file at time,
			// you can disable upload button
			this.disable();
			
			// Uploding -> Uploading. -> Uploading...
			interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
		},
		onComplete: function(file, response){
			button.text('Upload');
						
			window.clearInterval(interval);
						
			// enable upload button
			this.enable();
			
			// add file to the list
			//('#example1 .files').text(file)
			var newname = file;
			$("#msgid1").html("<p><a href='./download.php?k=fdljl3423lk4jl23j4&q=" + file.substr(0,file.length - 4) + ".m4r'>Download</a></p>");
			$.download('./download.php','q=' + file.substr(0,file.length - 4) + '.m4r&k=fdljl3423lk4jl23j4','get' );


		}
	});

});/*]]>*/
