/***  
	Copyright Raduk Alexey
	ohana2005@yandex.ru

***/
var _mutual_timer = new Timer(30);
_mutual_timer.start();
var js_title;
var objAjax;
var _html;
var global_here = location.href.replace(location.hash, '').replace(URLroot, '');
window.onload = function(){
	init_vars();
	fix_noncontent_links();
	init_tabs2();
	test_menu();
	init_links();	
	init_tabs_hover();
	create_levels();
	make_current_tab();
	init_currency();
	init_noncontent_links();
	objAjax = new AjaxLoader( $( 'text-content' ), URLroot );
	objAjax.MakeLinks( $( 'text-content' ) ); 
	init_history();
	rebuild_history();
	ShowHeader();
	init_title_div();
	init_js_title($("graph-block"));
	Zebra();
}

var interval;
var menus = [];
var main_header;
var tab_root;
var levels_div;
var menu_root;
var arr_levels = [];
var arr_plashki = [];
var here_links = [];
var currentMenuClassName = '';
var recurssionLevel = 0;
var current_menu_index = 0;
var console;
var no_current = false;
var main_zaglushka;
var content_div;

function init_vars(){
	tab_root = $('tabs2');
	menu_root = $('menus');
	levels_div = $('levels');
	console = $("console");
	main_header = $("main-header");
	main_zaglushka = $("main-zaglushka");
	content_div = $("text-content");
	_html = $T("HTML")[0]
}
function init_tabs2(){
	if(!tab_root){
		return;
	}
	var under_tabs = $('under-tabs');
	under_tabs.bgr_div = $C('bgr', under_tabs, 'DIV')[0];
	var under_pad = $('under-pad');
	under_pad.bgr_div = $T('DIV', under_pad)[0];
	var border_root = $('bordered');
	tab_root.items = $C('tab', tab_root, 'DIV');
	tab_root.items.each(function(item, index){
		item.color = item.className.split(' ')[1];
		item._onclick = function(){
			tab_root.items.toStart(this);
			switch_style(this.color);
			menus.each(function(mnu){
				mnu.plashka.className = mnu.plashka.className.replace("-current", "");
			});
			var classes = this.menu.plashka.className.split(" ");
			classes[0] += "-current";
			this.menu.plashka.className = classes.join(" ");
			tab_root.items.each(function(itm){
				itm.className = itm.className.replace(" tab-current", "");
			});
			this.className += " tab-current";
		}
		item.onclick = function(){
		}
	});
}

