window.onload = function(){
	init_left_menu();
	init_popup();
	//init_flash_coords();
	init_scroll_regions();
	init_scroll_production();
	//init_photo_production();
	init_tbl();
	
	init_scroll_small_pic();


}

function ShowLangPanel( ) {

	if ( $('lang-menu').style.display == 'none' ) {
		$('lang-menu').style.display = 'block';
	}
	else {
		$('lang-menu').style.display = 'none'
	}

}


var tim = new Timer(50);
tim.start();
var mov_speed = 25;
var popup_div;
function init_left_menu(){

	var root = $("left-menu");
	root.mov_div = $("mov-div");
	root.mov_plashka = $("blue-plashka");
	root.submenu = $("submenu");
	root.submenu_root = $("submenu-root");
	root.city_object = $("city-object") || $("map-object");
	root.city_object_link = $("city-object-link");
	root.news_block = $("news-block");
	root.STATE = 0;//closed
	root.mov = new Movement(root.mov_div, tim);
	root.currentItem = null;
	root.mov.onstop.register("change start", function(){
		if(this.STATE == 1){
			this.STATE = 2;
		}else if(this.STATE == 3){
			this.show_city_object()
			this.STATE = 0;
		}
	}.bind(root));
	root.items = $C("m-item", root, "DIV");
	root.items.each(function(item){
		item.root = root;
		item.link = $T("A", item, true);
		item.dropdown = $C("dropdown", item, "DIV", true);
		if(item.dropdown){
			item.submenu_items = $C("s-menu-item", item.dropdown, "DIV");
		}
		item.hover = $C("hover", item, "DIV", true);
		item.over = function(){
			this.className += " over";
			if(this.root.t_out){
				clearTimeout(this.root.t_out);
				this.root.t_out = null;
			}
			if(!this.submenu_items){
				this.root.hide();
				return false;
			}
			clearElem(this.root.submenu_root);
			this.submenu_items.each(function(s_item){
				this.root.submenu_root.appendChild(s_item.cloneNode(true));
			}.bind(this))
			this.root.currentItem = this;
			this.root.show();
		}
		item.out = function(){
			this.className = this.className.replace(/over/gi, "");
			if(this.root.here_item == this){
				return false;
			}
			this.root.currentItem = null;
			this.root.t_out = setTimeout(function(){
				if(this.root.here_item){
					if(this.root.here_item.submenu_items){
						this.root.here_item.over();
					}else{
						this.root.here_item.over();
						this.root.hide();
					}
				}else{
					this.root.hide();
				}
			}.bind(this), 100);
		}
		item.onmouseover = function(e){
			if(this.root.here_item){
				if(this.root.here_item == this){
					return false;
				}else{
					this.root.here_item.out();
				}
			}
			this.over();
		}.bind(item);
		item.onmouseout = function(e){
			if(this.root.here_item == this){
				return false;
			}
			var ev = window.event || e;
			var target = ev.toElement || ev.relatedTarget;
			if(target == this || _isChild(target, this) || target == this.root.mov_plashka || _isChild(target, this.root.mov_plashka)){
				return false;
			}
			this.out();
		}.bind(item);
	});
	root.mov_plashka.onmouseout = function(e){
		if(this.here_item == this.currentItem){
			return false;
		}
		var ev = window.event || e;
		var target = ev.toElement || ev.relatedTarget;
		if(target == this.mov_plashka || _isChild(target, this.mov_plashka)){
			return false;
		}
		this.currentItem.out();
	}.bind(root);
	root.show = function(){
		this.STATE = 1;//IS_SHOWING
		this.mov.moveTo(0, this.mov.moveable.top, mov_speed);
		this.hide_city_object();
	}
	root.hide = function(){
		this.STATE = 3;//IS_HIDING
		this.mov.moveTo(-420, this.mov.moveable.top, mov_speed);
	}
	root.hide_city_object = function(){
		if(this.city_object){
			this.city_object.style.display = "none";
			this.city_object_link.style.display = "none";
		}
		this.submenu.style.zIndex = 3;
	}
	root.show_city_object = function(){
		if(this.city_object){
			this.city_object.style.display = "block";
			this.city_object_link.style.display = "block";
		}
		this.submenu.style.zIndex = 1;
	}
	root.items.each(function(item){
		if(item.className.indexOf("here") != -1){
			root.here_item = item;
			item.over();
			throw $break;
		}
	})
}
function init_popup(){
	var root = $("popup");
	if(!root){
		return false;
	}
	root.dims = Style.$D(root);
	root.img = $T("IMG", root, true);
	root.d_city = $("popup-city");
	root.d_distance = $("popup-distance");
	root.onclick = hide_me;
	popup_div = root;
}
function init_flash_coords(){
	var root = $("big-flash");
	if(!root){
		return false;
	}
	root.dims = Style.$D(root);
	root._left = parseInt(Style.$(root, "left"));
	var coords = $C("flash-coords", null, "DIV");
	coords.each(function(item){
		item.dims = Style.$D(item);
		get_offset(item, root);
	});
	flash_coords.each(function(f_c){
		coords.each(function(item){
			if(f_c.x >= item.offset_left && f_c.x < item.offset_left + item.dims.width){
				insertCoord(f_c, item);
				throw $break;
			}
		});
	});
}
function get_offset(item, root){
	item.offset_top = root.dims.height - item.dims.height;
	item.offset_left = parseInt(Style.$(item, "left")) - root._left;
}
function insertCoord(obj, item){
	var c = document.createElement("DIV");
	c.parent = item;
	c.className = "coord";
	c._data = obj;
	c._left = obj.x - item.offset_left;
	c._top = obj.y - item.offset_top;
	c.style.left = c._left + "px";
	c.style.top = c._top + "px";
	c.onclick = function(){
		show_popup(this);
	}
	item.appendChild(c);
}
function show_popup(coord){
	coord.parent.appendChild(popup_div);
	popup_div.style.display = "block";
	popup_div.style.left = coord._left - Math.floor(popup_div.dims.width / 2) + 25 + "px";
	popup_div.style.top = coord._top - popup_div.dims.height + "px";
	popup_div.img.src = coord._data.src;
	popup_div.d_city.innerHTML = coord._data.city;
	popup_div.d_distance.innerHTML = coord._data.distance;
}
function hide_me(){
	this.style.display = "none";
}
function init_scroll_regions(){

	var root = $("regions");
	if(!root){
		return false;
	}
	var bar = $("scrollbar");
	var arr = $C_arr(["minus", "plus", "track", "thumb"], bar, "DIV");
	root.scroll = new Scroll(
		$("scroll-holder"), $("scroll-content"),
		{
			X: null, 
			Y: {
				bar: bar,
				bMinus: arr.shift(),
				bPlus: arr.shift(),
				track: arr.shift(),
				thumb: arr.shift()
			}
		}, tim, {speed: 10}
	);
	
	root.regions = $C("region", root, "DIV");
	root.regions.each(function(r){
		r.OPEN = r.className.indexOf("open") != -1;
		r._title = $C("title", r, "DIV", true);
		r.ontoggle = new DOMEvent(root);
		r.open = function ( ) {
			
			root.regions.each( 
				function ( item ) {
					item.close( );
				}
			);
			
			this.OPEN = true;
			this.className += " open";
			
		}
		r.close = function(){
			this.OPEN = false;
			this.className = this.className.replace("open", "");		
		}
		r.toggle = function(){
			if(this.OPEN){
				this.close();	
			}else{
				this.open();			
			}
			this.ontoggle.fire();
		}
		r.ontoggle.register("refresh", function(){
			this.scroll.refresh();
		});
		r._title.onclick = r.toggle.bind(r);
	})
}

