// JavaScript Document
var menu = {
	init : function(){
		menu.topbar = $('#topbar')
			.bind('close', function(e, switchContent){
				menu.topbar.children('.active').trigger('close', [switchContent || false]);
			});
		if($('#block-wrap').length) menu.topbar.hover($.sOn, $.sOff);
		$('#support').hover($.sOn, $.sOff);
		
		menu.topbar.children().each(function(){
			var content = $(this).children('div.top-content').clone(true);
			if(content.attr('id') == 'tag-media') menu.media.init(content);
			$(this).children('div.top-content').remove();
			
			//Настройка блока контента
			if(content.not('.noscroll').length) $.proxy(menu.scrollInit, content.get(0))();
			
			$(this).bind({
				open: function(){
					var child = this;
					if(menu.topbar.queue().length>3) return false;
					menu.topbar.queue(function(deq){
						$(this).add(child).active(true);
						menu.tint.show(content);
						deq();
					});
				},
				close: function(e, switchContent){
					var child = this;
					e.stopPropagation();
					menu.topbar.queue(function(){
						menu.tint.hide(switchContent || false);
						$(child).add(this).active(false);
						$(this).dequeue();
					});
				}
			});
		});
		
		$('#viewing').click(function(e){
			if($(this).active()) menu.topbar.trigger('close');
			else{
				menu.topbar.trigger('close', [true]);
				$(this).trigger('open');
			}
		});
		$('.top-toggle > a', menu.topbar).click(function(){
			if($(this.parentNode).hasClass('active')) return false;
			if(menu.topbar.is(':animated')) return;
			menu.topbar.trigger('close', [true]);
			$(this.parentNode).trigger('open');
		});
		$('#topbar-close').click(function(){
			menu.topbar.trigger('close');
		});
		$(window).resize(function(){
			var aHeight = $(window).height() - 50;
			$('#tint div.top-content').height(aHeight);
			$('#tint div.content-scrollbar').height(aHeight-180);
			$('#tint div.top-content').each(function(){
				var slide = $('div.top-slide', this), scrollbar = $('div.content-scrollbar', this);
				if(aHeight >= slide.outerHeight())
					scrollbar.hide().trigger('refresh', [{size: 1, pos: 0}, true]);
				else 
					scrollbar.show().trigger('refresh', [{size: aHeight / $('.top-slide', this).outerHeight()}, true]);
			});
		}).resize();
	},
	scrollInit: function(){
		$(this).wrapInner('<div class="top-slide" />');
		var slide = $('div.top-slide', this), elm = $(this), scrollb;
		elm.prepend(scrollb = $('<div class="content-scrollbar" />'))
			.mousewheel(function(e, delta){
				scrollb.trigger('move', [((delta>0)?-1:1) * 80/Math.abs(slide.outerHeight() - elm.height())])
			});
		scrollb.scrollbar({
			vert: true,
			scroll: function(e, pos){
				slide.css('top', -pos.pos * Math.abs(slide.outerHeight() - elm.height()));
			}
		}).selectHover();
	},
	tint : {
		/*
			add - создает тинт
			remove - удаляет тинт
			show - показывает тинт (+ add())
			hide - скрывает тинт (+ remove())
			obj - сам тинт
		*/
		add: function(){
			return this.obj = $('#tint').length ? $('#tint') : $('#area').append('<div id="tint" />').find('#tint');
		},
		remove: function(){
			this.tint.remove();
		},
		show : function(content){
			var content = content || false;
			menu.topbar.queue(function(deq){
				var tint = menu.tint.add();
				if(tint.is(':hidden')) tint.fadeIn('fast', function(){deq()})
					tint.append(content.hide() || null);
					content.fadeIn('fast', function(){
						deq();
					});
				$(window).resize();
			});
		},
		hide : function(switchContent){
			var switchContent = switchContent || false;
			
			menu.topbar.queue(function(deq){
				if(!switchContent)
					menu.tint.obj.fadeOut('fast', function(){
						$(this).children().detach();
						deq();
					});
				else 
					menu.tint.obj.children().fadeOut('fast', function(){
						$(this).detach();
						deq();
					});
			});
		}
	},
	media:{
		init : function(context){
			blog.media = (blog.media == 0) ? bo.medias : blog.media;
			this.bin = $.map($.extend(
				$.map((bo.medias).toString(2).split(''), function(){return '0'}), 
				blog.media.toString(2).split('').reverse()), 
				function(el){return el == '1'}
			); //Convert dec to array with booleans
			this.links = blog.medias;
			this.li = $('ul > li', context);
			
			this.li.children('a').addClass('js').click(function(){
				var i = menu.media.li.index($(this).parent('li')); //this index
				menu.media.bin[i] = menu.media.bin[i] ? false : true; //toggle
				menu.media.li.eq(i).find('span:last').text(menu.media.bin[i]?'on':'off') //set label
				menu.media.setLink();
				
				return false;
			});
			
			this.setLink(context);
		},
		setLink : function(context){
			blog.media = parseInt(
				$.map(this.bin, function(el){
					return el ? '1' : '0'
				}).reverse().join('')
			, 2)
			$('#media-submit', context).attr('href', blog.medias[(blog.media >= 7)?0:blog.media]);
		}
	}
}
$(document).ready(menu.init);
