var dom = (document.getElementById)? true : false;
var ie = (dom && document.all && !window.opera) ? true : false;
var ie4 = (document.all && !dom)? true : false;
var nn6 = (!ie && dom) ? true : false
var nn4 = (document.layers)? true : false 



function popupWin(name, uri, wdt, hgt) {
	var posCode = ''
	if (nn4 || dom) {
		if ( (screen.height < 481) && (hgt > 400) ) { hgt = 400 }
		posX = Math.round((screen.width - wdt) / 2)
		posY = Math.round((screen.height - hgt) / 2 - 60)
//		posCode = (nn4 || nn6) ? ",screenX="+posX+",screenY="+posY : ",left="+posX+",top="+posY
		posCode = (ie) ? ",left="+posX+",top="+posY : ",screenX="+posX+",screenY="+posY
	}
//	alert(ie + '-' + screen.width + ' ' + screen.height + ' ' + posCode)
	popupedWin = window.open(uri, name, "status=yes,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,location=no,width="+wdt+",height="+hgt+posCode)
	popupedWin.focus()
}

function putElem(elemId) {
	if (dom) document.getElementById(elemId).style.display = "block";
	else if (ie4) document.all[elemId].style.display = "block";
}

function putElemTr(elemId) {
	if (ie) {
		if (dom) document.getElementById(elemId).style.display = "block";
		else if (ie4) document.all[elemId].style.display = "block";
	} else {
		if (dom) document.getElementById(elemId).style.display = "table-row";
	}
}

function putStep(stepId) {
	for (var i=1; i<=3; i++) {
		if (dom) document.getElementById('step'+i).style.display = "none";
		else if (ie4) document.all['step'+i].style.display = "none";
	}
	if (ie) {
		if (dom) document.getElementById('step'+stepId).style.display = "block";
		else if (ie4) document.all['step'+stepId].style.display = "block";
	} else {
		if (dom) document.getElementById('step'+stepId).style.display = "table-row";
	}

}

function putElemIn(elemId) {
	if (dom) document.getElementById(elemId).style.display = "inline";
	else if (ie4) document.all[elemId].style.display = "inline";
}

function removeElem(elemId) {
  if (dom) document.getElementById(elemId).style.display = "none";
  else if (ie4) document.all[elemId].style.display = "none";
}


function changeElem(elemId) {
	
	if (document.getElementById(elemId).style.display == "none") {
		putElem(elemId);
		}
	else	{
		removeElem(elemId);
		}
}

function show_hide ( id , focusfield) {
	
	var hdn_div = document.all['bm_div_'+id];
	var xpand_img = document.all['bm_shimg_'+id];
	var tit_div = document.all['bm_div_title_'+id];
	var ff = document.all[focusfield];

	if( hdn_div.style.display == 'block' ) {
		hdn_div.style.display = 'none';
		xpand_img.src = '/pictures/items/sh0.gif';
		tit_div.className = "bm_div_title_hidden";
	} else {
		hdn_div.style.display = 'block';
//		ff.value = 'csdcd';
		if(ff) {ff.focus();}
		xpand_img.src = '/pictures/items/sh1.gif';
		tit_div.className = "bm_div_title";
	}
}

function show ( id ) {
	
	var hdn_div = document.all['bm_div_'+id];
	var xpand_img = document.all['bm_shimg_'+id];
	var tit_div = document.all['bm_div_title_'+id];

	hdn_div.style.display = 'block';
	xpand_img.src = '/pictures/items/sh1.gif';
	tit_div.className = "bm_div_title";
}

function hover ( id ) {
	var tit_div = document.getElementById('bm_div_title_'+id);
	var matches = tit_div.className.match("bm_div_title((_hidden)?)((_active)?)");
	tit_div.className = "bm_div_title"+matches[1]+"_active";

}

function dishover ( id ) {
	var tit_div = document.getElementById('bm_div_title_'+id);
	var hdn_div = document.getElementById('bm_div_'+id);
	var matches = tit_div.className.match("bm_div_title((_hidden)?)((_active)?)");
	tit_div.className = "bm_div_title"+matches[1];
}


function hover ( id ) {
	var tit_div = document.getElementById('bm_div_title_'+id);
	var matches = tit_div.className.match("bm_div_title((_hidden)?)((_active)?)");
	tit_div.className = "bm_div_title"+matches[1]+"_active";
}

function sF()
{

	if (document.forms.workform.elements.action) 
	{
	document.forms.workform.elements.action.value="Пожалуйста подождите...";
	document.forms.workform.elements.action.className="btn_cancel";
	document.forms.workform.elements.action.disabled="true";
	}
	if (document.forms.workform.elements.save) 
	{
	document.forms.workform.elements.save.value="Пожалуйста подождите...";
	document.forms.workform.elements.save.className="btn_cancel";
	document.forms.workform.elements.save.disabled="true";
	}
}

