// timer for the loading message
var timer;

pageEvents = function() {
	return {
		init: function(wishpotUserId) {
			// setup the loading message animation			
			YAHOO.util.Event.onAvailable('wishpot-loading-message', this.loadingMessageHandler);
			
			// setup the call to the Wishpot feed 
			YAHOO.util.Event.onContentReady('c-wishpot-registry', this.getRssHandler, wishpotUserId);
		},
		
		// Makes a call to the PHP proxy that makes the actual RSS call to Wishpot to 
 		// get wedding registry data.
		getRssHandler: function(wishpotUserId) {
			// entryPoint is the base URL
			entryPoint = '/apps/public/mysite/wishpot_feed.php';
			queryString = encodeURI('?wishpot_user_id=' + wishpotUserId);
			sUrl = entryPoint + queryString;
			request = YAHOO.util.Connect.asyncRequest('GET', sUrl, { success:successHandler, failure:failureHandler });
		},
		
		// kicks off the loading animation
		loadingMessageHandler: function() {
			showLoadingMessage();
		}
	}
}();

/* Displays an animated loading message while the Wishpot RSS feed loads. */
function showLoadingMessage() {
	var div = document.getElementById('wishpot-loading-message');
	div.innerHTML = div.innerHTML + ".";
	timer = setTimeout("showLoadingMessage()", 250);
}

/*
 * Callback function for successful retrieval of Wishpot registry HTML. 
 */
function successHandler(o){
	clearTimeout(timer);
	var div = document.getElementById('c-wishpot-registry');
	div.innerHTML = o.responseText; 
}

/*
 * Callback function for failed retrieval of Wishpot registry HTML. 
 */
function failureHandler(o){
	var div = document.getElementById('c-wishpot-registry');
	div.innerHTML = 'ERROR: Unable to retrieve wedding registry items from Wishpot. (' + o.status + " " + o.statusText + ')';
}

