/***
 * Creates new box manager manages all boxes on current site
 */
function boxManager() {
	this.boxes = new Array();

	this.boxesforclick = true;
}

// Close boxes
boxManager.prototype.closeBoxes = function () {
	for(var b=0;b<this.boxes.length;b++) {
		this.closeBox(this.boxes[b].id);
	}
};

// Close boxes
boxManager.prototype.closeLastBox = function () {
	var lastBox = this.boxes.pop();

	if(lastBox) {
		this.boxes.unshift(lastBox);
		this.closeBox(lastBox.id);
	}
};

boxManager.prototype.setBox = function (box) {
	if(!box || !box.id) {
		return;
	}

	var notfound = true;
	for(var b=0;b<this.boxes.length;b++) {
		if(this.boxes[b].id == box.id) {
			notfound = false;
		}
	}
	
	if(notfound) {
		if(getObjectName(box) == 'box' || getObjectName(box) == 'calendar') {
			EventManager.initEventsForBox(box);
		}

		this.boxes.push(box);
	}

	this.setBoxAsTop(box);
};

boxManager.prototype.isBoxClosed = function (boxId) {
	for(var b=0;b<this.boxes.length;b++) {
		if(this.boxes[b].id == boxId) {
			return this.boxes[b].isClosed();
		}
	}

	return true;
};

boxManager.prototype.getBox = function (boxId) {
	for(var b=0;b<this.boxes.length;b++) {
		if(this.boxes[b].id == boxId) {
			return this.boxes[b];
		}
	}
	
	if(typeof this.getBox.arguments[1] != 'undefined') {
		var obj = this.getBox.arguments[1];
	} else {
		var obj = null;
	}
	if(typeof this.getBox.arguments[2] != 'undefined') {
		var title = this.getBox.arguments[2];
	} else {
		var title = '';
	}
	if(typeof this.getBox.arguments[3] != 'undefined') {
		var width = this.getBox.arguments[3];
	} else {
		var width = '';
	}
	if(typeof this.getBox.arguments[4] != 'undefined') {
		var height = this.getBox.arguments[4];
	} else {
		var height = '';
	}
	if(typeof this.getBox.arguments[5] != 'undefined') {
		var resizeable = this.getBox.arguments[5];
	} else {
		var resizeable = '';
	}

	return new box(boxId, obj, title, width, height, resizeable);
};

boxManager.prototype.showBox = function (boxId) {
	if(typeof this.showBox.arguments[1] != 'undefined') {
		var obj = this.showBox.arguments[1];
	} else {
		var obj = null;
	}
	if(typeof this.showBox.arguments[2] != 'undefined') {
		var title = this.showBox.arguments[2];
	} else {
		var title = '';
	}
	if(typeof this.showBox.arguments[3] != 'undefined') {
		var width = this.showBox.arguments[3];
	} else {
		var width = '';
	}
	if(typeof this.showBox.arguments[4] != 'undefined') {
		var height = this.showBox.arguments[4];
	} else {
		var height = '';
	}
	if(typeof this.showBox.arguments[5] != 'undefined') {
		var resizeable = this.showBox.arguments[5];
	} else {
		var resizeable = '';
	}

	var box = this.getBox(boxId, obj, title, width, height, resizeable);

	if(box) {
		box.showBox();
		this.setBox(box);
		
		return box;
	}
	
	return null;
};

boxManager.prototype.closeBox = function (boxId) {
	var box = this.getBox(boxId);

	if(box) {
		if(getObjectName(box) == 'filterbox') {
			box.closeFilterbox();
		} else if(getObjectName(box) == 'box') {
			box.closeBox();
		} else if(getObjectName(box) == 'calendar') {
			box.closeCalendar();
		}

		for(var b=0;b<this.boxes.length;b++) {
			if(this.boxes[b].id == box.id) {
				this.boxes.splice(b, 1);
				break;
			}
		}

		this.boxes.unshift(box);
	}
};

boxManager.prototype.setBoxAsTop = function (box) {
	if(!box || !box.id) {
		return;
	}
	
	if(getObjectName(box) == 'filterbox' ||
		getObjectName(box) == 'box' ||
		getObjectName(box) == 'calendar') {

		for(var b=0;b<this.boxes.length;b++) {
			if(this.boxes[b].id == box.id) {
				this.boxes.splice(b, 1);
				break;
			}
		}

		this.boxes.push(box);

		var newZIndex = 5000;
		for(var b=0;b<this.boxes.length;b++) {
			newZIndex += 25;
			SetStyle(this.boxes[b].divbox, 'zIndex', newZIndex);
		}
	}
};

boxManager.prototype.setBoxesOnClick = function(value) {
	if(value == 1 || value == true || value == '1') {
		this.boxesforclick = true;
	} else {
		this.boxesforclick = false;
	}
};