function create_levels(mnu){
	clearElem(levels_div);
	var previousLinkWidth = 0;
	var current_menu;
	if(!mnu){
		menus.each(function(menu, index){
			if(menu.Levels.here_items.length){
				current_menu = menu;
				current_menu_index = index;
				throw $break;
			}
		});
	}else{
		current_menu = mnu;
	}
	if(!current_menu){
		no_current = true;
		return;
	}
	current_menu.Levels.here_items.each(function(item, index){
		var link = item.link.cloneNode(true);
		link.level_index = item.parent_item.level_index;
		link.item_index = item.parent_item.item_index;
		link.number = item.number;
		var level = document.createElement('DIV');
		level.className = 'level';
		if(index == 0){
			level.className += " first";
		}
		level.appendChild(link);
		levels_div.appendChild(level);
		level.style.width = link.offsetWidth + 'px';
		level._width = link.offsetWidth;
		level._left = previousLinkWidth;
		level.index = index;
		level.style.left = previousLinkWidth + 'px';
		create_level_button(level);
		level.button.menu = current_menu;
		level.button.level = level;
		level.button.link = link;
		link.onmouseover = function(){
			var level_index = this.link.level_index;
			var item_index = this.link.item_index;
			var currentLevel = this.menu.showLevelItem(level_index, item_index);
			var currentItem = this.menu.get_item(level_index, item_index, this.link.number);
			var L_index = this.link.level_index;
			var top = 42 - currentItem.offsetHeight * currentItem.number + currentLevel.menu_elem.offsetTop - 17 * this.link.level_index;
			var left = this.level._left + 10 - (41 * L_index) + (L_index > 1 ? 11 : 0) ;
			this.menu.appear(left , top);			
			if(this.menu.tab.b_bottom){
				if(this.menu.tab.b_bottom.className.indexOf("b-bottom") == -1){
					this.menu.tab.b_bottom.className += " b-bottom"; 
				}
			}
			return false;
		}.bind(level.button);
		level.button.onclick = link.onmouseover;
		if(item == current_menu.Levels.here_items.last()){
			historyArray._insert(item.link);
			if(item.menu_item){
				level.button.className += " button-right-arrow";
				level.button.showItem = item.menu_item;
				level.button.clicked = false;
				createMenuItem(level.button);
				level.button.onclick = function(){
					if(!this.clicked){
						showMenuItem(this)
					}else{
						hideMenuItem(this);
					}
					this.clicked = !this.clicked;


				}
			}
//			link.onclick = level.button.onclick.bind(level.button);
		}else{
			link.onclick = item.link.onclick;
		}
		previousLinkWidth += link.offsetWidth + 40;
		arr_levels.push(level);
	});
}
function create_level_button(level){
	level.button = document.createElement('DIV');
	level.button.className = 'button';
	level.button.level = level;
	level.appendChild(level.button);
}
function test_menu(){
	var plashki = $C("plashka", $("menus"), "DIV");
	plashki.each(function(pl){
		var root = $C("super-menu2", pl, "DIV")[0];
		var M1 = new Menu1(
			root,
			$C('track', root, 'DIV')[0],
			$C('move-item', root, 'DIV')[0],
			$C('menu-root', root, "DIV")[0], 'UL', 'LI', 'm-item', _mutual_timer
		);
		M1.speed = 15;
		M1.plashka = pl;
		M1.menu_content_div = $C("menu-content", pl, "DIV")[0];
		M1.menu_content_div.onmouseover = function(e){
			if(!this.correct_height){
				return;
			}
			this.grow2.resizeable.resizeBy(0, this.correct_height);
			this.correct_height = 0;
		}.bind(M1);
		M1.plashka.menu = M1;
		M1.onafterinit.register("afterinit", function(){
			this.plashka.style.width = this.track_width + 20 + "px";
			this.track.style.height = this.menu_root.offsetHeight + "px";
		}.bind(M1));
		var backButtonStyle = {
			width: 22
		};
		M1.setBackButtonStyle(backButtonStyle);
		M1.appear = function(left, top){
			this.plashka.style.visibility = "visible";
			this.plashka.style.left = left + "px";
			this.plashka.style.top = top + "px";
		}
		M1.hide = function(){
			this.plashka.style.visibility = "hidden";
			this.plashka.style.left = "-1000px";
		}
		M1.plashka.bottom_png_corner = $C("ie6-png", $C("p-bottom", this.plashka, "DIV")[0], "DIV")[0];
		M1.paddingLeftValue = 25;
		M1.correct_height = 0;
		M1.onmovestop.register("plashka-resize", function(){
			var steps = 10;
			var resX = 0;
			var resY = 0;
			var deltaWidth = this.track_width - this.previous_width;
			var deltaHeight = this.track_height - this.previous_height;
			this.grow1.grow(deltaWidth, deltaHeight);
			if(deltaHeight < 0){
				this.correct_height = deltaHeight;
				deltaHeight = 0;
			}else{
				this.correct_height = 0;
			}
			this.grow2.grow(deltaWidth, deltaHeight);
		}.bind(M1));
		M1.track_min_width = 227;
		M1.init();
		M1.grow1 = new Grow(M1.track, _mutual_timer);
		M1.grow2 = new Grow(M1.plashka, _mutual_timer);
		M1.grow2.ongrow.register('corner', function(){
			this.plashka.bottom_png_corner.style.left = this.grow2.resizeable.width - 14 + "px";
		}.bind(M1))
		M1.grow2.ongrown.register('corner', function(){
			this.plashka.bottom_png_corner.style.left = this.grow2.resizeable.width - 14 + "px";
		}.bind(M1))
		M1.onmenushow.register('correct-plashka', function(){
			this.plashka.style.width = this.track_width + 20 + "px";
			this.plashka.style.height = this.track_height + 40 + "px";
			this.plashka.bottom_png_corner.style.left = this.track_width + 20 - 14 + "px";			
			this.grow1.resizeable.width = this.track_width;
			this.grow1.resizeable.height = this.track_height;
			this.grow2.resizeable.width = this.track_width + 20;
			this.grow2.resizeable.height = this.track_height + 40;
		}.bind(M1))
		menus.push(M1);
	});
}

