/***  
	Copyright Raduk Alexey
	ohana2005@yandex.ru

***/
var console;
var js_title;
var recurssionLevel = 0;
var menuSpeed = 25;
var menu1_settings = {
	width: 330,
	height: 275
};
var menu0_settings = {
	width: 260,
	height: 170
};
var SECOND_MENU_SETTINGS = {
	width: 100, 
	height: 80
};
window.onload = function(){
	//init_tabs();
	init_currency();
	//console = $('console');
	//createMenu3($('menu1'), menu1_settings);
	//createMenu3($('menu2'), menu1_settings);
	//createMenu3($('menu0'), menu0_settings);
	init_js_title($("graph-block"));
	init_title_div();
	if(window.Zebra){
		Zebra();	
	}
}
function init_tabs(){
	var root = $('tabs');
	var content_div = $('news-content');
	if(!root)
		return;

	var items = $C('tab', root, 'DIV');
	var content_sets = $C('content-set', content_div, 'DIV');
	items.each(function(item, index){

		item.content = content_sets[index];
		item.opened = false;
		if(index == 0){
			item.cornerDiv = $C('c-left', content_div)[0];
		}
		if(item.className.indexOf('here') != -1){
			item.opened = true;
			if(item.cornerDiv){
				item.cornerDiv.className += " no-corner";
			}
			item.content.style.display = "block";
		}
		item.onclick = function(){
			if(this.opened)
				return;
			items.each(function(it){
				if(it.opened){
					it.className = it.className.replace('here', '');
					it.opened = false;
					it.content.style.display = "none";
					if(it.cornerDiv){
						it.cornerDiv.className = it.cornerDiv.className.replace('no-corner', '');
					}
				}
			});
			item.className += " here";
			item.opened = true;
			item.content.style.display = "block";
			if(item.cornerDiv){
				item.cornerDiv.className += ' no-corner';
			}
		}
	});
}
function init_currency(){
	var currency_root = $("currency");
	currency_root.stop = false;
	var root = $('move-item');
	if(!root)
		return;
	var btn = $('toggle-btn');
	root.mov = new Movement(root, 30);
	root.mov.setLimits('X', 280, 280);
	var limfunc = function(){this.stop();};
	root.mov.onleftlimitreach.register('left-limit', limfunc.bind(root.mov));
	root.mov.onrightlimitreach.register('right-limit', limfunc.bind(root.mov));
	btn.step = -8;
	root.mov.setSteps(btn.step, 0);
	var item1 = $("item1");
	btn.item = item1;
	btn._left1 = "188px";
	btn._left2 = "483px";
	btn._left = Style.getElementStyle(btn, "left");
	var item2 = $("item2");
	var btn2 = $('toggle-btn2');
	btn2._left = Style.getElementStyle(btn2, "left");
	btn2.item = item2;
	btn2._left1 = "198px";
	btn2._left2 = "493px";
	currency_root.move_count = 0;
	btn.onclick = function(){
		root.mov.onstop.register("rearrange", function(btn){
			setTimeout(
				function(){
					btn._neighbour_button.style.left = btn._neighbour_button._left1;
					btn.style.left = btn._left2;
					this.appendChild(btn.item); 
					this.mov.Moveable.moveBy(280, 0);
				}.bind(this),
				1
			);
		}.bind(root, this), 1);
		if(root.mov.isMoving == false){
			root.mov.start();
		}
		currency_root.move_count++;
	}
	btn._neighbour_button = btn2;
	btn2._neighbour_button = btn;
	btn2.onclick = btn.onclick.bind(btn2);
	currency_root.buttons = [btn, btn2];

	currency_root.int_func = function(){
		if(!this.stop){
			this.buttons[this.move_count % 2].onclick();
		}
	}.bind(currency_root)
	currency_root.interval = setInterval(currency_root.int_func, 7000);
	currency_root.onmouseover = function(){
		this.stop = true;
	}
	currency_root.onmouseout = function(){
		this.stop = false;
	}
}
function init_menu_hover(item){
	var next_btn = document.createElement('DIV');
	item.link = $T('A', item)[0];
	next_btn.className = 'forward-button';
	next_btn.style.left = item.link.offsetWidth + 10 + "px";
	item.style.width = item.link.offsetWidth + 33 + "px";
	item.style.position = "relative";
	item.appendChild(next_btn);
	item.next_btn = next_btn;
	item.next_btn.parent = item;
	item.onmouseover = function(){
		this.next_btn.style.display = "block";
	}
	item.onmouseout = function(){
		this.next_btn.style.display = "none";
	}
}
function create_back_item(root, index){
	var b_holder = document.createElement('DIV');
	b_holder.className = 'back-item';
	b_holder.style.left = 26 * index + "px";
	b_holder.style.top = 22 * index + 'px';
	root.appendChild(b_holder);
	root['back_button' + index] = b_holder;
	create_back_button(b_holder);
	create_link_holder(b_holder);
}
function create_back_button(parent){
	parent.button = document.createElement('DIV');
	parent.button.className = 'back-button';
	parent.appendChild(parent.button);
}
function create_link_holder(parent){
	parent.link_holder = document.createElement('DIV');
	parent.link_holder.className = 'link-holder';
	parent.appendChild(parent.link_holder);
}
function buildMenuLevel(elem, namespace){
	namespace.recLevel ++;
	namespace.menuDepth = namespace.recLevel;
	if(elem.className){
		elem.className += " menu-level" + namespace.recLevel;
	}else{
		elem.className = "menu-level" + namespace.recLevel;
	}
	var biggest_width = 0;
	var items = $A(elem.childNodes);
	items.each(function(item){
		if(item.nodeName.toUpperCase() == "LI"){
			var lnk = $T('A', item)[0];
			if(lnk.offsetWidth > biggest_width){
				biggest_width = lnk.offsetWidth;
			}
			var children = $A(item.childNodes);
			children.each(function(child){
				if(child.nodeName.toUpperCase() == 'DIV' && child.className == 'm-item'){
					init_menu_hover(item, item);
					item.menuItem = child;
					item.level = namespace.recLevel;
					item.link = $T('A', item)[0];
					if(namespace.recLevel > 0){
						if(typeof namespace.levels[item.level] == 'undefined'){
							namespace.levels[item.level] = [];
						}
						namespace.navigation[item.level - 1] = item.link.cloneNode(true);
						namespace.levels[item.level].push(child);
						var padding_left_value = 0;
						var padding_top_value = 0;
						for(var i = 0; i < namespace.recLevel; i++){
							create_back_item(child, i);
							child['back_button' + i].button.level = i;
							child['back_button' + i].button.onclick = function(objRoot){
								objRoot.mov.moveTo(-objRoot.WIDTH_VAL * this.level, 0, menuSpeed * (item.level - this.level));

							}.bindAvoidingEvent(child['back_button' + i].button, namespace);
							child['back_button' + i].link_holder.appendChild(namespace.navigation[i].cloneNode(true));
							try{
								child['back_button' + i].image = $T("IMG", child['back_button' + i].link_holder)[0];
								child['back_button' + i].image.src = child['back_button' + i].image.src.replace(/\.png$/, "_here.png");
							}catch(e){
								//							
							}
							padding_left_value += 27;
							padding_top_value += 20;
						}
						if(padding_left_value){
							padding_left_value += 15;
						}
						child.style.position = "absolute";
						child.style.top = "0";
						child.style.visibility = "hidden";
						child.style.left = namespace.WIDTH_VAL * namespace.recLevel + "px";
						child.style.width = namespace.WIDTH_VAL - padding_left_value + "px";
						child.style.paddingLeft = padding_left_value + "px";
						child.style.height = namespace.HEIGHT_VAL + "px";
						child.ul = $T('UL', child)[0];
						child.ul.style.paddingTop = padding_top_value + "px";
						child.parentNode.removeChild(child);
						namespace.movItem.appendChild(child);
					}
					item.next_btn.onclick = function(objRoot){
						objRoot.levels[this.level].each(function(it){
							it.style.visibility = "hidden";
						});
						objRoot.navigation[item.level - 1] = item.link.cloneNode(true);
						this.menuItem.style.visibility = "visible";
//						this.parentNode.style.border = "solid 1px red";
						objRoot.mov.moveTo(
							-objRoot.WIDTH_VAL * this.level, 
							0, 
							menuSpeed);
					}.bindAvoidingEvent(item, namespace)
					buildMenuLevel(child.ul, namespace);
				}
			})
		}
	});
	elem.menu_width = biggest_width;
	namespace.recLevel --;
}
function createMenu3(root, settings){
	if(!root){
		return;
	}
	var temp_var;
	temp_var = $C('zaglushka', root, 'DIV');
	root.zaglushka =  typeof temp_var[0] != 'undefined' ? temp_var[0] : false;
	var temp_zaglushka = $C('zaglushka2', root, 'DIV');
	if(temp_zaglushka){
		root.temp_zaglushka  = temp_zaglushka[0];
	}
	if(root.zaglushka){
		create_zaglushka(root);
	}

	root.movItem = $C('move-item', root, 'DIV')[0];
	root.menuRoot = $C('menu-root', root, 'UL')[0];
	root.recLevel = 0;
	root.menuDepth = 0;
	root.WIDTH_VAL = settings.width;
	root.HEIGHT_VAL = settings.height;
	root.navigation = [];

	root.mov = new Movement(root.movItem, 50);
	root.levels = [[root.menuRoot]];


	buildMenuLevel(root.menuRoot, root);
	if(root.temp_zaglushka){
		root.temp_zaglushka.style.display = "none";
	}
}
function create_zaglushka(root){
	root.status = 0;
	root.t_out = {};
	root.t_out_val = 300;
	root.onmouseover = function(){
		if(this.status == 2)
			return;
		this.status = 1;//initialized
		this.t_out = setTimeout(function(){
			this.zaglushka.style.display = "none";
			this.status = 2;//displayed
		}.bind(this), this.t_out_val);
	}
	root.onmouseout = function(e){
		var ev = window.event || e;
		var target = ev.toElement || ev.relatedTarget;
		var inside = (_isChild(target, this) || target == this);
		if(!inside){
			if(this.status == 1){
				clearTimeout(this.t_out);
				this.status = 0;
				return;
			}
			setTimeout(function(){
				this.zaglushka.style.display = "block";
				this.zaglushka.style.height = 372 + "px";
				this.status = 0;
			}.bind(this), this.t_out_val);
		}
	}
}
function init_title_div(){
	js_title = $('js-title');
	js_title.content = $C("content", js_title, "DIV")[0];
	js_title.span = document.createElement("SPAN");
	Style.setElementStyle(js_title.span, {position: "absolute", left: -1000, top: -1000}, null, true);
	document.body.appendChild(js_title.span);
	js_title.opacity = new Opacity(js_title, 50);
	js_title.opacity.onafterfade.register("alert", function(){
		this.style.display = "none";
	}.bind(js_title));
	document.onmousemove = function(e){
		var ev = window.event || e;
		js_title.style.left = ev.clientX + 15 + "px";
		js_title.style.top = ev.clientY + 10 +"px";
	}
}
function init_js_title(trigger_elem){
	trigger_elem._title = trigger_elem.title;
	trigger_elem.title = "";
	trigger_elem.t_out = null;
	trigger_elem.onmouseover = function(e){
		if(this.t_out){
			return false;
		}
		js_title.content.innerHTML = this._title;
		js_title.span.innerHTML = this._title;
		js_title.style.width = js_title.span.offsetWidth + 10 + "px";
		this.t_out = setTimeout(
			function(){
				js_title.style.display = "block";
				if(js_title.opacity){
					js_title.opacity.setOpacity(0);
					js_title.style.visibility = "visible";
					js_title.opacity.appear(100, 10);
				}else{
					js_title.style.visibility = "visible";
				}
			}, 500
		);
	}
	trigger_elem.onmouseout = function(e){
		clearTimeout(this.t_out);
		this.t_out = null;
		if(js_title.opacity){
			js_title.opacity.fade(0, 10)
		}else{
			js_title.style.display = "none";
			js_title.style.visibility = "hidden";
		}
	}
}
