$(document).ready(function() {
	//$('#browse').flash(
	  //  { expressInstall: true }
	//);
	$(this).pngFix();
	$('#browse').fileUpload({
		'uploader': './js/plugins/upload/upload.swf',
		'script': './index.php',
		'cancelImg': './images/delete.png',
		'buttonImg': './images/browse.jpg',
		'width': 168,
		'height': 53,
		'multi': true,
		'simUploadLimit': 1,
		'sizeLimit':  42949672960,
		'displayData': 'percentage',
		'onSelectOnce': function(){
			$('#upload').show();
			$('#passwordContainer').show();
		},
		'onAllComplete': function() {
			$('#upload').hide();
			$('#passwordContainer').hide();
				var caption = null;
				var time = 10000;
				var body = ('<img src="css/images/check.jpg" width="128" height="128" align="left"><p>Your files have been uploaded successfully. You may now access them from the "My Stuff" pull down menu. Remember, files are only kept for 10 days.</p>');
				notification(caption, body, time);
				$('#password').val('');
		}
	});
	
	function notification(caption, body, time) {
		$('#notTitle').html(caption);
		$('#notBody').html(body);
		$('#notification').fadeIn();
		setTimeout("$('#notification').fadeOut()", time);
	}
	$('#close').click(function() {
		$('#notification').fadeOut();
		return false;
	});
	
	function startUploadProcess(){
		if($('#password').val() == ''){
			alert('You must create a password in order to access these files later!');
		}else{
			$('#browse').fileUploadSettings('scriptData','&password='+$('#password').val());
			$('#upload').hide();
			$('#passwordContainer').hide();
			$('#browse').fileUploadStart();
			return false;
		}
	}
	$('#upload').click(function() {
		startUploadProcess();
		return false;
	});

	$('#password').keydown(function(e){
		if(e.keyCode == 13){
			startUploadProcess();
		}
	})
	//Feedback routines
	$('#feedBackDrawer').toggle(
		function() {
			$('#feedBackContainer').animate({
				top: '0px'
			}, 1300);
			return false;
		},
		function() {
			$('#feedBackContainer').animate({
				top: '-255px'
			}, 1300);
		return false;
		}
	)

	var options = {
		success: thankYou
	};

	$('#feedBackForm').submit(function() {
		$(this).ajaxSubmit(options);
		return false;
	});

	function thankYou() {
		alert('Thanks for the feedback!');
		$('#feedBackContainer').animate({
			top: '-255px'
		}, 1300);
		$(':text').val('');
		$('#feedBackBody').val('');
	return false;
	}
	$('#myStuff').toggle(
		function() {
			$('#filePasswordContainer').animate({
				opacity: 'toggle',
				marginTop: "29px"
			}, 1300);
			$('#filePassword').focus();
			$('#filePassword').keydown(function(e){
				if(e.keyCode == 13){
					getDownload($('#filePassword').val());
				}
			})
		},
		function() {
			$('#filePasswordContainer').animate({
				opacity: 'toggle',
				marginTop: "0px"
			}, 1300);
			$('#filePassword').val('');
		}
	);

	$('#goGetIt').hover(
	function() {
		$(this).css('cursor','pointer');
	}, null
	);

	function getDownload(pass){
		$('#downloadFiles').html('<div id="loading"><img src="images/loading.gif" />');
		$('#downloadFiles').load('index.php?ajax=true&password='+pass);
		$('#downloadFiles').dialog({
			height: 550,
			width: 700,
			draggable: false,
			maximizable: true,
			resizable: false,
			show: 'blind',
			title: 'Download Files',
			zIndex: 5000,
			bgiframe: true,
			modal: true,
			close: function() {
				$(this).html('');
				$(this).dialog('destroy');
		 }
		 });
	}

	$('#goGetIt').click(function() {
		getDownload($('#filePassword').val());
	});
});