/**
 * Copyright (c) 2008 Logisoft Szymon Sawicki
 *
 * All rights reserved
 *
 */
 
window.Menu = {};

/**
 * klasa Menu
 * @param idOrElement - root ul.id
 * @param name
 * @param customConfigFunction
 * @return
 */
Menu = function init(idOrElement, name, customConfigFunction)
{
	this.name = name;
	this.type = "menu";
	this.closeDelayTimer = null;
	this.closingMenuItem = null;
//alert(name);
	this.config();
	if (typeof customConfigFunction == "function") {
		this.customConfig = customConfigFunction;
		this.customConfig();
	}
	this.rootContainer = new MenuContainer(idOrElement, this);
}

Menu.prototype.config = function()
{
	  this.collapseBorders = true;
	  this.quickCollapse = true;
	  this.closeDelayTime = 400;
};


/**
 * klasa MenuContainer
 * @param idOrElement
 * @param parent
 * @return
 */
window.MenuContainer = {};

MenuContainer = function initialize(idOrElement, parent)
{
	this.type = "menuContainer";
	this.menuItems = [];
	this.init(idOrElement, parent);
}

MenuContainer.prototype.init = function(idOrElement, parent)
{
	 if(!$(idOrElement))
	  	return;
	  this.element = $(idOrElement);
	  this.parent = parent;
	  this.parentMenu = (this.type == "menuContainer") ? ((parent) ? parent.parent : null) : parent;
	  this.root = parent instanceof Menu ? parent : parent.root;
	  this.id = this.element.attr("id");

	  if (this.type == "menuContainer") 
	  {
	  	if (this.element.hasClass("level1")) this.menuType = "dropdown";
		else if (this.element.hasClass("level2")) this.menuType = "flyout";
		else this.menuType = "flyout";

	    if (this.menuType == "flyout" /*|| this.menuType == "dropdown"*/)
	    {
	      this.isOpen = false;
		  this.element.css({"position": "absolute", "top": "0px", "left": "0px", "visibility": "hidden"});
	    }
	    else 
	    {
	      this.isOpen = true;
	    }
	  } 
	  else 
	  {
	    this.isOpen = this.parentMenu.isOpen;
	  }

	  var childNodes = this.element.children();
	  if (childNodes == null) return;

	  for (var i = 0; i < childNodes.length; i++) 
	  {
	    var node = childNodes[i];
	    
	    if (node.nodeType == 1) 
	    {
	      if (this.type == "menuContainer")
	      {
	        if (node.tagName.toLowerCase() == "li")
	        {
	          this.menuItems.push(new MenuItem(node, this));
	        }
	      } 
	      else
	      {
	        if (node.tagName.toLowerCase() == "ul") 
	        {
	          this.subMenu = new MenuContainer(node, this);
	        }
	      }
	    }

	  }
	  
} // MenuContainer.init()

MenuContainer.prototype.getBorders = function(element) {
	  var ltrb = ["Left","Top","Right","Bottom"];
	  var result = {};
	  dom_element = this.element.get(0);

	  for (var i = 0; i < ltrb.length; ++i) 
	  {
	    if (dom_element.currentStyle)
	      var value = parseInt(dom_element.currentStyle["border"+ltrb[i]+"Width"]);
	    else if (window.getComputedStyle)
	      var value = parseInt(window.getComputedStyle(dom_element, "").getPropertyValue("border-"+ltrb[i].toLowerCase()+"-width"));
	    else
	      var value = parseInt(dom_element.style["border"+ltrb[i]]);
	    result[ltrb[i].toLowerCase()] = isNaN(value) ? 0 : value;
	  };
	  return result;
} // MenuContainer.getBorders()


