var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isIE6 = (navigator.userAgent.indexOf("MSIE 6.0") != -1) ? true : false;
var isIE7 = (navigator.userAgent.indexOf("MSIE 7.0") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var delay = 6000;

function e(id){ return document.getElementById(id); }
function change_bg(num){
if (num==undefined) num = 1;
fadeOpacity('bg','fade','',num);
//e('bg').style.backgroundImage = "url(img/header-bg-3.jpg)";
//fadeOpacity('bg','block');
}

var timer;
var btimer = {};
var allHeights = {};
function scrolldelta(id, dy) {
    if (dy != 0) {
        doScroll(id,dy);
    } else {
        clearTimeout(timer);
    }
}

function doScroll(id, dy) {
    if (!e(id)) { if (timer!=undefined) clearTimeout(timer); return false; }
    var target = e(id);
    target.scrollTop = target.scrollTop + dy;
    timer = setTimeout("doScroll('"+id+"',"+dy+");", 25);
}

function slideGO(id){
	if (!allHeights[id]) allHeights[id] = parseInt(e(id).offsetHeight);
	var m = (isIE) ? 30 : 10;
	var dy = /minimize/i.test(e('i'+id).src) ? -m : m;
    slide_block(id, dy);
    if (isIE && id == 'bl1' && e('jurIEbag')) e('jurIEbag').style.display = 'none';
}

function slide_block(id, dy){
    if (!e(id)) { if (btimer[id]!=undefined) clearTimeout(btimer[id]); return false; }
    var target = e(id);

    var height = parseInt(target.offsetHeight)+dy;
    target.style.height = ((height>90) ? height : 90)+'px';
    if (height>90 && height<=allHeights[id]) btimer[id] = setTimeout("slide_block('"+id+"',"+dy+");", 1);
    else {
    	clearTimeout(btimer[id]);
    	e('i'+id).src = (height<=90) ? e('i'+id).src.replace(/minimize/i,"maximize") : e('i'+id).src.replace(/maximize/i,"minimize");
    	if (isIE && id == 'bl1' && e('jurIEbag') && height>90) e('jurIEbag').style.display = '';
    }
}


function reset_sub_menu(){
	var li = e('menu').getElementsByTagName('li');
	for (var i=0; i<li.length; i++) {
			if (li[i].firstChild.className=='hov') li[i].firstChild.className = '';
			var j = i+1;
			if (e('subMenu'+j)) e('subMenu'+j).style.display = 'none';
	}
}

function second_menu(num,el,widths){
	reset_sub_menu();
	if (el!=undefined && el) {
		if (el.className=='') el.className = 'hov';
		el.blur();
		sm_hide = true;
		if (e('subMenu'+num)) {
			var subMenu = e('subMenu'+num);
			subMenu.style.display = '';
			subMenu.style.left = widths + 'px';
			//subMenu.style.left = el.offsetWidth+'px';
	    }
    }
}

var sm_hide=false;
function hide_sub_menu(){
  if (sm_hide) { sm_hide=false; reset_sub_menu(); }
}

function show_sub_menu(val){
  if (val===true) {
  	sm_hide = true;
    delayhidemenu();
   } else {
  	sm_hide = false;
    clearhidemenu();
   }
}

var disappeardelay=2000;
function delayhidemenu(){ delayhide=setTimeout("reset_sub_menu();",disappeardelay); }
function clearhidemenu(){ if (typeof delayhide!="undefined") clearTimeout(delayhide); }

document.onclick=hide_sub_menu;

/*
setElementOpacity - установка прозрачности
getOpacityProperty - проверка, есть ли возможность менять прозрачность
fadeOpacity - плавное изменение прозрачности
*/
function setElementOpacity(oElem, nOpacity)
{
	var p = getOpacityProperty();
	(setElementOpacity = p=="filter"?new Function('oElem', 'nOpacity', 'nOpacity *= 100;	var oAlpha = oElem.filters["DXImageTransform.Microsoft.alpha"] || oElem.filters.alpha;	if (oAlpha) oAlpha.opacity = nOpacity; else oElem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";'):p?new Function('oElem', 'nOpacity', 'oElem.style.'+p+' = nOpacity;'):new Function)(oElem, nOpacity);
}

function getOpacityProperty(){
	var p;
	if (typeof document.body.style.opacity == 'string') p = 'opacity';
	else if (typeof document.body.style.MozOpacity == 'string') p =  'MozOpacity';
	else if (typeof document.body.style.KhtmlOpacity == 'string') p =  'KhtmlOpacity';
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) p =  'filter';

	return (getOpacityProperty = new Function("return '"+p+"';"))();
}
/* Функции для плавного изменения прозрачности:
1) fadeOpacity.addRule('opacityRule1', 1, 0.5, 30); //вначале создаем правило, задаем имя правила, начальную прозрачность и конечную, необязательный параметр задержки, влийяющий на скорость смены прозрачности
2) fadeOpacity('elemID', 'opacityRule1'); // выполнить плавную смену прозрачности элемента с id равным elemID, по правилу opacityRule1
3) fadeOpacity.back('elemID'); //вернуться в исходное сотояние прозрачности
*/
function fadeOpacity(sElemId, sRuleName, bBackward, num){
	if (num==undefined) num = '';
	var elem = e(sElemId);
	if (!elem || !getOpacityProperty() || !fadeOpacity.aRules[sRuleName]) return;

	var rule = fadeOpacity.aRules[sRuleName];
	var nOpacity = rule.nStartOpacity;

	if (fadeOpacity.aProc[sElemId]) {clearInterval(fadeOpacity.aProc[sElemId].tId); nOpacity = fadeOpacity.aProc[sElemId].nOpacity;}
	if ((nOpacity==rule.nStartOpacity && bBackward) || (nOpacity==rule.nFinishOpacity && !bBackward)) return;

	fadeOpacity.aProc[sElemId] = {'nOpacity':nOpacity, 'tId':setInterval('fadeOpacity.run("'+sElemId+'","'+num+'")', fadeOpacity.aRules[sRuleName].nDalay), 'sRuleName':sRuleName, 'bBackward':Boolean(bBackward)};
}

