(function($){
$.nvzMenu = {
	status: function() {
		$.post($.nvzMenu.s.cgi,{info:$.defaults.pageid},function($j){
			var _o = JSON.parse($j);
			if (_o.status) return false;
			$('#display b').html(_o.name).after('&nbsp;'+(_o.note||''));
		});
	},
	pulldown: function($data) {
		var _s = $.nvzMenu.s;
		$(_s.pulldown).slideToggle(200,function(){
			$(_s.iddiv+' img').attr('src', 
				$(_s.iddiv+' img').attr('src').match(/=s/) ? 
					'/=n.png' : '/=s.png');

			var _f = $('#topbar input[name="email"]');
			if (_f.length && $(_s.pulldown).is(':visible')) {
				if (_f.val().length > 0)
					$('#topbar input[name="pw"]').get(0).focus();
				else _f.get(0).focus();
			} else if ($data == 'search')
				$('#search input:first').get(0).focus();
		});
	},
	counts: function() {
		var _div = $.nvzMenu.s;
		if (!_div.total) {
			$(_div.iddiv+' .status').fadeOut();
			$(_div.pulldown+' .block .num').parent().fadeOut();
		}
		$.post($.nvzMenu.s.cgi, { count: 1 }, function($j) {
			var _o = JSON.parse($j);
			if (_o.status) return false;

			var _total = 0;
			$.each(['n','f','t','r'],function(){
				_div.num[this] = _o[this].count||'';
				if (_o[this].count) {
					$(_div.pulldown+' .block[title="'+_o[this].type+'"]')
						.fadeIn().find('.num').html('<a href="/dashboard:'
							+this+'">'+_o[this].count+'<\/a>');
    
					_total = _total + eval(_o[this].count);
				} else {
					$(_div.pulldown+' .block[title="'+_o[this].type+'"]')
						.hide();
					delete _o[this];
				}
			});
			_div.num['a'] = _total;

			if (_div.total != _total)
				$(_div.iddiv+' .status')
					.css({backgroundColor:$.mau.s4,color:$.mau.sA}).fadeIn().html(_total);

			document.title = (_total ? '('+_total+') ' : '')
				+ document.title.replace(/^\([^\s]+\s/,'');

			$.nvzMenu.notify(_total,_o['f']||_o['n']);

			_div.total = _total;
		},"text");
		setTimeout("$.nvzMenu.counts()", 50000);
	},
	notify: function($total,$n) {
		var _s = $.nvzMenu.s;
		if (typeof($.dashboard) == 'object' && $total != _s.total && _s.total != 0) {
			$.dashboard.menu();
			return false;
		}
		if ($n && $n.type == 'Friends') {
			$(_s.iddiv+' .status, '+_s.pulldown+' .block[title="Friends"] .num a')
				.css({backgroundColor:$.mau.cY,color:$.mau.s0});
		}
		if ($n && $n.type == 'Notes' && _s.notes != $n.count && !$('#quicknotes').data('edit')) {
			_s.notes = $n.count;
			$(_s.iddiv+' .status, '+_s.pulldown+' .block[title="Notes"] .num a')
				.css({backgroundColor:$.mau.cG,color:$.mau.s0});

			if (!$('#quicknotes').length) {
				$('<div id="quicknotes"><\/div><br style="clear:both">')
					.css({color:$.mau.sB})
					.appendTo('#topbar div.pulldown');

				$('#quicknotes div.ctrls img').live('click',function(){
					var _box = $(this).parent().parent().parent();
					var _self = $(this);
					if ($(this).attr('alt') == 'reply') {
						$('#quicknotes').data('edit',1);
						$(this).parent().parent().comment({
							padding: 9, select: _box.data('perm'),
							callback: function($o){$.notes.write(
							{note:$o.text,to:_box.data('to'),perm:$o.perm,parent:_box.data('id'),who:_box.data('who')},
							function(){ 
								$('#quicknotes').data('edit',0);
								_self.prev().trigger('click'); }); }
						});
					} else {
						$(this).remove();
						$.notes.mark({id:_box.data('id'),who:_box.data('who')},
						function(){ 
							_box.remove(); 
							_s.notes = _s.notes - 1;
						});
					}
				});
			} else $('#quicknotes').empty();

			for (var $i=0;$i<$n.data.length;$i++) {
				$('<div class="note"><div>'
					+'<div class="ctrls">'
					+'<img alt="mark" src="/=i_check.png">'
					+'<img alt="reply" src="/=i_reply.png">'
					+'<\/div><img src="/='+$n.data[$i].fr+'&s=30">'
					+($n.data[$i].to == $.defaults.id 
					? ' ':'<img src="/='+$n.data[$i].to+'&s=30">')
					+$n.data[$i].note
					+'<\/div><\/div>')
				.data('perm',$n.data[$i].perm)
				.data('id',$n.data[$i].id)
				.data('to',$n.data[$i].fr)
				.data('who',$n.data[$i].to)
				.hover(function(){ $(this).find('div.ctrls').show() },
					function(){ $(this).find('div.ctrls').hide() })
				.css({backgroundColor:$.mau.s3})
				.appendTo('#quicknotes');
				$('#quicknotes div.ctrls img')
					.css({cursor:'pointer', backgroundColor: $.mau.sA, padding: '2px', opacity: .2, border: '1px solid '+$.mau.sD})
					.hover(function(){$(this).css('opacity',1);},function(){$(this).css('opacity',.2);})
				if ($i == 2) break;
			}
		}
	},
	activate: function() {
		var _s = $.nvzMenu.s;
		$(_s.iddiv+' .status').hide();
		$(_s.pulldown+' .block .num').parent().hide();

		var _txt = $('#topbar td.mid').html().replace(/strong>[\s\n]*/g,'strong>');
		$('#topbar td.mid').html(_txt);

		_s.fgcolor = $(_s.iddiv).css('color');
		_s.bgcolor = $(_s.iddiv).css('backgroundColor');

		$(_s.iddiv+','+_s.menudiv).hover(
			function(){ $(this).css({backgroundColor:_s.fgcolor,color:_s.bgcolor})},
			function(){ $(this).css({backgroundColor:_s.bgcolor,color:_s.fgcolor})}
		).css({cursor: 'pointer'});

		$(_s.iddiv).find('img').click(function($e,$data){ $.nvzMenu.pulldown($data) });

		if ($('#display b').length > 0)
			$.nvzMenu.status();

		$(document).keypress(function(e){
			var _ob = e.srcElement || e.target;
			if (!_ob) return true;
    
			if (_ob.tagName.match(/TEXTAREA|INPUT/)) return true;
			
			if (e.which == 102) {
				if (navigator.userAgent.match(/Chrome|MSIE/)) 
					e.returnValue = false;
				if ($('#topbar .pulldown').is(':hidden'))
					$('#userid img').trigger('click','search');
				else
					$('#search input:first').eq(0).focus();
				return false;
			}
		});

		if ($.defaults.sid)
			$.notice.retrieve($.defaults.page);
	},
	item: function($op) {
		var _s = $.nvzMenu.s;
		_s.fgcolor = $(_s.iddiv).css('color');
		_s.bgcolor = $(_s.iddiv).css('backgroundColor');

		$('<strong class="'+$op.name+'"><span>'+$op.label+'<\/span><\/strong>')
			.css({marginLeft: '2px', cursor: 'pointer'})
			.hover(
				function(){ $(this).css({backgroundColor:_s.fgcolor,color:_s.bgcolor})},
				function(){ $(this).css({backgroundColor:_s.bgcolor,color:_s.fgcolor})})
			.click($op.cb)
			.appendTo('#topbar td.mid');
	},
	resize: function($w) {
		$w = $w || 570;
		$('#topbar td.mid').width($w);
		$('#topbar td.right').width(600-$w);
	},
	searchbox: function($cb) {
		$('<strong class="search"><span>'+$.Trans.Search+'<\/span>'
			+' <input size="10" type="text"><\/strong>')
			.appendTo('#topbar td.mid');
		var _doit = function($e) {
			if ($e.which == 13)
				$cb($(this).val());
		};
		$('#topbar strong.search input').keypress(_doit).click(function(){return false});
	},
	shortcuts: function($sc) {
		$.nvzMenu.s.scdir = eval($sc);
		$.post($.nvzMenu.s.cgi,{ shortcuts: $sc, current: $.defaults.page },
		function($j){
			var _j = JSON.parse($j);
			if (_j.status == 'err')
				return false;

			var _this = $('#topbar strong.shortcuts');
			var _first = 0;

			if (!_this.length) {
				$('<strong class="shortcuts"><span>'+$.Trans.Shortcuts+'<\/span><\/strong>')
					.appendTo('#topbar td.mid');
				_this = $('#topbar strong.shortcuts');
				_first = 1;
			}

			_j.push([$.Trans.Logout,'logout','-']);

			_this.aqDropMenu(_j,
			{stylize: true, cb: function($href,$t){ 
				document.location.href = $href;
			} }).fadeIn();

			if (_first)
				_this.find('img')
				.click(function(){ 
					var _i = $(this).attr('src');
					var _dir = _i.replace(/.\./,_i.match(/s/)?'n.':'s.');
					$(this).attr('src', _dir);
					_this.hide();
					$.nvzMenu.shortcuts($.nvzMenu.s.scdir*-1);
					return false; });

			return false;
		});
	},
	s: {
		cgi: '/--/modules', total: 0, notes: 0, num: [],
		iddiv: '#userid', menudiv: '#topbar strong',
		pulldown: '#topbar .pulldown'
	}
};

$.fn.aqDropMenu = function(arry,options) {
	var opts = $.extend({ }, options);

	return this.each(function() {
		var $menu = $('.aqDropMenu',this);
		var _first = 0;
		if (!$menu.length) {
			$('<div style="display:none" class="aqDropMenu"><\/div>')
				.appendTo(this);

			$menu = $('.aqDropMenu',this);
			_first = 1;
		}

		var _fg = $menu.parent().css('color');
		var _bg = $menu.parent().css('backgroundColor');
		var _bot = eval($menu.parent().css('paddingBottom').replace(/px$/,''))+1+'px';
		var _mar = eval($menu.parent().css('paddingLeft').replace(/px$/,''));
		var _off = navigator.userAgent.match(/MSIE/)?_mar:0;

		if (_first) {
			// let's make the buton nice
			if (opts.stylize) {
				$(this).css({ cursor: 'pointer', marginLeft: '2px' })
					.hover(
						function(){ $(this).css({backgroundColor: _fg, color: _bg}) },
						function(){ $(this).css({backgroundColor: _bg, color: _fg}) }
					)
				$('<img src="/=s.png">').insertAfter($(this).find('span'));
				_off = _off - 2;
			}

			$menu.hover(
				function(){ $(this).show() },
				function(){ $(this).slideUp('fast').removeAttr('checked') }
			);
			$(this).click(function(){
				var _cur = $menu.attr('checked');

				$(this).parent().find('[checked=1]')
					.removeAttr('checked').fadeOut('fast');

				if (!_cur)
					$menu.slideDown('fast').attr('checked',1)
						.css({left: $menu.parent().position().left - _off});
			});

			$menu.css({position: 'absolute', zIndex: 1005,
				marginTop: _bot, padding: Math.abs(_mar/2),
				backgroundColor: _bg, color: _fg});
		} else {
			$menu.find('A').remove();
		}

		for (var i=0;i<arry.length;i++) {
			$menu.append('<a href="'+arry[i][1]+'">'+arry[i][0]+'<\/a>');
			if (arry[i][2] == '-')
				$menu.find('A:last').css('borderTop','1px solid '+_fg);
			else if (arry[i][2] == '*')
				$menu.find('A:last').css('fontWeight','bold');
		}

		$menu.find('A').hover(
			function(){ $(this).css({ backgroundColor: _fg, color: _bg }); },
			function(){ $(this).css({ backgroundColor: _bg, color: _fg }); }
		).css({ backgroundColor: _bg, color: _fg, padding: Math.abs(_mar), display: 'block' });

		if ($.isFunction(opts.cb)) {
			$menu.find('A').click(function(){
				$menu.fadeOut();
				opts.cb($(this).attr('href'),$(this).html());
				return false;
			});
		}

		return false;
	});
};

$.load_menus = function() {
	var _bit = $.nvzMenu.s.friendType;
	if ($.translate.count && _bit != 'plugin') {
		$.nvzMenu.shortcuts(10);
		switch (_bit) {
		case 'owner':
			$('#topbar strong.listname').fadeIn().aqDropMenu([
				[$.Trans.WriteBlog,"/write:"+$.defaults.page,'*'],
				[$.Trans.Blurbs,"/blurb:"+$.defaults.page],
				[$.Trans.Notes,"/notes:"+$.defaults.page],
				[$.Trans.Options,"/preference:"+$.defaults.page,'-'],
				[$.Trans.Invites,"/invites:"+$.defaults.page],
				[$.Trans.Portrait,"/image:"+$.defaults.page]
			]);
			break;
		case 'yes':
			var _mitems = [
				[$.Trans.WriteNote,"note"],
				[$.Trans.Unsubscribe,"unsub"]
			];

			if ($.nvzMenu.s.ftype.match(/^forum/))
				_mitems = [
					[$.Trans.WriteBlog,"write",'*'],
					[$.Trans.Blurbs,"blurb"],
					[$.Trans.WriteNote,"note",'-'],
					[$.Trans.Unsubscribe,"unsub"]
				];

			$('#topbar strong.listname').fadeIn().aqDropMenu(_mitems,
			{cb: function($href){  
				switch ($href) {
				case 'note': 
					$('#display').comment({padding: 9, 
						callback: function($o){$.notes.write({
							note: $o.text, to: $.defaults.pageid, perm: $o.perm }); }
					});
					break;
				case 'unsub':
					$.subscription.del($.defaults.pageid);
					break;
				case 'write':
					document.location.href = '/write:'+$.defaults.page;
					break;
				case 'blurb':
					document.location.href = '/blurb:'+$.defaults.page;
					break;
				}	
			} });
			break;
		case 'no':
		case 'follow':
			$('#topbar strong.tools').fadeIn()
				.html(_bit=='no'
					? ('<img src="/=i_check.png"> '+$.Trans.Subscribe)
					: $.Trans.Unsubscribe)
				.css({marginLeft: '2px'})
				.click(function(){
					(_bit == 'no') 
					?  $.subscription.add($.defaults.pageid)
					:  $.subscription.del($.defaults.pageid);
				});
		default:
			$('#topbar strong.listname').fadeIn()
			.click(function(){
				document.location.href = $(this).find('> span').html();
			})
			.find('> img').remove();
			break;
		}
	}
	else setTimeout("$.load_menus();",1000);
};

$.history = {
	visitors: function($who){
		$.history.s.who = $who || $.history.s.who;
		$.post($.history.s.cgi, 
			{ history: $.history.s.top, who: $.history.s.who }, function($j) {
			var _o = JSON.parse($j);
			if (_o.status == 'ok') {
				var _list = _o.result;
				if (_list.length > 1)
					$($.history.s.div).empty();
				else {
					var _p = $($.history.s.div).find('[title="'+(_list[0][1]||'&nbsp;')+'"]');
					if (!_p.length)
						_p = $($.history.s.div).find('div:last');
					_p.fadeTo(100, 0, function(){ $(this).remove(); });
				}
				
				for (var i=0; i<_list.length; i++)
					$('<div title="'+(_list[i][1]||'&nbsp;')+'" class="block2">'
						+ '<span class="title">'+(_list[i][1]||'&nbsp;')+'<\/span>'
						+ '<img title="'+(_list[i][1] || 'someone')+' visited '
						+ $.aqStamp(_list[i][5])+' ('+_list[i][2]+ ' times)" '
						+ 'src="/='+(eval(_list[i][0])||999+'&b=1203805224')+'&s=29">'
						+ '<\/div>')
					.css('cursor','pointer')
					.data('to',_list[i][0])
					.data('friend',(_list[i][3]||6))
					.data('name',(_list[i][1]||''))
					.prependTo($.history.s.div);

				if ($.history.s.top == -1)
					$($.history.s.div+' > div').live('click',function(){
						if ($(this).data('friend') <= 5) {
							var _ob = $(this);
							_ob.parent().find('> div').css({borderBottomColor:'transparent'}); 
							_ob.css({borderBottomColor:$.mau.cY})
							.parent().comment({
								width: 240, padding: 9, destroy: true,
								callback: function($o){ 
									if ($o.text && $o.perm) {
										$.notes.write(
											{note:$o.text,to:_ob.data('to'),perm:$o.perm},
											function(){
												_ob.parent().find('> div').css({borderBottomColor:'transparent'}); 
											}
										);
									} else
										_ob.parent().find('> div').css({borderBottomColor:'transparent'}); 
								}
							});
						} else
							document.location.href = '/'+$(this).data('name');
					});
				$.history.s.top = _o.top;
			}
		},"text");
		setTimeout("$.history.visitors()", 10000);
	},
	birthdays: function() {
		$.post($.history.s.cgi, {notify: 1}, function($j) {
			var _o = JSON.parse($j);
			if (_o.status == 'err')
				return false;
			var _list = _o.result;
			$.each(_list,function(){
				$('#display').append('<a class="icn" href="/'+this[1]+'"><div class="img" title="'
					+this[3]+$.Trans.hasBirthdayOn
					+this[2]+'" style="background-image: url(/='
					+this[0]+'&s=30)"><div class="new">'+this[4]+'<\/div><\/div><\/a>')
			});
			$('#display div.new').css('opacity',.5);
		},"text");
	},
	s: {
		cgi: '/--/modules', div: '#history', top: -1, who: 0
	}
};
})(jQuery);
