var array = new Array();
var speed = 5;
var timer = 1;

function trigger(link,target){
	link.checked == true ? document.getElementById(target).style.display = 'block' : document.getElementById(target).style.display = 'none';
	return false;
}

function sortsearchresults(link){
	var needclass = link.parentNode.className;
	var current = document.getElementById('searchresults').firstChild;
	var currmenuitem = document.getElementById('searchresultsmenu').firstChild;
	while ( current != null ) {
		if ( current.tagName == 'LI' & needclass != 'show0' ){
			current.className.replace(' blind','') == needclass ? current.className = current.className.replace(' blind','') : current.className = current.className.replace(' blind','') + ' blind';
		} else if ( current.tagName == 'LI' & needclass == 'show0' ) {
			current.className = current.className.replace(' blind','');
		}
		current = current.nextSibling;
	}
	while ( currmenuitem != null ) {
		if ( currmenuitem.tagName == 'DIV' ){
			currmenuitem.className.replace(' selected','') != needclass ? currmenuitem.className = currmenuitem.className.replace(' selected','') : currmenuitem.className = currmenuitem.className.replace(' selected','') + ' selected';
		}
		currmenuitem = currmenuitem.nextSibling;
	}
	return false;
}

function showblock(link){
	link.className = '';
	if (link.id == 'one-header'){
		document.getElementById('one-slideblock').style.display = 'block';
		document.getElementById('two-header').className = 'servicelink';
		document.getElementById('two-slideblock').style.display = 'none';
	}
	if (link.id == 'two-header'){
		document.getElementById('two-slideblock').style.display = 'block';
		document.getElementById('one-header').className = 'servicelink';
		document.getElementById('one-slideblock').style.display = 'none';
	}
	return false;
}

function showhideanswer(link){
	if (link.parentNode.className == 'showanswer'){
		link.parentNode.className = 'hideanswer';
	} else if (link.parentNode.className == 'hideanswer'){
		link.parentNode.className = 'showanswer';
	}
	return false;
}

function showbn(link){
	var needclass = link.className;
	var current = link.parentNode.firstChild;
	while ( current != null ) {
		if ( current.tagName == 'A' ){ current.className == needclass ? current.style.display = 'none' : current.style.display = 'inline';
		} else if ( current.tagName == 'B' ) { current.className == needclass ? current.style.display = 'inline' : current.style.display = 'none';
		} else if ( current.tagName == 'I' ) { current.className == needclass ? current.style.display = 'inline' : current.style.display = 'none'; 
		}
		current = current.nextSibling;
	}
	return false;
}

function showcityselector(link){
	var cityname = link.parentNode.id.replace('city','select');
	var citywidth = current = link.parentNode;
	var cities = document.getElementById('cities');
	var takemeup = document.getElementById('takemeup');
	var top = 0;
	var topprev = 0;
	while ( current.tagName != 'BODY' ){
		if ( current.offsetTop != topprev ){
			topprev = current.offsetTop;
			top = top + current.offsetTop;
		}
		current = current.parentNode;
	}
	cities.style.width = citywidth.offsetWidth + 76 + 'px';
	cities.style.top = top - document.getElementById(cityname).offsetTop + 'px';
	document.getElementById(cityname).firstChild.className = 'nolink';
	document.getElementById(cityname).firstChild.style.fontWeight = 'bold';
	takemeup.style.width = citywidth.offsetWidth + 60 + 'px';
	top - document.getElementById(cityname).offsetTop < 0 ? takemeup.style.display = 'block' : takemeup.style.display = 'none';
	return false;
}

function shiftcityselector(link){
	document.getElementById('cities').style.top = '15px';
	link.style.display = 'none';
	return false;
}

function showcrumbs(link){
	var crumbs = document.getElementById(link.id.slice(0,8));
	var crumb = document.getElementById('select_' + link.id);
	var current = link.parentNode;
	var top = topprev = left = leftprev = 0;
	while ( current.tagName != 'BODY' ){
		if ( current.offsetTop != topprev ){
			topprev = current.offsetTop;
			top = top + current.offsetTop;
		}
		if ( current.offsetLeft != leftprev ){
			leftprev = current.offsetLeft;
			left = left + current.offsetLeft;
		}
		current = current.parentNode;
	}
	var current = crumb.parentNode.firstChild;
	var width = 0;
	while ( current != null ) {
		if ( current.tagName == 'A' ) {
			current.style.display = 'inline';
			if ( current.offsetWidth > width ) {
				width = current.offsetWidth;
			}
			current.style.display = 'block';
		}
		current = current.nextSibling;
	}
	crumbs.style.width = width + 66 + 'px';
	crumbs.style.top = top + link.offsetTop - link.parentNode.offsetTop - crumb.offsetTop + 'px';
	crumbs.style.left = left + link.offsetLeft - link.parentNode.offsetLeft + 'px';
	crumb.className = 'nolink';
	crumb.onclick = function() { crumbs.style.top = '-1000px'; return false; };
	return false;
}

