/*
 * Copyright 2009 - 2011 Horacio Daniel Ros
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2 of the License, or any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * Project:       Javascript Menu.
 * Author:        Horacio Daniel Ros
 * E-mail:        horaciodrs@gmail.com
 * Site:          http://www.javabs.com.ar
 * Date:          2011/07/28
 * Last Update:   2011/07/28
 */

function MenuItem(){

	this.Id;
	this.MenuId;
	this.HeaderId;
	this.ItemId;
	this.ParentItemId;
	this.SubMenuId;
	this.root;
	this.ParentSubMenu;
	this.Parent;
	
	this.htmlDiv;
	this.htmlIcon;
	this.htmlText;
	this.htmlArrow;
	
	this.Icon;
	this.Text;
	this.Image;
	this.Link;
	
	this.Width = 200;
	this.Height = 25;
	this.IconSize = 24;
	
	this.SubMenuWidth = 200;
	
	this.cssItem;
	
	this.RelatedSubMenues = new Array();
	
	this.CloseRelatedSubMenues = function(){
		
		var Menu = MenuManager.Menues.GetObjectById(this.MenuId);
		
		this.RelatedSubMenues = null;
		this.RelatedSubMenues = new Array();
		
		var parent_sub_menu = this.ParentSubMenu;
		
		while(parent_sub_menu){
		
			this.RelatedSubMenues.push(parent_sub_menu);
		
			if(parent_sub_menu.ParentItemId)
				parent_sub_menu = Menu.Items.GetObjectById(parent_sub_menu.ParentItemId).ParentSubMenu;
			else
				break;
			
		}
		
		Menu.SubMenues.Walk(function(pIndex, pObject, pRelatedSubMenues){
			
			var Ocultar = true;
			
			for(var i=0; i<pRelatedSubMenues.length; i++){
				if(pRelatedSubMenues[i].Id == pObject.Id){	
					Ocultar = false;
					break;
				}
			}
			
			if(Ocultar == true){
				pObject.htmlDiv.hide();
			}
			
		}, this.RelatedSubMenues);
		
	}
	
	this.RelatedItems = new Array();
	
	this.PaintRelatedItems = function(){
		
		var Menu = MenuManager.Menues.GetObjectById(this.MenuId);
		
		this.RelatedItems = null;
		this.RelatedItems = new Array();
		
		var parent_item = Menu.Items.GetObjectById(this.ParentSubMenu.ParentItemId);
		
		if(!parent_item){
			Menu.Headers.GetObjectById(this.HeaderId).htmlDiv.cssClass('mnuHeaderItem_over');
		}else{
		
			while(parent_item){
			
				this.RelatedItems.push(parent_item);
			
				if(parent_item.ParentSubMenu.ItemId)
					parent_item = Menu.Items.GetObjectById(parent_item.ParentSubMenu.ItemId);
				else
					break;
				
			}
		
		}
		
		Menu.Items.Walk(function(pIndex, pObject, pRelatedItems){
			
			for(var i=0; i<pRelatedItems.length; i++){
				if(pRelatedItems[i].Id == pObject.Id){
					//Pinto el item.
					pObject.htmlDiv.cssClass('mnuSubmenuItem_over');
					//Pinto el Texto del item.
					go(pObject.htmlDiv.id+'.Text').cssClass('mnuSubmenuText_over')
				}
			}
			
		}, this.RelatedItems);
		
		if(this.RelatedItems.length > 0)
			Menu.Headers.GetObjectById(this.RelatedItems[this.RelatedItems.length-1].HeaderId).htmlDiv.cssClass('mnuHeaderItem_over');
		
	}
	
	this.PonerEventos = function(){
		
		this.htmlDiv.ParentSubMenu = this.ParentSubMenu;
		
		this.htmlDiv.setAttribute('MenuId', this.MenuId);
		this.htmlDiv.setAttribute('SubMenuId', this.SubMenuId);
		this.htmlDiv.setAttribute('ItemId', this.Id);
		
		this.htmlDiv.mover(function(me){
			
			var Menu = MenuManager.Menues.GetObjectById(me.getAttribute('MenuId'));
			var Item = Menu.Items.GetObjectById(me.getAttribute('ItemId'));
			
			//Pinto el item.
			me.cssClass('mnuSubmenuItem_over');
			//Pinto el Texto del item.
			go(me.id+'.Text').cssClass('mnuSubmenuText_over')
			
			Item.CloseRelatedSubMenues();
			Item.PaintRelatedItems();
			
			if(me.getAttribute('SubMenuId')){
			
				var SubMenu = Menu.SubMenues.GetObjectById(me.getAttribute('SubMenuId'));
				
				if(SubMenu){
				
					var tempx = parseInt(me.rx()) + parseInt(SubMenu.Parent.Width);
					var tempy = parseInt(me.ry()) + IsZero(document.body.scrollTop, document.documentElement.scrollTop)
				
					SubMenu.htmlDiv.setX(tempx);
					SubMenu.htmlDiv.setY(tempy);
					SubMenu.htmlDiv.raiseTop();
					SubMenu.htmlDiv.alpha(Menu.Alpha);
					SubMenu.htmlDiv.display();
					
				}
				
			}
			
		});
		
		this.htmlDiv.mout(function(me){
			
			me.cssClass('mnuSubmenuItem');
			go(me.id+'.Text').cssClass('mnuSubmenuText')
			
		});
		
		this.htmlDiv.click(function(me){
		
			var Menu = MenuManager.Menues.GetObjectById(me.getAttribute('MenuId'));
			var Item = Menu.Items.GetObjectById(me.getAttribute('ItemId'));
			
			if(Item.Link)
				setTimeout(Item.Link, 0);
			
		});
		
	}
	
	this.Init = function(){
		
		var Menu = MenuManager.Menues.GetObjectById(this.MenuId);
		
		this.htmlDiv = this.Parent.newdiv(this.Id+'.Item');
		this.htmlDiv.css('position', 'absolute');
		this.htmlDiv.css('overflow', 'hidden');
		this.htmlDiv.css('cursor', 'pointer');
		
		if(this.cssItem)
			this.htmlDiv.cssClass(this.cssItem);

		if(this.Icon){
			this.htmlIcon = this.htmlDiv.newdiv(this.Id+'.Item.Icon');
			this.htmlIcon.css('position', 'absolute');
			this.htmlIcon.css('overflow', 'hidden');
			this.htmlIcon.css('cursor', 'pointer');
			this.htmlIcon.setX(0);
			this.htmlIcon.setY(0);
			this.htmlIcon.newimg(this.Id+'.Item.Icon.Image', this.Icon);
			
			if(!this.IconSize)
				this.IconSize = this.htmlIcon.clientWidth;
			
		}
		
		if(this.Text){
			this.htmlText = this.htmlDiv.newdiv(this.Id+'.Item.Text');
			this.htmlText.css('position', 'absolute');
			this.htmlText.css('overflow', 'hidden');
			this.htmlText.css('cursor', 'pointer');
			this.htmlText.cssClass('mnuSubmenuText');

			if(this.Icon)
				this.htmlText.setX(this.IconSize);
			else
				this.htmlText.setX(0);

			this.htmlText.setY(0);
			this.htmlText.html(this.Text);
		}
		
		
		//else
			if(Menu.cssSubMenuItem)
				this.htmlDiv.cssClass(Menu.cssSubMenuItem);
		
		/*
		if(!this.Width)
			this.Width = this.htmlDiv.clientWidth;
		
		if(!this.Height)
			this.Height = this.clientHeight;
		*/
			
		this.htmlDiv.setW(this.Width);
		this.htmlDiv.setH(this.Height);
		
		if(this.root.childs('div').length > 0){
		
			var newSubMenu = new MenuSubMenu();
			
			this.SubMenuId = this.Id+'.SubMenu'
			
			newSubMenu.Id = this.SubMenuId;
			newSubMenu.ParentItemId = this.Id;
			newSubMenu.MenuId = this.MenuId;
			newSubMenu.root = this.root;
			newSubMenu.Parent = this;
			newSubMenu.Width = this.SubMenuWidth;
			
			Menu.SubMenues.Add(newSubMenu);
			
			newSubMenu.Init();
			
		}
		
		this.PonerEventos();
		
	}
	
}

