
$(document).ready(function(){
	
	/*
     * Hide and show user notifications
     */
    if($('.notifications').length) {
        $('.notifications').animate({opacity: 1.0}, 5000).slideUp('fast');
    }
    
    /*
     * Hilight error inputs
     */
    $('form span.help.error').each(function(){
    	$(this).prev().css('borderColor', $(this).css('color'));
    });
	
    if($('.uploadtrack').length) {

        $(".uploadtrack .button.remove").click(function() {
            var input = $(this).parent().find("input[type=file]").get(0);
            $(input).replaceWith($(input).clone(true));

            $(this).parent().removeClass('hasfile');
            $(this).parent().find('.button.upload').show();
            $(this).hide();
            $(this).parent().next().text('');
        });
        
        $('.uploadtrack').each(function(){
            $(this).find('.button.remove').hide();
        	if(!$(this).find('.error').length)
        		$(this).addClass("hidden");
        });
        if($('.uploadtrack:not(.hidden)').length == 0)
    		$('.uploadtrack.hidden:first').removeClass('hidden');
        
        var checkExtension = function(input) {
        	var valid = { '.mp3':1, '.aac':1, '.wav':1, '.aiff':1, '.aif':1 }
            var re = /\.\w+$/;
            var ext = input.value.toLowerCase().match(re);
            if (ext in valid) {
              return true;
            } else {
              alert("Invalid audo file.\n\nWe accept wav, aiff and mp3 although you should choose an uncompressed format such as wav of aiff.");
              input.value = "";
              return false;
            }
        }
        
        $(".uploadtrack input[type=file]").change(function() {
        	if(checkExtension(this)) {
            	$(this).parent().parent().next().text("You selected: "+this.value.substring(0,40));
            	$(this).parent().parent().parent().addClass('hasfile');
                $(this).parent().parent().find('.button.remove').show();
                $(this).parent().parent().find('.button.upload').hide();
            	$('.uploadtable .hasfile:last').nextAll('tr').eq(0).removeClass('hidden'); 
        	}
        });

		$('#uploadform').uploadProgress({
	        progressBar: '#progress .bar .fill',
	        progressUrl: '/upload/progress/',
	        start: function() {
				
				$('#uploadform').children().hide();
		        $('#progress').css('display','block').fadeIn();
		        	
		    	if ($.data(this, 'submitted')) return false;
		    	$("#uploadform button[type=submit]").hide();
		    	$.data(this, 'submitted', true);
	        },
	        jqueryPath: "/media/js/jquery-1.3.2.min.js",
	        uploadProgressPath: "/media/js/jquery.uploadProgress.js",
	        uploading: function(upload) {
	        	if (upload.percents == 100)
	                window.clearTimeout(this.timer);
	            $('#progress .text').text("Time remaining: "+upload.time);
	            $('#progress .smalltext').text("Currently uploading: "+upload.file);
	        },
	        interval: 2000
        });
    }
    
});



