/**
 * The DockUpdater
 * 
 * @author		michael
 * @category	ZeitFür2.de
 * @package		public/scripts
 * @copyright	Copyright(c) 2009 - ZeitFür2.de
 */
var DockUpdater = {
	dockUpdater: null,
	onlineStatusUpdater: null,
	callback: null,
	/**
	 * Initializes the DockUpdater
	 * 
	 * @param function  callback  The callback function
	 */
	init: function(callback) {
		this.callback = callback;
		this.start();
	},
	/**
	 * Loads the chat
	 */
	refresh: function() {
		this.getChats();
	},
	/**
	 * Starts the DockUpdater
	 */
	start: function() {
		// immediate refresh
		this.refresh();
		// refresh chat every 5 s
		this.dockUpdater = setInterval('DockUpdater.refresh()', 5000);
		setTimeout("$.get('/dock/notify-online-status')", 2000);
		this.onlineStatusUpdater = setInterval("$.get('/dock/notify-online-status')", 30000);
	},
	/**
	 * Stops the DockUpdater
	 */
	stop: function() {
		clearInterval(this.dockUpdater);
	},
	/**
	 * Retrieves all open chats as JSON
	 */
	getChats: function() {
		var outer = this;
		$.getJSON('/chat/get-chats',
			function(data) {
				var showDockChatList = false;
				// display the count of chats
				$('#dock-chat-list-count').text(data.chats.length);
				$('#dock-chat-count').text(data.chats.length);
				// build the chat list
				if (data.chats.length>0) {
					// update the list
					$('#dock-chat-list-chats').empty();
					$.each(data.chats, function(i, item){
						var div = $('<div>').attr({
							'title': 'Chat mit '+item.partnerLoginName+' öffnen',
							'class': 'dock-chat-list-entry',
							'id': 'dock-chat-list-entry-'+item.chatId,
						});
						var link = $('<a>').attr({
							'href': '/chat/chat/?c='+item.chatId+'&TB_iframe=true&height=441&width=420'
						}).attr('class', 'thickbox').appendTo(div);
						var image = $('<img>').attr({
							'src': item.partnerThumbnail,
							'border': 0,
							'alt': '',
							'class': 'imgthmb'
						}).appendTo(link);
						// set news icon, only if chat window is closed
						if (item.news && $('#TB_iframeContent').length==0) {
							var newsDiv = $('<div>').attr({
								'id': 'dock-chat-list-entry-marker-'+item.chatId,
								'style': 'position: absolute;padding: 0px;left: 75px;top: 0px;display: inline'
							});
							var newsImage = $('<img>').attr({
								'src': '/images/icon_chat.gif',
								'border': 0,
								'alt': '',
								'style': 'width: 16px; height: 16px'
							}).appendTo(newsDiv);
							newsDiv.appendTo(div);
							showDockChatList = true;
						}
						div.appendTo('#dock-chat-list-chats');
					});
				} else {
					$('#dock-chat-list-chats').empty();
				}
				// set the chat list window state
				dockChatListWindowState = (showDockChatList || data.dockChatListWindowState=='block')?'block':'none';
				$('#dock-chat-list').css('display', dockChatListWindowState);
				// execute callback function
				outer.callback();
			}
		);
	},
	/**
	 * Sets the window state of the dock chat list window (visible/hidden)
	 */
	setChatListWindowState: function() {
		var outer = this;
		$('#dock-chat-list').toggle();
		$.ajax({
			type: 'GET',
			async: false,
			url: '/dock/set-dock-chat-list-window-state',
			data: {s: $('#dock-chat-list').css('display')},
			dataType: 'json',
			success: function(data){
			}
		});
	}
};