function MenuSubMenu(){

	this.Id;
	this.HeaderId;
	this.ItemId;
	this.MenuId;
	this.Parent;
	
	this.htmlDiv;
	
	this.root;
	
	this.Items = new ObjectsList();
	
	this.Width = 200;
	this.ItemHeight = 25;
	
	this.cssSubMenu;
	
	this.TimerClose;
	
	this.GetWidth = function(){
		return this.Width;
	}
	
	this.CountItems = function(){
		
		var Menu = MenuManager.Menues.GetObjectById(this.MenuId);
		var count=0;
		
		for(var i=0; i<Menu.Items.Count(); i++){
			if(Menu.Items.Objects[i].ParentSubMenu.Id == this.Id){
				count++;
			}
		}
		
		return count;
		
	}
	
	this.BuildItems = function(){
		
		var Menu = MenuManager.Menues.GetObjectById(this.MenuId);
		var index = 0;
		
		this.htmlDiv.setW(this.GetWidth());
		this.htmlDiv.setH(this.CountItems()*this.ItemHeight);
		
		for(var i=0; i<Menu.Items.Count(); i++){
			if(Menu.Items.Objects[i].ParentSubMenu.Id == this.Id){
				Menu.Items.Objects[i].htmlDiv.setY(index*this.ItemHeight);
				index++;
			}
		}
	}
	
	this.PonerEventos = function(){
		
		return;
		
	}
	
	this.Init = function(){
		
		var Menu = MenuManager.Menues.GetObjectById(this.MenuId);
		
		this.htmlDiv = go(document.body).newdiv(this.Id);
		
		this.htmlDiv.css('position', 'absolute');
		this.htmlDiv.css('overflow', 'hidden');
		this.htmlDiv.css('background-color', '#f5f5f5');
		
		if(this.cssSubMenu){
			this.htmlDiv.cssClass(this.cssSubMenu);
		}else if(Menu.cssSubMenu){
			this.htmlDiv.cssClass(Menu.cssSubMenu);
		}
		
		this.htmlDiv.hide();
		
		this.root.childs('div').Walk(function(pIndex, pObject, pSubMenu){
			
			var newItem = new MenuItem();
			
			newItem.Id = pSubMenu.Id+'.Item.'+pIndex;
			newItem.MenuId = pSubMenu.MenuId;
			newItem.HeaderId = pSubMenu.HeaderId;
			newItem.ItemId = pSubMenu.ItemId;
			newItem.root = pObject;
			newItem.Parent = pSubMenu.htmlDiv;
			newItem.ParentSubMenu = pSubMenu;
			newItem.Width = pSubMenu.Width;
			
			if(pObject.getAttribute('Text'))
				newItem.Text = pObject.getAttribute('Text');
			
			if(pObject.getAttribute('Icon'))
				newItem.Icon = pObject.getAttribute('Icon');
				
			if(pObject.getAttribute('Link'))
				newItem.Link = pObject.getAttribute('Link');
				
			if(pObject.getAttribute('SubMenuWidth'))
				newItem.SubMenuWidth = pObject.getAttribute('SubMenuWidth');
			
			Menu.Items.Add(newItem);
			
			newItem.Init();
			
		}, this);
		
		this.BuildItems();
		this.PonerEventos();
		
	}
	
}