function init_tabs_hover(){
	tab_root.items.each(function(item){
		var iClass = item.className.split(" ").last();
		menus.each(function(menu){
			if(menu.plashka.className.indexOf(iClass) != -1){
				menu.tab = item;
				/*
				if(item.className.indexOf("grey2") != -1){
					item.b_bottom = $C("b-bottom", item, "DIV")[0];
				}
				*/
				item.menu = menu;
				item.menu.plashka.className = "plashka-" + item.color + " " + item.menu.plashka.className;
				item.menu.plashka.top_item = $C("p-top", item.menu.plashka, "DIV")[0];
				item.opened = false;
				item.onmouseover = function(e){
					if(this.menu.plashka.className.indexOf("current") != -1){
						return;
					}


					var ev = window.event || e;
					var target = ev.fromElement || ev.relatedTarget;
					if(!target){
						return;
					}
					if(this.opened){
						return;

					}

					this.opened = true;
					menus.each(function(mnu){
						mnu.hide();

					});

					this.className += " tab-current";


					this.menu.appear(this.offsetLeft, 47);
					this.menu.showLevelItem(0, 0);
				}
				item.onmouseout = function(e){
					if(this.menu.plashka.className.indexOf("current") != -1){
						return;
					}

					var ev = e || window.event;
					var target = ev.toElement || ev.relatedTarget;
					if(target == this || target == this.menu.plashka || _isChild(target, this) || _isChild(target, this.menu.plashka) || !target){
						return;
					}

					this.className = this.className.replace(" tab-current", "");


					this.opened = false;
					this.menu.hide();
				}
				item.menu.plashka.onmouseout = function(e){
					var no_go = false;
					var ev = e || window.event;
					var target = ev.toElement || ev.relatedTarget;
					if(this.menu.plashka.className.indexOf("current") != -1){
						if(target == this || _isChild(target, this) || !target){
							no_go = true;
						}
					}else{
						if(target == this || target == this.menu.tab || _isChild(target, this) || _isChild(target, this.menu.tab) || !target){
							no_go = true;
						}
					}
					if(no_go){
						return;
					}

					this.menu.tab.className = this.menu.tab.className.replace(" tab-current", "");


					this.menu.tab.opened = false;
					this.menu.hide();
				}
				throw $break;
			}
		})
	})
}
function menu_rebuild(oMenu, li_item){
	menus.each(function(menu){
		menu.Levels.here_items.each(function(h_item){
			h_item.className = "";
		});
		menu.Levels.here_items = [];
	})
	oMenu.Levels.here_items.each(function(here_item){
		here_item.className = "";
	});
	li_item.className = "here";
	oMenu.Levels.here_items = [];
	oMenu.Levels.here_items.push(li_item);
	li_item.parent_item._openers.each(function(op){
		op.className = "here";
		oMenu.Levels.here_items.push(op);
	}.bind(oMenu));
	oMenu.Levels.here_items.toEnd(oMenu.Levels.here_items[0]);
	create_levels(oMenu);
	oMenu.tab._onclick.call(oMenu.tab);
	oMenu.tab.opened = false;
	oMenu.hide();
	global_here = li_item.link.href.replace(URLroot, "");
}
function menu_load(link){
	main_header.style.visibility = "hidden";
	main_zaglushka.style.display = "block";
	var ret;
	historyArray._insert(link);
	if(!historyArray.startCalled){
		ret = BackButton.Go( link.href.replace(global_domain, "") );
	}
	objAjax.LoadPage( link.href );
	return ret;
}
function init_links(){
	menus.each(function(menu){
		menu.Levels.levels.each(function(level){
			level.each(function(item){
				item._items.each(function(_itm){
					_itm.link.onclick = function(li_item){
						// Rebuilding menu
						menu_rebuild(this, li_item);
						// Loading content... 
						BackButton.usingHistory = false;
						historyArray.startCalled = false;
						menu_load(li_item.link);
						return false;
						// Returning :-) 
					}.bindAvoidingEvent(menu, _itm);
					noncontent_links.each(function(lnk, link_index){
						if(_itm.link.href == lnk.href){
							lnk.onclick = _itm.link.onclick;
							noncontent_links.remove(link_index);
						}
					});
				});
			});
		});
		menu._back_items.each(function(itm){
			itm.link.onclick = itm.opener.link.onclick;
		});
	});
}
function make_current_tab(){
	if(no_current){
		return;
	}
	tab_root.items[current_menu_index]._onclick.call(tab_root.items[current_menu_index]);
}
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, _mutual_timer);
	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];
	_mutual_timer.registerEvent({
		name: "currency",
		func: function(root){
			if(!root.stop){
				root.buttons[root.move_count % 2].onclick();
			}				
		},
		oThis: _mutual_timer,
		args: [currency_root],
		freq: 240
	});
	currency_root.onmouseover = function(){
		this.stop = true;
	}
	currency_root.onmouseout = function(){
		this.stop = false;
	}
}
function init_history(){
	BackButton.frame_src = URLroot + "js/frame.html";
	BackButton.usingHistory = false;
	BackButton.create(_mutual_timer, 10);
	BackButton.onhistorychange.register("handle", function(param){
		var obj;
		if(!param || param == "main"){
			badLocations.each(function(loc){
				if(location.href.indexOf(loc) != -1){
					obj = {
						link: null,
						href: loc,
						func: function(){return true},
						toString: function(){return this.href}
					}
					throw $break;
				}	
			});
			if(!obj){
				obj = historyArray[0];
				//historyArray.startCalled = true;
			}
			historyArray.startCalled = true;
		}else{
			obj = historyArray._find(param);
			historyArray.startCalled = false;
		}
		if(obj){
			var badLoc = badLocations.includes(obj.href);
			if(badLoc !== false){
				switch_style("grey2");
				menu_load(obj);
				refresh_menu();
				return;
			}
			//Object is found in history array
			if(obj.href.indexOf(".html") != -1){
				if(obj.href.substr(0, obj.href.lastIndexOf("\/") + 1) == global_here){
					menu_load(obj);
				}else{
					var menu_href = obj.href.substr(0, obj.href.lastIndexOf("\/") + 1);
					var menu_link = find_link(menu_href);
					if(menu_link){
						var b = menu_link.onclick.bound
						menu_rebuild(b[0], b[1]);
						menu_load(obj);
					}else{
						alert("Not found!!");
					}
				}
			}else{
				menu_rebuild(obj.func.bound[0], obj.func.bound[1]);
				menu_load(obj.func.bound[1].link);
				historyArray.startCalled = false;
			}
		}else{
			//Use menu tree search
			if(badLocations.includes(param) !== false){
				var link = noncontent_links._find(param);
				link.onclick();
				return false;
			}
			try{
				if(param.indexOf(".html") != -1){
					obj = find_link(param.substr(0, param.lastIndexOf("\/") + 1));
				}else{
					obj = find_link(param);
				}
				menu_rebuild(obj.onclick.bound[0], obj.onclick.bound[1]);
				var dummy_link = {href: param};
				menu_load(dummy_link);
			}catch(e){
				refresh_menu();
				switch_style("red");
				alert(404);
			}
		}	
	})
	if(!location.hash){
		BackButton.Go("main");
	}else{
		if(location.hash != "#main"){
			var url = location.hash.replace("#", "");
			BackButton.Go(url);
			BackButton.onhistorychange.fire(url)
		}
	}
	window.onunload = function(){
//		SetHistoryCookie(historyArray.toString());
	}
}
function rebuild_history(){
	var temp = historyArray[0];
	historyArray[0] = historyArray[historyArray.length - 1]
	historyArray[historyArray.length - 1] = temp;
}
function find_link(url){
	var ret = false;
	var reg = new RegExp(url + "$");
	menus.each(function(menu){
		var $break1 = false;
		menu.Levels.levels.each(function(level){
			var $break2 = false;
			level.each(function(item){
				var $break3 = false;
				item._items.each(function(_itm){
					if(_itm.link.href.match(reg)){
						ret = _itm.link;
						$break3 = true;
						throw $break;
					}
				});
				if($break3){
					$break2 = true;
					throw $break; 
				}
			});
			if($break2){
				$break1 = true;
				throw $break; 
			}
		});
		if($break1){
			throw $break; 
		}
	});
	return ret;
}

