var xml_dependencies_el;
var xml_categories_el;
var ddls;

function ff_selectNodes(sXPath)
{
	var oEvaluator=new XPathEvaluator();
	var oResult=oEvaluator.evaluate(sXPath,this,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);

	var aNodes=new Array;

	if (oResult!=null)
	{
		var oElement=oResult.iterateNext();
		while (oElement)
		{
			aNodes.push(oElement);
			oElement=oResult.iterateNext();
		}
	}

	return aNodes;
}

function ddlChanged(ddl,index)
{
	index--;

	var article_cats=xml_dependencies_el.selectNodes("/root/article[categories/category/@id="+ddl.value+"]/categories/category");

	if (index<2)
	{
		if (index==0)
			ddls[2].disabled=true;

		ddls[index+1].disabled=(ddl.value=="0");
		clear_ddl(ddls[index+1]);
		createOption(ddls[index+1],"0",chooseOptionTitle);

		if (!ddls[index+1].disabled)
		{
			var cats=xml_categories_el.selectNodes("/root/part_"+(index+2)+"/category");
			for (var c=0;c<cats.length;c++)
			{
				var cid=cats[c].attributes[0].nodeValue;
				
				var exist=false;
				if (!exist)
					for (var b=0;b<article_cats.length;b++)
						if (cid==article_cats[b].attributes[0].nodeValue)
						{
							exist=true;
							break;
						}
				if (exist)
					createOption(ddls[index+1],cid,cats[c].attributes[1].nodeValue);
			}
		}
	}

	var catid=ddl.value;
	
	var podminky=[];
	if (((index==0)&&(catid!="0"))||(index!=0))
	{
		podminky[0]=ddls[0].value;

		if (((index==1)&&(catid!="0"))||(index>1))
		{
			podminky[1]=ddls[1].value;
			
			if ((index==2)&&(catid!="0"))
				podminky[2]=ddls[2].value;
		}
	}
	var podm_len=podminky.length;
	
	var articles_el=document.getElementById("articles");
	for (var a=0;a<articles_el.childNodes.length;a++)
		if (articles_el.childNodes[a].id!=null)
		{
			var art_el_id=articles_el.childNodes[a].id;
			art_el_id=art_el_id.substring(8,art_el_id.length);
			var show=(podm_len==0);
		
			if (!show)
			{
				show=true;
				for (var b=0;b<podminky.length;b++)
				{
					var eee=xml_dependencies_el.selectNodes("/root/article[(@id="+art_el_id+") and (categories/category/@id="+podminky[b]+")]").length;
					if (eee==0)
					{
						show=false;
						break;
					}
				}
			}
			
			articles_el.childNodes[a].style.display=(show ? "block" : "none");
		}	
}

function init_dependencies()
{
	ddls=new Array();
	for (var a=0;a<3;a++)
		ddls[a]=document.getElementById("ddl_"+(a+1));

	if (Br.IE)
	{
		xml_dependencies_el=document.getElementById("xml_dependencies");
		xml_dependencies_el.loadXML(xml_dependencies_str);
	}
	else
	{
		var parser=new DOMParser();
		xml_dependencies_el=parser.parseFromString(xml_dependencies_str,"text/xml");
		xml_dependencies_el.selectNodes=ff_selectNodes;
	}

	if (Br.IE)
	{
		xml_categories_el=document.getElementById("xml_categories");
		xml_categories_el.loadXML(xml_categories_str);
	}
	else
	{
		var parser=new DOMParser();
		xml_categories_el=parser.parseFromString(xml_categories_str,"text/xml");
		xml_categories_el.selectNodes=ff_selectNodes;
	}
	
	ddls[1].disabled=true;
	ddls[2].disabled=true;

	fill_ddls();
}

function createOption(select,value,text)
{
	var opt=document.createElement("OPTION");
	if (Br.IE)
		select.add(opt);
	else
		select.appendChild(opt);

	opt.value=value;
	opt.text=text;
}

function fill_ddls()
{
	//for (var a=0;a<3;a++)
	//	createOption(ddls[a],"0",chooseOptionTitle);
		
	var cats=xml_categories_el.selectNodes("/root/part_1/category");
	for (var a=0;a<cats.length;a++)
		createOption(ddls[0],cats[a].attributes[0].nodeValue,cats[a].attributes[1].nodeValue);
}

function clear_ddl(ddl)
{
	while (ddl.childNodes.length!=0)
		ddl.removeChild(ddl.childNodes[0]);
}




//-- MENU START ----

var at_timeout = 50;


function at_show_aux(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  p.className = "active";

  var top  = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
  var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;

  for (; p; p = p.offsetParent)
  {
    if (p.style.position != 'absolute')
    {
      left += p.offsetLeft;
      top  += p.offsetTop;
    }
  }

  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}


function at_hide_aux(parent, child)
{
  document.getElementById(parent).className        = "parent";
  document.getElementById(child ).style.visibility = "hidden";
}


function at_show()
{
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  at_show_aux(p.id, c.id);

  clearTimeout(c["at_timeout"]);
}


function at_hide()
{
  var c = document.getElementById(this["at_child"]);

  c["at_timeout"] = setTimeout("at_hide_aux('"+this["at_parent"]+"', '"+this["at_child" ]+"')", at_timeout);
}


function at_attach(parent, child, position)
{
  p = document.getElementById(parent);
  c = document.getElementById(child );

  p["at_child"]    = c.id;
  c["at_child"]    = c.id;
  p["at_parent"]   = p.id;
  c["at_parent"]   = p.id;
  c["at_position"] = position;

  p.onmouseover = at_show;
  p.onmouseout  = at_hide;
  c.onmouseover = at_show;
  c.onmouseout  = at_hide;
}


function dhtmlmenu_build_aux(parent, child, position)
{
  document.getElementById(parent).className = "parent";

  document.write('<div class="vert_menu" id="'+parent+'_child">');

  var n = 0;
  for (var i in child)
  {
    if (i == '-')
    {
      document.getElementById(parent).href = child[i];
      continue;
    }

    if (typeof child[i] == "object")
    {
      document.write('<a class="parent" id="'+parent+'_'+n+'">'+i+'</a>');
      dhtmlmenu_build_aux(parent+'_'+n, child[i], "x");
    }
    else document.write('<a id="'+parent+'_'+n+'" href="'+child[i]+'">'+i+'</a>');
    n++;
  }

  document.write('</div>');

  at_attach(parent, parent+"_child", position);
}


function dhtmlmenu_build(menu)
{
  for (var i in menu) dhtmlmenu_build_aux(i, menu[i], "y");
}


//----- MENU END ------
