// определение типа браузера
var isIE = navigator.appName == "Microsoft Internet Explorer";
var isNetscape = navigator.appName == "Netscape";
var isOpera = navigator.appName == "Opera";

// открытие flash
function flash(flash_src,flash_w,flash_h,flash_m,flash_t,flash_var) {
	if (flash_m) {flash_m_param='<param name="menu" value="false">'; flash_m_print = ' menu="false"';} 
	else {flash_m_param = '';flash_m_print = ' ';}
	if (flash_t) {flash_t_param = '<param name="wmode" value="transparent">'; flash_t_print = ' wmode="transparent"';} 
	else {flash_t_param = '';flash_t_print = ' ';}
	if (flash_var) {flash_var_param ='<param name="FlashVars" value="'+flash_var+'">'; flash_var_print = ' FlashVars="'+flash_var+'"';} 
	else {flash_var_param = '';flash_var_print = ' ';}
	printflash='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+flash_w+'" height="'+flash_h+'"><param name="movie" value="'+flash_src+'"><param name="quality" value="high">'+flash_m_param+flash_t_param+flash_var_param+'<embed src="'+flash_src+'" quality="high"'+flash_m_print+flash_t_print+flash_var_print+'pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="'+flash_w+'" height="'+flash_h+'"></embed></object>';
	document.write(printflash);
}

// показать/скрыть
function show(object) {
	if (document.getElementById && document.getElementById(object) != null) {
         if (document.getElementById(object).style.display == 'block') {
		 	node = document.getElementById(object).style.display='none';
		 }
		 else {
			 node = document.getElementById(object).style.display='block';
		}
    }
}
function hide(object) {
	if (document.getElementById && document.getElementById(object) != null) {
         node = document.getElementById(object).style.display='none';
    }
}

var preimages = new Array (
	"input.gif",
	"textarea.gif",
	"input_code.gif",
	"close.gif",
	"button.gif",
	"pop.png",
	"option.png",
	"option_top.png",
	"option_bottom.png",
	"option_high.png",
	"arrow_bot.gif",
	"top_bg.png",
	"top_left.png",
	"top_right.png"
);
// обработка загрузки страницы для растягивания
var othersizes;
var isAlpha=false;
function loaded(other,alphaCSS) {
	if (alphaCSS) isAlpha=true;
	changenews();
	showcomments();
	selection('feedback');
	setformkeys('formcomment');
	setformkeys('formmail');	
	othersizes=other;
	size();
	window.onresize=function() {size();}
	preload_img(preimages,"/img/");
	setTimeout("showalert()",1000);
	window.onclick=window.onkeydown=function() {setclickall();};
}
// функция прелоада изображений, для отсутствия подгрузок во время анимации
function preload_img(preimages,src) { 
	if (document.images) {
		var preimage = new Array();
		for (i=0;i<preimages.length;i++) {
			preimage[i] = new Image();
			preimage[i].src = src+preimages[i];
		}
	}
}
// показать комментарии в случае #comments
function showcomments() {
	test=location.href.split("#");
	if (test[1]=="comments") show("comments");
}
function setformkeys(id) {
	if (document.getElementById && $(id) != null) {
		object = $(id);
		for (i=0;i<object.elements.length;i++) {
			elem=object.elements.item(i);			
			if (elem.tagName=='INPUT') elem.onkeypress=function(event) {keyEnter(event,this.parentNode);}
		}	
		object.onkeypress=function(event) {keyCtrlEnter(event,this);}
	}
}
// Ctrl + Enter
function keyCtrlEnter(event,object) {
	if((event.ctrlKey && event.keyCode == 13)||(event.keyCode == 10)) object.submit.click();
}
// Enter
function keyEnter(event,object) {
	if(event.keyCode == 13) object.submit.click();
}
// Получение элемента
function $(id) { 
	return document.getElementById(id);
}
function size() {
	a=document.documentElement.clientHeight;
	//b=window.innerHeight;
	//alert (a+" "+b);
	if (a<651) a=651;
	if (document.getElementById("content") != null && a-othersizes>30)document.getElementById("content").style.height=a-othersizes+"px";
	else document.getElementById("content").style.height=30+"px";	
}

// фиксирование события в Google Analytics и Яндекс Метрика
function loadaction(url) {
	pageTracker._trackPageview(url);
	yaCounter2880343.hit('http://www.steem.ru' + url);
	return true;
}

