// JavaScript Document

var cState = 0;
$(window).keydown(function(evt) {
	var key = evt.keyCode;
	switch (key) {
		case 80: cState = 1; break;
		case 69: if (cState == 1) {cState = 2;} else {cState = 0;} break;case 78: if (cState == 2) {cState = 3;} else {cState = 0;} break;
		case 73: if (cState == 3) {cState = 4;} else {cState = 0;} break;case 83: if (cState == 4) {cState = 5;} else {cState = 0;} break;
		default: cState = 0;
	}
	if (cState == 5) {if ($('#secret').length < 1) {$('#wrapper').append('<div id="secret" onclick="$(this).remove();"></div>');}}
});

function showRow(obj) {$(obj).find('.btn').addClass('mo');}
function hideRow(obj) {$(obj).find('.btn').removeClass('mo');}

function toggleLogin() {
	var iTop = parseInt($('#login').css('top'));
	$('#login').animate({top: ((iTop < 0) ? 0 : -28) + 'px'}, 200);
	if (iTop < 0) {$('#user').focus();}
}

function valAddClient() {
	if ($('#txtAClient').val() == '' || $('#txtALogin').val() == '' || $('#txtAPwd').val() == '') {
		alert('All fields must be completed to add a new client.');
		return false;
	}
	return true;
}

function valDelClient() {
	if ($('#txtDClient').val() == '') {alert('You must first select a client to delete.'); return false;}
	if (confirm("Deleting a user will also delete all of their files.\r\nClick 'OK' to delete '" + $('#txtDClient option:selected').text() + "'.")) {return true;} else {return false;}
}

function valDelFile() {
	if (confirm("You are about to delete:\r\n\r\n" + $('#txtDFile option:selected').text() + "\r\n\r\nClick 'OK' to delete this file.")) {return true;} else {return false;}
}

function valPwdClient() {
	if ($('#txtCClient').val() == '') {alert('You must first select a client\'s password to reset.'); return false;}
	if ($('#txtCPwd').val() == '') {alert('You must first enter a new password.'); return false;}
	return true;
}

function valUpload() {
	if ($('#txtUClient').val() == '') {alert('You must first select a client to upload a file for.'); return false;}
	if ($('#txtULabel').val() == '') {alert('You must first enter a file label before uploading.'); return false;}
	$('#fileUpload').uploadifySettings('folder', '/vault/'+$('#txtUClient').val());
	$('#fileUpload').uploadifyUpload();
}

function authenticate() {
	$.ajax({
		data:		{u: $('#user').val(), p: $('#pass').val()},
		type:		'POST',
		url:		'/_auth.php',
		success:	function(data) {
			if (data == 1) {window.location = '/client.php';} else {alert('Sorry, the information you provided does not match our records.');}
		}
	});
 return false;
}

$(document).ready(function() {
	jQuery.fn.getTitle = function() {
		var arr = jQuery("a.fancy2");
		jQuery.each(arr, function() {var title = jQuery(this).children("img").attr("title");jQuery(this).attr('title',title);});
	}

	/* Apply fancybox to multiple items */
	$("a.fancy").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'speedIn'					: 600,
		'speedOut'				: 200,
		'overlayColor'		: '#000',
		'titlePosition'		: 'inside'
	});
});
