// JavaScript Document

function JsMenu(configuration, data) {
	
	this.configuration = null;
	
	this.menuData = null;
	
	this.mainMenuContainer = null;
	
	this.subMenuContainer = null;
	
	this.imageRight = null;
	
	this.setConfiguration(configuration);
	
	this.render(data);
}

JsMenu.prototype.setConfiguration = function(configuration) {
	if (configuration == null || configuration == 'undefined') {
        throw new IllegalArgumentException("Configuration not given.");
    } else if (typeof configuration != 'object') {
        throw new IllegalArgumentException("Configuration is not a JSONObject.");
    } else {
		if (configuration.sytleMainMenu == 'undefined' || configuration.sytleMainMenu == null) {
			throw new IllegalArgumentException("sytleMainMenu parameter not found.");
        }
		if (configuration.styleMainMenuItem == 'undefined' || configuration.styleMainMenuItem == null) {
			throw new IllegalArgumentException("styleMainMenuItem parameter not found.");
        }
		if (configuration.styleSubMenu == 'undefined' || configuration.styleSubMenu == null) {
			throw new IllegalArgumentException("styleSubMenu parameter not found.");
        }
		if (configuration.styleSubMenuItem == 'undefined' || configuration.styleSubMenuItem == null) {
			throw new IllegalArgumentException("styleSubMenuItem parameter not found.");
        }
	}
	/* If the configuration JSONObject is complete and valid then it is assigned to the instance var */
	this.configuration = configuration;
}

JsMenu.prototype.createMainMenuContainer = function() {
	document.write("<div id='mainMenuContainer'></div>");
	return document.getElementById('mainMenuContainer');
}

JsMenu.prototype.getMainMenuContainer = function() {
	this.mainMenuContainer = this.createMainMenuContainer();
	return this.mainMenuContainer;
}

JsMenu.prototype.createSubMenuContainer = function() {
	document.write("<div id='subMenuContainer'></div>");
	return document.getElementById('subMenuContainer');
}

JsMenu.prototype.getSubMenuContainer = function() {
	if(this.subMenuContainer == null) {
		this.subMenuContainer = this.createSubMenuContainer();
	}
	return this.subMenuContainer;
}

JsMenu.prototype.createMainMenu = function() {
	var i = 0;
	var ulMainMenu = document.createElement("UL");
	ulMainMenu.className = this.configuration.sytleMainMenu;
    for(i=0; i < this.menuData.main.length; i++) {
        ulMainMenu.appendChild(this.createMainMenuItem(i));
    }
	return ulMainMenu;
}

JsMenu.prototype.createMainMenuItem = function(menuItem) {
	var liMainMenuItem = document.createElement("LI");
	var linkItem =  document.createElement("A");
	var labelItem = document.createTextNode("");
	liMainMenuItem.className = this.configuration.styleMainMenuItem;
	linkItem.className = this.configuration.styleMainMenuItem;
	liMainMenuItem.id = this.menuData.main[menuItem].name;
	liMainMenuItem.setAttribute("menuType","main");
	if(this.menuData.main[menuItem].text) {
		labelItem.appendData(this.menuData.main[menuItem].text);
	} else {
		labelItem.appendData(this.menuData.main[menuItem].name);
	}
	if(this.menuData.main[menuItem].link) {
		linkItem.href = this.menuData.main[menuItem].link;
	} else {
		linkItem.href =  "#";
	}
	linkItem.appendChild(labelItem);
	liMainMenuItem.appendChild(linkItem);
	if(this.checkSubMenu(this.menuData.main[menuItem].name)) {
		liMainMenuItem.onmouseover = Show;
		liMainMenuItem.onmouseout = Hide;
		this.getSubMenuContainer().appendChild(this.createSubMenu(liMainMenuItem,this.menuData.main[menuItem].name));
	}
	return liMainMenuItem;
}

