$(function() {
	$("#home")
		.button({icons: { primary: "ui-icon-home"}})
		.click(function(){
			$("#main_frame").attr("src","links/home/index.html");
		});
	$("#contacts")
		.button({icons: { primary: "ui-icon-contact"}})
		.click(function(){
			$("#main_frame").attr("src","links/contacts/index.html");
		});
	$("#sitemap")
		.button({icons: { primary: "ui-icon-extlink"}})
		.click(function(){
			window.location.href="links/sitemap/index.html";
		});				
	$( "#qlinks" ).buttonset();
});

function write_menu() {
	document.write("<div id='menuBar'>")
	for(var i=0,mhc=0;i<MenuList.length;i++) {
		var la = MenuList[i].split("|");
		if (la[0] == "0") {
			var ic=parseInt(la[2]);
			if(ic==0){
				document.write("<div class='menu' style='padding: 0px 0px 0px 0px;");
				if(i==0)document.write("border-left:none;");
				document.write("' id='mainMenu"+mhc+"' onmouseover='colorMenu(this.id)' onmouseout='uncolorMenu(this.id)'>");
				document.write("<a id='Link"+mhc+"' href='"+la[3]+"'");
				if (la[4] != "") document.write(" target='" + la[4] + "' ");
				document.write(">");
				document.write( la[1] + "</a></div>");				
			}
			else{
				document.write("<div class='menu' id='mainMenu"+mhc+"'  onmouseover='colorNpullMenu("+mhc+")' onmouseout='uncolorNpullMenu("+mhc+")'>" + la[1] + "&nbsp;&#9660;</div>");
				document.write("<div class='item_panel ui-widget-content' id='itemPanel"+mhc+"' onmouseover='colorNpullMenu("+mhc+")' onmouseout='uncolorNpullMenu("+mhc+")'>");
				for(var j=0;j<ic;j++)
				{
					la = MenuList[i+j+1].split("|");
					if(la[0] == "1"){
						document.write("<a href='"+la[2]+"'");
						if (la[3] != "") document.write(" target='" + la[3] + "' ");
						document.write("><div class='item' id='item"+mhc+"_"+j+"' onmouseover='colorItem(this.id)' onmouseout='uncolorItem(this.id)' onclick='uncolorNpullMenu("+mhc+");uncolorItem(this.id);'>&nbsp;"+ la[1] + "</div></a>");
					}
				}
				document.write("</div>");
				$("#itemPanel"+mhc).hide();
			}
			mhc++;
			if(mhc==getMenuCount())
				document.write("<div class='menu' style='padding: 0px 0px 0px 0px;border-right:none;'></div>");
			else
				intvl[mhc]=0;
			i+=ic;
		}
	}
	document.write("</div>");
}



function getMenuCount(){
	for(var i=0,c=0;i<MenuList.length;i++){
		if ((MenuList[i].split("|"))[0] == "0")
			c++;
	}
	return c;
}

function log(msg) {
    setTimeout(function() {
        throw new Error(msg);
    }, 0);
}
var intvl=new Array();

var onMenu=-1;

function pull_down(mhc){
	if ($("#itemPanel"+mhc).is(':hidden')){
		$("#itemPanel"+mhc).show("fast",function(){
			var position=$("#itemPanel"+mhc).position();
			$( "#itemPanel"+mhc+":visible" )
				.removeAttr( "style" )
				.attr("style","top:"+position.top+"px;left:"+position.left+"px");
			intvl[mhc]=setInterval("pull_up("+mhc+")",500);
		});
	}
}

function pull_up(mhc){
	if(onMenu!=mhc && $("#itemPanel"+mhc).is(':visible')){
		$("#itemPanel"+mhc).hide("fast",function(){
			clearInterval(intvl[mhc]);
			uncolorMenu("mainMenu"+mhc);
		});
	}
}


function colorNpullMenu(mhc) {
	onMenu=mhc;
	pull_down(mhc);
	var menu_id="mainMenu"+mhc;
	colorMenu(menu_id);
}

function uncolorNpullMenu(mhc) {
	onMenu=-1;	
}

function colorMenu(obj) {
	document.getElementById(obj).style.background="url('images/menu_bg_sel.jpg') repeat-x";
	document.getElementById(obj).style.color = "White";
}

function uncolorMenu(obj) {
	document.getElementById(obj).style.background = "";
	document.getElementById(obj).style.color = "LightYellow";
}

function colorItem(obj) {
	document.getElementById(obj).style.background="brown url('images/bg.jpg') repeat-y";
	document.getElementById(obj).style.color = "White";
}

function uncolorItem(obj) {
	document.getElementById(obj).style.background = "";
	document.getElementById(obj).style.color = "Black";
}

function placeMenuPanel()
{
	for(var i=0;i<getMenuCount();i++){
		var obj=document.getElementById("itemPanel"+i);

		if(obj!=null){
			var menuItem=document.getElementById("mainMenu"+i);
			obj.style.left=menuItem.offsetLeft;
			var position=$("#mainMenu"+i).position();
			obj.style.top=position.top+menuItem.offsetHeight;
		}
	}
}