function MenuHeader(){
	
	this.Id;
	this.MenuId;
	this.SubMenuId;
	
	this.root;
	
	this.htmlDiv;
	
	this.Icon;
	this.Text;
	this.Image;
	this.Link;
	this.Width;
	this.Height;
	
	this.SubMenuWidth;
	
	this.CerrarNoRelacionados = function(){
	
		var Menu = MenuManager.Menues.GetObjectById(this.MenuId);
		
		Menu.SubMenues.Walk(function(pIndex, pObject, pSubMenuId){
			
			if(pObject.Id != pSubMenuId){
			
				pObject.htmlDiv.hide();
				
			}
			
		}, this.SubMenuId);
		
	}
	
	this.PonerEventos = function(){
		
		this.htmlDiv.setAttribute('MenuId', this.MenuId);
		this.htmlDiv.setAttribute('SubMenuId', this.SubMenuId);
		this.htmlDiv.setAttribute('HeaderId', this.Id);
		
		this.htmlDiv.mover(function(me){
			
			var Menu = MenuManager.Menues.GetObjectById(me.getAttribute('MenuId'));
			var SubMenu = Menu.SubMenues.GetObjectById(me.getAttribute('SubMenuId'));
			var Header = Menu.Headers.GetObjectById(me.getAttribute('HeaderId'));
			
			Menu.DespintarHeaders();
			
			me.cssClass('mnuHeaderItem_over');
			
			Header.CerrarNoRelacionados();
			
			if(SubMenu){
			
				SubMenu.htmlDiv.setX(me.rx());
				
				if(Menu.HeadersContainer.parentNode.style.position == 'fixed'){
					SubMenu.htmlDiv.setY(Sum([IsZero(document.body.scrollTop, document.documentElement.scrollTop), me.ry(), Menu.Height]));	
					MenuManager.MostrarBackArea(95);
				}else{
					var tempy = parseInt(me.ry()) + parseInt(Menu.Height) + parseInt(IsZero(document.body.scrollTop, document.documentElement.scrollTop));
					SubMenu.htmlDiv.setY(tempy);
					MenuManager.MostrarBackArea(SubMenu.htmlDiv.iTop);
				}
				
				
				
				//this.HeadersContainer
				
				SubMenu.htmlDiv.raiseTop();
				SubMenu.htmlDiv.alpha(Menu.Alpha);
				SubMenu.htmlDiv.display();
			}else{
				MenuManager.OcultarBackArea();
			}
			
		});
		
		this.htmlDiv.mout(function(me){
			
			var Menu = MenuManager.Menues.GetObjectById(me.getAttribute('MenuId'));
			var SubMenu = Menu.SubMenues.GetObjectById(me.getAttribute('SubMenuId'));
			
			me.cssClass('mnuHeaderItem');
			
		});
		
		this.htmlDiv.click(function(me){
			
			var Menu = MenuManager.Menues.GetObjectById(me.getAttribute('MenuId'));
			var Header = Menu.Headers.GetObjectById(me.getAttribute('HeaderId'));
			
			if(Header.Link)
				setTimeout(Header.Link, 0);
			
		});
		
	}
	
	this.Init = function(){
		
		var Menu = MenuManager.Menues.GetObjectById(this.MenuId);
		
		this.htmlDiv = Menu.HeadersContainer.newdiv(this.Id);
		
		this.htmlDiv.css('position', 'absolute');
		this.htmlDiv.css('overflow', 'hidden');
		this.htmlDiv.css('cursor', 'pointer');
		this.htmlDiv.html(this.Text);
		
		if(Menu.cssHeaderItem){
			this.htmlDiv.cssClass(Menu.cssHeaderItem);
		}
		
		/*
		if(Menu.Mode == 'Horizontal'){
			if(!this.Width)
				this.Width = this.htmlDiv.clientWidth;
			if(!this.Height)
				this.Height = Menu.Height;
		}
		*/
		
		this.Height = Menu.Height;
		
		this.htmlDiv.setW(this.Width);
		this.htmlDiv.setH(this.Height);
		
		if(this.root.childs('div').length > 0){
			
			this.SubMenuId = this.Id+'.SubMenu';
			
			var newSubMenu = new MenuSubMenu();
			
			newSubMenu.Id = this.SubMenuId;
			newSubMenu.HeaderId = this.Id;
			newSubMenu.MenuId = this.MenuId;
			newSubMenu.root = this.root;
			
			if(this.SubMenuWidth)
				newSubMenu.Width = this.SubMenuWidth;
			
			if(Menu.cssSubMenu)
				newSubMenu.cssSubMenu = Menu.cssSubMenu;
			
			Menu.SubMenues.Add(newSubMenu);
			
			newSubMenu.Init();
			
		}
		
		this.PonerEventos();
		
	}
	
}

