var IE = !window.getComputedStyle;
var IE6 = IE && navigator.appVersion.indexOf('MSIE 7') == -1;
function __load(){
	set_height();
	init_catalog();
	init_news_tabs();
	init_main_menu();
	init_bottom_tabs();
	init_poll();
	init_auth();
	fix_png();
}
function set_height(){
	var mid = $('middle');
	if(!mid){
		return false;
	}
	var right = $('right-col');
	var left = $('left-col');
	if(left.offsetHeight < right.offsetHeight + right.offsetTop){
		mid.style.height = right.offsetHeight + right.offsetTop + 'px';
	}else{
		mid.style.height = 'auto';
	}
}
function init_catalog(){
	var root = $("catalog");
	if(!root){
		return false;
	}
	var items = $C("image", root, "DIV");
	if ( items ) {
		items.each(function(item){
			var img = $T("IMG", item, 1);
			if(Number(img.height) < item.offsetHeight){
				img.style.top = Math.floor((item.offsetHeight - Number(img.height)) / 2) + "px";
			}
		});
	}
}
function init_news_tabs(){
	var root = $("news-block");
	if(!root){
		return false;
	}
	root.contents = $C("content", root, "DIV");
	var items = $C("tab", root, "DIV");
	var level = new menu_level(root, items, {}, {
		item: [
			menu_level.HELP.iterators.default_click,
			{
				name: 'tabs',
				func: function(item){
					item.root = item.model.vars.root; 
					item.content = item.root.contents[item.index];	
					
				}
			}
		]
	}, {root: root});
	level.onselect.register('show content', function(item){
		item.content.addClass('shown');
	});
	level.ondeselect.register('hide content', function(item){
		item.content.removeClass('shown')
	});

}
function init_bottom_tabs(){
	var root = $("bottom");
	if(!root){
		return false;
	}
	root.contents = $C("content", root, "DIV");
	root.changing_div = $C("changing", root, "DIV", 1);
	var items = $C("tab", root, "DIV");
	var level = new menu_level(root, items, {}, {
		item: [
			menu_level.HELP.iterators.default_click,
			{
				name: 'tabs',
				func: function(item){
					item.root = item.model.vars.root;
					item.content = item.root.contents[item.index];	
				}
			}
		]
	}, {root: root});
	level.onselect.register('show content', function(item){
		item.content.addClass('shown');
		if(item.index == 0){
			item.model.vars.root.changing_div.removeClass('clt');
		}
	});
	level.ondeselect.register('hide content', function(item){
		item.content.removeClass('shown');
		if(item.index == 0){
			item.model.vars.root.changing_div.addClass('clt');
		}
	});

}
var hover_bgr;
function init_main_menu(){
	var root = $('main-menu');
	if(!root){
		return false;
	}
	var items = $C("item", root, "DIV");
	hover_bgr = $("hover-bgr");
	items.each(function(item){
		item.dropdown = $C('dropdown', item, 'DIV', 1);
		if(item.dropdown){
			item.onmouseover = function(){
				if(this.t_out || this.OVER){
					return false;
				}
				this.t_out = setTimeout(
					function(){
						this.OVER = true;
						this.addClass('over');
						hover_bgr.style.height = this.dropdown.offsetHeight + "px";
						hover_bgr.show(1);
					}.bind(this), 300
				);
			};
			item.onmouseout = function(e){
				var ev = window.event || e;
				var target = ev.relatedTarget || ev.toElement;
				if(target == this || _isChild(target, this)){
					return false;
				}
				clearTimeout(this.t_out);
				this.t_out = false;
				this.OVER = false;
				this.removeClass('over');
				hover_bgr.style.height = 0 + "px";
				hover_bgr.hide(1);
			};
		}
	});
}
function init_poll(){
	var root = $('poll');
	if(!root || root.id == ''){//greatest crap in IE
		return false;
	}
	var ttl = $C('title', root, 'DIV', 1);
	var span = $T('SPAN', ttl, 1);
	var content = $C('content', root, 'DIV', 1);
	if (!content) {
		return;
	}
	
	content._span = span;
	span.onclick = function(){
		var c = 'c-open';
		if(this.hasClass(c)){
			this.removeClass(c);
			this._span.removeClass('no-border');
		}else{
			this.addClass(c);		
			this._span.addClass('no-border');
		}
		set_height();
	}.bind(content);
}
function init_auth(){
	var root = $('auth');
	if(!root){
		return false;
	}
	root.conts = $C('c', root, 'DIV');
	if ( root.conts ) {
		if(root.conts.length < 2){
			return false;
		}
		root.conts.each(function(c){
			if(c.hasClass('c-here')){
				root.here = c;
				root.link = $T('A', root, 1);
				root.link.onclick = function(){
					root.here.removeClass('c-here');
					root.conts[1].addClass('c-here');
					return false;
				}.bind(root);
			}
			root.onclick = function(e){
				var ev = window.event || e;
				stopPropagation(ev);
			}
			document.onclick = function(){
				this.conts[0].addClass('c-here');
				this.conts[1].removeClass('c-here');
			}.bind(root);

		});
	}
}
function fix_png(){
	if(!IE6){
		return false;
	}
	var imgs = $T('IMG');
	if ( imgs ) {
		imgs.each(function(img){
			var name = img.nameProp;
			if(name.match('\.png$')){
				var src = img.src;
				img.style.width = img.width + 'px';
				img.style.height = img.height + 'px';
				img.src = img.src.replace(name, 'blank.gif');
				img.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="crop")';
			}
		});
	}
}
function wo(link,ww,hh,title) {
	w=window.open(link,'',(ww ? 'width='+ww+',' : '')+(hh ? 'height='+hh+',' : '')+'toolbar=0,scrollbars=0,resizable=yes');
	if (link.indexOf('.html')==-1) {
		if (document.layers) title='';
		w.document.open();
		w.document.write('<html><head><title>'+title+'</title><meta http-equiv=Content-Type content="text/html; charset=windows-1251"></head>');
		w.document.write('<body bgcolor=white marginwidth=0 marginheight=0 topmargin=0 leftmargin=0><table width=100% height=100% border=0 cellspacing=0 cellpadding=0><tr><td align=center><img src='+link+' width='+ww+' height='+hh+' border=0 alt=""></td></tr></table></body></html>');
		w.document.close();
	}
	w.focus();
}
AttachEvent(window, 'load', __load);
