//
// базовые функции
//

function fixPNG(element,sizingMethod) {
	// sizingMethod = [scale|crop]
	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 = "/common/img/spacer.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='"+sizingMethod+"')";
	}
}


function getBrowserName() {
	if (navigator.appName=="Opera") return "opera";
	else if (navigator.appName=="Netscape") return "nn";
	else if (navigator.appName=="Microsoft Internet Explorer") return "ie";
	else return "any";
}

function ap_getObj(name) {
	// сделать ссылку на объект
	if (document.getElementById) return document.getElementById(name);
	else if (document.all) return document.all[name];
	else if (document.layers) return document.layers[name];
}

function show_hide_obj(obj_id, view) {
	// скрыть или отобразить obj_id
	obj=ap_getObj(obj_id);
	if (view==1) {
		obj.style.display="block";
	} else {
		obj.style.display="none";
	}
}

function LoadBigImage(src,w,h) {
	var l=screen.width/2-w/2;
	var t=screen.height/2-h/2-30;
	var scroll="no";
	var dw=screen.width-w;
	var dh=screen.height-h;
	if (dw<20 || dh<20) scroll="yes";
	var param="resizable=yes,scrollbars="+scroll+",Toolbar=0,location=0,Directories=0,Status=0,Menubar=0,"+"width="+w+", height="+h+", left="+l+", top="+t;
	windowID=window.open('', target="_blank",param);
	windowID.document.write("<html>\n<head>\n<title></title>\n</head>\n");
	windowID.document.write("<body leftmargin=\"0\" topmargin=\"0\" rightmargin=\"0\" bottomargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\n");
	windowID.document.write("<table cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%;height:100%;text-align:center;vertical-align:middle;\">\n<tr>\n<td>");
	windowID.document.write("<img src=\""+src+"\" width=\""+w+"\" height=\""+h+"\">\n");
	windowID.document.write("</td>\n</tr>\n</table>\n</body>\n</html>\n");
}

//
// библиотека меню
//

if(typeof HTMLElement != "undefined") {
	if(!HTMLElement.prototype.insertAdjacentElement) {
		HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode) {
			switch (where){
			case 'beforeBegin':
				this.parentNode.insertBefore(parsedNode,this)
				break;
			case 'afterBegin':
				this.insertBefore(parsedNode,this.firstChild);
				break;
			case 'beforeEnd':
				this.appendChild(parsedNode);
				break;
			case 'afterEnd':
				if (this.nextSibling)             
					this.parentNode.insertBefore(parsedNode,this.nextSibling);
				else this.parentNode.appendChild(parsedNode);
				break;
			}
		}
		HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr) {
			var r = this.ownerDocument.createRange();
			r.setStartBefore(this);
			var parsedHTML = r.createContextualFragment(htmlStr);
			this.insertAdjacentElement(where,parsedHTML)
		}	
		HTMLElement.prototype.insertAdjacentText = function(where,txtStr) {
			var parsedText = document.createTextNode(txtStr)
			this.insertAdjacentElement(where,parsedText)
		}
	}
	if (!HTMLElement.prototype.innerHTML) {
		HTMLElement.prototype.innerHTML = function (str) {
		   var r = this.ownerDocument.createRange();
		   r.selectNodeContents(this);
		   r.deleteContents();
		   var df = r.createContextualFragment(str);
		   this.appendChild(df);
		   return str;
		}	
		HTMLElement.prototype.outerHTML = function (str) {
		   var r = this.ownerDocument.createRange();
		   r.setStartBefore(this);
		   var df = r.createContextualFragment(str);
		   this.parentNode.replaceChild(df, this);
		   return str;
		}
	}
}

// глобальные переменные для меню
var cur_menu_id = "";
var menu_show = new Array();
var menu_gen = new Array();
var menu_id = "";
var menu_x = 0;
var menu_y = 0;
var popTimer = 0;
var menu_is_en=false;

function getSty(id) {
	var isNS4 = (document.layers ? true : false);
    return (isNS4 ? ap_getObj(id) : ap_getObj(id).style);
} 

function Item(pid, text, url, img, img_over, type, ori, div_style, table_style, td_style) {
	this.pid = pid;
	this.text = text;
	this.url = url;
	this.img = (img?img:false);
	this.img_over = (img_over?img_over:false);
	this.ori = ori;
	this.type = type;
	this.div_style = (div_style?div_style:false);
	this.table_style = (table_style?table_style:false);
	this.td_style = (td_style?td_style:false);
	this.x = 0;
	this.y = 0;
	this.h = 0;
	this.w = 0;
	if (url.substr(0,3)=="/en") menu_is_en=true;
}

