/* JavaScript file */

function addEvent(elem, type, handler){
	if (elem.addEventListener){
		elem.addEventListener(type, handler, false)
	} else {
		elem.attachEvent("on"+type, handler)
	}
}

var oldregion=0;

if (window.screen.width>900) {var mode=1;} else {var mode=0;}

function adv(){
		if (screen.width<=800) {
			document.write ('<div align="left" style="margin-left:50px;"><a href="http://www.adv.ru" target="_blank"><img src="/images/internal/adv.gif" border="0" alt="сделано в ADV"/></a></div>')
			
		}
		if (screen.width>=1024) {
			document.write ('<!--<div align="left" style="margin-left:100px;">--><a href="http://www.adv.ru" target="_blank"><img src="/images/internal/adv.gif" border="0" align="right" alt="сделано в ADV"/></a><!--</div>-->')
		}
	}

function wfloat (target,size) {
var wWidth=400;
var wHeight=316;
var cWidth=window.screen.width;
var cHeight=window.screen.height;
if(cHeight && cWidth){
	var leftOffset=Math.ceil((cWidth-wWidth)/2);
	var topOffset=Math.ceil((cHeight-wHeight)/2);
	var size = size || 'width=678,height=590';
	var target = target || 'wfloat';
	openwnd ('',target,'width='+wWidth+',height='+wHeight+'location=no,toolbar=no,resizable=no,menubar=no,status=no,scrollbars=no,top='+topOffset+',left='+leftOffset);
 }
return false;
}

// mozg

function ShowThis(divid) {document.getElementById(divid).style.display="block";}
function HideThis(divid) {document.getElementById(divid).style.display="none";}

function Show(divid) {
	if (document.getElementById(divid).style.display=="none") {
		document.getElementById(divid).style.display="block";
	} else {
		document.getElementById(divid).style.display="none";
	}
}

function showRegion(id) {
	if (oldregion!=id) hideRegion(oldregion);
	if (id!=0)ShowThis('region'+id);
	oldregion=id;
}
function hideRegion(id) {
	if (id!=0) HideThis('region'+id);
}

function directShow (id)
{
	var i = 1;
	while (document.getElementById('info'+i) != null && document.getElementById('info'+i))
	{
		if (id == i)
		{ 
			//ShowThis('block'+i);
			ShowThis('arr'+i);
			ShowThis('act'+i);
			ShowThis('info'+i);
			//document.getElementById('title'+i).style.display = "bold";
		}
		else
		{
			//HideLayer('block'+i);
			HideThis('arr'+i);
			HideThis('act'+i);
			HideThis('info'+i);
			//document.getElementById('title'+i).style.display = "normal";
		}
		i++;
	}
}

function ShowMag(id) {
//alert (id);
directShow(id);
//ShowThis('arr'+id);
//ShowThis('act'+id);
//ShowThis('info'+id);
}
function HideMag(id) {
HideThis('arr'+id);
HideThis('act'+id);
HideThis('info'+id);
}


/*
function showZone (objid){
	if (current_zone) hideLayer(current_zone)
	current_zone = 'zone'+objid
	showLayer(current_zone)
}

function showCity (id){
	if (current_city) hideLayer(current_city)
	current_city = 'city'+objid
	showLayer(current_city)
}

function showLayer(divid) {
	//alert(divid);
	var obj = document.getElementById(divid)
// mozilla ? 
	obj.style.visibility = 'visible'
}

function hideLayer(divid) {
	var obj = document.getElementById(divid)
// mozilla ? 
	obj.style.visibility = 'hidden'
}*/


function Citate(title, name)
	{
		if(document.all.atestlink)
		{
			document.all.atestlink.focus();
		}
		else
		{
			var	cite_open='<CITE>';
			var	cite_close='</CITE>';
	
			var pref_open=title + ' написал(а): ' + cite_open;
			var pref_close=cite_close + '\n\n';
	
			var o=document.all.bbsm_description;
	
			if(document.getSelection)
			{
				var selection=document.getSelection();
			}
			else
			{
				var selection=document.selection.createRange().text;
			}
	
			if(selection!='') o.value=pref_open + selection + pref_close + o.value;
			else
			{
				o.value=pref_open + document.all["mes_" + name].innerText + pref_close + o.value;
			}
	
			o.focus();
		}
	}

