$(document).ready(function() {
	var htmlObj=$("html");

	var htmlBody=$("body");
	htmlBody.filter(".repositionPopups").resize(function() {repositionPopups();});

	var bez_otveta=$("#bez_otveta");
	if(bez_otveta.is(":checked")) {$('#smail,#semail,#of_otvet,#zv').hide();}
	bez_otveta.click(function() {$('#smail,#semail,#of_otvet,#zv').toggle();});

	$("#assist,#adopt,#norm").click(function(event) {
		event.preventDefault();
		var type_theme=$(this).attr('id');
		setCookie('theme',type_theme,14);

		if(!htmlObj.hasClass(type_theme)){
			htmlObj.removeClass();
			if(type_theme!="norm")
				htmlObj.addClass(type_theme);
		}
	});

	$('.col_left .m-left_m #left_menu .menu__item:last, .menu_horizontal .menu__item:last').addClass("menu__item_last");

	var assist_font_size_selector=$(".assist_font_size_selector");
	assist_font_size_selector.css("font-size",assist_font_size_selector.css("font-size"));
	var assistFontSize=getCookie("assistFontSize");
	switch (parseInt(assistFontSize)) {
		case 2:
			$("body").addClass("assist_font_size_medium");
			assist_font_size_selector.children(".assist_font_size_medium").addClass("active");
			setCookie("assistFontSize",2,14);
			break;
		case 3:
			$("body").addClass("assist_font_size_large");
			assist_font_size_selector.children(".assist_font_size_large").addClass("active");
			setCookie("assistFontSize",3,14);
			break;
		default:
			assist_font_size_selector.children(".assist_font_size_norm").addClass("active");
			$("body").addClass("assist_font_size_norm");
			setCookie("assistFontSize",1,14);
	}
	assist_font_size_selector.find("a").click(function(e) {
		e.preventDefault();
		var parentLi=$(this).parent();
		parentLi.parent().children().removeClass("active");
		$("body").removeClass("assist_font_size_norm").removeClass("assist_font_size_medium").removeClass("assist_font_size_large").addClass(parentLi.attr("class"));
		switch (parentLi.attr("class")) {
			case "assist_font_size_medium":
				setCookie("assistFontSize",2,14);
				break;
			case "assist_font_size_large":
				setCookie("assistFontSize",3,14);
				break;
			default:
				setCookie("assistFontSize",1,14);
		}
		parentLi.addClass("active");
	});
	$("div.menu__item a#norm").click(function() {
		$("body").removeClass("assist_font_size_norm").removeClass("assist_font_size_medium").removeClass("assist_font_size_large");
		setCookie("assistFontSize",1,-1);
		assist_font_size_selector.find("li").removeClass("active").filter(":first").addClass("active");
	});

	if(document.location.href.indexOf("sud_delo")>-1) {
		hideRightBlockOnSmallViewport();
		$(window).resize(function() {
			hideRightBlockOnSmallViewport();
		});
	}

	$(".mapTreeRevealAll").click(function(e) {
		e.preventDefault();
		var contentBox=$(this).parentsUntil(".box").parent();
		contentBox.find("table.mapSubtree").show();
		contentBox.find("img.mapSubtreeToggle").attr("src",'images/minus.gif');

	});
	$(".mapTreeCollapseAll").click(function(e) {
		e.preventDefault();
		var contentBox=$(this).parentsUntil(".box").parent();
		contentBox.find("table.mapSubtree").hide();
		contentBox.find("img.mapSubtreeToggle").attr("src",'images/plus.gif');
	});

	$(".mapSubtreeToggle").click(function(e) {

		e.preventDefault();
		var parentTr=$(this).parentsUntil("tr").parent();
		var icon=parentTr.find("> td:first img.mapSubtreeToggle");
		var childTable=parentTr.find("> td > table.mapSubtree");
        if(childTable.filter(":visible").length>0) {

			childTable.hide();
			icon.attr("src",'images/plus.gif');
		}
		else {
         
			childTable.show();
			icon.attr("src",'images/minus.gif');
		}
	});

});

function detectIE6() {
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer"){
		var b_version = navigator.appVersion;
		var re = /MSIE\s+(\d\.\d\b)/;
		var res = b_version.match(re);
		if(res[1]<=6){
			return true;
		}
	}
	return false;
}

function getClientWidth()
{
  //return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
	var a = document.documentElement.clientWidth?document.documentElement.clientWidth:document.body.clientWidth;
	return parseInt(a);
}

