var TaskManager = Class.create();
TaskManager.prototype = {
	initialize : function(id){
		this.id = id;
		this.element = $(id);
		this.style = this.element.style;
		this.items = [];
		this.windowStartPos = {x:0, y:0};
	},
	addTask : function(task) {
		this.setTaskLocation(task);
		task.element.setStyle({zIndex: this.items.length});
		this.items.push(task);
		var taskButton = $(document.createElement("div"));
		taskButton.className = "taskButton";
		taskButton.style.cursor = "pointer";
		if(task.icon) {
			var iconImg = new Image();
			iconImg.src = task.icon;
			iconImg.style.width = "100%";
			iconImg.style.height = "100%";
			taskButton.appendChild(iconImg);
		}
		Event.observe(taskButton, "click", function(event) {
			task.toggle();
		});
		Event.observe(taskButton, "mouseover", function(event) {
			task.showToolTip(Event.pointerX(event), Event.pointerY(event));
		});
		Event.observe(taskButton, "mouseout", function(event) {
			task.hideToolTip(Event.pointerX(event), Event.pointerY(event));
		});
		Event.observe(taskButton, "contextmenu", function(event) {
			Event.stop(event);
		});
		task.taskButton = taskButton;
		this.element.appendChild(taskButton);
	},
	TASK_WINDOW_START : {x: 100, y: 30},
	TASK_WINDOW_DIFFSIZE : 30,
	setTaskLocation : function(task) {
		var taskPos = {x: this.TASK_WINDOW_START.x + this.windowStartPos.x,
						y: this.TASK_WINDOW_START.y + this.windowStartPos.y};
			this.windowStartPos.x += this.TASK_WINDOW_DIFFSIZE;
			this.windowStartPos.y += this.TASK_WINDOW_DIFFSIZE;
		if(this.windowStartPos.x > document.body.clientWidth*(0.3) ||
			this.windowStartPos.y > document.body.clientWidth*(0.3)) {
			this.TASK_WINDOW_START.x = this.TASK_WINDOW_START.x + this.TASK_WINDOW_DIFFSIZE;
			this.windowStartPos = {x: 0, y: 0};
		}
		task.setPosition(taskPos);
	},
	arrangeZindex : function(select) {
		this.setToFront(this.items[select]);
	},
	setToFront : function(aItem) {
		this.items = this.items.without(aItem);
		this.items.push(aItem);
		this.items.each(function(item, index) {
			item.element.setStyle({zIndex: index});
		});
	},
	closeTask : function(title) {
		var found = this.find(title);
		if(found)
			this.removeTask(found);
	},
	find : function(title) {
		var found = false;
		this.items.each(function(item) {
			if(title == item.title) {
				if(window.debug)
					window.jLog.log(item.title);
				found = item;
				throw $break;
			}
		});
		return found;
	},
	removeTask : function(task) {
		var taskPartition = this.items.partition(function(aTask) {
			return aTask == task || (aTask.parent == task);
		});
		this.items = taskPartition[1];
		taskPartition[0].each(function(aTask) {
			aTask.element.getElementsBySelector("*").each(function(elem) {
				elem.parentNode.removeChild(elem);
			});
			aTask.close();
			aTask.taskButton.parentNode.removeChild(aTask.taskButton);
			aTask.element.parentNode.removeChild(aTask.element);
			delete aTask;
			aTask = null;
		});
	}
};