/* CSS Switcher */
var css_link = $C("color-css", null, "link")[0];
var head_elem = $T("HEAD")[0];

function switch_style(color){
	var new_link = document.createElement("link");
	new_link.rel = "stylesheet";
	new_link.type = "text/css";
	new_link.href = URLroot + "css/" + color + ".css"; 
	head_elem.removeChild(css_link);
	head_elem.appendChild(new_link);
	css_link = new_link;
}
var hash_colors = {
	live: "grey",
	public: "grey2",
	"bank-for-business": "red"
}
var current_color = "";
for(var i in hash_colors){
	if(location.hash.indexOf("#" + i) != -1){
		current_color = hash_colors[i];
		break;
	}
}
if(!current_color){
	for(var i in hash_colors){
		if(location.href.indexOf(URLroot + i) != -1){
			current_color = hash_colors[i];
			break;
		}
	}
}
if(!current_color){
	current_color = "grey2";
}
var noncontent_links = [];
function fix_noncontent_links(){
	noncontent_links = $C("rebuild-link", document.body, "A");
	noncontent_links._find = function(href){
		var ret = false;
		this.each(function(link){
			if(link.href.indexOf(href) != -1){
				ret = link;
				throw $break;
			}
		});
		return ret;
	}
}
function init_noncontent_links(){
	noncontent_links.each(function(link){
		link.onclick = function(){
			switch_style("grey2");
			BackButton.usingHistory = false;
			menu_load(this);
			refresh_menu();
			return false;
		}
	});
}
function refresh_menu(){
	tab_root.items.each(function(tab){
		tab.className = tab.className.replace("tab-current", "");
		tab.menu.plashka.className = tab.menu.plashka.className.replace("-current", "");
	});
	menus.each(function(menu){
		menu.Levels.here_items = [];
	});
	create_levels();
	global_here = location.href.replace(location.hash, '').replace(URLroot, '');
}
switch_style(current_color);
function createMenuItem(btn){
	var pl = btn.menu.plashka.cloneNode(true);

	pl.className += " fake-plashka";
	var root = $C("super-menu2", pl, "DIV")[0];
	clearElem(root);
	var _item = btn.showItem.cloneNode(true);
	root.appendChild(_item);
	_item.style.position = "static";
	var back_items = $C("back-item", _item, "DIV");
	back_items.each(function(item){item.parentNode.removeChild(item)});
	var UL = $T("UL", pl)[0];
	pl.ul = UL;
	UL.style.paddingLeft = "0";
	btn.level.appendChild(pl);
	pl.btn = btn;
	pl.onmouseout = function(e){
		var ev = window.event || e;
		var target = ev.toElement || ev.relatedTarget;
		if(target != this && !_isChild(target, this) && target != null){
			this.btn.onclick.call(this.btn);
		}
	}
	pl.style.display = "none";
	pl.style.visibility = "visible";
	pl.style.left = "100%";
	pl.style.top = "0";
	var links = $T("A", pl);
	btn.showItem._items.each(function(item, index){
		links[index].onclick = item.link.onclick;
	});
	if(pl.className.indexOf("current") == -1){
		var cl = pl.className.split(" ");
		cl[0] = cl[0] + "-current";
		pl.className = cl.join(" ");
	}
	btn.fake_plashka = pl;
	//print(pl.className)
}
function showMenuItem(btn){
	btn.fake_plashka.style.display = "block";
	btn.fake_plashka.style.width = btn.fake_plashka.ul.offsetWidth + "px";
}
function hideMenuItem(btn){
	btn.fake_plashka.style.display = "none";
}
function flash_menu_reload(href){
	var link = find_link(href);
	if(link){
		var b = link.onclick.bound;
		menu_rebuild(b[0], b[1]);
		BackButton.usingHistory = false;
		historyArray.startCalled = false;
		menu_load({href: href});
	}else{
		alert("link not found!!");
	}
}
var badLocations = [
	URLroot + "registration/",
	URLroot + "sitemap/",
	URLroot + "registration/forgot/"
];
badLocations.includes = function(href){
	var ret = false;
	this.each(function(loc, index){
		if(loc.indexOf(href) != -1){
			ret = index;
			throw $break;
		}
	});
	return ret;
}
function clean_divs(root){
	var divs = $T("DIV", (root || $("text-content")));
	divs.each(function(div){
		if(div.innerHTML == "&nbsp;"){
			div.style.display = "none";
		}	
	})
}
function init_num_inputs(root, c_name){
	var ips = $C(c_name, root, "INPUT");
	if(ips){
		ips.each(function(ip, index){
			ip.val = ip._val = "";
			ip.onkeyup = format_num;
			ip.onblur = insert_formatted;
			ip.onfocus = insert_unformatted;
		})
	}
}
function insert_formatted(){
	this._val = this.value;
	this.value = this.val.replace(/^ /, "");
}
function insert_unformatted(){
	this.value = this._val;
}
function format_num(){
	if(this.value.length > 3 && this.value.indexOf(" ") == -1){
		this.val = this.value.substring(0, this.value.length % 3) + this.value.substr(this.value.length % 3).splitBy(3).each(function(v){return " " + v}).join('');
	}else{
		this.val = this.value;
	}
}
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 + _html.scrollTop + 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";
		}
	}
}