function getClientHeight()
{
  //return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	var a = document.documentElement.clientHeight?document.documentElement.clientHeight:document.body.clientHeight;
	return parseInt(a);
}

function getBodyScrollTop()
{
  var a = self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
  return parseInt(a);
}

function getBodyScrollLeft()
{
  var a = self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
  return parseInt(a);
}

function showTooltip(event,insText) {
	var ttObj = document.getElementById('divTooltip');
	if(ttObj) {
		if(arguments[2]) var w=arguments[2]; else w=250;
		if(arguments[3]) var h=arguments[3];
		if(typeof w == 'number') ttObj.style.width=w+'px';
		if(typeof h == 'number') ttObj.style.height=h+'px';
		ttObj.style.overflow='hidden';
		ttObj.innerHTML=insText;
		ttObj.style.left='-1000px';
		ttObj.style.top='-1000px';
		ttObj.style.display='block';
		repositionElement(event,ttObj);
	}
}

function hideTooltip() {
	var ttObj = document.getElementById('divTooltip');
	if(ttObj) {
		ttObj.style.display='none';
		ttObj.innerHTML='';
	}
}

function moveTooltip(event) {
	var ttObj = document.getElementById('divTooltip');
	repositionElement(event,ttObj);
	
}

function showPopup() {
	var popupDivB=document.getElementById('divFSPopupBottom');
	var popupDivT=document.getElementById('divFSPopupTop');
	if(popupDivB && popupDivT) {
		popupDivB.style.display='block';
		popupDivT.style.display='block';
		repositionPopups();
	}
}

function hidePopupDiv() {
	var popupDivB=document.getElementById('divFSPopupBottom');
	var popupDivT=document.getElementById('divFSPopupTop');
	if(popupDivB && popupDivT) {
		popupDivB.style.display='none';
		popupDivT.style.display='none';
		popupDivT.innerHTML='';
	}
	var sdpPopup=document.getElementById('divSDPSelect');
	if(sdpPopup) {
		sdpPopup.style.display='none';
	}
}

function repositionPopups() {
	var popupDivB=document.getElementById('divFSPopupBottom');
	var popupDivT=document.getElementById('divFSPopupTop');
	var browserW=getClientWidth();
	var browserH=getClientHeight();
	if(popupDivB && popupDivB.style.display=='block') {
		popupDivB.style.width=browserW+'px';
		popupDivB.style.height=browserH+'px';
	}
	if(popupDivT && popupDivT.style.display=='block') {
		if(popupDivT.offsetWidth+40<browserW) popupDivT.style.left=parseInt((browserW-popupDivT.offsetWidth)/2)+20+getBodyScrollLeft()+'px';
		else popupDivT.style.left=20+getBodyScrollLeft()+'px';
		if(popupDivT.offsetHeight+40<browserH) popupDivT.style.top=parseInt((browserH-popupDivT.offsetHeight)/2)+20+getBodyScrollTop()+'px';
		else popupDivT.style.top=20+getBodyScrollTop()+'px';
	}
	var sdpPopup=document.getElementById('divSDPSelect');
	if(sdpPopup && sdpPopup.style.display=='block') {
		if(sdpPopup.offsetWidth+40<browserW) sdpPopup.style.left=parseInt((browserW-sdpPopup.offsetWidth)/2)+20+getBodyScrollLeft()+'px';
		else sdpPopup.style.left=20+getBodyScrollLeft()+'px';
		if(sdpPopup.offsetHeight+40<browserH) sdpPopup.style.top=parseInt((browserH-sdpPopup.offsetHeight)/2)+20+getBodyScrollTop()+'px';
		else sdpPopup.style.top=20+getBodyScrollTop()+'px';
	}
}

function resizePopup() {
	var popupDivB=document.getElementById('divFSPopupBottom');
	var popupDivT=document.getElementById('divFSPopupTop');
	if(popupDivB && popupDivT) {
		popupDivB.style.width=getClientWidth()+'px';
		popupDivB.style.height=getClientHeight()+'px';
	}
}

function trSelected(trObj) {
	if(trObj.hasChildNodes()) {
		for(i=0;i<trObj.childNodes.length;i++) {
			if(trObj.childNodes[i].nodeType==1) {
				trObj.childNodes[i].style.backgroundColor='#fdf5e9';
			}
		}
	}
}