function check_sys() {
	var temp = new Array();
	var i = "";
	var hide;

	temp[temp.length] = menu_status;
	if (menu_status != 0) {
		if (menu[menu_status].pid != '') {
			i=menu[menu_status].pid;
			temp[temp.length] = i;
			while (menu[i].pid != '') {
				i=menu[i].pid;
				temp[temp.length]=i;
			}
		}
	}
	for (i=0; i<menu_show.length; i++) {
		hide = true;
		for(j=0; j<temp.length; j++) {
			if(menu_show[i] == temp[j]) {
				hide = false;
				break;
			}
		}
		if (hide == true) {
			getSty("a" + menu_show[i]).visibility = "hidden";
		}
	}
	if (cur_menu_id) {
		if (ap_getObj("a" + cur_menu_id)) {
			if (getSty("a" + cur_menu_id).visibility == "hidden") {
				ap_getObj("td_x_" + cur_menu_id).style.background = "#0A5F09"; //цвет фона mouse-out
				ap_getObj("td_y_" + cur_menu_id).src = "/common/img/arr-h.gif";
				cur_menu_id = '';
			}
		} else {
			if (popTimer) {
				ap_getObj("td_x_" + cur_menu_id).style.background = "#0A5F09"; //цвет фона mouse-out
				ap_getObj("td_y_" + cur_menu_id).src = "/common/img/arr-h.gif";
				cur_menu_id = '';
			}
		}
	}
}

function td_menu_over(id) {
	clearTimeout(popTimer);
	write_menu_custom(id);
	menu_status = id;
	var w=document.body.scrollWidth;
	check_sys();
	popTimer = 0;
	
	if (menu[id].pid == 0 && cur_menu_id) {
		ap_getObj("td_y_" + cur_menu_id).src = "/common/img/arr-h.gif";
		ap_getObj("td_x_" + cur_menu_id).style.background = "#007500";
		cur_menu_id = '';
	}
	if (menu[id].pid == 0) {
		ap_getObj("td_y_" + id).src = "/common/img/arr-v.gif";
		ap_getObj("td_x_" + id).style.background = "#007500";
		cur_menu_id = id;
	}
	if (ap_getObj("a" + id) && id != '') {
		getSty("a" + id).visibility = "visible";
		var add = true;
		for(i=0; i<menu_show.length; i++) if(menu_show[i] == id) add = false;
		if (add == true) menu_show[menu_show.length] = id;
	}
	
	check_sys();
}

function td_menu_out() {
	menu_status = 0;
	clearTimeout(popTimer);
	popTimer = setTimeout('check_sys()', 600);
}

//
// пишем меню
//

function write_menu_l1(ox, oy) {
	menu_x = ox;
	menu_y = oy;
	write_menu_custom(0);
}

