DDcontainer = [];
DDmenu = [];
DDmargin = [];
DDposition = []; 
DDrunningDown = [];
DDrunningUp = []; 
DDinterval = [];
DDtimeout = [];

var fade_changes = navigator.appName == 'Microsoft Internet Explorer' ? 200 : 10,
fadein_interval,
fadeout_interval;

function prepareDropdown(i) {
	DDcontainer[i] = document.getElementById('dropdown_container_'+i);
	DDmenu[i] = document.getElementById('dropdown_'+i);
	DDmargin[i] = 0 - DDmenu[i].offsetHeight;
	DDmenu[i].style.top = DDmargin[i]+'px';
	DDstep = 10;
	DDposition[i] = 'up';
	DDrunningDown[i] = false;
	DDrunningUp[i] = false;
	DDtimeout[i] = null;
}

function showDropdown(i, fromCycle) {
	if (!DDstep)
		return false;
	if (DDtimeout[i] != null && !fromCycle) {
		clearTimeout(DDtimeout[i]);
		DDtimeout[i] = null;
	}
	if (DDposition[i] == 'down')
		return;
	if (DDrunningUp[i]) {
		DDrunningUp[i] = false;
		clearInterval(DDinterval[i]);
	}
	if (!DDrunningDown[i]) {
		DDrunningDown[i] = true;
		DDposition[i] = null;
		DDinterval[i] = setInterval('showDropdown('+i+', "true")', 50);
	}
	if (DDmargin[i] + DDstep > -1) {
		DDmenu[i].style.top = '-1px';
		DDcontainer[i].style.height = DDmenu[i].offsetHeight+'px';
		DDposition[i] = 'down';
		DDrunningDown[i] = false;
		clearInterval(DDinterval[i]);
	} else {
		DDmargin[i] += DDstep;
		DDmenu[i].style.top = DDmargin[i]+'px';
		DDcontainer[i].style.height = (DDmenu[i].offsetHeight + DDmargin[i])+'px';
	}
}


function hideDropdown(i) {
	if (DDposition[i] == 'up')
		return;
	if (DDtimeout[i] == null) {
		DDtimeout[i] = setTimeout('hideDropdown('+i+')', 300);
		return;
	}
	if (DDrunningDown[i]) {
		DDrunningDown[i] = false;
		clearInterval(DDinterval[i]);
	}
	if (!DDrunningUp[i]) {
		DDrunningUp[i] = true;
		DDposition[i] = null;
		DDinterval[i] = setInterval('hideDropdown('+i+')', 50);
	}
	if (DDmargin[i] - DDstep < 0 - DDmenu[i].offsetHeight) {
		DDmenu[i].style.top = (0 - DDmenu[i].offsetHeight)+'px';
		DDcontainer[i].style.height = '1px';
		DDposition[i] = 'up';
		DDrunningUp[i] = false;
		clearInterval(DDinterval[i]);
	} else {
		DDmargin[i] -= DDstep;
		DDmenu[i].style.top = DDmargin[i]+'px';
		DDcontainer[i].style.height = (DDmenu[i].offsetHeight + DDmargin[i])+'px';
	}
}





function addClass(el,c){
	if(!el.className){
		el.className = c;
		return false;
	}
	var cls = el.className.split(' ');
	var found = false;
	for(var i=0;i<cls.length;i++)if(cls[i]==c)found = true;
	if(!found)cls[cls.length] = c;
	el.className = cls.join(' ');
}

function removeClass(el,c){
	if(!el.className)return false;
	var cls = el.className.split(' ');
	var found = false;
	var new_cls = new Array;
	for(var i=0;i<cls.length;i++)if(cls[i]!=c)new_cls[new_cls.length] = cls[i];
	el.className = new_cls.join(' ');
}

function hasClass(el,c){
	if(!el.className)return false;
	var cls = el.className.split(' ');
	for(var i=0;i<cls.length;i++)
		if(cls[i]==c)return true;
	return false;
}

function imageHover(el){
	var img = el.getElementsByTagName('img');
	if(!img[0])return false;
	img = img[0];
	if(!/hover/.test(img.src))
		img.src = img.src.replace(/\.([a-zA-Z]{1,4})$/g,'_hover.$1');
}

function imageOut(el){
	var img = el.getElementsByTagName('img');
	if(!img[0])return false;
	img = img[0];
	if(/hover/.test(img.src))
		img.src = img.src.replace(/_hover\./g,'.');
}

function absPosition(obj) {
    var x = y = 0;
    while(obj) {
          x += isNaN(obj.offsetLeft) ? 0 : obj.offsetLeft;
          y += isNaN(obj.offsetTop) ? 0 : obj.offsetTop;
          obj = obj.offsetParent;
    }
    return {x:x, y:y};
}