function overlay2(){


	var info = overlay_width + ", " + field_width + ", " + getClientWidth() + ", " + getDocumentWidth()+ ", " + Math.ceil((overlay_width-field_width)/2);
	document.getElementById('info').innerHTML=info;
}

function countFieldWidth(overlay_width, bg_width) {
	if (overlay_width < bg_width) {
		var field_width = overlay_width;
	} else {
		var field_width = bg_width;
	}
	return field_width;
}

function overlay() {
	var block_width = 200;
	var block_height = 250;
	var columns = getColumnsMax();
	var columns_max = columns;
	var overlay_width = getClientWidth();
	var min_overlay_width = 900; //меньше 900 не даём вообще странице сжиматься
	if (overlay_width < min_overlay_width) {
		overlay_width = min_overlay_width;
	}
	
	var bg_width = 1662;
	var bg_height = 872;
	var line_height = 350;
	var tg = (bg_height-line_height)/bg_width;
	
	var field_width = countFieldWidth(overlay_width,bg_width);
	if (field_width < bg_width) {
		var field_height = Math.ceil(line_height+field_width*tg);
	} else {
		var field_height = bg_height;
	}
	
	document.getElementById('works_overlay').style.width=overlay_width;
	document.getElementById('works_field').style.height=field_height;
	
	if (getDocumentWidth() > getClientWidth()) {
		if (getClientWidth() > min_overlay_width) {
			overlay_width = getClientWidth();
		} else {
			overlay_width = min_overlay_width;
		}
		field_width = countFieldWidth(overlay_width,bg_width);	
		document.getElementById('works_overlay').style.width=overlay_width;
		document.getElementById('works_field').style.height=field_height;
	}
	if (field_width < bg_width) {
		var position_correct = Math.ceil((bg_height-line_height)/2*(field_width-bg_width)/bg_width);
	} else {
		var position_correct = 0;
	}

	
	document.getElementById('works_field').style.left=Math.ceil((overlay_width-field_width)/2);
	document.getElementById('works_field').style.width=field_width;
	document.getElementById('works_field').style.backgroundPosition="center "+position_correct+"";
	
	for(var block_id = 1; block_id < columns_max+1; block_id++) {
		document.getElementById('block_'+block_id).style.visibility="hidden";
	}
	var i = 1;
	while (Math.ceil(field_width/i) > block_width && i <= columns_max) { 
		columns = i;
		i++;	
	}	
	var block_column_width = Math.ceil((field_width-16)/columns);
	for(var block_id = Math.ceil(columns_max-columns+1); block_id < columns_max+1; block_id++) {
		var pos_left = Math.ceil((block_id-1-(columns_max-columns))*block_column_width+block_column_width/2-block_width/2);
		document.getElementById('block_'+block_id).style.position="absolute";
		document.getElementById('block_'+block_id).style.rightMargin=0;
		document.getElementById('block_'+block_id).style.left=pos_left;
		document.getElementById('block_'+block_id).style.top=Math.ceil(-4+(line_height-block_height-block_width*tg)/2+(field_width-pos_left)*tg);
		document.getElementById('block_'+block_id).style.width=block_width;
		document.getElementById('block_'+block_id).style.height=block_height;
		document.getElementById('block_'+block_id).style.visibility="visible";
	}
	document.getElementById('bl_news_main').style.top=55;
	document.getElementById('bl_routine_main').style.top="auto";
	document.getElementById('bl_routine_main').style.bottom=-50;
	

	document.getElementById('last_new').style.width="100%";
	document.getElementById('pre_last_new').style.width="100%";
	if (overlay_width <= min_overlay_width) {
		document.getElementById('last_new').style.width="70%";
		document.getElementById('pre_last_new').style.width="85%";
	} else if (overlay_width <= min_overlay_width+100) {
		document.getElementById('last_new').style.width="70%";
	} else if (overlay_width <= min_overlay_width+200) {
		document.getElementById('last_new').style.width="80%";
	}

}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getDocumentWidth()
{
	return (document.body.scrollWidth > document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth;
}


function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}


function makeNoizz()
{
	var elem = document.getElementById('body');
	if (elem.className.match(/main_page_noizzed/)) {
		var new_classes = elem.className.replace(/main_page_noizzed/, ' ');
		document.getElementById('body').className = new_classes;
	} else {
		document.getElementById('body').className += " main_page_noizzed";
	}
	
}


    