function write_menu_custom(id) {
	var temp_menu = new Array();
	var index_menu = new Array();
	var str = '';

	for (i=0; i<menu_gen.length; i++) if (menu_gen[i] == id) return true;
	for (i=1; i<menu.length; i++) {
		if (menu[i].pid == id) {
			temp_menu[(temp_menu.length?temp_menu.length:0)] = menu[i];
			index_menu[(index_menu.length?index_menu.length:0)] = i;
		}
	}
	if (temp_menu.length) {
		if (menu_gen.length && menu[id].ori== 2) {
			xx = menu[id].x + menu[id].w;
			yy = menu[id].y;
		} else {
			xx = menu_gen.length?menu[id].x:'';
			yy = menu_gen.length?(menu[id].y + menu[id].h):'';
		}
		
		if (!menu_gen.length) {
			str =  '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
			str += '<tr bgcolor="#007500">';
			for (i=0; i<temp_menu.length; i++){
				if (menu[index_menu[i]].div_style != false) {
					str += '<td bgcolor="#759F5D" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '"  align="center" nowrap width="1%" background="/common/img/bg-m-r-a.gif" class="bgr"><div style="padding-top: 6px; padding-bottom: 6px;"><img src="/common/img/arr-v.gif" width="7" height="7" hspace="4" border="0" alt=""><b><span class="menu">'+ temp_menu[i].text +'</span></b>&nbsp;&nbsp;</div></td>';
				}else{
					str += '<td id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" align="center" nowrap width="1%" background="/common/img/bg-m-r.gif" class="bgr"><div style="padding-top: 6px; padding-bottom: 6px;"><img id="td_y_' + index_menu[i] + '" name="td_y_' + index_menu[i] + '" src="/common/img/arr-h.gif" width="7" height="7" hspace="4" border="0" alt=""><span class="menu">'+ temp_menu[i].text +'</span>&nbsp;&nbsp;</div></td>';
				}
			}
			str += '</tr>';
			str += '<tr>';
			for (i=0; i<temp_menu.length; i++) {
				str += '<td bgcolor="#0A5F09" id="td_x_' + index_menu[i] + '" name="td_x_' + index_menu[i] + '"><div style="width: 0px; height: 3px;"><spacer type="block" height="3"></spacer></div></td>';
			}
			str += '</tr>';
			str += '</table>';
		} else {
			str = "<div id='a" + id + "' style='position: absolute; left: " + xx + "px; top: " + yy + "px; width:" + (ap_getObj("td_" + id).offsetWidth+5) + "px; visibility: visible; z-index: 999999;'>";
			str += '<table border="0" id="menu_tbl_'+id+'" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" width="100%">';
			str += '<tr>';
			str += '<td bgcolor="#007500"><div style="width: 1px; height: 0px;"><spacer type="block" width="1"></spacer></div></td>';
			str += '<td colspan="3" background="/common/img/m-bg.gif" align="right"><img src="/common/img/m-r.gif" width="4" height="4" border="0" alt=""></td>';
			str += '</tr>';
			for (i=0; i<temp_menu.length; i++){
				str += '<tr>';
				str += '<td bgcolor="#007500"><div style="width: 1px; height: 0px;"><spacer type="block" width="1"></spacer></div></td>';
				if (temp_menu[i].url=="#") {
					str += '<td style="padding:3px 3px 2px 5px;font-size:10px;font-weight:bold;" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();">' + temp_menu[i].text + '</td>';
				} else {
					str += '<td width="100%" id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" nowrap><a href="' + temp_menu[i].url + '"' + ((temp_menu[i].table_style == '1')?(" target=_blank"):("")) + ' class="ddmenu"><img src="/common/img/m-li.gif" width="3" height="3" hspace="5" vspace="2" border="0" alt="">' + temp_menu[i].text + '</a><div style="width: 0px; height: 3px;"><spacer type="block" height="3"></spacer></div></td>';
				}
				str += '<td><div style="width: 10px; height: 0px;"><spacer type="block" width="10"></spacer></div></td>';
				str += '<td bgcolor="#007500"><div style="width: 1px; height: 0px;"><spacer type="block" width="1"></spacer></div></td>';
				str += '</tr>';
			}
			str += '<tr>';
			str += '<td colspan="4" bgcolor="#007500"><div style="width: 0px; height: 1px;"><spacer type="block" height="1"></spacer></div></td>';
			str += '</tr>';
			str += '</table>';
			str += "</div>";
		}
		if (menu_gen.length) {
			ap_getObj('place_menu2').innerHTML += str;
		} else {
			ap_getObj('place_menu').innerHTML = str;
		}
		for (i=0; i<temp_menu.length; i++) {
			if (menu_gen.length) {
				menu[index_menu[i]].x = ap_getObj("a" + id).offsetLeft + ap_getObj("td_" + index_menu[i]).offsetLeft;
				menu[index_menu[i]].y = ap_getObj("a" + id).offsetTop + ap_getObj("td_" + index_menu[i]).offsetTop;
			} else {
				menu[index_menu[i]].x = menu_x + ap_getObj("place_menu").offsetLeft + ap_getObj("td_" + index_menu[i]).offsetLeft;
				if (!menu_is_en && i == temp_menu.length-1) {
					// заплаточка
					if (getBrowserName()=="opera") menu[index_menu[i]].x-=20;
					else if (getBrowserName()=="ie") menu[index_menu[i]].x-=17;
					else menu[index_menu[i]].x-=37;
				}
				menu[index_menu[i]].y = menu_y + ap_getObj("td_" + index_menu[i]).offsetTop;
			}
			menu[index_menu[i]].h = ap_getObj("td_" + index_menu[i]).offsetHeight-3;
			menu[index_menu[i]].w = ap_getObj("td_" + index_menu[i]).offsetWidth;
		}
		menu_gen[(menu_gen.length?menu_gen.length:0)] = id;
	}
}

function clear_all() {
	menu_show = new Array();
	menu_gen = new Array();
	menu_id = "";
	menu_status = 0;
	ap_getObj('place_menu2').innerHTML = "";
	ap_getObj('place_menu').innerHTML = "";
	write_menu_l1(menu_x, menu_y);
}