MenuContainer.prototype.open = function() {
	  if (this.root.closeDelayTimer) window.clearTimeout(this.root.closeDelayTimer);
	  this.parentMenu.closeAll(this);
	  this.isOpen = true;
	  
	  if (this.menuType == "dropdown")
	  {
		  // TODO
//	  	this.element.css({
//			"left": (this.parent.element.position().left) + "px",
//			"top": (this.parent.element.position().top + Element.getHeight(this.parent.element)) + "px"
//		});

	  } 
	  else if (this.menuType == "flyout")
	  {
	    var parentMenuBorders = this.parentMenu ? this.parentMenu.getBorders() : new Object();
	    var thisBorders = this.getBorders();

	    dx = 2;
	    if(navigator.appName=='Opera')
	    	dx += 1;
	    else
	    	dx += 0;

	    if (
	      (this.parentMenu.element.position().left + this.parentMenu.element.width() + this.element.width() + 20) >
	      (window.innerWidth ? window.innerWidth : document.body.offsetWidth)
	    ) 
	    {
			this.element.css({
	      		"left": (- this.element.width() - (this.root.collapseBorders ?  0 : parentMenuBorders["left"])) + "px"
			});
	    } 
	    else
	    {
	    	this.element.css({
	    		"left": (this.parentMenu.element.width() - parentMenuBorders["left"] - (this.root.collapseBorders ?  Math.min(parentMenuBorders["right"], thisBorders["left"]) : 0) + dx) + "px"
			});
	    }
		this.element.css({
	    	"top": (this.parent.element.position().top - parentMenuBorders["top"] - this.menuItems[0].element.position().top) + "px"
		});
	  }
	  this.element.css({"visibility": "visible"});
	  if($('#results'))
	  	$('#results').hide();

} //  MenuContainer.open()

MenuContainer.prototype.close = function() {
	this.element.css({"visibility": "hidden"});
	this.isOpen = false;
	if($('#results'))
		$('#results').show();
	this.closeAll();
} // MenuContainer.close()

MenuContainer.prototype.closeAll = function(trigger) {
	for (var i = 0; i < this.menuItems.length; ++i) {
		this.menuItems[i].closeItem(trigger);
	}
}

/**
 * klasa MenuItem
 */
window.MenuItem = {};

MenuItem = function initialize(idOrElement, parent) 
{
	var menuItem = this;
	this.type = "menuItem";
	this.subMenu;
	this.init(idOrElement, parent);
	if (this.subMenu) 
	{
		this.element.mouseover(function() {
			menuItem.subMenu.open();
		})
	} 
	else 
	{
		if (this.root.quickCollapse) 
		{
		  this.element.mouseover(function() {
			menuItem.parentMenu.closeAll();
		  })
		}
	 }
	  var linkTag = this.element.children("a")[0];
	  if (linkTag) 
	  {
		 /*linkTag.focus(function(){
			 this.element.trigger('mouseover');
		 	}
		 );*/
		 this.link = linkTag;
		 this.text = linkTag.text;
	  }
	  if (this.subMenu) {
		this.element.mouseout(function() 
		{
		  if (menuItem.root.openDelayTimer) window.clearTimeout(menuItem.root.openDelayTimer);
		  if (menuItem.root.closeDelayTimer) window.clearTimeout(menuItem.root.closeDelayTimer);
		  eval(menuItem.root.name + ".closingMenuItem = menuItem");	  
		  menuItem.root.closeDelayTimer = window.setTimeout(menuItem.root.name + ".closingMenuItem.subMenu.close()", menuItem.root.closeDelayTime);
		})
	  }
} // MenuItem.initialize()

MenuItem.prototype.init = MenuContainer.prototype.init;
MenuItem.prototype.getBorders = MenuContainer.prototype.getBorders;
MenuItem.prototype.open = MenuContainer.prototype.open;
MenuItem.prototype.close = MenuContainer.prototype.close;
MenuItem.prototype.closeAll = MenuContainer.prototype.closeAll;

MenuItem.prototype.openItem = function() {
	  this.isOpen = true;
	  if (this.subMenu) { this.subMenu.open(); }
}

MenuItem.prototype.closeItem = function(trigger) {
	  this.isOpen = false;
	  if (this.subMenu) {
	    if (this.subMenu != trigger) this.subMenu.close();
	  }
}


/*************************
 * inicjalizacja obiektów
 *************************/

var menu;

function configMenu() {
  this.closeDelayTime = 900;
}

function initMenu() {
  menu = new Menu('#root', 'menu', configMenu);
}

$(document).ready(initMenu);
