//////////////////////////////////////////////////////////////////////////////
// getProfileWidgets() takes a userId and makes a call to DWR for that user's widgets
// it converts the DWR objects to widgetConfigs and sends them to CommunityWidgetManager for processing
//////////////////////////////////////////////////////////////////////////////

// these variables need to be global
var myWidgetMgr, userId, userName, loggedInUserId, loggedInUserStatus;

YAHOO.util.Event.onDOMReady( function() {

		userId = document.getElementById('txtUserId').value;
		userName = document.getElementById('txtUserName').value;
		try { loggedInUserId = document.getElementById('txtLoggedInUserId').value; } 
		catch(e) { loggedInUserId = 0; }
		try { loggedInUserStatus = document.getElementById('txtLoggedInUserStatus').value; } 
		catch(e) { loggedInUserStatus = ''; }
		var pageViewScope = document.getElementById('txtPageViewScope').value;
		var widgetConfigs = new Array();
		
		// this connects to dwr and gets the users widgets
		function getProfileWidgets(userId){
			WidgetLibraryService.getUserProfileWidgetsForUser(userId, processProfileWidgets);
		}
		
		// this drops the users widget data into an array of configs that the widget manager can use to build the widgets
		function processProfileWidgets(data){
			var userProfileWidgets = data;
			for (var i=0; i<userProfileWidgets.length; i++) {
				var widgetConfig = {
					userId : userId,
					userName : userName,
					userProfileWidgetId : userProfileWidgets[i].userProfileWidgetId,
					widgetId : userProfileWidgets[i].widget.widgetId,
					widgetType : userProfileWidgets[i].widget.widgetTypeCode,
					widgetUrl : 'http://family.go.com/xml/community/widgets/' + userProfileWidgets[i].widget.widgetTypeCode + '.html',
					widgetTitle : userProfileWidgets[i].widget.title,
					widgetRow : userProfileWidgets[i].row,
					widgetColumn : userProfileWidgets[i].col,
					widgetState : userProfileWidgets[i].widgetStateCode, // expanded, collapsed or removed
					//viewType : userProfileWidgets[i].viewType, // viewType is now set with page variable 'pageViewScope' instead of what's passed in with the widget
					viewType : pageViewScope, // public or private
					static : userProfileWidgets[i].widget.featured,
					urlSafeTitle : userProfileWidgets[i].widget.urlSafeTitle,
					sponsorAd : userProfileWidgets[i].widget.sponsorAd
				}
				widgetConfigs.push(widgetConfig);
			}
			
			// this sends the array of configs into the widget manager
			myWidgetMgr = new CommunityWidgetManager(widgetConfigs);
		}
		
		// this kicks everything off
		YAHOO.util.Event.onContentReady('profile_container', function(){
				getProfileWidgets(userId);
			});
		
	});

/*
DWR is sending us an array of UserProfileWidget objects that look like this:

UserProfileWidget
	Integer userProfileWidgetId; //unique id
	Integer userId; //the user that has this instance...used in back end
	Integer row;
	Integer col;
	String widgetStateCode; //"state" of the widget, if its minimized, etc.
	String viewType; //public, private
	Widget widget;
	
Widget
	Integer widgetId; //unique id
	String widgetCategoryCode;
	String widgetTypeCode; // type of the widget ex: "about me",
	String title;
	String description;
	Boolean isActive; //needed for backend
	Boolean isSystem; //needed for backend
	Boolean featured;
	Date createDate; //needed for backend
	Integer downloadCount; //needed for backend
	Integer parentContentId; //this is where the widget will get its info from...needed for backend
	UploadedImage image; //needed for backend
*/