(function($){
	// plugin definition
	$.fn.topMenu = function(options) 
	{
		// build main options before element iteration
		var opts = $.extend({}, $.fn.topMenu.defaults, options);

		//parameters
		var $container = $(opts.container);
		//variables
		var $aMenuItems = new Array();
		var $menuTable = null;
		var $divSecondLevel = null;
		var $selectedTable = null;
		var $bottomSelectedTable = null;
		var $menuSelected = null;
		var $submenuSelected = null;
		var $timer = null;
		var $myself = $(this);
			
		$myself.hide();
		
		$('a', this).each(function() {
		  $(this).addClass('link_menu');	
		});
		
		$('>li', this).each(function() {
			var _this = $(this);
			_this.subElements = new Array();
			$('ul.second > li', _this).each(function () {
				_this.subElements.push($(this));
				$('a', this).each(function() {
				  $(this).addClass('link_menu2');
				});
			});
			buildSecondLevelTable(_this);			
			$aMenuItems.push(_this);
		});

		$menuTable = buildMainTable();

    menuLine1 = document.createElement('div');
    menuLine1.className='menu_top';
    menuLine1.appendChild($menuTable);
    $container.append(menuLine1);
    
    $divSecondLevel = $(document.createElement('div'));
    $divSecondLevel.addClass('menu_bottom');
    $container.append($divSecondLevel);

		/*
		*  Draw a table for the 1st level menu
		*/
		function buildMainTable() {
			var table = document.createElement("table");
			table.setAttribute('width', '100%');
			table.setAttribute('border', '0px');
			table.setAttribute('cellSpacing', '0');
			table.setAttribute('cellPadding', '0');
			var row = table.insertRow(0);
			
			var widthCella = "100%"
			if ($aMenuItems.length > 0){
				widthCella = parseInt (100 / $aMenuItems.length) +"%"
			}

			for (i=0; i<$aMenuItems.length; i++) {
			  var cell = row.insertCell(i);
				$(cell).append(buildInnerTable($('>a',$aMenuItems[i]), $aMenuItems[i] ));
			  cell.setAttribute('height','32px');
			  cell.setAttribute('width',widthCella);			  
			  cell.setAttribute('align', 'center');
			  cell.setAttribute('valign', 'middle');
				cell.index = i;
				$aMenuItems[i].cell = cell;
											  
				//if ($aMenuItems[i].attr('selected')) {
				if ($aMenuItems[i].hasClass('selected')) {
				  $menuSelected = cell;
				}
				
			  $(cell).mouseover(function() {
			  	if ($timer) {clearTimeout($timer); $timer=null;}
			  	
			  	$divSecondLevel.empty();
			    $divSecondLevel.append($aMenuItems[this.index].menuTable);
			    
			    $('td', $divSecondLevel).bind('mousemove', function () {
						if ($timer) {clearTimeout($timer); $timer=null;}
						$('td.box_submenu_active',$divSecondLevel).removeClass('box_submenu_active').addClass('box_submenu');
						$('td.box_submenu',this).attr('class', 'box_submenu_active');
			    });
			    
					$('td', $divSecondLevel).mouseout(function() {
					  if ($timer == null) { $timer = setTimeout(restoreSelected, opts.delaySelected); }
					});
					
					$('td.box_menu_active',$menuTable).attr('class','box_menu');
					$('td.box_menu_parent_active',$menuTable).attr('class','box_menu_parent');
					

					if($('td:nth-child(1)',this).hasClass('box_menu_parent')==true){
						$('td:nth-child(1)',this).attr('class', 'box_menu_parent_active');
					}else{
						$('td:nth-child(1)',this).attr('class', 'box_menu_active');
					}
					
			  });

				$(cell).mouseout(function() {
				  if ($timer == null) { $timer = setTimeout(restoreSelected, opts.delaySelected); }
				});

			}
			return table;
		}
		
		function buildSecondLevelTable(menuNode) {
			var table = document.createElement("table");
			table.setAttribute('width','100%');
			table.setAttribute('border','0');
			table.setAttribute('cellSpacing','0');
			table.setAttribute('cellPadding','0');

			var row = table.insertRow(0);
			for (i=0; i<menuNode.subElements.length; i++) {
			  var cell = row.insertCell(i);
				$(cell).append(buildBottomInnerTable(menuNode, i));
			  cell.setAttribute('align','center');
			  cell.setAttribute('valign','top');
			  cell.setAttribute('height', '30px');
			  menuNode.subElements[i].cell = cell;
			  
			  $(cell).addClass('box_submenu_line');
			  
			  // if (menuNode.subElements[i].attr('selected')) {
			  if (menuNode.subElements[i].hasClass('selected')) {
			    $submenuSelected = cell;
			    $submenuSelected.parentMenu = menuNode;
			  }
			}

			menuNode.menuTable = table;
		}

		function buildInnerTable(label, objx) {
			table = document.createElement('table');
			table.setAttribute('width','100%');
			table.setAttribute('border','0');
			table.setAttribute('cellSpacing','0');
			table.setAttribute('cellPadding','0');
		
			row = table.insertRow(0);

			cell = row.insertCell(0);
			cell.setAttribute('align', 'center');
			cell.setAttribute('width', '100%');

			if(objx.hasClass("parent")==true){
				$(cell).addClass('box_menu_parent').append(label);
			}else{
				$(cell).addClass('box_menu').append(label);
			}
			
			return $(table);
		}

		function buildBottomInnerTable(menuNode, index) {
			var label = menuNode.subElements[index].html()
			table = document.createElement('table');
			table.setAttribute('width','100%');
			table.setAttribute('border','0');
			table.setAttribute('cellSpacing','0');
			table.setAttribute('cellPadding','0');
		
			row = table.insertRow(0);
			
			var colIndex = 0;

			cell = row.insertCell(colIndex);
			cell.setAttribute('align', 'center');
			cell.setAttribute('valign', 'top');
			cell.setAttribute('height', '32px');
			cell.setAttribute('width', '100%');
			$(cell).addClass('box_submenu');
			cell.innerHTML = label;

			
			return $(table);
		}

		function restoreSelected() {
      if ($menuSelected) {
        $($menuSelected).trigger('mouseover');
      }
      if ($submenuSelected) {
      	$($submenuSelected.parentMenu.cell).trigger('mouseover');
      	$($submenuSelected).trigger('mousemove');
      }
		}

		restoreSelected();

		return this;

	}
	$.fn.topMenu.defaults = {container: '#menuContainer', delaySelected: 3000};
})(jQuery);

