var babas2 = [];
var interval = 3.7;
var foto_width = 250;
var foto_height = 375;
var selects = [];

window.onload = function(){
	init_menu();
	init_vip();
	init_right();
	init_foto_items();
	init_news_list();
	init_popup();          

	if(IE6){
		ie6_fix_register();
		ie6_fix_userdata();
		ie6_fix_inputs();
		selects = $T("SELECT");
	}
	init_thumbs();
};
function init_menu(){
	var root = $("top-menu");
	if(!root){
		return false;
	}
	var items = $C("item", root, "DIV");
	var total_width = 0;
	items.each(function(item){
		if(item.parentNode == root){
			total_width += item.offsetWidth;
		}
	});
	items.each(function(item){
		if(item.parentNode == root){
			if(IE6){
				item.onmouseover = function(){
					this.addClass("over");
				}.bind(item);
				item.onmouseout = function(){
					this.removeClass("over");
				}.bind(item);
			}
			item.dropdown = $C("dropdown", item, "DIV", 1);
			if(item.dropdown){
				item.dropdown.items = $C("item", item.dropdown, "DIV");
				var w = 0;
                if (item.dropdown.items){
				    item.dropdown.items.each(function(it){
					    w += it.offsetWidth;
				    });
				    item.dropdown.style.width = w + "px";
				    if(item.offsetLeft + item.dropdown.offsetWidth >= 800){
					    item.dropdown.style.left = -item.dropdown.offsetWidth + item.offsetWidth + "px";
				    }
                }
			}
		}
	});
}
function init_vip(){
	var root = $("vip-shit");
	if(!root){
		return false;
	}
	root.items = $C("item", root, "DIV");
	if(root.hasClass("js-main")){
		root.conts = $C("cont", root, "DIV");
		root.items.each(function(item, ind){
			var type = item.className.split(" ").last().split("-").last();
			item._cont = root.conts.detect(function(c){
				return c.className.split(" ").last().split('-').last() == type;
			});
			item.root = root;
			item.onmouseover = function(){
				if(this.root.here_item){
					this.root.here_item.removeClass("over");
					this.root.here_item._cont.removeClass("s-over");
				}
				this.addClass("over");
				this._cont.addClass("s-over");
				this.root.here_item = this;
			};
			if(!ind){
				item.onmouseover();
			}
		});
	}else{
		root.items.each(function(item){
			item.root = root;
			item.links = $T("A", item);
			item.text_div = $C("texto", item, "DIV", 1);
			item.text_div.style.top = Math.floor((item.offsetHeight - item.text_div.offsetHeight) / 2) + "px";
			item.links.each(function(link){
				link.onmouseover = function(){
					this.root.items.each(function(it){
						it.removeClass("over");
						if(it.t_out){
							clearTimeout(it.t_out);
						}
					});
					this.addClass("over");
				}.bind(item);
				link.onmouseout = function(){
					this.t_out = setTimeout(function(){
						this.removeClass("over");
					}.bind(this), 500);
				}.bind(item);
			});
		});
	}
}
function init_right(){
	var root = $("right-col") || $("right-col2");
	if(!root){
		return false;
	}
	var h = root.offsetHeight + 7;
	var par = root.parentNode;
	if(IE6){
		par.style.height = h + "px";
	}else{
		par.style.minHeight =  h + "px";
	}
}
var foto_items_root;
function init_foto_items(){
	var root = $("foto-items");
	window.onresize = function(){
		if(window.foto_items_root){
			place_foto_items(foto_items_root);
		}
		if(window.popup){
			popup.place();
		}
		if(IE6){
			ie6_place_bgr(registration);
		}
	};
	if(!root){
		return false;
	}
	place_foto_items(root);
	foto_items_root = root;
}
function place_foto_items(root){
	if(!root.lines){
		root.lines = $C("line", root, "DIV");
	}
	root.lines.each(function(line){
		line.i_width = 0;
		if(!line.items){
			line.items = $C("item", line, "DIV");
		}
		line.items.each(function(item){
			line.i_width += item.offsetWidth;
			if(item == line.items.last()){
				item.IS_LAST = true;
			}
		});
		var margin = Math.floor((line.offsetWidth - line.i_width) / (line.items.length - 1));
		line.items.each(function(item){
			if(!item.IS_LAST){
				item.style.marginRight = margin + "px";
			}
		});
	});
}
function init_news_list(){
	var root = $("news-list");
	if(!root){
		return false;
	}
	root.items = $C("item", root, "DIV");
	root.items[0].addClass("open"); //added by j_man
	root.items.each(function(item){
		item.root = root;
		if(item.hasClass("open")){
			root.here_item = item;
		}
		item._link = $T("A", $C("link", item, "DIV", 1), 1);
		item._link.onclick = function(){
			if(this.root.here_item){
				this.root.here_item.removeClass("open");
			}
			this.addClass("open");
			this.root.insertBefore(this, this.root.firstChild);
			this.root.here_item = this;
		}.bind(item);
	});
}
var popup, zaglushka, min_height, footer;
var sroot;
var popup_button;
var ie6_height_div;
function init_popup(){
	var root = $("popup");
	if(!root){
		return false;
	}
	popup = root;
	zaglushka = $("zaglushka");
	min_height = $("min-height");
	footer = $("footer");
	zaglushka.style.height = min_height.offsetHeight + footer.offsetHeight + "px";
	popup.style.width = foto_width + 20 + "px";
	popup.style.height = foto_height + 40 + "px";
	sroot = $T("HTML", null, 1);
	popup_button = $("watch");
	Object.extend(popup, popup_functions);
	popup_button.onclick = popup.show.bind(popup);

	popup._img = $T("img", popup, 1);
	popup._links = $T("A", popup);
	popup.div_name = $C("name", popup, "DIV", 1);
	popup.b_close = $C("button", popup, "DIV", 1);
	popup.b_close.onclick = popup.close.bind(popup);
	window.onscroll = function(){
		popup.place();
	};
	if(IE6){
		ie6_height_div = document.createElement("DIV");
		ie6_height_div.className = "ie6-height";
		document.body.appendChild(ie6_height_div);
	}
}