// old open image func's
function photo_win(img_path,img_alt) {
	var str, StrBody;

	str = 'resizable=yes,width=100,height=100,screenX=100,screenY=100,left=30,top=30';
	
	if (document.all){ 
		StrBody = '<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="White" onblur="self.window.close();" onresize="self.window.location.reload();">';
	} else {
		StrBody = '<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="White" onblur="self.window.close();">';
	}
		
	var win_op = window.open('','show', str);
	win_op.document.open();
	win_op.document.writeln('<html>');
	win_op.document.writeln('<head>');
	win_op.document.writeln('<title>' + img_alt + '</title>');
	win_op.document.writeln('<script>');
	win_op.document.writeln('function resize_img() {');
	win_op.document.writeln('var width = document.image.width;');
	win_op.document.writeln('var height = document.image.height;');
	win_op.document.writeln('if (!(document.all)){ self.resizeTo(width,height); } else { self.resizeTo((width+12),(height + 31));');
	win_op.document.writeln(' }');
	win_op.document.writeln('}');
	win_op.document.writeln('</script>');	
	win_op.document.writeln('</head>');
	win_op.document.writeln(StrBody);
	win_op.document.writeln('<img name="image" src='+ img_path + ' border=0 alt="'+img_alt+'" onload="resize_img();">');
	win_op.document.writeln('</body>');
	win_op.document.writeln('</html>');		
	win_op.document.close();
}

function photo_win2(img_path,img_alt, w, h) {
	var str, StrBody;

	str = 'resizable=yes,width='+(w+20)+',height='+(h+20)+',screenX=100,screenY=100,left=30,top=30';
	
	if (document.all){ 
		StrBody = '<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="White" onblur="self.window.close();" onresize="self.window.location.reload();">';
	} else {
		StrBody = '<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="White" onblur="self.window.close();">';
	}

	var win_op = window.open('','show', str);
	win_op.document.open();
	win_op.document.writeln('<html>');
	win_op.document.writeln('<head>');
	win_op.document.writeln('<title>' + img_alt + '</title>');
	win_op.document.writeln('<script>');
	win_op.document.writeln('function resize_img() {');
	win_op.document.writeln('var width = document.image.width;');
	win_op.document.writeln('var height = document.image.height;');
	win_op.document.writeln('if (!(document.all)){ self.resizeTo(width,height); } else { self.resizeTo((width+12),(height + 31));');
	win_op.document.writeln(' }');
	win_op.document.writeln('}');
	win_op.document.writeln('</script>');	
	win_op.document.writeln('</head>');
	win_op.document.writeln(StrBody);
	win_op.document.writeln('<img name="image" src='+ img_path + ' border=0 alt="'+img_alt+'" onload="resize_img();">');
	win_op.document.writeln('</body>');
	win_op.document.writeln('</html>');		
	win_op.document.close();
}

function open_win(x) {
	// открытие картинки в новом окне
	var strBody="<body onLoad='win_load()' style='padding:0px; margin:0px;'>";
	var strWin="width=100, height=100, top=100, left=100, scrollbars=0, resizable=yes";
	var win_op=window.open('', 'show', strWin);
	win_op.document.open();
	win_op.document.write('<html>');
	win_op.document.write('	<head>');
	win_op.document.write('	<title>Untitled</title>');
	win_op.document.write('	<script language="JavaScript">');
	win_op.document.write('	function win_load()');
	win_op.document.write('	{');
	win_op.document.write('	var imgH;');
	win_op.document.write('	var imgW;');
	//win_op.document.write('	var open_pic="'+x+'"; ');
	win_op.document.write('	var agent = navigator.userAgent;');
	win_op.document.write('	browserName = navigator.appName;');
	win_op.document.write('	browserVer = parseInt(navigator.appVersion);');
	win_op.document.write('	var broser;');
	win_op.document.write('	if (window.opera) { broser=3;}');
	win_op.document.write('	else if (browserName == "Microsoft Internet Explorer") { broser=1;}');
	win_op.document.write('	else if ((browserVer >3) &&(browserName == "Netscape")) { broser=2;}');
	win_op.document.write('	imgH=document.getElementById("big_pic").offsetHeight;');
	win_op.document.write('	imgW=document.getElementById("big_pic").offsetWidth;');
	win_op.document.write('	if (broser==1) {	self.resizeTo(imgW+9,imgH+30);}');
	win_op.document.write('	else if (broser==2) {	self.resizeTo(imgW+9,imgH+51);}');
	win_op.document.write('	else if (broser==3) {	self.resizeTo(imgW+10,imgH+44);}');
	//win_op.document.write('	document.getElementById("big_pic").src=open_pic;');
	win_op.document.write('	}');
	win_op.document.write('	</script>');
	win_op.document.write('	</head>');
	win_op.document.write('	<body onLoad="win_load();" style="padding:0px; margin:0px;">');
	win_op.document.write('	<img src="'+x+'" id="big_pic" onClick="window.close();">');
	win_op.document.write('	</body>');
	win_op.document.write('	</html>');
	win_op.document.close();
}