function Menu(){

	this.Id;
	
	this.Headers = new ObjectsList();
	this.SubMenues = new ObjectsList();
	this.Items = new ObjectsList();
	
	this.HeadersContainer;
	
	this.Mode = 'Horizontal';								//Indica si el menu es horizontal o vertical.
	
	this.cssHeaderItem;
	this.cssSubMenu;
	this.cssSubMenuItem;
	
	this.Alpha = 100;
	
	this.Height;
	
	this.DespintarHeaders = function(){
		this.Headers.Walk(function(pIndex, pObject, p){
			pObject.htmlDiv.cssClass('mnuHeaderItem');
		}, null);
	}
	
	this.GetLeftPositionByHeaderIndex = function(pIndex){
		
		var aux = 0;
		
		for(var i=0;i<pIndex; i++){
			aux += parseInt(this.Headers.Objects[i].Width);
		}
		
		return aux;
		
	}
	
	this.BuildHeaders = function(){
	
		if(this.Mode == 'Horizontal'){
			
			this.Headers.Walk(function(pIndex, pObject, pMenu){
				
				pObject.htmlDiv.setX(pMenu.GetLeftPositionByHeaderIndex(pIndex));
				pObject.htmlDiv.setY(0);
				pObject.htmlDiv.setW(pObject.Width);
				pObject.htmlDiv.setH(pObject.Height);
				
			}, this);
			
		}
		
	}
	
	this.Init = function(){
		
		this.HeadersContainer = go(this.Id);
		this.HeadersContainer.css('overflow', 'hidden');
		this.HeadersContainer.cssClass('mnuHeader');
		
		this.HeadersContainer.setH(this.Height);
		
		go(this.Id).childs('span').Walk(function(pIndex, pObject, pMenu){
			
			var newHeader = new MenuHeader();
			
			newHeader.Id = pMenu.Id+'.Header.'+pIndex;
			newHeader.MenuId = pMenu.Id;
			newHeader.root = pObject;
			
			if(pObject.getAttribute('Text')){
				newHeader.Text = pObject.getAttribute('Text');
			}
			
			if(pObject.getAttribute('Link')){
				newHeader.Link = pObject.getAttribute('Link');
			}
			
			if(pObject.getAttribute('Width')){
				newHeader.Width = pObject.getAttribute('Width');
			}
			
			if(pObject.getAttribute('SubMenuWidth')){
				newHeader.SubMenuWidth = pObject.getAttribute('SubMenuWidth');
			}
			
			pMenu.Headers.Add(newHeader)
			
			newHeader.Init();
			
		}, this);
		
		this.BuildHeaders();		
		
	}
	
}
 
