//
// MAGYNHARD Java-Script Library
// (C) 2007 Matthäus Beyrle
//

var MGH =  new Class.create();

MGH.prototype =	{
	initialize: function(innerDiv) {
		this._mainMenuButton = new Array();
		this._callBackRef = this.onClick.bindAsEventListener(this);
		this._innerDiv = innerDiv;
	},

	onClick: function(event) {		
		var linkId = Event.element(event);
		linkId = linkId.id;
		var func = 0;
		//alert("click-event! ID: " + linkId);
		
		// main-buttons
		for(var i = 0; i < this.countMainButtons(); i++) {
			if(linkId == this._mainMenuButton[i].getId()) {
				func = this._mainMenuButton[i].getFunctionLink();
			}
		}
		

    
		//execute function
		if(func != 0) {
			func();
		}
		else {
			alert("BINDING NOT FOUND!");
		}
	},
	
	createMainButton: function(buttonName, buttonImg, functionLink) {
		var tmp = new MGH_MainMenuButton(buttonName, buttonImg, functionLink);
		this._mainMenuButton.push(tmp);
	},
	
	countMainButtons: function() {
		return this._mainMenuButton.length;
	},
	
	create: function() {			
  
		// main menu entries		
		for(var i = 0; i < this.countMainButtons(); i++) {
			var span = document.createElement("span");
			span.className = "menu_main";
			
      var Id = "mbt_" + i;
			span.id = Id;
			this._mainMenuButton[i].setId(Id);
      
				var img = document.createElement("img");
				img.className = "menu_main";
				img.src = this._mainMenuButton[i].getImg();
				img.alt = "";
				img.id = Id;
				
			span.appendChild(img);
				
				var spacer = document.createTextNode(" ");
			
			span.appendChild(spacer);			
				
			span.appendChild(document.createTextNode(this._mainMenuButton[i].getName()));     
			
			Event.observe(span, "click", this._callBackRef,false)
			
      $(this._innerDiv).appendChild(span);
      
		}
	}
}




var MGH_MainMenuButton = new Class.create()

MGH_MainMenuButton.prototype = {
	initialize: function(buttonName, buttonImg, functionLink) {
		this._buttonName = buttonName;
		this._buttonImg = buttonImg; 
		this._functionLink = functionLink;
		this._id = -1;
	},
	
	getName: function() {
		return this._buttonName;
	},
	
	getImg: function() {
		return this._buttonImg;
	},
	
	getFunctionLink: function() {
		return this._functionLink;
	},
	
	setId: function(id) {
		this._id = id;
	},
	
	getId: function() {
		return this._id;
	}	
}


// =================== LEFT MENU BUTTONS

/*
function AddLeftMenuEntry(name, link)
{
  var menu_entry = '<span class="left_box" onclick="' + link + '();">' + name + '</span><br/>';
  left_menu = document.getElementById("left_box");
  left_menu.innerHTML += menu_entry;
}

function ClearLeftMenu()
{
  left_menu = document.getElementById("left_box");
}
*/

var MGH_LeftMenu =  new Class.create();

MGH_LeftMenu.prototype =	{
	initialize: function(innerDiv) {
		this._leftMenuButton = new Array();
		this._callBackRef = this.onClick.bindAsEventListener(this);
		this._innerDiv = innerDiv;
	},

	onClick: function(event) {		
		var linkId = Event.element(event);
		linkId = linkId.id;
		var func = 0;
		//alert("click-event! ID: " + linkId);
		
		// left-buttons
		for(var i = 0; i < this.countLeftButtons(); i++) {
			if(linkId == this._leftMenuButton[i].getId()) {
				func = this._leftMenuButton[i].getFunctionLink();
			}
		}
		
		//execute function
		if(func != 0) {
			func();
		}
		else {
			alert("BINDING NOT FOUND!");
		}
	},
	
	createLeftButton: function(buttonName, buttonImg, functionLink) {
		var tmp = new MGH_LeftMenuButton(buttonName, buttonImg, functionLink);
		this._leftMenuButton.push(tmp);
	},
	
	countLeftButtons: function() {
		return this._leftMenuButton.length;
	},
	
  clear: function() {
      elem = document.getElementById("left_box");
      elem.innerHTML = "";
  },
  
	create: function() {			
		// === left menu entries		
		for(var i = 0; i < this.countLeftButtons(); i++) {
			var span = document.createElement("span");
			if(this._leftMenuButton[i].getImg() != "title")	span.className = "menu_left";
			else											span.className = "menu_left_title";
			var Id = "lbt_" + i;
			span.id = Id;
			this._leftMenuButton[i].setId(Id);
			
				if(this._leftMenuButton[i].getImg() != "title")
				{
					var img = document.createElement("img");
					img.className = "menu_left";
					img.src = this._leftMenuButton[i].getImg();
					img.alt = "";
					img.id = Id;
			        img.width = 16;
			        img.height = 16;
					
					span.appendChild(img);
				}
				
				var spacer = document.createTextNode(" ");
			
			span.appendChild(spacer);
			
				var span2 = document.createElement("span");
				span2.id = Id;
				span.appendChild(document.createTextNode(this._leftMenuButton[i].getName()));
				
			span.appendChild(span2);
			
        var br = document.createElement("br");
      
      span.appendChild(br);
      
			Event.observe(span, "click", this._callBackRef,false)
			
			$(this._innerDiv).appendChild(span);
		}
	}
}

var MGH_LeftMenuButton = new Class.create()

MGH_LeftMenuButton.prototype = {
	initialize: function(buttonName, buttonImg, functionLink) {
		this._buttonName = buttonName;
		this._buttonImg = buttonImg; 
		this._functionLink = functionLink;
		this._id = -1;
	},
	
	getName: function() {
		return this._buttonName;
	},
	
	getImg: function() {
		return this._buttonImg;
	},
	
	getFunctionLink: function() {
		return this._functionLink;
	},
	
	setId: function(id) {
		this._id = id;
	},
	
	getId: function() {
		return this._id;
	}	
}
