var m_max_width = 150;
var IE = !window.getComputedStyle && !!document.getElementById;
var IE6 = IE && navigator.appVersion.indexOf('MSIE 7') == -1;
window.onload = function(){
	init_hover();
	init_left();
	init_tables();
	init_menu2();
	init_weather();
	init_news();

}
function init_hover(){
	var root = $("top-menu");
	var items = $C("m-item", root, "DIV");
	items.each(function(item){
		item.link = $T("A", item)[0];
		var arr = $C_arr(["hover-pad", "center-bgr", "dropdown"], item, "DIV");
		item.pad = arr.shift();
		item.pad.bgr = arr.shift();
		item.pad.style.width = item.link.offsetWidth + "px";
		item.pad.bgr.style.height = item.link.offsetHeight - 6 + "px";

		item.dropdown = arr.length ? arr.shift() : null;
		if(item.dropdown){
			item.dropdown.links = $T("A", item.dropdown);
		}
		item.onmouseover = function(){
			this.className += " over";
			if(item.dropdown && !item.dropdown._WIDTH_INITIALIZED){
				var m_width = 0;
				item.dropdown.links.each(function(link){
					if(link.offsetWidth > m_width){
						m_width = link.offsetWidth;
					}
					if(m_width >= m_max_width){
						m_width = m_max_width;
						link.parentNode.style.width = m_width + "px";
						link.parentNode.style.whiteSpace = "normal";
					}
				});
				item.dropdown.style.width = m_width + 20 + "px";
				item.dropdown._WIDTH_INITIALIZED = true;
			}
		}
		item.onmouseout = function(){
			var reg = /over/gi;
			this.className = this.className.replace(reg, "");				
		}
	})
}
function init_left(){
	var root = $("press-center");
	if(!root){
		return false;
	}
	var _here = $C("here", root, "LI");
	if(!_here){
		return false;
	}
	_here = _here[0];
	if(_here.offsetHeight > 31){
		_here.className += " two-line";
	}
}
function init_tables(){
	var root = $("text-content");
	if(root){
		if(tables){
			var tables = $C("tbl", root, "TABLE");
			tables.each(function(tbl){
				var trs = $T("tr", tbl);
				var last_tr = trs.last();
				var first_tr = trs[0];
				if(first_tr.className.indexOf("theader") != -1 || $C("theader", first_tr, "TD")){
					var sec_tr = trs[1];
					sec_tr.className = "second";
				}
			});
		}
		/*if(IE6){
			var tbls = $T('TABLE', root);
			if(tbls){
				tbls.each(function(tbl){
					if(tbl.offsetWidth > 600){
						tbl.style.marginLeft = -200 + 'px';
						setTimeout(function(){
							if(this.offsetLeft == -200){
								this.style.position = 'relative';
								this.style.left = 200 + 'px';
							}							
						}.bind(tbl), 500);
					}
				});
			}
		}*/
	}
}
function init_menu2(){
	var root = $("top-menu2");
	if(!root){
		return false;
	}
	var pre_menu = $("top-menu");
	pre_menu.style.paddingTop = "15px";
	root.style.paddingBottom = "24px";
}
//weather
var t_today, t_tomorrow;
function init_weather(){
	var root = $("weather");
	if(!root || root.className == "cursi-valyt"){
		return false;
	}
	var d_span = $("weather-date");
	var toggle = $("weather-toggle");
	t_today = $("weather-today");
	t_tomorrow = $("weather-tomorrow");
	toggle._today = true;
	toggle.onclick = function(){
		clearElem(d_span);
		clearElem(this);
		var show_tbl, span_val, link_val;
		if(this._today){
			span_val = _tomorrow;
			link_val = "Сегодня";
			t_tomorrow.style.display = "inline";
			t_today.style.display = "none";
		}else{
			t_tomorrow.style.display = "none";
			t_today.style.display = "inline";
			span_val = _today;
			link_val = "Завтра";
		}
		d_span.appendChild(document.createTextNode(span_val));
		this.appendChild(document.createTextNode(link_val));
		this._today = !this._today;
		return false;
	}
}
function submitForm(){
	var par = this.parentNode;
	while(par.nodeName.toUpperCase() != "FORM" && par.nodeName.toUpperCase() != "HTML"){
		par = par.parentNode;
	}
	if(par.nodeName.toUpperCase() == "HTML"){
		return false;
	}
	if(par.onsubmit){
		var s = par.onsubmit();
		if(typeof s == "undefined" || s){
			par.submit();
		}
	}else{
		par.submit();
	}
}
function init_news(){
	var root = $('inside-news');
	if(!root){
		return false;
	}
	var items = $C('image', root, 'DIV');
	items.each(function(item){
		var imgs = $T('IMG', item);
		var img;
		imgs.each(function(i){
			if(i.src.indexOf('no-photo.gif') == -1){
				img = i;
			}
		});
		img.style.position = 'relative';
		var top = 2;
		if(!IE){
			top = 3;
		}
		if(window.opera){
			top = 4;
		}
		img.style.top = top + 'px';
	});
}