function MnuMngr(){

	this.Menues = new ObjectsList();
	
	this.htmlBack;
	
	this.CloseAllSubMenues = function(){
		
		this.Menues.Walk(function(pIndex, pMenu, p){
			
			pMenu.SubMenues.Walk(function(pIndex, pObject, p){
			
				pObject.htmlDiv.hide();
			
			});
			
		});	
		
	}
	
	this.MostrarBackArea = function(pY){
		this.htmlBack.setX(0);
		this.htmlBack.setY(pY);
		this.htmlBack.setW(Screen.Width);
		this.htmlBack.setH(Screen.Height-pY);
		this.htmlBack.display();
		this.htmlBack.raiseTop();
	}
	
	this.ResetHeaders = function(){
		this.Menues.Walk(function(pIndex, pMenu, p){
		
			pMenu.Headers.Walk(function(pIndex, pHeader, p){
			
				pHeader.htmlDiv.cssClass('mnuHeaderItem');
			
			});
		
		});
	
	}
	
	this.OcultarBackArea = function(){
		this.htmlBack.hide();
	}
	
	this.Init = function(){
	
		this.htmlBack = go(document.body).newdiv('MenuManager.Back.Div');
		this.htmlBack.css('position', 'fixed');
		this.htmlBack.css('overflow', 'hidden');
		this.htmlBack.css('background-color', '#f5f5f5');
		this.htmlBack.html('&nbsp;');
		this.htmlBack.alpha(1);
		this.htmlBack.hide();
		
		this.htmlBack.mover(function(){
			MenuManager.CloseAllSubMenues();
			MenuManager.OcultarBackArea();
			MenuManager.ResetHeaders();
		});
		
		go('div{Type}{Menu}').Walk(function(pIndex, pObject, pParam){
			
			var newMenu = new Menu();
			
			newMenu.Id = pObject.getAttribute('id');
			
			if(pObject.getAttribute('Height'))
				newMenu.Height = pObject.getAttribute('Height');
			
			if(pObject.getAttribute('Alpha'))
				newMenu.Alpha = pObject.getAttribute('Alpha');
			
			if(pObject.getAttribute('cssHeaderItem'))
				newMenu.cssHeaderItem = pObject.getAttribute('cssHeaderItem');
			
			if(pObject.getAttribute('cssSubMenuItem'))
				newMenu.cssSubMenuItem = pObject.getAttribute('cssSubMenuItem');
			
			if(pObject.getAttribute('cssSubMenu'))
				newMenu.cssSubMenu = pObject.getAttribute('cssSubMenu');
			
			pParam.Menues.Add(newMenu);
			
			newMenu.Init();
			
		}, this);
		
	}
	
}

var MenuManager = new MnuMngr();

Page.OnLoad(function(){

	MenuManager.Init();
	
});

Page.OnResize(function(){
	
	MenuManager.CloseAllSubMenues();
	
})

go(window).onscrolling(function(me){
	MenuManager.CloseAllSubMenues();
});