// анимация новостей
var news;
var newstime;
var nfirst=nend=newspos=0;
var newslines=newsline=1;
var newsheight=30;
var newsplus=40;
var newsi=0;
var newsclone=false;
var autonews=false;
function changenews() {
	if (document.getElementById && $("changednews") != null) {
		news=$("changednews");
		a=1;
		for (i=0;i<news.childNodes.length;i++) if (news.childNodes.item(i).tagName=="DIV") {
			if (a==1) newsclone="<div>"+news.childNodes.item(i).innerHTML+"</div>";
			a++;		
		}
		if (a>1) newslines=a;
		if (newsclone) {
			if (a>1) {
				news.innerHTML=news.innerHTML+newsclone;
				autonews=true;
			}
			else autonews=false;
		}	
		if (autonews) {
			newswaittime();
			news.onmouseover=function() {autonews=false; if (newsi==0) clearTimeout(newstime);}
			news.onmouseout=function() {autonews=true; if (newsi==0) newswaittime();}
		}
	}
}
function newswaittime() {
	clearTimeout(newstime);	
	if (newsline==newslines) { // если элемент последний в списке
		newsline=1; 
		newspos=0;
	}
	nfirst=(newsline-1)*newsheight; // начальная позиция
	nend=newsline*newsheight; // конечная позиция
	newspos=nfirst;
	news.style.marginTop=-newspos+"px";
	newsi=0;
	newstime = setTimeout('changenewstime()',3500);	
}
function changenewstime() {
	clearTimeout(newstime);
	newstime = setTimeout('newsmove()',newsplus);
}
function newsmove() {
	if (newsi<1) {
		newspos=Math.round(nfirst+(nend-nfirst)*(1-Math.pow(1-newsi,2)));
		newsi+=newsplus/1000;
		news.style.marginTop=-newspos+"px";
		changenewstime();
	}
	else {
		newsline++;
		newswaittime();
	}
}

// открытие с установкой фокуса
var focusobject=false;
function openfocus(name,focusname) {
	object=$("form"+name);
	focusobject=object[focusname];
	openup("add"+name,true);
}

// всплывающие слои
var nameup;
var opacitylog;
var opacitylogback;
var transp=0;
var transpb=100;
var showtime=30;
var closeother=false;
// открытие слоя
function openup(name,closeother) {
	scrollTop = document.documentElement.scrollTop;
	// позиционирование форм
	up=document.getElementById(name); 
	if (up.style.display!='block' && closeother) {
		up.style.left="50%"; 
		up.style.top="50%";
		up.style.marginLeft="-225px"; 
		up.style.marginTop=scrollTop-153+"px";
	} 		
	// закрыть другой слой (есть слои для которых закрывать ничего не нужно)
	if (document.getElementById(nameup) != null) {
		if (nameup!=name && closeother) closeup(nameup);		
	}
	up=document.getElementById(name);
	if (closeother) nameup=name;
	closeother=false;	
	// если слой открыт, то его закрыть
	if (up.style.display == 'block') { 
 		closeup(name);
 	} else {
		if (isAlpha) {
			transp=0;
			clearTimeout(opacitylog);	
			setopacity(name);
		}
		toopen(name);		
	}
}
// закрытие слоя
function closeup(name) { 
	if (isAlpha) {
		transpb=100;
		clearTimeout(opacitylogback);
		setopacityback(name);
	} else toclose(name);	
}
// слой показать
function toopen(name) { 
	up=document.getElementById(name);
	up.style.display = 'block';
	focusup();
}
// слой полностью скрыть
function toclose(name) { 
	up=document.getElementById(name);
	up.style.display = 'none';	
}
// выставление фокусов при открытии различных слоёв
function focusup() { 
	if (focusobject) focusobject.focus();
	focusobject=false;
}
// анимация появления слоя
function setopacity(name) {
	up=document.getElementById(name);
	up.style.opacity=transp/100;
	if (transp<100) opacitylog=setTimeout('openopacity("'+name+'")',40);
}
function openopacity(name) {
	transp+=25;	
	setopacity(name);
}
// анимация скрытия слоя
function setopacityback(name) {
	up=document.getElementById(name);
	up.style.opacity=transpb/100;
	if (transpb>0) opacitylogback=setTimeout('openopacityback("'+name+'")',40);
	else toclose(name);
}
function openopacityback(name) {
	transpb-=25;
	setopacityback(name);
}