var popup_functions = {
	show: function(){
		this.style.display = "block";
		zaglushka.style.display = "block";
		this.place();
		next_baba();
		this.interval = setInterval(
			next_baba, interval * 1000
		);
		if(IE6){
			selects.each(function(sel){
				sel.style.visibility = "hidden";
			});
		}
	},
	place: function(){
		var iw = IE ? document.body.clientWidth : window.innerWidth;
		var scrollLeft = IE ? sroot.scrollLeft : window.pageXOffset;
		var scrollTop = IE ? sroot.scrollTop : window.pageYOffset;
		var ih = IE ? document.body.clientHeight : window.innerHeight;

		if(IE6){
			ih = ie6_height_div.offsetHeight;
		}
		
		var deltaX = iw - this.offsetWidth;
		var deltaY = ih - this.offsetHeight;
		if(deltaY < 0){
			this.style.top =  scrollTop + "px";
		}else{
			this.style.top = Math.floor(deltaY / 2) + scrollTop + "px";
		}
		if(deltaX < 0){
			this.style.left = scrollLeft + "px";
		}else{
			this.style.left = Math.floor(deltaX / 2) + scrollLeft + "px";		
		}
	},
	close: function(){
		this.style.display = "none";
		zaglushka.style.display = "none";
		clearInterval(this.interval);
		if(IE6){
			selects.each(function(sel){
				sel.style.visibility = "visible";
			});
		}
	}
}
function next_baba(){
	if(!babas.length){
		babas = babas2.clone();
		babas2 = [];
	}
	var baba = babas.random(1);
	babas2.push(baba);
	//var img_name = popup._img.src.substr(popup._img.src.lastIndexOf("\/") + 1);
	//popup._img.src = popup._img.src.replace(img_name, baba.photo);
	popup._img.src = baba.photo;
	popup._links.each(function(link){
		link.href = baba.link;
	});
	clearElem(popup.div_name);
	popup.div_name.app(Text(baba.name));
}
var registration, user_data;
function ie6_fix_register(){
	var root = $("registration");
	if(!root){
		return false;
	}
	var bgr_div = document.createElement("DIV");
	bgr_div.className = "ie6-bgr";
	root.appendChild(bgr_div);
	root.bgr_div = bgr_div;
	ie6_place_bgr(root);
	registration = root;	
}
function ie6_fix_userdata(){
	var root = $("user-data");
	if(!root || !root.hasClass('js-fix')){
		return false;
	}
	var bgr_div = document.createElement("DIV");
	bgr_div.className = "ie6-bgr";
	root.appendChild(bgr_div);
	root.bgr_div = bgr_div;
	ie6_place_bgr(root);
	user_data = root;	
}
function ie6_place_bgr(root){
	if(!root){
		return false;
	}
	root.bgr_div.style.width = root.offsetWidth + "px";
	root.bgr_div.style.height = root.offsetHeight + "px";
}
function ie6_fix_inputs(){
	var ips = $I("image");
	if(ips){
		ips.each(function(ip){
			if(ip.src.match(/\.png$/)){
				var src = ip.src;
				var imgName = src.substr(src.lastIndexOf('\/') + 1);
				ip.style.width = ip.offsetWidth + "px";
				ip.style.height = ip.offsetHeight + "px";
				ip.src = ip.src.replace(imgName, 'blank.gif');
				ip.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="crop")';
			}
		});
	}
}
function init_thumbs(){
	var root = $("thumbs");
	if(!root){
		return false;
	}
	root.thumbs = $C("thumb", root, "DIV");
	root.big_imag = $("big-image");
	root.thumbs[0].addClass('here');
	root.thumbs.each(function(th){
		th.link = $T("A", th, 1);
		if(th.link.hasClass('empty')){
			throw $continue;
		}
		th.root = root;
		th.img = $T("IMG", th, 1);
		th.link.onclick = function(){
			if(this.hasClass('here')){
				return false;
			}
			if(this.root.here_item){
				this.root.here_item.removeClass("here");
			}
			this.addClass('here');
			this.root.here_item = this;
			this.root.big_imag.src = this.img.src.replace('/thumb', '');
			return false;
		}.bind(th);
		if(th.hasClass("here")){
			root.here_item = th;
		}
	});
}