JsMenu.prototype.render = function(data) {
	var mainMenuContainer = this.getMainMenuContainer();
	var subMenuContainer = this.getSubMenuContainer();
	if (data == null || data == 'undefined') {
        throw new IllegalArgumentException("menu data not given.");
    } else if (typeof data != 'object') {
        throw new IllegalArgumentException("menu data is not a JSONObject.");
    } else {
		if (data.main == 'undefined' || data.main == null) {
			throw new IllegalArgumentException("menu data main parameter not found.");
        } else if(typeof data.main != 'object' ) {
			throw new IllegalArgumentException("menu data main parameter is not an array.");
		}
	}
	this.menuData = data;
	mainMenuContainer.appendChild(this.createMainMenu());
}

JsMenu.prototype.checkSubMenu = function(name) {
	if( (this.menuData[name] != 'undefined' || this.menuData[name] != null) && 
	   (typeof this.menuData[name] == 'object' ) &&
	   (this.menuData[name].name != null || this.menuData[name].name != "undefined")  ){
		return true;	
	} else {
		return false;
	}
}

JsMenu.prototype.createSubMenu = function(parent, name) {
	var divMenu = document.createElement("DIV");
	divMenu.id = "child_" + name;
	if(this.configuration.widthItem) {
		divMenu.style.width = parseInt(this.configuration.widthItem) + "px";
	} else {
		divMenu.style.width = "200px";
	}
	if(parent.getAttribute("menutype") == "sub") {
		divMenu.setAttribute("parentItem",parent.getAttribute("menuname"));
	} else {
		divMenu.setAttribute("parentItem","main");
	}
	divMenu.style.display = "none";
	divMenu.style.position = "absolute";
	divMenu.onmouseover=SShow;
	divMenu.onmouseout=SHide;
	var ulMenu  = document.createElement("UL");
	var i = 0;
	ulMenu.className =  this.configuration.styleSubMenu;
	for(i=0;(i < this.menuData[name].length); i++ ) {
		ulMenu.appendChild(this.createSubMenuItem(name,i))
	}
	divMenu.appendChild(ulMenu);
	return divMenu;
}

JsMenu.prototype.createSubMenuItem = function(menuName,menuItem) {
	var liItem = document.createElement("LI");
	var imageItem = null;
	var linkItem =  document.createElement("A");
	var imageRight = null;
	liItem.className = this.configuration.styleSubMenuItem;
	linkItem.className = this.configuration.styleSubMenuItem;
	liItem.id = this.menuData[menuName][menuItem].name;
	liItem.setAttribute("menuType","sub");
	liItem.setAttribute("menuName",menuName);
	liItem.setAttribute("menuItem",menuItem);

	var labelItem = document.createTextNode(" ");
	// Test if the text parameter is given.
	if(this.menuData[menuName][menuItem].text) {
		labelItem.appendData(this.menuData[menuName][menuItem].text);
	} else {
		labelItem.appendData(this.menuData[menuName][menuItem].name);
	}
	if(this.configuration.imagesPath) {
		imageItem = new Image(16,16);
		imageItem.border = 0;
		if(this.menuData[menuName][menuItem].image) {
			imageItem.src = this.configuration.imagesPath + "/" + this.menuData[menuName][menuItem].image;
		} else if(this.configuration.bulletImage) {
			imageItem.src = this.configuration.imagesPath + "/" + this.configuration.bulletImage;
		}
	}
	if(this.menuData[menuName][menuItem].link) {
		linkItem.href = this.menuData[menuName][menuItem].link;
	} else {
		linkItem.href = "#";
	}
	if(imageItem != null ) {
		linkItem.appendChild(imageItem);
	}
	linkItem.appendChild(labelItem);
	liItem.appendChild(linkItem);
	if(this.checkSubMenu(this.menuData[menuName][menuItem].name)) {
		if(this.configuration.imagesPath) {
			imageRight = new Image(16,16);
			imageRight.src = this.configuration.imagesPath + "/" + "right.gif";
			imageRight.border = 0;
			imageRight.style.align = "right";
			linkItem.appendChild(imageRight);
		}
		liItem.onmouseover = Show;
		liItem.onmouseout = Hide;
		this.getSubMenuContainer().appendChild(this.createSubMenu(liItem, this.menuData[menuName][menuItem].name));
	}
	return liItem;
}