fadeOpacity.addRule = function(sRuleName, nStartOpacity, nFinishOpacity, nDalay){fadeOpacity.aRules[sRuleName]={'nStartOpacity':nStartOpacity, 'nFinishOpacity':nFinishOpacity, 'nDalay':(nDalay || 30),'nDSign':(nFinishOpacity-nStartOpacity > 0?1:-1)};};
fadeOpacity.back = function(sElemId){fadeOpacity(sElemId,fadeOpacity.aProc[sElemId].sRuleName,true);};
fadeOpacity.run = function(sElemId,num){
	var proc = fadeOpacity.aProc[sElemId];
	var rule = fadeOpacity.aRules[proc.sRuleName];

	proc.nOpacity = Math.round(( proc.nOpacity + .1*rule.nDSign*(proc.bBackward?-1:1) )*10)/10;
	setElementOpacity(document.getElementById(sElemId), proc.nOpacity);
	if (proc.nOpacity==rule.nStartOpacity || proc.nOpacity==rule.nFinishOpacity) clearInterval(fadeOpacity.aProc[sElemId].tId);
	if (proc.nOpacity==0 && num!='') {
		if (num=='' || num==1 || num<=0 || num>=4) num = 2;
		else if (num==2) num++;
		else if (num==3) num = 1;
		e('bg').style.backgroundImage = "url(/d_roseestr/img/header-bg-"+num+".jpg)";
		fadeOpacity('bg','block','',num);
	} else if (proc.nOpacity==1 && num!='' && num!=undefined) {
		setTimeout(new Function("change_bg('"+num+"');"),delay);
	}
}
fadeOpacity.aProc = {};
fadeOpacity.aRules = {};
fadeOpacity.addRule('block', .0, 1, 150);
fadeOpacity.addRule('fade', 1, 0, 100);



function doShowHide (element) {
	  var item = null;
	  if (document.getElementById) {
		item = document.getElementById(element);
	  } else if (document.all){
		item = document.all[element];
	  } else if (document.layers){
		item = document.layers[element];
	  }

	  if (!item) {	  }
	  else if (item.style) {
		if (item.style.display == "none"){ item.style.display = ""; }
		else {item.style.display = "none"; }
	  }else{ item.visibility = "show"; }
}


var horizontal_offset="9px"
var vertical_offset="0"
var ie=document.all
var ns6=document.getElementById&&!document.all

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
}
else{
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
}
return edgeoffset
}

function showhint(menucontents, obj, e, tipwidth){
	tipwidth = 'auto';
if ((ie||ns6) && document.getElementById("hintbox")){
dropmenuobj=document.getElementById("hintbox")
dropmenuobj.style.display = '';
document.getElementById('hint_cont').innerHTML=menucontents
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth-15+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
dropmenuobj.style.visibility="visible"
obj.onmouseout=hidetip
}
}

function hidetip(e){
dropmenuobj.style.visibility="hidden"
dropmenuobj.style.left="-500px"
dropmenuobj.style.display = '';
}



$(document).ready(function(){
	setTimeout(change_bg,delay);

	$(".acc .maximized-service .cont").slideDown();
	$(".acc h4").click(function(){
		$(".acc .cont:visible").slideUp("slow");
		$(".acc div.maximized-service").removeClass("maximized-service").addClass("minimized-service");
		//.addClass("minimized-service");
		$(this).next("div:hidden").slideToggle("slow");
		//$(this).parent("div:first").removeClass("maximized-service").addClass("minimized-service");
		$(this).parent("div:visible").removeClass("minimized-service").addClass("maximized-service");

		//$(this).toggleClass("active");
		//$(".acc").siblings("h4").removeClass("active");
	});

});