// перемещение слоев (только при нажатии на объект DIV, исключение DIV с классом "cont clickact")
var upx;
var upy;
var msavx;
var msavy;
var activemoveup;
function moveup(activemoveup,ev) {
	ev=isIE?event:ev;
	evtarget=isIE?ev.srcElement:ev.target;
	if (evtarget.tagName=="DIV"||evtarget.tagName=="SPAN"||evtarget.tagName=="I"||evtarget.tagName=="IMG") {
		document.body.style.cursor="move";
		evtarget.style.cursor="move";
		upx=parseInt(activemoveup.style.marginLeft)?parseInt(activemoveup.style.marginLeft):0; // текущие положение слоя
		upy=parseInt(activemoveup.style.marginTop)?parseInt(activemoveup.style.marginTop):0;
		msavx=ev.screenX+document.documentElement.scrollLeft; // текущее положение мыши + положение прокрутки
		msavy=ev.screenY+document.documentElement.scrollTop;
		document.onmousemove=function(ev){
			ev=isIE?event:ev;
			if (isIE) document.selection.clear(); else window.getSelection().removeAllRanges(); // очистка выделения при перемещении
			activemoveup.style.marginLeft=upx+ev.screenX-msavx+document.documentElement.scrollLeft+"px";
			activemoveup.style.marginTop=upy+ev.screenY-msavy+document.documentElement.scrollTop+"px";			
		}
		document.onmouseup=function() {
			document.body.style.cursor="auto";
			evtarget.style.cursor="default";
			document.onmousemove=document.onmouseup=function() {return false;};
		}
	}
}
// опции в инпуте
var optstime;
var optshow=false;

function selection(object) {
	if (document.getElementById && $(object+"options") != null) {
		opts=$(object+"options");
		for (i=0;i<opts.childNodes.length;i++) if (opts.childNodes.item(i).tagName=="A") {
			opt=opts.childNodes.item(i);
			opt.nam=object;
			opt.cont=opt.innerHTML;
			opt.onmouseout=function() {if (optshow) optstime=setTimeout('closeup("'+this.nam+'options'+'")',500);}
			opt.onmouseover=function() {clearTimeout(optstime);optshow=true;}
			opt.onclick=function() {
				clearTimeout(optstime);
				document.getElementById(this.nam+"input").value=this.cont;
				closeup(this.nam+"options");
				optshow=false;
				return false;
			}
		}
	}
}
function openfeedback(objectid) {
	opts=document.getElementById("feedbackoptions");
	a=0;
	for (i=0;i<opts.childNodes.length;i++) if (opts.childNodes.item(i).tagName=="A") {
		a++;
		if (a==objectid) {
			document.getElementById("feedbackinput").value=opts.childNodes.item(i).innerHTML;
		}
	}
	openfocus('mail','name');
}

// показ алерта
var alerttime;
var alerttext;
var alertpos=0;
var alertheight=37;
var alertplus=40;
var alerti=0;
function showalert() {
	if (document.getElementById && $("alerttext") != null) {
		alerttext=$("alerttext");
		alerttext.style.marginTop=-alertheight+"px";
		alerttext.style.display='block';
		for (i=0;i<alerttext.childNodes.length;i++) if (alerttext.childNodes.item(i).className=="cont") {
			alertwidth=alerttext.childNodes.item(i).offsetWidth+24+24;		
		}
		alerttext.style.marginLeft=-Math.round(alertwidth/2)+"px";
		alertmove();
	}
}
function changealerttime() {
	clearTimeout(alerttime);
	alerttime = setTimeout('alertmove()',alertplus);
}
function alertmove() {
	if (alerti<1) {
		alertpos=Math.round(alertheight*(Math.pow(1-alerti,5)));
		alerti+=alertplus*2/1000;
		alerttext.style.marginTop=-alertpos+"px";
		changealerttime();
	}
	else {		
		alerti=0;
		clearTimeout(alerttime);
		alerttime = setTimeout('alertmover()',10000);	
	}
}

function changealerttimer() {
	clearTimeout(alerttime);
	alerttime = setTimeout('alertmover()',alertplus);
}
function alertmover() {
	if (alerti<1) {
		alertpos=Math.round(alertheight*(Math.pow(alerti,4)));
		alerti+=alertplus*2/1000;
		alerttext.style.marginTop=-alertpos+"px";
		changealerttimer();
	}
	else {		
		alerttext.style.display='none';
	}
}

// глобальный клик
function setclickall() {
	// убрать алерт
	if (alertpos==0 && alerti==0 && alerttime) {
		clearTimeout(alerttime);
		alertmover();
	}
	//alert(version);
}

