var fade_changes = navigator.appName == 'Microsoft Internet Explorer' ? 30 : 10,					
fadein_interval,					
fadeout_interval;

if (document.uniqueID && document.compatMode && !window.XMLHttpRequest && document.execCommand){
	document.execCommand("BackgroundImageCache", false, true);
}

function showMenu(parent){
	var els = parent.getElementsByTagName('div');
	for(var i=0;i<els.length;i++)if(els[i].className=='submenu')
		var submenu = els[i];
	if(!submenu)return false;
	addMenu(submenu,'plus');
}

function removeAllShadows(){
	return false;
	var els = document.getElementsByTagName('div');
	for(var i=0;i<els.length;i++)
		if(hasClass(els[i],'item'))
			removeClass(els[i],'shadow');
}

function hideMenu(parent){
	var els = parent.getElementsByTagName('div');
	for(var i=0;i<els.length;i++)if(els[i].className=='submenu')
		var submenu = els[i];
	if(!submenu)return false;
	addMenu(submenu,'minus');
}

function addShadow(el){
	return false;
	if(hasClass(el,'item')){
		if(hasClass(el,'first')){
			addClass(el,'shadow2');
		}
		else if(hasClass(el,'last')){
			addClass(el.childNodes[0],'shadow3');
		}	
		else{
			addClass(el,'shadow2');
			addClass(el.getElementsByTagName('a')[0],'shadow3');
		}
	}
	else if(hasClass(el,'main')){
		addClass(el.parentNode,'shadow2');
		if(hasClass(el.parentNode,'last')){
			addClass(el,'shadow3');
		}	
		else{
			addClass(el.parentNode,'shadow2');
			addClass(el,'shadow3');
		}
	}
}

function removeShadow(el){
	if(hasClass(el,'item')){
		removeClass(el,'shadow2');
		removeClass(el.getElementsByTagName('a')[0],'shadow3');
	}
	else{
		removeClass(el.parentNode,'shadow2');
		removeClass(el,'shadow3');
	}
}

function bgImage(el){
	if(hasClass(el,'selected'))return false;
	removeShadow(el);
	if(hasClass(el,'main')){
		el.style.backgroundImage = "none";
	}
	else{
		el.style.backgroundImage = "url('/images/menu/top/item.jpg')";
	}
	el.style.color = '#525252';
}

function bgImageHover(el){
	if(hasClass(el,'selected'))return false;
	addShadow(el);
	el.style.backgroundImage = "url('/images/menu/top/item_hover.jpg')";
	el.style.color = '#FFFFFF';
}

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 addMenu(submenu,action){
	//Устанавливаем позицию подменю
	var parent_coord = absPosition(submenu.parentNode);
	var set = parent_coord.x - (navigator.userAgent.indexOf('Firefox')<0 ? Math.round((f_clientWidth()-1008)/2) : 0) + 'px';
	//submenu.style.left = set;
	//submenu.style.top = parent_coord.y + 28 + 'px';
	submenu.style.left = 0 + 'px';
	submenu.style.top = 28 + 'px';
	//ID подменю
	submenu_id = submenu.getAttribute('id');
	if(!submenu_id){
		submenu_id = 'submenu' + menu_id;
		menu_id++;
		submenu.setAttribute('id',submenu_id);
	}
	var add = {submenu:submenu,action:action,submenu_id:submenu_id}
	var found = false;
	for(var i=0;i<menues.length;i++)if(menues[i].submenu_id==submenu_id){
		found = true;
		menues[i].action = action;
	}
	if(!found)
		menues[menues.length] = add;
}

var menues = new Array,menu_id = 1;
var menuInterval = setInterval(function(){
	for(var i=0;i<menues.length;i++){
		if(!menues[i].percent)menues[i].percent = 0;
		switch(menues[i].action){
			case 'plus':
				menues[i].percent += 5;
				addShadow(menues[i].submenu.parentNode);
				addClass(menues[i].submenu.parentNode,'selected');
			break;
			case 'minus':
				menues[i].percent -= 5;
				removeShadow(menues[i].submenu.parentNode);
				removeClass(menues[i].submenu.parentNode,'selected');
			break;
		}
		if(menues[i].percent<0){
			menues[i].percent = 0;
			continue;
		}
		else if(menues[i].percent>100){
			menues[i].percent = 100;
			continue;
		}
		listOpen(menues[i].submenu,menues[i].percent);
	}
},25);

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 listOpen(list,percent){
	//Получаем внутренний элемент
	var els = list.getElementsByTagName('div');
	for(var i=0;i<els.length;i++)if(els[i].className=='inner')
		var inner = els[i];
	if(!inner)return false;
	//Обрабатываем входящие данные
	if(percent>100)
		percent = 100;
	else if(percent<0)
		percent = 0;
	percent = percent / 100;
	//Скрываем/показываем элемент
	list.style.display = 'block';
	if(percent>0){
		if(list.style.display=='none')
			list.style.display = 'block';
	}
	else{
		if(list.style.display!='none')
			list.style.display = 'none';
	}
	//Сохраняем исходную высоту элемента
	list_preheight = parseInt(list.getAttribute('preheight'));
	if(!list_preheight){
		list.setAttribute('preheight',list.offsetHeight);
		list_preheight = list.offsetHeight;
	}
	//Просчитываем margin-top и height
	var list_height = Math.round(list_preheight * percent);
	var inner_marginTop = list_preheight - list_height;
	//Устанавливаем margin-top и height
	list.style.height = list_height + 'px';
	inner.style.marginTop = '-' + inner_marginTop + 'px';
}


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(el,period,callback){
	if(fadein_interval||fadeout_interval)return false;
	var interval_length = fade_changes,
	opacity_change = Math.round( 100 / ( period / fade_changes) );
	fadeout_interval = setInterval(function(){
		setOpacity(el,'-'+opacity_change);
		if(getOpacity(el)==0){
			clearInterval(fadeout_interval);
			fadeout_interval = false;
			if(callback)callback();
		}
	},interval_length);
}