function getStyle(x,styleProp)
{
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function catchEmphBlocks() {
	var elsArr = new Array();
	var els = new Array();
	elsArr = document.getElementsByTagName('DIV');
	for (var i in elsArr)
		els[i] = elsArr[i];
	for (var i in els) {
		if(els[i].className&&(els[i].className=='emph_block'||els[i].className=='emph_block_solid')) {
			var emphBlock = els[i];
			var overlayBlock = document.createElement('div');
			overlayBlock.className = 'overlay';
			overlayBlock.style.width = emphBlock.offsetWidth+'px';
			emphBlock.parentNode.insertBefore(overlayBlock,emphBlock);
			var output = document.getElementById("output");
			if (overlayBlock != null && emphBlock != null) {
				var emphBlockTop = absPosition(emphBlock).y - absPosition(output).y - ((isNaN(parseInt(getStyle(emphBlock,'padding-top')))?0:parseInt(getStyle(emphBlock,'padding-top'))) + (isNaN(parseInt(getStyle(emphBlock,'padding-bottom')))?0:parseInt(getStyle(emphBlock,'padding-bottom'))));
				overlayBlock.style.height = emphBlock.offsetHeight + 30+"px";
				emphBlock.style.background = 'transparent';
				if (emphBlock.className == 'emph_block_solid')
					 overlayBlock.style.background = '#9AA8D6';
				else {
					 var marginTop = - (emphBlockTop)-(navigator.appName=='Microsoft Internet Explorer'||navigator.appName=='Opera'?5:15)+"px";
					 overlayBlock.style.background = '#9AA8D6 url(/images/page/bg6.jpg) no-repeat 0 '+marginTop;
				}
			}
		}
	}
}

function submit_fly_form(el) {
	var text = [];
	var ok = true;
	if(el.message.value == ''){
		text[text.length] = 'Не введен текст заявки';
	}
	if(el.name.value == ''){
		text[text.length] = 'Не введено Ваше имя';
	}
	if(el.phone.value == '' && el.email.value == ''){
		text[text.length] = 'Должно быть заполнено хотя бы одно из полей - "E-mail" или "Телефон"';
	}
	if(el.captcha.value == ''){
		text[text.length] = 'Не введено число на картинке';
	}
	if(text.length>0){
		ok = false;
		var message = 'Ошибки в заполнении формы:\n';
		for(var i=0;i<text.length;i++)
			message += ' * ' + text[i] + '\n';
		alert(message);
	}
	return ok;
}

function submit_zapros_form(form) {
	if (form.name.value == '') { 
		alert ('Вы не заполнили имя контактного лица'); 
		return false; 
	}
	else if (form.mail.value == '' && form.tel.value == '') { 
		alert ('Вы не заполнили Ваши контакты - e-mail или телефон'); 
		return false; 
	}
	else if(form.captcha.value == '') { 
		alert ('Не введено число на картинке');
		return false;
	}
	return true;
}

function setOpacity(el,value){
	if(typeof(value=='string')&&value.match(/(\+|-)/)){
		var method = value.match(/(\+|-)/)[0];
		value = parseInt(value.match(/(\d+)/));
		switch(method){
			case '+':value = getOpacity(el) + value;break;
			case '-':value = getOpacity(el) - value;break;
		}
	}
	if(isNaN(value))value = 100;
	if(value<0)value = 0;
	if(value>100)value = 100;
	el.style.opacity = value/100;
	el.style.filter = 'alpha(opacity=' + value + ')';
}

function getOpacity(el){
	var value;
	if(el.style.filter){
		value = el.style.filter.match(/(\d+)/)[0];
	}
	else if(el.style.opacity){
		value = el.style.opacity;
		value = value * 100;
	}
	if(isNaN(value))value = 100;
	return parseInt(value);
}

function fadeOut(els,period,callback){
	if(!els.length)els = new Array(els);
	if(fadein_interval||fadeout_interval)return false;
	var interval_length = fade_changes,
	opacity_change = Math.round( 100 / ( period / fade_changes) );
	fadeout_interval = setInterval(function(){
		for(var i=0;i<els.length;i++)
			setOpacity(els[i],'-'+opacity_change);
		if(getOpacity(els[0])==0){
			clearInterval(fadeout_interval);
			fadeout_interval = false;
			for(var i=0;i<els.length;i++)
				els[i].style.display = 'none';
			if(callback)callback();
		}
	},interval_length);
}

function disappear(){
	clearInterval(backCount);
	fadeOut(
		new Array(
			document.getElementById('welcome_jet'),
			document.getElementById('disappear'),
			document.getElementById('buttons'),
			document.getElementById('timer')
		),
		1000,
		function(){
			document.getElementById('welcome_layout').style.display='none';
			document.getElementById('tPage').style.display = '';
		}
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

