var mysheet=document.styleSheets[0];
var totalrules=mysheet.cssRules? mysheet.cssRules.length : mysheet.rules.length;
if (mysheet.insertRule){ //if Firefox
mysheet.insertRule(".js{visibility:visible}", totalrules);
mysheet.insertRule(".njs{display:none}", totalrules+1);
}
else if (mysheet.addRule){ //else if IE
mysheet.addRule(".js", "visibility:visible");
mysheet.addRule(".njs", "display:none");
}

var t='';
var c_e=null; //current element
var counter_code_revision = 2;
var bx_code_revision = 1;


//show screenshot section
var scr_to=null;
var scr=document.createElement('img');
scr.style.position='absolute';
scr.style.display='none';
scr.className='popimg';
scr.src='/scr/tomsknet.ru.jpg';
document.documentElement.lastChild.appendChild(scr);

function show_scr(e,s)
{
var posx=0;var posy=0;
if (!e) e = window.event;if (e.pageX || e.pageY) {posx = e.pageX;posy = e.pageY;}
else if (e.clientX || e.clientY) {
posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;
}

scr.src='/favicon/null.gif';
scr.src='/scr/'+s;
scr.style.top=posy-165+'px';
scr.style.left=posx+15+'px';

//alert(posx+' '+posy);
scr_to=setTimeout("scr.style.display=''",300);
}


function show_img(e,s,w,h)
{
//alert('hooy');

var posx=0;var posy=0;
if (!e) e = window.event;if (e.pageX || e.pageY) {posx = e.pageX;posy = e.pageY;}
else if (e.clientX || e.clientY) {
posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;
}

scr.src='/favicon/null.gif';
scr.src=s;
scr.style.top=posy-h-15+'px';
scr.style.left=posx+15+'px';
scr.style.width=w+'px';
scr.style.height=h+'px';

//alert(posx+' '+posy);
scr_to=setTimeout("scr.style.display=''",300);
}



function hide_scr()
{
clearTimeout(scr_to);
scr.style.display='none';
}

//


// (c) niggaz
function r_sc(href,rnd)
{
if (rnd) href+="&rnd="+(new Date().getTime());
var script = document.createElement('SCRIPT');
script.setAttribute('type','text/javascript');
script.setAttribute('src',href);
document.documentElement.firstChild.appendChild(script);
}

function r_css(href)
{
//return false


css=document.createElement("link");
css.id='stylesheet';
css.setAttribute("rel", "stylesheet");
css.setAttribute("type", "text/css");
css.setAttribute("href", href);
document.getElementsByTagName("head").item(0).removeChild(gid('stylesheet'));
document.getElementsByTagName("head").item(0).appendChild(css);
}


function gid(id) {return document.getElementById(id);}

function remove(id) {gid(id).parentNode.removeChild(gid(id));}

function gen_bnrcode(bhost,bid,title,flash,img_type,width,height)
{
	var host=document.getElementById('a').value;
	if (!host) {
		alert('Введите доменное имя сайта');
		return false;
	}
	if (!valid_domain(host)) {
		alert('Неверное доменное имя сайта');
		return false;
	}
	var bl = 'http://c.tbex.ru/_/'+bid+'!'+bhost+'.html?a='+host;
	var bi = 'http://c.tbex.ru/_/'+bid+'!b'+['.jpg','.gif','.png'][img_type-1]+'?a='+host;
	var bf = 'http://c.tbex.ru/_/'+bid+'!b.swf?a='+host;

	if (flash) {

		var c='<object type="application/x-shockwave-flash" data="'+bf+'" width="'+width+'" height="'+height+'">'+
		'<param name="movie" value="'+bf+'" />'+
		'<param name="wmode" value="opaque">'+
		'<param name="flashvars" value="tbe_href='+escape(bl)+'" />'+
		'<a href="'+bl+'" title="'+title+'">'+title+'</a></object>';
	}
	else {
		var c='<a href="'+bl+'"><img alt="'+title+'" title="'+title+'" style="border:none;width:'+width+'px;height:'+height+'px" src="'+bi+'"></a>';
	}
	document.getElementById('c').value=c;

	return false;
}


function gen_code() {
	if (t=='') {alert('Выберите внешний вид счетчика');return false}
	var host=document.getElementById('h').value;
	if (host=='') {alert('Укажите имя сайта');return false}

	if (host.substring(0,7)=='http://') {
		host=host.substring(7);
		document.getElementById('h').value = host;
	}

	if (!/^([a-z0-9\-_]+[\.]{1})+([a-z]{2,4})$/i.test(host)) {
		alert('Неправильное имя сайта');
		return false;
	}

	var tt=t
	pos=t.indexOf('_')
	col=t.substring(0,pos)
	size=t.substring(pos+1)

	var pref='0abcdefghijklmnopqrstuvwxyz';
	var p=pref.indexOf(host[0]);
	if (-1==p) p=0;
	p=pref[p];

	//var cp=document.getElementById('cp').value
	//if (cp) cp='&cp='+cp; else cp=''
	el=document.getElementById('inline').checked?'span':'p';


	code='<'+el+' style="margin:0;padding:0" id="tbec"><'+
			'script type="text/javascript">setTimeout(\'var tbex=document.createElement("SCRIPT");tbex.type="text/javascript";tbex.src="http://c.tbex.ru/'+p+'/'+col+'!'+size+'!'+host+'!c.js?rev='+counter_code_revision+'"+String.fromCharCode(38)+"rnd="+(new Date().getTime());document.documentElement.firstChild.appendChild(tbex)\',1)'+'<'+'/script></'+el+'>';

	co=document.getElementById('c');
	co.value=code;
	return false;
}


