var WORDPRESS_DS_UTILS = {
	/**
	 * Resize the photo according the given max dimensions.
	 * 
	 * @param {Object} image
	 * @param {Object} maxwidth
	 * @param {Object} maxHeight
	 */
	resizePhoto: function(image, maxwidth, maxHeight){
		var currentWidth, currentHeight;
		
		currentWidth = image.width;
		currentHeight = image.height;
		
		if (currentWidth > maxwidth || currentHeight > maxHeight) {
			if (currentWidth > maxwidth) {
				currentHeight = Math.floor(currentHeight * (maxwidth / currentWidth));
				currentWidth = maxwidth;
			}
			
			if (currentHeight > maxHeight) {
				currentWidth = Math.floor(currentWidth * (maxHeight / currentHeight));
				currentHeight = maxHeight;
			}
		}
		
		image.width = currentWidth;
		image.height = currentHeight;
		
		image.parentNode.parentNode.style.width = currentWidth + 2 + 'px';
		image.parentNode.parentNode.style.visibility = 'visible';
		image.parentNode.parentNode.style.position = 'relative';
		
		YAHOO.util.Dom.setStyle('rightsidebar', 'height', 'auto');
		YAHOO.util.Dom.setStyle('leftsidebar', 'height', 'auto');
		WORDPRESS_DS_UTILS.adjustHeight();
	},
	
	/**
	 * Adjust the height of the sidebars.
	 */
	adjustHeight: function() {
		var left = YAHOO.util.Dom.getRegion('leftsidebar');
		var leftHeight = left.bottom - left.top;
		var content;
		var contentHeight;
		var height;
		
		if (YAHOO.util.Dom.getRegion('content')) {
			content = YAHOO.util.Dom.getRegion('content');
			contentHeight = content.bottom - content.top;

			if (contentHeight > leftHeight) {
				height = contentHeight; 
			} else {
				height = leftHeight;
			}
		}
		
		if (YAHOO.util.Dom.getRegion('content-home')) {
			var right = YAHOO.util.Dom.getRegion('rightsidebar');
			var rightHeight = right.bottom - right.top;
			content = YAHOO.util.Dom.getRegion('content-home');
			contentHeight = content.bottom - content.top;

			if (contentHeight > leftHeight && contentHeight > rightHeight) {
				height = contentHeight; 
			} else if (leftHeight >  contentHeight && leftHeight > rightHeight) {
				height = leftHeight;
			} else {
				height = rightHeight;
			}
			
			YAHOO.util.Dom.setStyle('rightsidebar', 'height', height + 'px');
		}
			
		YAHOO.util.Dom.setStyle('leftsidebar', 'height', height + 'px');
	}
};

var WORDPRESS_DS_AJAX = {
	phpBBUsersOnlineSuccess: function(o) {
		if(o.responseText !== undefined) {
			var data = YAHOO.lang.JSON.parse(o.responseText); 
			var title = YAHOO.util.Dom.get('ds-wp-phpbb-users-online-title');
			if (title) {
				title.innerHTML = 'Wie is er online (' + data.total + ')';
			}
			
			var content = YAHOO.util.Dom.get('ds-wp-phpbb-users-online-content');
			if (content) {
				content.innerHTML = 'Gasten: ' + data.guests + '<br/>' + data.users;
			}
		}
	},

	phpBBUsersOnlineFailure: function(o) {
		var content = YAHOO.util.Dom.get('ds-wp-phpbb-users-online-content');
		if (content) {
			content.innerHTML = 'Info niet beschikbaar';
		}
	},
	
	chatUsersOnlineSuccess: function(o) {
		if(o.responseText !== undefined) {
			var total = 0;
			var data = YAHOO.lang.JSON.parse(o.responseText); 
			
			var content = YAHOO.util.Dom.get('ds-wp-chat-users-online-content');
			if (content) {
				var contentHtml = '';
				for (var r = 0; r < data.length; r++) {
					contentHtml += '<h2>' + data[r].room + '</h2>';
					contentHtml += '<ul>';
					
					var users = data[r].users;
					if (users) {
						for (var u = 0; u < users.length; u++) {
							contentHtml += '<li>' + users[u] + '</li>';
							total++;
						}
					}
					
					contentHtml += '</ul>';
				}
				
				if (data.length === 0) {
					content.innerHTML = 'Er zijn geen gebruikers aan het chatten';
				} else {
					content.innerHTML = contentHtml;	
				}
			}

			var title = YAHOO.util.Dom.get('ds-wp-chat-users-online-title');
			if (title) {
				title.innerHTML = 'Wie is er in de chat (' + total + ')';
			}
		}
	},
	
	chatUsersOnlineFailure: function(o){
		var content = YAHOO.util.Dom.get('ds-wp-chat-users-online-content');
		if (content) {
			content.innerHTML = 'Info niet beschikbaar';
		}
	}
}