// moved the methods from the profile.jsp page to this javascript file to clean up profile page
// also modified the ajax call to use the net.js library
// also made this more object oriented
// we may need to make this a general library and use it on the various pages that make this call
// currently, each page includes a duplicate set of this code

function ProfileUpdater (varProfileUserId) {
	this.viewerId = '0';
	this.userIds = '';
	this.debug = false;
	this.isLive = 'false'; // not really used
	this.profileUserId = varProfileUserId;	
	this.userNameMap = new Object();

	this.ajaxProfileHelper = new net.ContentLoader( this, '/servlet/profile/updateProfile?addPageView=true', 'GET', [] );

	this.ajaxUpdate = function (request) {	
			var data = eval('(' + request.responseText + ')');	

			if (this.debug)
				alert(req.responseText);

			if (this.debug)
				alert('update buttons');
				
			var isLoggedIn = data.loggedIn;
			// remove As Friend, Bookmark me and send message buttons
			if (isLoggedIn == 'true') {
				this.viewerId = data.userId;
				var bStatus = data.buttonsStatus;
			} else {
				// do nothing
			}
			
			if (this.debug)
				alert('update mood');
			// update mood
			if(data.mood != 'null') {
				var imageIcon = '<img src="/images/en/dynamic/emoticons/' + data.mood + '.gif" border="0">';	
				var moodIcon = document.getElementById('moodIcon');
				var text = eval(data.mood);
				
				if(moodIcon) {
					moodIcon.innerHTML =  '<span>' + text + '</span><br>' + imageIcon;	
				}
			}
			
			if (this.debug)
				alert('update live guest count');
			
			var viewGuestCount = document.getElementById('viewGuestCount');
			viewGuestCount.innerHTML = '<span>' + data.viewGuestCount + '</span>';
			
			if (this.debug)
				alert('update live message');
			if (data.liveMessage != null) {
				var liveText = document.getElementById('liveMessageText');
				liveText.innerHTML = data.liveMessage;
				document.getElementById('liveMessage').style.display = 'block';
			}
			
			if(data.isLive == true){
				this.isLive = true; // not really used
				// don't need to do anything since large player is already displaying on the page
			} else {
				// write media player instead of host chat
				//if(myPlayerManager.valid == 1) {
					//writeMediaPlayerOnProfile(820, 490, 'http://player.stickam.com/flash/stickam/stickam_player.swf?app=stickam_media_player.swf&userID=' + this.profileUserId + '&langID=en&skinType=media&skinName=media0&', 'high'); 			
				//}
			}
			
			if (this.debug)
				alert('update statuses');
			// update status for all users
			for(var i=0; i<data.users.length; i++) {
				var userId = data.users[i].user.userId;
				var isOnline = data.users[i].user.status.online;
				var isChatroom = data.users[i].user.status.chatroom;
				var isLive = data.users[i].user.status.live;
				var roomId = data.users[i].user.status.roomId;
				
				if (this.debug)
					alert('userId:'+userId+', isOnline:'+isOnline+', isChatroom:'+isChatroom+', isLive:'+isLive);
					
				var status_tag_name = 'status_'+userId;
				
				if (this.debug)
					alert('process status image with id:"'+status_tag_name+'"');
					
				var statusImages = document.getElementsByName(status_tag_name);

				if (this.debug)
					alert('Found '+statusImages.length+' status images need to be updated for userId:'+userId);
					
				for (var j=0; j<statusImages.length; j++) {

					if (isOnline == 'false' && isChatroom == 'false' && isLive == 'false') {
						// do nothing, since offline image is already there
					} else {
						var newInnerHtml = '';
						
    					if (isOnline == 'true' && isChatroom == 'false' && isLive == 'false') {
    						newInnerHtml += '<a href="/profile/' + this.userNameMap[userId] + '" border="0" target="_top"><img src="/images/en/icons/icon_online.gif" title="Online Now" width="16" height="16" border="0"></a>&nbsp;';
    					}
    					
    					if (isLive == 'true') {
    						newInnerHtml += '<a href="javascript:;" onclick="MM_openBrWindow(\'/largeChat.do?uId=' + userId + '\',\'member_big_vchat_' + userId + '\',\'width=550,height=630\')" border="0" ><img src="/images/en/icons/icon_live.gif" title="Live" width="16" height="16" border="0"></a>&nbsp;';
						}    					
    					
    					if (isChatroom == 'true') {
    						newInnerHtml += '<a href="javascript:;" onclick="MM_openBrWindow(\'/member/loadChatRoom.do?roomId=' + roomId + '\',\'videochat\',\'width=900,height=630\')" border="0" ><img src="/images/en/icons/icon_chatroom.gif" title="Chat Room" width="16" height="16" border="0"></a>&nbsp;';
    					}    						
						
						if(isOnline == 'true') {
							newInnerHtml += '<a id="invitation_' +  userId + '" href="javascript:;" onClick="return openInvitation(this,\'invitationSubcontent\',\'leftbottom\',\'' + userId + '\',\'' + this.userNameMap[userId] + '\',1,-20,0);" border="0">';
							newInnerHtml += '<img src="/images/en/icons/icon_invite.gif" title="Live Invite" width="16" height="16" border="0">';
							newInnerHtml += '</a>&nbsp;';																	
						}
							
						statusImages[j].innerHTML = newInnerHtml;
					}
				}
			}
		};
	
	this.handleError= function (param) {
			//alert(["This is handleError,\nbut I'm not sure what to do about this:\n", param].join("\n"));
		};	

	this.updateStatus = function() {
				var userIdsParam = 'userIds=' + this.userIds;
				var profileUserIdParam = 'profileUserId=' + this.profileUserId;
				this.ajaxProfileHelper.sendRequest(userIdsParam, profileUserIdParam); 
	}
	
	this.registerMyId = function(userId, userName) {
		this.userIds += (userId+',');
		this.userNameMap[userId] = userName;
	}
}
