/*
 * EventHook
 */
function EventHook(){
	
	this.listeners = new Array();
	this.boxes = new Array();
}

EventHook.prototype.listeners = null;
EventHook.prototype.boxes = null;
EventHook.prototype.contentPosition = 0;
EventHook.prototype.boxWidth = 248;
EventHook.prototype.addMoveTo = true;

EventHook.prototype.setAddMoveTo = function(addMoveTo){
	
	this.addMoveTo = addMoveTo;
}

EventHook.prototype.addBox = function(obj,position){
		
	for (var i in this.boxes){
		
		if (this.boxes[i] == obj)
			return false;
	}
	
	//Set default position of last space if position is not set 
	if (typeof(position) == "undefined")
		position = this.boxes.length-1;
	
	//Add the new box into the correct spot, to the right of position
	if (position != -1)
		this.boxes.splice(position+1,0,obj);
	else
		this.boxes.push(obj);
		
	//If we're moving to new boxes, move there if this box is not in the current viewport	
	if (this.addMoveTo && position > parseInt(this.contentPosition)+2) {
		this.contentPosition = position-2;
		this.moveContent();
	}
	
	//Create the new icon
	var newIcon = $("#template_box_icon").clone().attr("id",obj.id.substr(1) +"_icon");
	newIcon.find(".icon_image").attr("src","images/" + obj.icon);
	
	if (position == -1 ){
		
		newIcon.appendTo("#layout_content_icons");
	} else {
		
		var children = $("#layout_content_icons").children();					
		var counter = -1;
		children.each( function(){
					
			counter++;
			if (counter == position){
				
				newIcon.insertAfter("#" + $(this).attr("id"));
			}
		});						
	}
	
	newIcon.fadeIn(2000);
		
	newIcon.click(function(event){
		
		event.preventDefault();
		
		var children = $("#layout_content_icons").children();					
		var counter = -1;
		var icon = this;
		children.each( function(){
				
			counter++;
			if (this == icon){
			
				eventhook.moveTo(counter);
			}
		});						
	});
	
	newIcon.mouseover(function(){
		
		var id = $(this).attr("id");
		var boxid = id.substr(0,id.length-5);
		
		$("#tip_box_icon .tip_text").html($("#" + boxid).find(".box_title_text").html());
		
	}).tooltip({
		effect: 'toggle',
		delay: 0,
		offset: [0, 0],
		tip: "#tip_box_icon",
		lazy: false,
		relative: true,
		predelay: 0
	});	
		
	console.log("EventHook: Added box " + obj + " - " + this.boxes.length + " boxes.");
}

EventHook.prototype.removeBox = function(obj){
	
	for (var i = 0; i < this.boxes.length; i++) {
	
		if (this.boxes[i] == obj) {
			
			this.boxes.splice(i, 1);
			$("#layout_content_icons").find(obj.id+"_icon").fadeOut(1000,function(){$(this).remove()});
			
			i--;
			console.log("EventHook: Removed box " + obj + " - " + this.boxes.length + " boxes.");
		}
	}
	
	this.moveContent();
}	

EventHook.prototype.updateBox = function(obj){
				
	$("#layout_content_icons").find(obj.id+"_icon .icon_image").attr("src","images/" + obj.icon);			
	console.log("EventHook: Updated box " + obj.title + " with icon " + obj.icon);
}

EventHook.prototype.getContentPosition = function(){
	
	return this.contentPosition;
}

EventHook.prototype.getBoxAt = function(position){
	
	return this.boxes[position];
}

EventHook.prototype.closeAllBoxes = function(){
	
	console.log("EventHook: Closing all boxes");
	for (var i in this.boxes){
		
		this.boxes[i].remove();
	}
}

EventHook.prototype.addListener = function(obj,eventType){
	
	for (var i in this.listeners) {
		
		if (this.listeners[i][0] == obj && this.listeners[i][1] == eventType)
			return false;			
	}	
	
	this.listeners.push(new Array(obj,eventType));
	console.log("EventHook: Added listener " + obj + " for event " + eventType + " - " + this.listeners.length + " listeners.");
	
}

EventHook.prototype.triggerEvent = function(eventType){
	
	console.log("EventHook: Event triggered: " + eventType + " - " + this.listeners.length + " listeners");
	for (var i in this.listeners) {
		
		if (this.listeners[i][1] == eventType){
			
			this.listeners[i][0].eventCallback(eventType);
		}
	}
}

EventHook.prototype.removeListener = function(obj){
	
	for (var i = 0; i < this.listeners.length; i++) {
	
		if (this.listeners[i][0] == obj) {
			eventType = this.listeners[i][1];
			this.listeners.splice(i, 1);
			i--;
			console.log("EventHook: Removed listener " + obj + " for event " + eventType + " - " + this.listeners.length + " listeners.");
		}
	}	
}

EventHook.prototype.moveLeft = function(){
	
	this.moveTo(this.contentPosition - 1);
}

EventHook.prototype.moveRight = function(){
	
	this.moveTo(this.contentPosition + 1);
}

EventHook.prototype.moveTo = function(contentPosition){
	
	this.contentPosition = contentPosition;
	this.moveContent();	
}

EventHook.prototype.checkContentPosition = function(){
	
	if (this.contentPosition > (this.boxes.length-4))
		this.contentPosition = this.boxes.length-4;
		
	if (this.contentPosition < 0)
		this.contentPosition = 0;	
}

EventHook.prototype.moveContent = function(){
		
	this.checkContentPosition();	
	$("#layout_content").stop(true,false);
	$("#layout_content").animate({left: -1 * this.contentPosition * this.boxWidth},400);
}

