/*
 * @encoding: UTF-8
 */ 
var $window = $(window);
var clog = (window.console && console.log) || function(){},
	cdir = (window.console && console.dir) || function(){};

var IShellNotUseJSToDoCSSWork = function T (container) {
	if (!container) {
		return;
	}

	var submenus = $(".submenu", container);
	for (var i = 0, submenu; submenu = submenus[i]; ++i) {
		var submenu = $(submenu);
		var iframe = submenu.find("iframe");
		if (iframe.length > 0) {
			iframe = iframe[0];
			var contentWindow = iframe.contentWindow;
			
			/* О, Боже, что я творю?! */
			contentWindow.onresize = function(){
				var li = submenu.parents("li:first"),
					ldyn = li.find(".ldyn"),
					_submenu = submenu,
					rdyn = li.find(".rdyn"),
					msie67 = $.browser.msie && $.browser.version < 8; 
				
				return function(evt){
					var li_offsetLeft = li.attr("offsetLeft"),
						li_offsetWidth = li.attr("offsetWidth"),
						submenu_offsetLeft = 50;
					
					if (msie67) {
						/* IE, arrr! */
						li_offsetLeft = li.parents(":first").attr("offsetLeft") + li.parents(":first").parents(":first").attr("offsetLeft");
					}
					
					
					var li_submenu_delta = li_offsetLeft - submenu_offsetLeft;
					
					var width = 0, lwidth, rwidth;
					
					width = this.document.documentElement.offsetWidth;
					
					lwidth = li_submenu_delta - 30;
					rwidth = width - li_offsetWidth - li_offsetLeft + 20;
					
					/* снова костыли... */
					if (msie67) {
						//rwidth += 16;
					}
					
					if (lwidth < 0) {
						lwidth = 0;
					}
					
					if (rwidth < 0) {
						rwidth = 0;
					}
					
					ldyn[0].style.width = lwidth + "px";
					rdyn[0].style.width = rwidth + "px";
				}
			}();
			contentWindow.onresize.apply(contentWindow);
			$window.resize(function(h){
				return function(){
					h.onresize.apply(h);
				}
			}(contentWindow));
		}
	}
}

var MenuController = function T (container) {
	var t = this;
	t.cache = [];
	t.container = $(container);
	t.selectedItemIndex = -1;
	t.items = $(".top-menu > li.have-submenu");
	t.hideTimer = null;
	t.items.each(function(index){
		t.cache[index] = {"li": $(this), "index": index};
	});
	for (var i = 0, cacheItem; cacheItem = t.cache[i]; ++i)
	{
		cacheItem["li"].mouseover((function(itemIndex, BM) {
			return function(){
				clearTimeout(t.hideTimer);
				t.showSubmenu(itemIndex);
			}
		})(i)).mouseout((function(){
			return function(){
				t.hideTimer = setTimeout(function(){t.showSubmenu(-1)}, 300);
			}
		})());
	}
}

MenuController.prototype = {
	showSubmenu: function T (key)
	{
		var t = this, openClass = "opened", item;
		if (item = t.cache[t.selectedItemIndex])
		{
			item["li"].removeClass(openClass);
		}
		
		if (item = t.cache[key]) {
			item["li"].addClass(openClass);
		}

		t.selectedItemIndex = key;
	}
}

var TabSelector = function(container){
	var t = this;
	t.tabs = $(".tab-headings td", container);
	t.bodies = $(".tab-bodies .tb", container);
	for (var i = 0, tab, $tab; tab = t.tabs[i]; ++i){
		$tab = $(tab);
		$tab.click(function(i){
			return function(e){
				t.showTab(i);
				e.preventDefault();
			}
		}(i));
	}
}
TabSelector.prototype = {
	showTab: function(index) {
		var selected = "selected";
		this.tabs.removeClass(selected);
		this.bodies.hide();
		$(this.tabs[index]).addClass(selected);
		$(this.bodies[index]).show();
	}
}

var NotepadLink = function(el) {
	
}

$(function(){
	var menu = document.getElementById("menu");
	var cookieStr = document.cookie + "";
	var cookieArr = cookieStr.split(/;\s*/);
	var nomenu = false;
	for (var i = 0, cook; cook = cookieArr[i]; ++i) {
		var v = cook.split("=");
		if (v[0] == "nomenu") {
			nomenu = true;
		}
	}
	if (!nomenu) {
		menu && new IShellNotUseJSToDoCSSWork(menu) && new MenuController(menu);
	}
	
	$("dl.accordion").find("dt").click(function(e) {
		var t = $(this),
			opened = !!t.hasClass("ui-state-active");
		if (opened) {
			document.location = t.find("a").attr("href");
		}
		
		if (t.next().text().replace(/\s*/g, "").length == 0) {
			document.location = t.find("a").attr("href");
		}
	}).end().accordion({
		header: "dt",
		autoHeight: false,
		fillHeight: false
	});
	$(".tabs").not(".nojs").each(function(){
		new TabSelector(this);
	});
});

$(function(){
	/* Да, тут бага, но Леха о ней знает и поправит */
	$(".previews a").lightBox();
	$("a[rel=lightbox]").lightBox();
	$(".add-to-notepad").click(function(e){
		var t = $(this);
			notepadUrl = "/user/note.php";
		$.post(notepadUrl, {xhr: true, link: this.href}, function (data) { /*t.remove()*/ });
		e.preventDefault();
	});
});