/**
 * Constructs a new IllegalArgumentException object.
 * This exception is thrown to indicate invalid argument values.
 * @class
 * <p>
 * This exception is thrown to indicate invalid argument values.
 * </p>
 * @param {String} message, The error message  of the exception instance
 * @constructor
 */
function IllegalArgumentException(message) {
	/**
     * <p>
     * Represents the name of the exception.
	 * @type String
     * </p>
     */
    this.NAME = "IllegalArgumentException";

	/**
     * <p>
     * Represents the error message of the exception.
	 * @type String
     * </p>
     */
    this.message = message;
}

/**
 * <p>
 * This method return the name of the exception class
 * </p>
 * @return NAME {String}, the name of this exception class
 */
IllegalArgumentException.prototype.getName = function() {
    return this.NAME;
}

/**
 * <p>
 * This method return the message of the exception instance
 * </p>
 * @return message {String}, the error message of the exception instance
 */
IllegalArgumentException.prototype.getMessage = function() {
    return this.message;
}

findPos = function(obj) {
	var curleft = curtop = curHeight = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		curHeight = obj.offsetHeight;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			curHeight += obj.offsetHeight;
		}
	}
	return [parseInt(curleft),parseInt(curtop),parseInt(curHeight)];
}

Show = function() {
	if(this.getAttribute("menuType") == "sub") {
		var divParent = document.getElementById("child_" + this.getAttribute("menuname"));
		var childMenu = document.getElementById("child_" + this.id);
		childMenu.style.left = (parseInt(divParent.style.left) + parseInt(divParent.style.width)) + "px";
		//childMenu.style.top  = parseInt(divParent.style.top) + ((parseInt(this.getAttribute("menuitem")) + 1)*parseInt(this.getAttribute("heightItem"))) + "px" ;
		if(document.all || navigator.appName == "Opera" ){
			var this_height = parseInt(this.currentStyle.height) + parseInt(this.currentStyle.paddingBottom) + parseInt(this.currentStyle.paddingTop);
			//alert(this_height);
		} else {
		var computedStyle = window.getComputedStyle(this, "");
			var this_height = parseInt(computedStyle.getPropertyValue("padding-top")) + 
						  parseInt(computedStyle.getPropertyValue("padding-bottom")) +
						  parseInt(computedStyle.getPropertyValue("height"));
		}
		childMenu.style.top  = parseInt(divParent.style.top) + ((parseInt(this.getAttribute("menuitem")))*this_height) + "px" ;
		//childMenu.style.top = parseInt(divParent.style.left) + parseInt(this.style.height) * (parseInt(this.getAttribute("menuitem")) + 1) + "px";
		childMenu.style.display = "block" ;
		return;
	}
	
	var obj = document.getElementById(this.id);
	var pos = findPos(obj);
	var childMenu = document.getElementById("child_" + this.id);
	if(this.id == "Ventas") {
		childMenu.style.left = pos[0] + obj.offsetWidth;
		childMenu.style.top = pos[1] + obj.offsetHeight;
	} else {
		childMenu.style.left = pos[0] + "px";
		childMenu.style.top = pos[1] + obj.offsetHeight + "px";
	}
	childMenu.style.display = "block";
}

Hide = function(){
	var subMenu = document.getElementById("child_" + this.id);
	subMenu.style.display = "none";
}

SShow = function() {
	var parentMenu = this.getAttribute("parentItem");
	var parentNode = null;
	while(parentMenu != "main") {
		parentNode = document.getElementById("child_" + parentMenu);
		parentNode.style.display = "block";
		parentMenu = parentNode.getAttribute("parentItem");
	}
	this.style.display = "block";
}

SHide = function() {
	var parentMenu = this.getAttribute("parentItem");
	var parentNode = null;
	while(parentMenu != "main") {
		parentNode = document.getElementById("child_" + parentMenu);
		parentNode.style.display = "none";
		parentMenu = parentNode.getAttribute("parentItem");
	}
	this.style.display = "none";
}
