var wikiObj = {
	downloadURL: '/download/',
	dispatchPath : "/upload/_dispatch.php",
	detailsLoc : "/upload/_details.php"
};

$(function() {
	$('#fileid, #filepassword').trigger('focus');
	$("#switch").toggle(
		function () {
			$("#switch").blur();
			$("#d_but").fadeOut("slow");
			$("#proper").attr('class', 'proper');
			$("#block").slideToggle(800, function (){
				$("#ffname").focus();
			});
		},
		function () {
			$("#switch").blur();
			$("#d_but").fadeIn("slow");
			$("#block").slideToggle(500, function (){
				$("#proper").attr('class', 'noproper');
			});
		}
	);

	$("#protected, label[for=protected]").bind("click", function() {
		if( $('#protected').is(":checked") ) {
			$("#confpassw, #passw").attr("class","inputfield").attr("readonly", "").trigger('focus');
			$("label[for=passw],label[for=confpassw], ").attr("class", "");
		} else {
			$("#passw, #confpassw").attr("class","disabledfield").attr("readonly", "readonly");
			$("label[for=passw],label[for=confpassw]").attr("class", "passive");
		}
	});

	$(".linkfield").bind("click", function(e) {
		$(this).select();
	});
	
	$("#dbi_but").bind("click", function() {
		var fid = $.trim($('#fileid').val());
		if( !fid ) {
			$('#fileid').val('')
			alert("Please enter file id");
			$('#fileid').trigger("focus");
			return false;
		}
		window.location = wikiObj.downloadURL + fid + "/";
		return false;
	});

	$("#downloadform").bind("submit", function() {
		if( $("#filepassword").length ) {
			if( $.trim($("#filepassword").val()) == '' ) {
				alert("Please, enter password for the file!");
				$("#filepassword").trigger("focus");
				return false;
			}
		}
		return true;
	});

	$("#upd_but").bind("click", function(e) {
		if( $('#protected').is(":checked") ) {	
			if( $.trim($('#passw').val()) == '' ) {
				alert('You should specify the password if you want to protect the file!');
				$('#passw').trigger("focus");
				return false;
			}	
			if( $('#passw').val() != $('#confpassw').val() ) {
				alert('The password and the retyped passwords do not match! Please, retype the password once again!');
				$('#confpassw').val('').trigger("focus");
				return false;
			}	
		}
		$('#act').val('update');
		return true;
	});
});