function trUnselected(trObj) {
	if(trObj.hasChildNodes()) {
		for(i=0;i<trObj.childNodes.length;i++) {
			if(trObj.childNodes[i].nodeType==1) {
				trObj.childNodes[i].style.backgroundColor='#ffffff';
			}
		}
	}
}

function appendNewFileInput(tblId) {
	if(document.getElementById(tblId) && document.getElementById('img_count')) {
		var tblObj=document.getElementById(tblId);
		var newTr=document.createElement('tr');
		var newTd=document.createElement('td');
		var newInput=document.createElement('input');
		newTr.setAttribute('id','imgFileInput'+(parseInt(document.getElementById('img_count').value)+1));
		newInput.setAttribute('type','file');
		newInput.setAttribute('size','90');
		newInput.setAttribute('id','prPhoto'+(parseInt(document.getElementById('img_count').value)+1));
		newInput.setAttribute('name','prPhoto'+(parseInt(document.getElementById('img_count').value)+1));
		newTd.appendChild(newInput);
		newTr.appendChild(newTd);
		if(tblObj.hasChildNodes()) {
			var tbodyEl=tblObj.firstChild;
			while(tbodyEl.nodeType!=1 && (tbodyEl.nodeName!='TBODY' || tbodyEl.nodeName!='tbody') && tbodyEl.nextSibling) {
				tbodyEl=tbodyEl.nextSibling;
			}
			if(tbodyEl.nodeType==1 && (tbodyEl.nodeName=='TBODY' || tbodyEl.nodeName=='tbody')) {
				tbodyEl.appendChild(newTr);
			}
			else {
				tblObj.appendChild(newTr);
			}
		}
		else {
			tblObj.appendChild(newTr);
		}
		document.getElementById('img_count').value=parseInt(document.getElementById('img_count').value)+1;
	}
}

function replace_inp(text){	return $.trim(text.replace(/[^à-ÿ¸0-9.,:;\n_ \-¹]+/gi, '')); }


var modal_div= new Array();

function modWin(type,creat_idDiv){
	var popID = '#popup'; //Get Popup Name
	if(type=='close') {
		$('#fade , '+popID).fadeOut(function() {
			$('#fade').remove();  //fade them both out
		});
		return;
	}
	var add_m=true;
	for(var ind in modal_div){
		if(modal_div[ind]==creat_idDiv){
			add_m=false;
		}
	}

	if(add_m) modal_div.push(creat_idDiv);
	/*
	 var index_in=modal_div.indexOf(creat_idDiv);
	 if(index_in<0){
	 modal_div.push(creat_idDiv);
	 }
	 */

	for(var index in modal_div){
		if(modal_div[index]==creat_idDiv)
			$('#poptxt #'+creat_idDiv).show();
		else
			$('#poptxt #'+modal_div[index]).hide();
	}

	//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
	var winH = $(window).height();
	var winW = $(window).width();

	//Fade in the Popup and add close button
	$(popID).css('max-width',parseInt(winW)-parseInt($(popID).css('padding-left'))-parseInt($(popID).css('padding-right'))-10+'px');
	$(popID).css('max-height',parseInt(winH)-parseInt($(popID).css('padding-top'))-parseInt($(popID).css('padding-bottom'))-10+'px');
	$(popID).css('top',  winH/2-$(popID).outerHeight()/2+'px');
	$(popID).css('left', winW/2-$(popID).outerWidth()/2+'px');
	$(popID).fadeIn();

	//Fade in Background
	$('<div id="fade"></div>').click(function(e) {e.stopPropagation();modWin('close');}).appendTo($('body'));
	$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies
}

function setCookie (name, value, days, path, domain, secure) {
	var exp_date=new Date();
	exp_date.setDate(exp_date.getDate() + days);
	document.cookie = name + "=" + escape(value) +"; expires=" + exp_date.toUTCString() +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

//del 

function addFileField() {
	var cont=$('#fileInputsCont');
	var filesCount=cont.children('input').length;
	if(filesCount>8) {
		$('#crfld').hide();
	}
	if(cont.length>0) {
		cont.append("<br><input type='file' id='attfile"+(filesCount+1)+"' name='attfile"+(filesCount+1)+"'>");
	}
}

function hideRightBlockOnSmallViewport() {
	var docWidth=parseInt($(document).width());
	var colRight=$(".col_right");
	if(colRight.length>0) {
		if(docWidth<1300) colRight.filter(":visible").hide();
		else colRight.filter(":hidden").show();
	}
}