/*
 * Box 
 */
function Box(){

	if (!isdefined("boxId"))
		boxId = 0;
	
	this.children = new Array();
}

Box.prototype.drawBox = true;
Box.prototype.title = "";
Box.prototype.template = "";
Box.prototype.banner = "";
Box.prototype.userId = 0;
Box.prototype.gameId = 0;
Box.prototype.starting = 1;
Box.prototype.number = 10;
Box.prototype.containerId = "";
Box.prototype.showId = "";
Box.prototype.id = "";
Box.prototype.children = null;
Box.prototype.owner = null;
Box.prototype.icon = "boxicons/default.png";
Box.prototype.singleChild = true;				//Can this type of object only appear as a child to another object once

Box.prototype.setTitle = function(title){
	
	this.title = title;
	return this;
}

Box.prototype.setBanner = function(banner){
	
	this.banner = banner;
	return this;
}

Box.prototype.setDrawBox = function(drawBox){
	
	this.drawBox = drawBox;
	return this;
}

Box.prototype.setUserId = function(userId){
	
	this.userId = userId;
	return this;
}

Box.prototype.setGameId = function(gameId){
	
	this.gameId = gameId;
	return this;
}

Box.prototype.setOwner = function(owner){
	
	this.owner = owner;
	return this;
}

Box.prototype.setNumber = function(number){
	
	this.number = number;
	return this;
}

Box.prototype.addTo = function(id,after){

	boxId++;	
	this.id = "#box_" + boxId;
	this.showId = this.id;
	var newBox;
		
	//Create the box
	if (this.drawBox){
		
		this.containerId = this.id + " .box_content";
		newBox = $("#div_template_box").clone().attr("id","box_" + boxId);
		newBox.find(".box_title_text").html(this.title);
		newBox.find(".box_close").click(this.jqueryCallbackRemoveFunc(this));
		
	} else {

		this.containerId = this.id;		
		newBox = $("<span></span>").attr("id","box_" + boxId);
	}
	
	//Add in the correct place in the DOM
	if (after == null){
		newBox.appendTo(id);
	} else {
		newBox.insertAfter(id + " " + after);
	}	
	
	if (this.drawBox){
		
		if (after == null)
			eventhook.addBox(this);
		else {	
			var children = $(id).children();
						
			var counter = -1;
			var box = this;
			children.each( function(){
				
				counter++;
				if ("#" + $(this).attr("id") == after) {
					eventhook.addBox(box, counter);
				}
			});			
		}	
	}
	
	//Add banner if required
	if (this.banner != ""){
		$('<img src = "' + this.banner + '"/>').appendTo(this.containerId);
	}
	
	//Add template if required
	if (this.template != ""){
		$(this.template).clone().css("display","").attr("id","").appendTo(this.containerId);
	}
	
	$(this.showId).show("fast");
	$(this.showId).find(".box_loading").effect("pulsate",{},1000,null);
	this.addContent();
}

Box.prototype.loaded = function(obj){
	
	if (!obj)
		obj = this;

	setupLinks();
	
	$(obj.showId).find(".box_loading").stop(true,true);
	$(obj.showId).find(".box_loading").show();
	$(obj.showId).find(".box_loading").hide();	
	$(obj.showId).find(".box_content").slideDown("slow");
	
	eventhook.updateBox(this);
}

Box.prototype.jqueryCallbackRemoveFunc = function(obj){
	
	return function(event){
	
		event.preventDefault();
		obj.remove(obj);
	}
}

Box.prototype.remove = function(obj){
		
	if (!obj)
		obj = this;

	eventhook.removeListener(obj);
	$(obj.id).hide("slow",obj.hideComplete(obj));
}

Box.prototype.hideComplete = function(obj){
	
	return function(){
	
		$(obj.id).remove();
		eventhook.removeBox(obj);
		if (obj.owner){
			
			obj.owner.childRemoved(obj);
		}
	}
}

Box.prototype.addChild = function(child){
		
	for (var i in this.children){
	
		if (getObjectClass(this.children[i]) == getObjectClass(child) && 
				child.userId == this.children[i].userId && child.singleChild)
		{
			
			delete child;
			return false;
		}
	}
		
	this.children.push(child);
	child.addTo("#layout_content", this.id);
}

Box.prototype.childRemoved = function(child){

	childLocation = this.children.indexOf(child);
	
	if (childLocation == -1){
		
		alert("Error: Child not found")
	} else {
		
		delete(this.children[childLocation]);		
	}
	
}
