var isIE = (navigator.appName == "Microsoft Internet Explorer");

var myimg = new Image();
myimg.src = "img/background-submenu.png";

var st_id_selected = 0;

var submenus = new Array();
var vqty = 0;

var fvis = -1;

function fixPNG(element)
{
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
	{
		var src;

		if (element.tagName=='IMG')
		{
			if (/\.png$/.test(element.src))
			{
				src = element.src;
				element.src = "img/null.gif";
			}
		}
		else
		{
			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
			if (src)
			{
				src = src[1];
				element.runtimeStyle.backgroundImage="none";
			}
		}

		if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
}

function pagesize()
{
	var coeff = isIE ? 4 : 0;
	var h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
	var ch = h - 62 - 103 - coeff;
	var mh = h - 265 - 103 - coeff;

	var dh = ch - 80 - coeff;
	var elem = document.getElementById ("content-td");
	elem.style.height = ch + 'px';
	var elem2 = document.getElementById ("menu-td");
	elem2.style.height = mh + 'px';

	var welem = document.getElementById ("outerWrapper");
	if (welem)
	{
	    welem.style.height = ch - 30;
	}

	var celem = document.getElementById ("page");
	if (celem)
	{
	    celem.style.display = "block";
	}

	var ctd = document.getElementById ("contact-td-bottom");
	if (!ctd)
		var ctd = document.getElementById ("contact-td-bottom-en");
	var bl = getObjectLeft (ctd);
	var mcl = document.getElementById ("main-contact-link");
	if (mcl)
	{
	    if (bl > 0)
	    {
		    mcl.style.left = (bl + 160) + "px";
		}
		else
		{
		    var ww = getInsideWindowWidth ();
		    var bl = 2 + 397 + parseInt ((ww - 765)/2);
		    mcl.style.left = (bl + 160) + "px";
		}
	}

	var dbr = document.getElementById ("bottom-rights");
	if (dbr)
	{
		if (isIE)
		    var t = h - 83 + 9;
		else
		    var t = h - 83 + 13;
	    dbr.style.top = t + "px";
	    var ww = getInsideWindowWidth ();
	    var l = 550 + parseInt ((ww - 765)/2);
	    dbr.style.left = l + "px";
	}
}

function navshow ()
{
	var vis_elem = document.getElementById ("fvis");

	if (vis_elem && parseInt (vis_elem.innerHTML) >= 0)
	{
		fvis = parseInt (vis_elem.innerHTML);
	}

	var elem2 = document.getElementById ("menu-td");
	var mh = parseInt (elem2.style.height);

	vqty = parseInt ((mh - 38) / 30);
	var sel = document.getElementById ("selected-id");

    var divsset = document.getElementsByTagName ("div");

	if (fvis > divsset.length - vqty)
	    fvis = -1;

	var k = 0;
	var j = 0;
	var div_id = '';
	if (parseInt (sel.innerHTML) > 0)
	{
	    var comp_v = 'submenu-' + sel.innerHTML;
	    for (var i = 0; i < divsset.length; i++)
	    {
	        var cl = divsset[i].className;
			if (cl == 'submenu-button' || cl == 'submenu-button-selected')
			{
			    if (divsset[i].style.display == 'block')
			    {
				    div_id = divsset[i].getAttribute ("id");
				    if (div_id == comp_v)
				    {
				        j = k;
				    }
				    k++;
			    }
			}
	    }
	}

	k = 0;
	var kv = 0;
    for (i = 0; i < divsset.length; i++)
    {
        var cl = divsset[i].className;
		if (cl == 'submenu-button' || cl == 'submenu-button-selected')
		{
		    if (divsset[i].style.display == 'block')
		    {
			    var eh = getObjectHeight (divsset[i]);
			    if (eh > 30)
			        vqty = vqty - 1;
			    submenus[k] = divsset[i].getAttribute ("id");

				if ((kv < vqty && k + vqty <= j) || (k < fvis))
			    {
			        divsset[i].style.display = 'none';
			    }
			    else if (kv >= vqty)
			    {
			        divsset[i].style.display = 'none';
			    }
			    else
			    {
				    if (fvis < 0)
				    {
				        fvis = k;
				    }
			        kv++;
			    }
			    k++;
		    }
		}
    }

    var ml = getObjectLeft (elem2);
	if (ml > 0)
	    var sl = ml + 265 - 1;
	else
	{
	    var ww = getInsideWindowWidth ();
	    var sl = 2 + 397 + 265 + parseInt ((ww - 765)/2);
	}

	var nhome = document.createElement ("img");
	nhome.id = "menu-nav-home";
	nhome.className = "menu-nav-home";
    nhome.style.width = "24px";
    nhome.style.height = "24px";
	nhome.style.position = "absolute";
	nhome.style.top = "7px";
    nhome.style.left = "5px";
    nhome.src = "img/background-menu-nav-home.png";
    nhome.onclick = function() {mainmenu()};
    nhome.style.cursor = "pointer";
    nhome.style.cursor = "hand";
    fixPNG(nhome);

	if (submenus.length > 0)
	{
		var ntop = document.createElement ("img");
		ntop.id = "menu-nav-top";
		ntop.className = "menu-nav-top";
	    ntop.style.width = "24px";
	    ntop.style.height = "24px";
		ntop.style.position = "absolute";
		ntop.style.top = "39px";
	    ntop.style.left = "5px";

		if (vqty < submenus.length)
		{
		    if (fvis > 0)
		    {
			    ntop.src = "img/background-menu-nav-top.png";
			    ntop.onclick = function() {submenuup()};
			    ntop.style.cursor = "pointer";
			    ntop.style.cursor = "hand";
			}
			else
			    ntop.src = "img/background-menu-nav-top-p.png";
		    fixPNG(ntop);
		}
		else
		{
		    ntop.src = "img/null.gif";
		}

		var nbot = document.createElement ("img");
		nbot.id = "menu-nav-bottom";
		nbot.className = "menu-nav-bottom";
	    nbot.style.width = "24px";
	    nbot.style.height = "24px";
		nbot.style.position = "absolute";
		nbot.style.top = "71px";
	    nbot.style.left = "5px";
	    nbot.style.zIndex = "1000";

		if (vqty < submenus.length)
		{
			if (fvis + vqty == submenus.length)
			    nbot.src = "img/background-menu-nav-bottom-p.png";
			else
			{
			    nbot.src = "img/background-menu-nav-bottom.png";
                nbot.onclick = function() {submenudown()};
			    nbot.style.cursor = "pointer";
			    nbot.style.cursor = "hand";
			}
		    fixPNG(nbot);
		}
		else
		{
		    nbot.src = "img/null.gif";
		}
	}

	var menunav = document.createElement ("div");
	menunav.id = "menu-nav";
	menunav.className = "menu-nav";
	menunav.style.position = "absolute";
    menunav.style.left = sl + "px";
    menunav.style.border = "none";
    menunav.style.padding = "0px";
    menunav.style.backgroundPosition = "0% 0%";
    menunav.style.backgroundRepeat = "no-repeat";
    menunav.style.width = "34px";
    menunav.style.height = "102px";
	menunav.style.visibility = "visible";

	if (isIE)
		navtop = 265;
	else
	    navtop = 265;

	menunav.style.top = navtop + "px";

	if (submenus.length && vqty < submenus.length)
	{
		menunav.appendChild (ntop);
		menunav.appendChild (nbot);
	    menunav.style.backgroundImage = "url(img/background-menu-nav.png)";
	}
	else
	{
	    menunav.style.backgroundImage = "url(img/background-menu-nav-small.png)";
	}
    menunav.style.filter = "expression(fixPNG(menunav))";
	menunav.appendChild (nhome);
	document.body.appendChild (menunav);
}

function linksupd ()
{
    var asset = document.getElementsByTagName ("a");
	var ahref = '';

    for (var i = 0; i < asset.length; i++)
    {
        var cl = asset[i].className;
		if (cl == 'submenu-link')
		{
		    ahref = asset[i].href;
			if (ahref.indexOf("vis=") == -1)
			    asset[i].setAttribute('href', ahref + '&fvis=' + fvis);
			else
			    asset[i].setAttribute('href', ahref.substr (0, ahref.indexOf("vis=") - 1) + '&fvis=' + fvis);
		}
	}
}

function submenudown ()
{
    var ind = fvis;
    var elem_name = submenus[ind];
	var elem = document.getElementById(elem_name);
	elem.style.display = "none";

	ind = fvis + vqty;
    elem_name = submenus[ind];
	elem = document.getElementById(elem_name);
	elem.style.display = "block";

	fvis++;

    var velem = document.getElementById("menu-nav-top");
    velem.src = "img/background-menu-nav-top.png";
    fixPNG(velem);
    velem.onclick = function() {submenuup()};
    velem.style.cursor = "pointer";
    velem.style.cursor = "hand";

	if (fvis + vqty == submenus.length)
	{
	    var nelem = document.getElementById("menu-nav-bottom");
	    nelem.src = "img/background-menu-nav-bottom-p.png";
	    fixPNG(nelem);
	    nelem.onclick = function() {return false};
	    nelem.style.cursor = "auto";
	}

    linksupd ();

}

function submenuup ()
{
    var ind = fvis + vqty - 1;
    var elem_name = submenus[ind];
	var elem = document.getElementById(elem_name);
	elem.style.display = "none";

	ind = fvis - 1;
    elem_name = submenus[ind];
	elem = document.getElementById(elem_name);
	elem.style.display = "block";

	fvis--;

    var velem = document.getElementById("menu-nav-bottom");
    velem.src = "img/background-menu-nav-bottom.png";
    fixPNG(velem);
    velem.onclick = function() {submenudown()};
    velem.style.cursor = "pointer";
    velem.style.cursor = "hand";

	if (fvis == 0)
	{
	    var nelem = document.getElementById("menu-nav-top");
	    nelem.src = "img/background-menu-nav-top-p.png";
	    fixPNG(nelem);
	    nelem.onclick = function() {return false};
	    nelem.style.cursor = "auto";
	}

    linksupd ();
}

function changemenuview (menu_name, flag, lang)
{
	var elem_name = "menu-" + menu_name;
	var elem = document.getElementById(elem_name);

	var img_elem_name = "img-" + menu_name;

    if (flag == 'show')
    {
        var img_name = (lang == 'en') ? "img/menu-" + menu_name + "-active-en.png" : "img/menu-" + menu_name + "-active.png";
    }
    else if (flag == 'hide')
    {
        var img_name = (lang == 'en') ? "img/menu-" + menu_name + "-passive-en.png" : "img/menu-" + menu_name + "-passive.png";
    }

    var myimg = new Image();
    myimg.src = img_name;
	document.images[img_elem_name].src = myimg.src;
	fixPNG(document.images[img_elem_name]);
}

function changesubmenuview (menu_id, flag)
{
	var elem_name = "submenu-" + menu_id;
	var elem = document.getElementById(elem_name);

	if (isIE)
	{
		var elem_name2 = "submenu-selected-" + menu_id;
		var elem2 = document.getElementById(elem_name2);

	    if (flag == 'show')
	    {
	        elem.style.display = "none";
	        elem2.style.display = "block";
	    }
	    else if (flag == 'hide')
	    {
	        elem.style.display = "block";
	        elem2.style.display = "none";
	    }
	}
	else
	{
	    if (flag == 'show')
	    {
			elem.className = "submenu-button-selected";
	    }
	    else if (flag == 'hide')
	    {
			elem.className = "submenu-button";
	    }
	}
}

function showimage(image_name, image_width, image_height, w_title)
{
	var newWindow;

    var left = parseInt((screen.availWidth/2) - (image_width/2));
    var top = parseInt((screen.availHeight/2) - (image_height/2));
    width = parseInt (image_width) + 20;
    height = parseInt (image_height) + 20;
    var windowFeatures = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top + ",menubar=0,location=0,directories=0,hotkeys=0,personalbar=0,toolbar=0,titlebar=0,status=0";

    if (!newWindow || newWindow.closed)
    {
        newWindow = window.open ("javascript:\"<title>" + w_title + "</title><img src='" + image_name + "'>\"", "editWind", windowFeatures);
    }
    else
    {
        newWindow.focus();
    }
}

function OpenImagePopup (imgPath, imgWidth, imgHeight, title) {
    var win = window.open('','preview',
    'width=' + imgWidth + ',height=' + imgHeight + ',left=0,top=0,screenX=0,screenY=0,resizable=1,scrollbar=1,status=0');

    var winDoc = win.document;
    var alt = title;
    var content = '<html><head>'+
        '<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">' +
		'<title>' + title + '</title>' +
		'<style>body{overflow: hidden;margin:0;}img{border:0;}</style>' +
		'</head><body><a href="javascript:self.close()">' +
		'<img alt="' + alt + '" id="image" src="' + imgPath + '" /></a></body></html>'
    win.document.write(content);

    winDoc.body.onload = function() {
    	var obj = winDoc.getElementById('image');
    	var w = obj.width, h = obj.height;
    	var iHeight= document.body.clientHeight, iWidth = self.innerWidth;

    	var left = (self.opera ? iWidth : screen.availWidth)/2 - w/2;
    	var top =  (self.opera ? iHeight : screen.availHeight)/2 - h/2;
    	win.resizeTo(w+10, h+26);
    	win.moveTo(left, top);
    }

    win.onload = winDoc.body.onload;

    win.document.close();
    win.focus();
}

function scrollerupd (selectedObj, index, scroller, contentElem, ch_old)
{
	if (selectedObj)
	{
		var vis = scroller.scrollWrapper.style.visibility;
		var oh = scroller.ownerHeight;
		var ch = contentElem.scrollHeight;

	    scroller.contentScrollHeight = ch;
		if (oh < ch && vis == "hidden")
		{
			scroller.scrollWrapper.style.visibility = "visible";
		}
		if (oh >= ch && vis == "visible")
		{
		    scroller.thumb.style.top = "11px";
			scroller.scrollWrapper.style.visibility = "hidden";
			shiftTo (scroller.ownerContentID, 0, 0);
		}
		if (oh < ch && ch_old < ch && vis == "visible")
		{
			var barLength = scroller.scrollWrapperLength - (scroller.quirks.scrollBorder*2);
			var buttonLength = scroller.buttonLength;
			barLength -= buttonLength*2;
			var docElem = scroller.contentElem;
			var docTop = Math.abs (parseInt (docElem.style.top));
			var scrollFactor = docTop/(scroller.contentScrollHeight - scroller.ownerHeight);
			shiftTo (scroller.thumb, 0, Math.round ((barLength - scroller.thumbLength) * scrollFactor) + buttonLength);
		}
		if (oh < ch && ch_old > ch && vis == "visible")
		{
			var barLength = scroller.scrollWrapperLength - (scroller.quirks.scrollBorder*2);
			var buttonLength = scroller.buttonLength;
			var thumbLength = scroller.thumbLength;
			var wellTop = buttonLength;
			var wellBottom = barLength - buttonLength - thumbLength;
			var wellSize = wellBottom - wellTop;
			var thumbTop = parseInt (getElementStyle (scroller.thumb.id, "top", "top"));
			var scrollFactor = (thumbTop - buttonLength)/wellSize;
			var docElem = scroller.contentElem;
			var docTop = Math.abs (parseInt (docElem.style.top));
			var scrollHeight = scroller.contentScrollHeight;
			var height = scroller.ownerHeight;
			shiftTo (scroller.ownerContentID, 0, -(Math.round ((scrollHeight - height)*scrollFactor)));

			barLength -= buttonLength*2;
			docTop = Math.abs (parseInt (docElem.style.top));
			scrollFactor = docTop/(scroller.contentScrollHeight - scroller.ownerHeight);
			shiftTo (scroller.thumb, 0, Math.round ((barLength - scroller.thumbLength) * scrollFactor) + buttonLength);
		}
        selectedObj = null;
	}
}

function showstufftext (st_id, lang)
{
    var elem = document.getElementById ("stuff-speech-" + st_id);
    var elem2 = document.getElementById ("stuff-link-" + st_id);

	if (elem && elem2)
	{
		setSelectedThumb ("thumb0");
		if (selectedObj)
		{
		    var index = selectedObj.index;
		    var scroller = scrollBars[index];
			var contentElem = document.getElementById("innerWrapper");

			var ch_old = contentElem.scrollHeight;
		}

	    var elem_disp = elem.style.display;
	    if (elem_disp == "none")
	    {
	        elem.style.display = "block";
	        if (lang == 'en')
		        elem2.innerHTML = "&#187; hide an issue";
			else
		        elem2.innerHTML = "&#187; скрыть обращение";
	    }
	    else
	    {
	        elem.style.display = "none";
	        if (lang == 'en')
		        elem2.innerHTML = "&#187; read an issue";
			else
		        elem2.innerHTML = "&#187; читать обращение";
	    }

		scrollerupd (selectedObj, index, scroller, contentElem, ch_old);
	}

}

function showblocktext (b_id)
{
    var elem = document.getElementById ("block-short-" + b_id);
    var elem2 = document.getElementById ("block-text-" + b_id);

	if (elem && elem2)
	{
		setSelectedThumb ("thumb0");
		if (selectedObj)
		{
		    var index = selectedObj.index;
		    var scroller = scrollBars[index];
			var contentElem = document.getElementById("innerWrapper");

			var ch_old = contentElem.scrollHeight;
		}

	    var elem_disp = elem2.style.display;
	    if (elem_disp == "none")
	    {
	        elem.style.display = "none";
	        elem2.style.display = "block";
	    }
	    else
	    {
	        elem.style.display = "block";
	        elem2.style.display = "none";
	    }

		scrollerupd (selectedObj, index, scroller, contentElem, ch_old);
	}

}

function mainmenu ()
{
	var sm_elem = document.getElementById("submenu-content");

	if (sm_elem)
	{
	    sm_elem.style.display = "none";
	}

	var mm_elem = document.getElementById("mainmenu-content");

	if (mm_elem)
	{
	    mm_elem.style.display = "block";
	}

	var nav_elem = document.getElementById("menu-nav");
	if (nav_elem)
	    nav_elem.style.display = "none";
}