function gen_flashbnrcode(host,bid,title,w,h)
{
if (!document.getElementById('a').value) {alert('Введите доменное имя сайта');return false;}
var a=document.getElementById('a').value;

c='<object type="application/x-shockwave-flash" data="http://tbex.ru/?f='+bid+'&a='+a+'" width="'+w+'" height="'+h+'" title="'+title+'">'+
'<param name="movie" value="http://tbex.ru/?f='+bid+'&a='+a+'" />'+
'<param name="flashvars" value="tbe_href=http://tbex.ru/?l='+bid+'&a='+a+'&tbe_target=_blank" />'+
'<img border="0" src="http://tbex.ru/?b='+bid+'&a='+a+'" alt="'+title+'" title="'+title+'" />'+
'</object>';

co=document.getElementById('c');
//while (co.hasChildNodes()) co.removeChild(co.firstChild);
//co.appendChild(document.createTextNode(c));
co.value=c;

return false;
}

function gen_bnrgrpcode()
{
	var pref = document.getElementById('pref').value;
	if (!pref) {
		return false;
	}
	
	var gid = document.getElementById('gid').value;
	if (!gid) {
		return false;
	}

	var host=document.getElementById('h').value;
	if (!host) {
		alert('Укажите имя сайта');
		return false;
	}

	if (host.substring(0,7)=='http://') {
		host=host.substring(7);
		document.getElementById('h').value = host;
	}


	if (!valid_domain(host)) {
		alert('Неправильное имя сайта');
		return false;
	}

	var p=pref.indexOf(host[0]);
	if (-1==p) p=0;
	p=pref[p];

	var s=document.getElementById('s_w').value+''+document.getElementById('s_h').value;
	document.getElementById('c').value='<s'+'cript type="text/javascript">var tbe_tmp=document.getElementsByTagName("script");if("undefined"==typeof tbe_bp)var tbe_bp=[];tbe_bp.push(["'+gid+'",tbe_tmp[tbe_tmp.length-1]]);if(typeof tbe_g'+gid+'=="undefined")setTimeout(\'var tbe_tmp=document.createElement("SCRIPT");tbe_tmp.type="text/javascript";tbe_tmp.src="http://c.tbex.ru/'+p+'/'+gid+'!'+s+'!'+host+'!g.js'+"?rev="+bx_code_revision+'";document.documentElement.firstChild.appendChild(tbe_tmp);\',1);var tbe_g'+gid+'=1;<'+'/script>';
	return false;
}




// (c) niggaz
function getDomEntry(){var ss=document.getElementsByTagName('script');return ss[ss.length-1];}

function inf(e)
{
// e=(e)?e:((window.event)?event:null)
// e.parentNode.style.display='block'
// e.style.display='none'
// c_e=e.parentNode

// r_sc('/site/'+c_e.previousSibling.firstChild.nodeValue+'/?vjs=1')
var cont=gid('s'+e);
if (!cont.firstChild) 
 {
  cont.innerHTML='Загрузка...';
  r_sc('/skwjs/'+e);
 }

cont.style.display=cont.style.display=='none'?'block':'none';
// e.returnValue=false
// e.preventDefault()
 return false;
}

// (c) http://xhtml.ru/2006/06/30/create-element/

// .appendChild(elem('div',{'class': 'myDivCSSClass', 'id': 'myDivId'},{'width': '200px', 'height': '250px', 'background': '#3B9D00', 'color': '#fff'},'текст текст текст текст текст текст текст текст'))
// .appendChild(elem('span',null,null,'текст текст текст текст'))
// .appendChild(elem('a',{'href':'http://site.com'},null,'текст текст текст текст'))
function elem(name, attrs, style, text, children) {
    var e=document.createElement(name);
    if (attrs) {
        for (key in attrs) {
            if (key == 'class') {
                e.className = attrs[key];
            } else if (key == 'id') {
                e.id = attrs[key];
            } else if (key == 'innerHTML') {
                e.innerHTML = attrs[key];
            } else if (key == 'colspan') {
                e.colSpan = attrs[key];
            } else {
                e.setAttribute(key, attrs[key]);
            }
        }
    }
    if (style) {
        for (key in style) {
            e.style[key] = style[key];
        }
    }
    if (text) {
        e.appendChild(document.createTextNode(text));
    }

    if (children) {
        for (key in children) {
            e.appendChild(children[key]);
        }
    }
    return e;
}


function tgl(id) {
gid(id).style.display=gid(id).style.display=='none'?'':'none';
}

function inv_checkbox(name_pref) {
 if (typeof name_pref=='undefined') name_pref='';
 c=document.getElementsByTagName('input');
 for (var i in c) 
    if ('checkbox'==c[i].type && !c[i].disabled && name_pref==c[i].name.substring(0,name_pref.length))
        c[i].checked=c[i].checked?false:true;
}

function valid_domain(h) {
 return /^([a-z0-9\-_]+[\.]{1})+([a-z]{2,4})$/i.test(h); 
}