$(document).addEvent('endofbody', function(event) {
	ProfileChat.startUpCheck();
});

ProfileChat = new Class( {
	fetchId :-1,

	initialize : function(element) {
	    this.enabled = false;
		this.element = $(element);
		this.adopt();
		this.start();
	},

	adopt : function() {
		var div = $('profileActionBeboIm');
		if (!$defined(div)) {
			dbug.log('Bebo IM div not present');
			return;  //Bebo IM div not present; chat is not enabled or Profile.jsp showIM = false
		}
		if (typeof AimSession == "undefined") {
			dbug.log('AimSession does not exist');
			return;  //chat disabled; maybe by user.
		}
		var parent = div.getParent();
		if (parent.match('li')) {
			parent.addClass('bebo-im');  //AboutMe module has all links in li elements
		}
		img = new Element('img', {
			'class': 'aimState',
			'id': 'imgState',
			'width': '12px',
			'height': '12px',
			'src': '/img/chat/icon-offline.png',
			'alt': $V('Member','profileBeboStatusTitle') + ': offline',
			'title': $V('Member','profileBeboStatusTitle') + ': offline'
		});
		div.adopt(img);
		var span = new Element('span', {
			'class': 'text',
			'id': 'beboText',
			'text': $V('Member','profileBeboIMTitle')
		});
		div.adopt(span);
		var link = new Element('a', {
			'id': 'beboLink',
			'text': $V('Member','profileBeboIMTitle'),
			'href': '#'
		});
		div.adopt(link);
		link.addEvent('click', function(event) {
			 //launch Bebo IM with profile user
			Chat.focusConversation($V('Member','profileChatUsername'));
			return false;
		});
		$(document).addEvent('onPresenceState', function(buddy) {
			ProfileChat.getInstance().processPresenceState(buddy.aimId, buddy.state);
		});
		AimSession.getInstance().addEvent('connectionStateChange', function(inst, state) {
			ProfileChat.getInstance().startStopFetchPresence(state);
		});
		this.enabled = true;
	},

	start : function() {
		if (!this.enabled) {
			dbug.log('not enabled');
			return;
		}
		dbug.log('ProfileChat.start called. AimSession.getInstance().state=' + AimSession.getInstance().state);
		// start if online; give 1/2 sec delay for first fetchEvents to see if user is in buddylist
		// if not online, then connectionStateChange event will start
		if (AimSession.getInstance().state == 'online') {
			fetchId = this.fetchPresence.delay(500);
		}
	},
	
	processPresenceState : function(aimId, state) {
		dbug.log('processPresenceState aimId=' + aimId + ' state=' + state);
		var img = $('imgState');
		img.set('src', '/img/chat/icon-' + state + '.png');
		img.set('alt', $V('Member','profileBeboStatusTitle') + ': ' + state);
		img.set('title', $V('Member','profileBeboStatusTitle') + ': ' + state);
		$('beboText').setStyle('display', (state == 'offline') ? 'inline' : 'none');
		$('beboLink').setStyle('display', (state == 'offline') ? 'none' : 'inline');
		
		if (!$defined(Dock)) {
			dbug.log('no Dock');
			return;  // Bebo Chat UI not present
		}
		if (!Dock.initialized) {
			dbug.log('dock not initialized');
			return;  //Bebo Chat dock not started
		}

		$('profileActionBeboIm').set('styles',{
	         'display': 'block'
	    });
		
		var inst = ProfileChat.getInstance();
		inst.fetchId = inst.fetchPresence.delay(30000);
	},
	
	fetchPresence : function() {
		dbug.log('calling fetchPresence');
		var aimId = $V('Member','profileChatUsername');
		var inst = AimSession.getInstance();
		var buddy = $defined(inst.buddyList) ? inst.buddyList.get(aimId) : null;
		if ($defined(buddy)) {
			// aimId already in buddy list
			ProfileChat.getInstance().processPresenceState(aimId, buddy.state);
		} else {
			// fetch from WIM
			inst.fetchPresence(aimId);	
		}
	},
	
	startStopFetchPresence : function(state) {
		dbug.log('calling startStopFetchPresence state=' + state);
		var inst = ProfileChat.getInstance();
		if (state == 'online') {
			if (inst.fetchId == -1) {
				// give 1/2 sec delay to allow first fetchEvents to see if user is in buddylist
				inst.fetchId = inst.fetchPresence.delay(500);
			}
		} else {
			if (inst.fetchId != -1) {
				$try($clear(inst.fetchId));
				inst.fetchId = -1;
			}
		}
	}
});

ProfileChat.startUpCheck = function() {
	var el = $('profileActionBeboIm');
	if (!$defined(el)) {
		dbug.log('Bebo IM div not present');
		return;  //Bebo IM div not present; chat is not enabled or Profile.jsp showIM = false
	}
	
	var inst = ProfileChat.getInstance();	
};

ProfileChat.getInstance = function() {
	if (!$defined(ProfileChat.current)) {
		dbug.log('new ProfileChat instance');
		ProfileChat.current = new ProfileChat();
	}
	return ProfileChat.current;
};
