is_sub = false;
is_sub_sub = false;
only_clear = false;
only_clear_sub = false;
sub_count = 0;
sub_sub_count = 0;
changed = false;
changed_sub = false;

function changeMainBg (where)
{
	var r, id = 0;
	
	oTable = document.getElementById('dynamic');
	for (r = 0; r < oTable.rows.length; r++)
	{
		oRow = oTable.rows[r];

		if (oRow.id == where) {
			id = r;
			break;
		}
	}
		
	var bg = oRow.style.backgroundImage;
	var new_bg = '';
	
	// TOP
	if (bg.indexOf ('top2.gif') > -1) {
		    new_bg = 'top';
	} else
	if (bg.indexOf ('top.gif') > -1) {
	    new_bg = 'top2';
	} else
	// MIDDLE
	if (bg.indexOf ('middle2.gif') > -1) {
		new_bg = 'middle';
	} else
	if (bg.indexOf ('middle.gif') > -1) {
		new_bg = 'middle2';
	} else
	// BOTTOM
	if (bg.indexOf ('bottom2.gif') > -1) {
		new_bg = 'bottom';
	} else
	if (bg.indexOf ('bottom.gif') > -1) {
		new_bg = 'bottom2';
	}
	
	oRow.style.background = 'url(\'menu/'+new_bg+'.gif\')';
}

function changeBg (where)
{
	var r, id = 0;

	oTable = document.getElementById('dynamic');
	for (r = 0; r < oTable.rows.length; r++)
	{
		oRow = oTable.rows[r];

		if (oRow.id == where) {
			id = r;
			break;
		}
	}
	
	oRow = oTable.rows[id];
	if (!changed) {
		oRow.style.background="url('menu/sub2.gif')";
		changed = true;
	} else {
		oRow.style.background="url('menu/sub.gif')";
		changed = false;
	}
}

function changeSubBg (where)
{
	var r, id = 0;

	oTable = document.getElementById('dynamic');
	for (r = 0; r < oTable.rows.length; r++)
	{
		oRow = oTable.rows[r];

		if (oRow.id == where) {
			id = r;
			break;
		}
	}
	
	oRow = oTable.rows[id];
	if (!changed_sub) {
		oRow.style.background="url('menu/sub_sub2.gif')";
		changed_sub = true;
	} else {
		oRow.style.background="url('menu/sub_sub.gif')";
		changed_sub = false;
	}
}

function subSubMenu (where, name, link, set_sub_sub)
{
	var r, id;

	if (is_sub_sub) {
		// trinam sub sub meniu
		for (r = 0; r < oTable.rows.length; r++) {
				oRow = oTable.rows[r];
				
				if (oRow.id == 'sub_'+where+'_'+name) {
					only_clear_sub = true;
				}
				
				if (oRow.id.substr (0,7) == 'sub_sub') {
					oTable.deleteRow (r);
					r--;
				}
		}
		
		if (set_sub_sub) is_sub_sub = false;
		sub_sub_count = 0;
		
		if (only_clear_sub) {
			if (set_sub_sub) only_clear_sub = false;
			return;
		}

	}

	oTable = document.getElementById('dynamic');	
	for (r = 0; r < oTable.rows.length; r++) {
		oRow = oTable.rows[r];

		if (oRow.id == where) {
			id = r;
			break;
		}
	}
	
	oRow = oTable.insertRow (id + 1 + sub_sub_count);
	oRow.vAlign = 'top';
	oRow.id = 'sub_'+where+'_'+name;
	oRow.style.height=31;
	oRow.style.background="url('menu/sub_sub.gif')";
	
	var padding = 0;
	if (name.length > 18) {
		padding = 2;
		name = name.replace (' ', '<br>');
	}
	else padding = 9;
	
	oCell = oRow.insertCell (0);
	oCell.innerHTML = '<div class="sub_sub_menu" style="padding-top: '+padding+'"px onMouseOver="changeSubBg (\''+oRow.id+'\')" onMouseOut="changeSubBg (\''+oRow.id+'\')" onClick="document.location=\''+link+'\'">'+name+'</div>';
	++sub_sub_count;
	
	is_sub_sub = set_sub_sub;
}

function subMenu (where, name, link, set_sub, sub_sub)
{
	oTable = document.getElementById('dynamic');
	var r, id;
		
	if (is_sub) {
	 //trinam rowsus
	 for (r = 0; r < oTable.rows.length; r++) {
	 	oRow = oTable.rows[r];
		
		if (oRow.id == 'sub_'+where+'_'+name) {
			only_clear = true;
		}
		
		if (oRow.id.substr(0, 3) == 'sub') {
			oTable.deleteRow (r);
			r--;
		}
	 }
	 sub_count = 0;
	 if (set_sub) {
	 	is_sub = false;
	}
	if (only_clear) {
		if (set_sub) only_clear = false;
		return;
	}
}
	
	// dedam rowsus
	for (r = 0; r < oTable.rows.length; r++) {
		oRow = oTable.rows[r];
		if (oRow.id == where) {
			id = r;
			break;
		}
	}
	oRow = oTable.insertRow (id + 1 + sub_count);
	oRow.style.height=31;
	oRow.style.background="url('menu/sub.gif')";
	
	oCell = oRow.insertCell (0);	
//	oCell.innerHTML = '<a href="'+url+'" style="text-decoration:none"><div onMouseOver="changeBg ('+oRow.rowIndex+')" onMouseOut="changeBg ('+oRow.rowIndex+')" class="menu">'+url+"</div></a>";
	oRow.id = 'sub_'+where+'_'+name;
	
	oRow.vAlign = 'top';
	
	//subSubMenu(\'sub_'+where+'_'+name+'\', \'linkas1\', \'#\', false);subSubMenu(\'sub_'+where+'_'+name+'\', \'linkas2\', \'#\', true);

	if (sub_sub.length == 0) {
		sub_sub = 'document.location=\''+link+'\'';
	}
	oCell.innerHTML = '<div style="padding-top: 9px" onMouseOver="changeBg (\''+oRow.id+'\')" onMouseOut="changeBg (\''+oRow.id+'\')" onClick="'+sub_sub+'" class="sub_menu">'+name+'</div>';		
	
	is_sub = set_sub;
	sub_count++;
}

function adminPages (self)
{
	var titles = document.all.titles;
	var pages = document.all.pages;
	
	
	if (self.name == 'pages') {
		titles.selectedIndex = pages.selectedIndex;
	} else {
		pages.selectedIndex = titles.selectedIndex;
	}
	
	document.all.title.value = titles.options[titles.selectedIndex].text;
}

function w_open(sek_id, p_kodas, get_sek) {
window.open('techniniai.php?sek_id='+sek_id+'&p_kodas='+p_kodas+'&get_sek='+get_sek+'','techninio', 'height=550, width=590, scrollbars=1, menubar=0, toolbar=0, location=0, status=0');
}

function newItem (id) 
{
	window.open('?d='+id, 'Window', 'height=450, width=530, scrollbars=0, menubar=0, toolbar=0, location=0, status=0');
}

function preloadImage (src)
{
	img = new Image();
	img.src = src;
}

function loadMenu ()
{
	var menu = new Array ("top2", "bottom2", "middle2", "sub", "sub2", "sub_sub", "sub_sub2");
	for (i in menu) {
		preloadImage ("menu/"+menu[i]+".gif");
	}
}

loadMenu ();