scroll_to_here_production = function ( ) {
	
	var content = $C("here", $("scroll-content"), "DIV");
	var root = $("scrollbar");

	content.each( function( item ) {
		if( item.offsetTop) {
			content.oTop = item.offsetTop;
			content.oHeight = item.offsetHeight;
		}
	})

	return (root.offsetHeight < ( content.oTop + content.oHeight) ) ? content.oTop : 0;
	
}

function init_scroll_production(){
	
	var root = $("scrollbar");
	var production = $("production");
	
	if(!root){
		return false;
	}
		
	var bar = $("scrollbar");
	var arr = $C_arr(["minus", "plus", "track", "thumb"], bar, "DIV");
	root.scroll = new Scroll(
		$("scroll-holder"), $("scroll-content"),
		{
			X: null, 
			Y: {
				bar: bar,
				bMinus: arr.shift(),
				bPlus: arr.shift(),
				track: arr.shift(),
				thumb: arr.shift()
			}
		}, tim, {speed: 10}
	);
	
	var offsetTop = scroll_to_here_production( );
	if ( offsetTop ) {
		root.scroll.moveContentTo(0, offsetTop);
	}
	if ( production ) {
		production.style.visibility = 'visible';
	}

}

function init_photo_production(){
	var root = $("production-photos");
	if(!root){
		return false;
	}
	root.small_items = $C("item", $("small-photos"), "DIV");
	root.big_image = $T("IMG", $("big-photo"), true);
	root.here_item = {deselect: function(){}};
	root.small_items.each(function(item){
		item.root = root;
		item.HERE = item.className.indexOf("here") != -1;
		item.image = $T("IMG", item, true);
		item.image.extension = item.image.src.substr(item.image.src.lastIndexOf("."));
		item.image.reg = new RegExp(item.image.extension + "$");
		item.image.f_name = item.image.src.substr(item.image.src.lastIndexOf("\/") + 1).replace(item.image.reg, "").replace(/-here$/, "");
		item.image.index = item.image.f_name.substr(item.image.f_name.lastIndexOf("-") + 1);
		item.image.here_reg = new RegExp("-here" + item.image.extension + "$");
		item.select = function(){
			this.HERE = true;
			this.root.here_item.deselect();
			this.className += " here";
			var img = this.image;
			//img.src = img.src.replace(img.reg, "-here" + img.extension);
			this.root.here_item = this;
			this.root.big_image.load(img);
		}
		item.deselect = function(){
			this.HERE = false;
			this.className = this.className.replace("here", "");
			var img = this.image;
			//img.src = img.src.replace(img.here_reg, img.extension);
		}
		item.onclick = item.select;
		if(item.HERE){
			root.here_item = item;
		}
	});
	root.big_image.load = function(img){
		this.src = this.src.replace(this.src.substr(this.src.lastIndexOf("\/") + 1), img.index + "-big.jpg");
	}
}
function init_tbl(){
	var tbls = $C("tbl", $("text-content"), "TABLE");
	tbls.each(function(table){
		var rows = $T("TR", table);
		rows.each(function(row, index){
			if(index % 2){
				row.className = "zebra";
			}
		})
	})
}