function check_email(s)
{ flag=true;
	if (window.RegExp) {
		st="a";ex=new RegExp(st);
		if (st.match(ex)) {
			r1=new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
			r2=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
			b=(!r1.test(s)&&r2.test(s));
		} else { flag=false; }
	} else { flag=false; }
	if(!flag) b=(s.indexOf("@")>0&&s.indexOf(".")>0&&s!=""&&s!="Please, enter your e-mail");
	return (b);
}
function f(param) { param.focus();param.select(); }
function check(email)
{	if(val(email)) return true;
		else return false;
}
function val(param)
{	s=param.value;
	if(check_email(s)) { return true;
	} else {
		alert("Please, enter a valid e-mail address.");
		f(param);return false; }
}

function photo (url) {
	wnd = null;
	var url = url || '';
	var alt = alt || '';
	var width = width || 600;
	var height = height || 400;
	var size = 'width='+width+',height='+height;
	wnd = window.openwnd (url,'wfloat',size+'toolbar=no,menubar=no,scrollbars=no,status=no,resizable=no');
	wnd.document.open();
	wnd.document.write("<html><head><script>function resize(){");
	wnd.document.write("self.resizeTo (photo.width,photo.height); } onload=resize;");
	wnd.document.write("</script><title>Увеличенное изображение</title></head><body bottommargin=0 topmargin=0 leftmargin=0 marginwidth=0 marginheight=0><img src='"+url+"' alt='"+alt+"' border='0' vspace='0' hspace='0' id='photo'/><body></html>");
  wnd.document.close();
  
return false;
}

function show_img(a,name) 
{
	if (document.getElementById(name).src){
		document.getElementById(name).src = a;
	}
	
}


function validate_form() {
	document.form.kanom.value = ''+document.form.kanom1.value+document.form.kanom2.value+document.form.kanom3.value+document.form.kanom4.value;
	if (!check_empty(document.form.kanom.value))
	{
		alert('Вы не ввели идентификационный номер!');
		return false;
	}
}

function check_empty(text) {
	return (text.length > 0);
}

function ShowHide(divid) {

                var d = document.getElementById("blockcity");
					var dc = d.getElementsByTagName("div");
					for (var i=0; i<dc.length; i++) {
						dc[i].style.display = "none";
					}
					document.getElementById(divid).style.display = "block";
}

var _cache = {};

function switchField(t, id) {
	var o = null;

	if( _cache[id] ) {
		o = _cache[id];
	} else {
		//o = document.getElementById(id);
		o = document.forms[0][id];
		o = o.parentNode;

		_cache[id] = o;
	}

	if( !o ) {
		return;
	}

	/*
	if( o.className.length ) {
		o.className = '';
	}
	*/

	o.style.display = ( t.checked ) ? '' : 'none';
}

function hideField(t, id) {
	var o = null;

	if( _cache[id] ) {
		o = _cache[id];
	} else {
		//o = document.getElementById(id);
		o = document.forms[0][id];
		o = o.parentNode;

		_cache[id] = o;
	}

	if( !o ) {
		return;
	}

	o.style.display = 'none'
}

function getBrowserName() {
	if (navigator.userAgent.toLowerCase().indexOf('msie')!=-1) return 'msie';
	if (navigator.userAgent.toLowerCase().indexOf('firefox')!=-1) return 'firefox';
	if (navigator.userAgent.toLowerCase().indexOf('opera')!=-1) return 'opera';
	return false;
}

