if (typeof Mendeley === 'undefined') { Mendeley = {}; }
if (typeof Mendeley.Oapi === 'undefined') { Mendeley.Oapi = {}; }

Mendeley.Oapi = {
	loginUser:  function() {
		var action = https_url + '/login/';
		var formData = jQuery('#login-form').serialize();
		jQuery.post(action, formData, function(response){
			if(response.success) {
				document.location.href = hroot + response.redirect;
			}
			else {
				jQuery('#login-fail').html(response.errorMessage);
				jQuery('#login-fail').fadeIn();
			}
		}, 'json');
	},

	registerApplication: function() {
		jQuery('#register-button').attr('disabled', 'disabled');
		// Application title and URI should be required
		var appTitle = jQuery('input[name=application_title]');
		var appURI = jQuery('input[name=application_uri]');
		jQuery('.formerror_msg').remove();
		jQuery('.formerror').removeClass('formerror');
		if(appTitle.val() == '') {
			jQuery('#register-button').removeAttr("disabled");
			if(appTitle.val() == '') {
				appTitle.addClass('formerror');
				appTitle.after('<div class="formerror_msg"">Please provide an application title.</div>');
			}
		} else {
			var formData = jQuery('#app-register-form').serialize();
			var action = hroot + '/applications/register/';
			jQuery.post(action, formData, function(response) {
				if(response.success) {
					document.location.href = hroot+'/applications/';
				} else {
					jQuery('#register-button').removeAttr("disabled");
					if(response.errors.title) {
						appTitle.addClass('formerror');
						appTitle.after('<div class="formerror_msg">Please provide an application title.</div>');
					}
				}
			}, 'json');
		}
	},

	updateApplication: function() {
		// Application title and URI should be required
		var appTitle = jQuery('input[name=application_title]');
		var appURI = jQuery('input[name=application_uri]');
		jQuery('.formerror_msg').remove();
		jQuery('.formerror').removeClass('formerror');
		jQuery('#update-message').fadeOut();		
		if(appTitle.val() == '' || appURI.val() == '') {
			if(appTitle.val() == '') {
				appTitle.addClass('formerror');
				appTitle.after('<div class="formerror_msg">Please provide an application title.</div>');
			}
			if (appURI.val() == '' || appURI.val() == 'http://') {
				
				appURI.addClass('formerror');
				appURI.after('<div class="formerror_msg">Please provide an application URL.</div>');
			}
		} else {
			var formData = jQuery('#app-update-form').serialize();
			var action = hroot + '/applications/update/';
			jQuery.post(action, formData, function(response) {
				if(response.success) {
					jQuery('#update-message').html(response.message);
					jQuery('#update-message').fadeIn();
				} else {
					if(response.errors.title) {
						appTitle.addClass('formerror');
						appTitle.after('<div class="formerror_msg">Please provide an application title.</div>');
					}

					if(response.errors.uri) {
						appURI.addClass('formerror');
						appURI.after('<div class="formerror_msg">Please provide an application URL.</div>');
					}
				}
			}, 'json');
		}
	},

	deleteApplication: function(id) {
		var answer = window.confirm('Are you sure you want to delete this application?');
		if(answer) {
			var action = hroot+'/applications/delete/';
			var formData = 'application_id='+id;

			jQuery.post(action, formData, function(response) {
				jQuery('#delete-message').html(response.message);
				jQuery('#delete-message').fadeIn();
				setTimeout(function() { jQuery('#delete-message').fadeOut() }, 4000);
				jQuery('#app_'+id).fadeOut();
				jQuery('#app_'+id).remove();
			}, 'json');

		}
	},

	showApplicationDetails: function() {
		
	}
}


if (window.addEventListener) {
	var kkeys = [], dr = "68,82,65,84,87"; // All the way.
	window.addEventListener("keydown", function(e) {
		kkeys.push(e.keyCode);
		if (kkeys.toString().indexOf(dr) >= 0) {
			$('.DRATW').fadeIn(); // Oh my god!
			kkeys = [];
			e.preventDefault();
			// It's so intense...
		}
	}, true);
}