//////////////////////// POPUP на главной /////////////////////////////
function ShowPopup( x, y, name, image, distance ) {
							
	var displY = 235;
	var displX = 92;
	
	document.getElementById( 'popup-image' ).src =  '{root}' + image;
	document.getElementById( 'popup-city' ).innerHTML =  name;
	document.getElementById( 'popup-distance' ).innerHTML =  distance;
	
	var popup = document.getElementById( 'popup' );
	
	popup.style.left = (x - displX) + 'px';
	popup.style.top = (y - displY) + 'px';
	popup.style.display = 'block';
	
}

function showPopupCity( numDot, coordX, coordY ) {
	
	return;
	
	var cities = xmlCityObject.getElementsByTagName('city');
	
	if ( cities.length ) {
	
		for ( var i = 0; i < cities.length; i++ ) {
			
			if ( cities[i].getAttribute( 'id' ) == numDot ) {
				var city = cities[i];
				break;
			}
			
		}
		
		if ( !city ) return;
		
		if ( city ) {
			
			var image = city.getElementsByTagName('photo')[0].firstChild.nodeValue;
			var name = city.getElementsByTagName('name')[0].firstChild.nodeValue;
			var distance = city.getElementsByTagName('distance')[0].firstChild.nodeValue;
			
			ShowPopup( coordX, coordY, name, image, distance );
		}
		
	}
	
}

function init_scroll_small_pic ( ) {
	
	var root = $("scrollbars");
	//var production = $("production");
	
	if(!root){
		return false;
	}
			
	var bar = $("scrollbars");
	var arr = $C_arr(["minus", "plus", "track", "thumb"], bar, "DIV");
	
	root.scroll = new Scroll(
		$("small-holder"), $("small-content"),
		{
			X: null, 
			Y: {
				bar: bar,
				bMinus: arr.shift(),
				bPlus: arr.shift(),
				track: arr.shift(),
				thumb: arr.shift()
			}
		}, tim, {speed: 10}
	);
	
	return;
	
	var offsetTop = scroll_to_here_production( );
	if ( offsetTop ) {
		root.scroll.moveContentTo(0, offsetTop);
	}
	if ( production ) {
		production.style.visibility = 'visible';
	}

}