function openLayer(foto) {
   // blur background
   var value = 4;
   var hgt = document.body.scrollHeight + 'px';
   var wdt = document.body.scrollWidth + 'px';
   if (document.getElementById('blurDiv')) deleteLayer('blurDiv');
   var bl = document.createElement('DIV');
   bl.id = 'blurDiv';
   bl.style.width = wdt;
   bl.style.height = hgt;
   bl.style.opacity = value/10;
   bl.style.filter = 'alpha(opacity=' + value*10 + ')';
   document.body.appendChild(bl);
   
   //open 'window'
   var w_height = 700;
   var w_width  = 800;
   var wpup = document.createElement('DIV');
   wpup.id = 'fotowindow';
   wpup.style.width = w_width;
   wpup.style.height = w_height;
   
   wpup.style.left = Math.round(document.body.scrollWidth/2 - w_width/2);
   wpup.style.top  = '60px';
   
   document.body.appendChild(wpup);
   
   $('#fotowindow').append($('#popupW'));
   
   var img = document.createElement('IMG');
   img.src=foto;
   $('#popupfoto').append(img); 
   
   document.getElementById('popupW').style.display="block"; //table is here
   document.getElementById('popupW').style.zIndex = "99";
}
function closeLayer() {
   var popupW = document.getElementById('popupW');
   popupW.style.display = 'none';
   popupW.style.zIndex = '0';
   document.body.appendChild(popupW);
   $('#popupfoto img').remove();
   $('#blurDiv').remove();
   $('#fotowindow').remove();
   
}

var comments = new Array();

function anotherComm(direction){
   if(comments){
   var cnode = document.getElementById('friendcomm');
   var current = cnode.getAttribute('val');
   var idc = current*1+direction;
   if(idc >= 0 && idc < comments.length){
      document.getElementById('friendcomm').setAttribute('val',idc);
      cnode.innerHTML = comments[idc][2];
      document.getElementById('friend').innerHTML = comments[idc][0] + ' | ' + comments[idc][1];
   }
   }
}

function setRating(ball){
   var stars = document.getElementById('rating').getElementsByTagName('div');
   //document.getElementById('rating').setAttribute('val',ball);
   if(ball > 0)
   for(i = 0;i < 5; i++){
      if(i < ball)
      stars[i].childNodes[0].src='/images/star_a.jpg'
      else
      stars[i].childNodes[0].src='/images/star_p.jpg'
   }
}

function setBall(ball){
   document.getElementById('rating').setAttribute('val',ball);
}
function getBall(){
   return document.getElementById('rating').getAttribute('val');
}

function viewRating(ball){
   var stars = document.getElementById('rating').getElementsByTagName('div');
   document.getElementById('rating').setAttribute('val',ball);
   if(ball > 0)
   for(i = 0;i < 5; i++){
      if(i < ball)
      stars[i].childNodes[0].src='/images/star_a.jpg'
      else
      stars[i].childNodes[0].src='/images/star_p.jpg'
   }
}

function sendRating(){
   //ajax(id , user, document.getElementById('rating').val
}

function sendComment(){
   //id , user
}
function fillLayer(){
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

function showBHint(node, path, domain){
   var position = findPos(node);
   document.getElementById('bloghint').style.top = position[1];
   document.getElementById('bloghint').style.left = position[0];
   var tmp = '&lt;div style=&quot;text-align:center; background-color:#fff; width: 250px; font-family: Helvetica, Tahoma; font-size:11px; font-weight:bolder; border:2px solid #e7c38c; margin: 10px 0 15px 0; padding:0;&quot; &gt;   &lt;div style=&quot;background-color:#e7c38c; width:100%; border-bottom:2px solid #000;&quot;&gt;&lt;img width=&quot;160&quot; vspace=&quot;24&quot; height=&quot;30&quot; border=&quot;0&quot; src=&quot;http://DOMAIN/images/image/econica.gif&quot; alt=&quot;Эконика&quot;/&gt;&lt;/div&gt;   &lt;div style=&quot;margin: 5px 0 5px 0;&quot;&gt;Фотоконкурс на сайте &quot;Эконики&quot;&lt;/div&gt;   &lt;div&gt;&lt;img width=&quot;100&quot; height=&quot;100&quot; border=&quot;0&quot; src=&quot;http://DOMAIN/IMG&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;   &lt;div style=&quot;margin: 5px 0 5px 0;&quot;&gt;&lt;a href=&quot;http://DOMAIN/forum/competition.xgi&quot;&gt;Голосовать&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;'
   tmp = tmp.replace(/IMG/,path).replace(/DOMAIN/g, domain);
   $('#bloghint textarea').html(tmp);
   $('#bloghint').show();
}

function closeBHint(){
   $('#bloghint').hide();
}