function fadeIn(el,period,callback){
	if(fadein_interval||fadeout_interval)return false;
	var interval_length = fade_changes,
	opacity_change = Math.round( 100 / ( period / fade_changes) );
	fadein_interval = setInterval(function(){
		setOpacity(el,'+'+opacity_change);
		if(getOpacity(el)==100){
			clearInterval(fadein_interval);
			fadein_interval = false;
			if(callback)callback();
		}
	},interval_length);
}

function showBanner(){
	var popup = document.getElementById('popup');
	var els = popup.getElementsByTagName('div');
	var content = document.getElementById('content');
	var coord = absPosition(content);
	for(var i=0;i<els.length;i++)if(els[i].className=='body')var popup_body = els[i];
	popup.style.left = coord.x - (navigator.userAgent.indexOf('Firefox')<0 ? Math.round((f_clientWidth()-1008)/2) : 0) + 'px';
	popup.style.top = coord.y + 'px';
	setOpacity(popup,'0');
	popup.style.display = 'block';
	setOpacity(popup_body,'0');
	fadeIn(popup,250,function(){
		fadeIn(popup_body,250);
	});
}

function hideBanner(){
	var popup = document.getElementById('popup');
	var els = popup.getElementsByTagName('div');
	for(var i=0;i<els.length;i++)if(els[i].className=='body')var popup_body = els[i];
	fadeOut(popup,500,function(){
		popup.style.display = 'none';
	});
}

function leaf2Pos(){
	var leaf2 = document.getElementById('leaf2');
	if(!leaf2)return false;
	var coord = absPosition(document.getElementById('leaf2_part'));
	leaf2.style.left = coord.x - 19 + 'px';
	leaf2.style.top = coord.y - 388 + 'px';
	leaf2.style.width = f_clientWidth() - (coord.x - 19) + 'px';
}

window.onload = function() {
	leaf2Pos();
	procH1();
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

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;
}

// Плавное проявление новинок -----------------------------------------------------------------

novinkiFadeInStep = 5; // кратное 100
novinkiFadeInIndex = -novinkiFadeInStep;
function novinkiFadeIn() {
	if (novinkiFadeInIndex < 0) {
		var novDiv = document.getElementById('novinki');
		novinkiOverlay = document.createElement('DIV');
		novinkiOverlay.style.position = 'absolute';
		novinkiOverlay.style.height = novDiv.offsetHeight+'px';
		novinkiOverlay.style.width = novDiv.offsetWidth+'px';
		novinkiOverlay.style.background = '#FFF';
		novDiv.parentNode.insertBefore(novinkiOverlay, novDiv);
		novDiv.style.visibility = 'visible';
		intervalNovinkiFadeIn = setInterval(novinkiFadeIn, 100);
		novinkiFadeInIndex += novinkiFadeInStep;
	}
	novinkiOverlay.style.filter = 'alpha(opacity='+(100 - novinkiFadeInIndex)+')';
	novinkiOverlay.style.opacity = 1 - (novinkiFadeInIndex / 100);
	if (novinkiFadeInIndex == 100) {
		clearInterval(intervalNovinkiFadeIn);
		novinkiOverlay.parentNode.removeChild(novinkiOverlay);
	}
	else
		novinkiFadeInIndex += novinkiFadeInStep;
}

// обработка заголовков H1 -------------------------------------------------------------------

function procH1() {
	if (document.location.pathname.indexOf('katalog-phyts') == -1) {
		var h1arr = new Array();
		var hArr = new Array();
		h1arr = document.getElementsByTagName('H1');
		for (var i in h1arr)
			hArr[i] = h1arr[i];
		var hOverlay;
		for (var i in hArr) {
			if (hArr[i].nodeName == 'H1') {
				hOverlay = hArr[i].cloneNode(true);
				if (hArr[i].className == '') {
					hArr[i].style.color = '#B7D79F';
					hOverlay.style.color = '#659641';
				}
				else if (hArr[i].className == 'lilac') {
					hArr[i].style.color = '#D1AFD1';
					hOverlay.style.color = '#9F5B9F';
				}
				else 
					continue;
				hOverlay.style.marginTop = -(hArr[i].offsetHeight + 1)+'px';
				hOverlay.style.marginRight = '1px';
				hOverlay.style.marginLeft = '-1px';
				hArr[i].appendChild(hOverlay);
			}
		}
	}
	if (document.location.pathname.indexOf('professionalnye-programmy') !== -1 
	|| document.location.pathname.indexOf('institut-krasoty/protsedury') !== -1) {
		var h2arr = new Array();
		var hArr = new Array();
		h2arr = document.getElementsByTagName('H2');
		for (var i in h2arr)
			hArr[i] = h2arr[i];
		var hOverlay;
		for (var i in hArr) {
			if (hArr[i].nodeName == 'H2') {
				hOverlay = hArr[i].cloneNode(true);
				if (hArr[i].className == '' && hArr[i].style.color == '') {
					hArr[i].style.color = '#9F5B9F';
					hOverlay.style.color = '#D1AFD1';
				}
				else if (hArr[i].className == 'green') {
					hArr[i].style.color = '#659641';
					hOverlay.style.color = '#B7D79F';
				}
				else 
					continue;
				hOverlay.style.marginTop = -(hArr[i].offsetHeight + 1)+'px';
				hOverlay.style.marginRight = '1px';
				hOverlay.style.marginLeft = '-1px';
				hArr[i].appendChild(hOverlay);
			}
		}
	}
}
