// Deferred values
Deferred = function(startFunction) {
    this.callbacks = [];
    this.errorCallbacks = [];
    this.start = startFunction;
}

Deferred.prototype.onResult = function(callback) {
    if (this.hasResult) {
        callback(this.result);
    } else {
        this.callbacks.push(callback);
        if (!this.started) {
            this.started = true;
            this.start();
        }
    }
    return this;
}

Deferred.prototype.onError = function(callback) {
    if (this.hasError) {
        callback(this.error);
    } else {
        this.errorCallbacks.push(callback);
        if (!this.started) {
            this.started = true;
            this.start();
        }
    }
    return this;
}

Deferred.prototype.gotResult = function(v) {
    this.hasResult = true;
    this.result = v;
    for (var i = 0; i < this.callbacks.length; i++) {
        this.callbacks[i](v);
    }
}
Deferred.prototype.gotError = function(v) {
    this.hasError = true;
    this.error = v;
    for (var i = 0; i < this.errorCallbacks.length; i++) {
        this.errorCallbacks[i](v);
    }
}

var savedPages = new Deferred(function() {
	var deferred = this;
	$.getJSON('/applications/my_content/saved_pages_json.rm?ts=' + (new Date).getTime(), function(json) {
		deferred.gotResult(json);
	});
})

var userDetails = new Deferred(function() {
	var userdeferred = this;
	$.getJSON('/applications/my_content/user_detail_json.rm?ts=' + (new Date).getTime(), function(json) {
		userdeferred.gotResult(json);
	});
})

$(function() {
	/* get AJAX content for logged-in user */
	loadMyContentSidebar();
	if (document.cookie.match(/KINGS_FUND_USER=/)) {
		savedPages.onResult(function(pages) {
			/* change 'add to my content' link to 'added' if current article is on the saved list */
			for (var i = 0; i < pages.savedPages.length; i++) {
				if (pages.savedPages[i].id == articleId) {
					$('.article-dates li.add').replaceWith('<li class="added first"><span>Added to my content</span></li>');
					$('.page-footer li.add').replaceWith('<li class="added"><span>Added to my content</span></li>');
					break;
				}
			}
		});
		if ($('#comment_name').length) {
			userDetails.onResult(function(user) {
				/* prepopulate comment name fields wherever used*/
				if(user.userDetails.length){
					$('#comment_name').addClass("prepopulated").attr("disabled","disabled").val(user.userDetails[0].firstname + ' ' + user.userDetails[0].secondname);
					$('#comment_email').addClass("prepopulated").attr("disabled","disabled").val(user.userDetails[0].emailaddress);
				}
			});
		}
	}
})

function loadMyContentSidebar() {
	if (document.defaultMyContentSidebar && !document.cookie.match(/KINGS_FUND_USER=/)) {
		/* not logged in and default content available, so use that */
		var sidebar = $('#my_content_sidebar');
		sidebar.html(document.defaultMyContentSidebar);
		applyMyContentSidebarBehaviour(sidebar);
	} else {
		var sidebarUrl = '/applications/my_content/sidebar.rm?ts=' + (new Date).getTime();
		/* pick up loginDestination from global variable set in top include */
		if (document.loginDestination && document.loginDestination != '') {
			sidebarUrl += '&login_destination=' + encodeURIComponent(document.loginDestination);
		}
		$('#my_content_sidebar').load(sidebarUrl, function() {
			applyMyContentSidebarBehaviour($(this));
		});
	}
}

function applyMyContentSidebarBehaviour(jqObj) {
}