function showcitycontacts(link){
	var cityname = link.parentNode.id.replace('select','');
	var current = document.getElementById('city_contacts').firstChild;
	var cities = document.getElementById('cities');
	while ( current != null ) {
		if ( current.tagName == 'DT' ){
			current.id.replace('city','') == cityname ? current.style.fontWeight = 'bold' : current.style.fontWeight = 'normal';
			current.id.replace('city','') == cityname ? current.style.display = 'block' : current.style.display = 'none';
			document.getElementById(current.id.replace('city','select')).firstChild.className = 'servicelink';
			document.getElementById(current.id.replace('city','select')).firstChild.style.fontWeight = 'normal';
		} else if ( current.tagName == 'DD' ) {
			current.id.replace('contacts','') == cityname ? current.style.display = 'block' : current.style.display = 'none';
		}
		current = current.nextSibling;
	}
	cities.style.top = '-1000px';
	var takemeup = document.getElementById('takemeup');
	takemeup.style.display = 'none';
	return false;
}


// вывод окна логина
function key(link){
	var locker = document.getElementById('profilelocker');
	var lockertitle = document.getElementById('lockertitle');
	if(locker.style.top == '-1000px') {
		locker.style.top = link.offsetTop + 'px';
		locker.style.left = link.offsetLeft - lockertitle.offsetWidth + 'px';
		return false;
	} else {
		locker.style.top = '-1000px';
		return false;
	}
}

// расчет высоты дивов
function slideitfirst(target,showfirst) {
	var slider = document.getElementById(target);
	var as = slider.getElementsByTagName('a');
	var aslength = as.length;
	for(i = 0; i < aslength; i++) {
		var a = as[i];
		var aid = a.id;
		if(aid.indexOf("header") != -1) {
			a.onclick = new Function("processClick(this)");
		}
	}
	var divs = slider.getElementsByTagName('div');
	var divshowfirst = divs[showfirst];
	var divslength = divs.length;
	for(i = 0; i < divslength; i++) {
		var div = divs[i];
		var divid = div.id;
		if(divid.indexOf("slideblock") != -1) {
			var section = divid.replace('-slideblock','');
			array.push(section);
			div.maxh = div.offsetHeight;
			div.style.display = 'none';
		}
	}
	if (showfirst != 0) {
		divs[showfirst-1].style.display = 'block';
		as[showfirst-1].className = 'nolink';
		
	}
}

// раскрывание выбранного и схлопывание остальных
function processClick(a) {
	var catlength = array.length;
	for(i = 0; i < catlength; i++) {
		var section = array[i];
		var head = document.getElementById(section + '-header');
		var cont = section + '-slideblock';
		var contdiv = document.getElementById(cont);
		head.className = 'servicelink';
		clearInterval(contdiv.timer);
		if(head == a && contdiv.style.display == 'none') {
			contdiv.style.height = '0px';
			contdiv.style.display = 'block';
			initSlide(cont,1);
		} else if(head != a && contdiv.style.display == 'block') {
			initSlide(cont,-1);
		}
	}
	a.className = 'nolink';
}

// установка переменных и вызов функции
function initSlide(id,dir) {
	var cont = document.getElementById(id);
	var maxh = cont.maxh;
	cont.direction = dir;
	cont.timer = setInterval("slide('" + id + "')", timer);
}

// раскрывание-схлопывание дива изменением высоты и прозрачности
function slide(id) {
	var cont = document.getElementById(id);
	var maxh = cont.maxh;	
	var currheight = cont.offsetHeight;
	var dist;
	cont.direction == 1 ? dist = (Math.round((maxh - currheight) / speed)) : dist = (Math.round(currheight / speed));
	dist < 1 ? dist = 1 : '';
	cont.style.height = currheight + (dist * cont.direction) + 'px';
	cont.style.opacity = currheight / cont.maxh;
	cont.style.filter = 'alpha(opacity=' + (currheight * 100 / cont.maxh) + ')';
	if(currheight < 2 && cont.direction != 1) {
		cont.style.display = 'none';
		clearInterval(cont.timer);
	} else if(currheight > (maxh - 2) && cont.direction == 1) {
		clearInterval(cont.timer);
	}
}

function chech_checkbox(form,button)
{
	var i=0;
	var marked=false;
	for (i=0;i<form.length;i++)
	{
		if (form.elements[i].name.indexOf("ans")!=-1)
		{
			if (form.elements[i].checked)
			{
				marked=true;
			}
		}	
	}
	if (marked)
	{
		button.disabled=false;
	} else
	{
		button.disabled=true;
	}
}

$(document).ready( function () {
	

	if(typeof(animate_ind)!="undefined"){
		$(".teaser").not("#hdb"+animate_ind).css("display", "none");
		animate_count = $(".teaser").length;
		setTimeout(ChangeBG, animate_period);
	}
	
	
});


(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$("tiret").style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$("tiret").style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

function ChangeBG()
{
	curent = $("#hdb"+animate_ind);
	animate_ind++;
	if (animate_ind > animate_count)
	{
		animate_ind = 1;
	};
	next = $("#hdb"+animate_ind);
	curent.fadeOut(animate_speed,function () {
       next.fadeIn(animate_speed);
      });
	document.getElementById('hdb'+animate_ind).style.removeAttribute('filter');	
	
	setTimeout(ChangeBG, animate_period);
}
