"use strict";var VinesUserBar=new Class({element:{},fx:{},timer:{},menu:{},queue:{startup:[],xp:[]},user:{},initialize:function(a){window.addEvent("domready",this.domready.bind(this))},domready:function(){this.element.userbar=$("userbar");if(!this.element.userbar||!this.element.userbar.hasClass("logged-in")){return false}this.initUserMenus();this.initXp();this.initNotifications();this.ready=true;this.processStartupQueue()},initUserMenus:function(){this.initUsername();this.initPm();this.initOnline()},initUsername:function(){this.initMenu("username");this.element.username_menu.getElements(":nth-child(3n-2)").addClass("first-col")},initPm:function(){this.initMenu("pm");this.menu.pm.addEvent("open",this.loadMenu.bind(this,["pm","/pm/toolbar-unread/"]))},initOnline:function(){this.initMenu("online");this.menu.online.addEvent("open",this.loadMenu.bind(this,["online","/follow/toolbar-currently-online/"]))},initMenu:function(a){this.element[a]=this.element.userbar.getElement(".profile ."+a);this.element[a+"_menu"]=this.element.userbar.getElement(".menu."+a);this.element[a].addEvent("click",this.toggleMenu.bindWithEvent(this,a));this.menu[a]=this.element[a+"_menu"]},toggleMenu:function(b,a){b.stop();if(this.element[a].hasClass("open")){this.closeMenu(a)}else{this.openMenu(a)}},openMenu:function(a){$each(this.menu,function(e,d){if(d!==a){this.closeMenu(d)}}.bind(this));this.menu[a].setStyles({left:"auto",right:"auto"});var c,b=this.element[a].getCoordinates(this.element[a].getOffsetParent());c=$("header").getCoordinates().width-b.left-b.width-5;this.menu[a].setStyle("right",c).addClass("open");this.element[a].addClass("open");this.menu[a].fireEvent("open")},loadMenu:function(b,a){this.menu[b].getElement(".update").set("html",'<img src="'+MEDIA_URL+'vine/img/icons/loader-rating.gif" class="fetching" />');var c=new Request.JSON({url:a,onSuccess:function(d){this.menu[b].getElement(".update").set("html",d.html)}.bind(this)});c.send()},closeMenus:function(){$each(this.menu,function(b,a){this.closeMenu(a)}.bind(this))},closeMenu:function(a){this.element[a].removeClass("open");this.element[a+"_menu"].removeClass("open")},initXp:function(){this.element.xp_bar=this.element.userbar.getElement(".exp .xp-bar");this.element.xp_current=this.element.userbar.getElement(".exp .base");this.element.xp_gained=this.element.userbar.getElement(".exp .gained");if(!this.element.xp_bar){return}this.user.xp=this.element.xp_bar.get("title").toInt();this.user.level=this.levelFromXp(this.user.xp);this.element.xp_current.setStyle("width",this.convertXpToPx(this.user.xp-this.xpToLevel(this.user.level)));u_coords=this.element.userbar.getCoordinates();this.element.xp_earned=new Element("ul",{"class":"xp-earned",styles:{width:u_coords.width,top:u_coords.height+u_coords.top+5,right:9}}).inject(this.element.userbar);this.fx.xp=new Fx.Morph(this.element.xp_gained,{link:"chain",transition:"linear"});this.fx.xp.addEvent("complete",function(){this.xp_animating=false;if(this.element.xp_current.getCoordinates().width+this.element.xp_gained.getCoordinates().width===this.element.xp_bar.getCoordinates().width){this.user.level+=1;var a=this.element.userbar.getElement(".exp .lvl").empty();var c=new Element("span",{"class":"pre"}).inject(a);var b=this.user.level+"";$each(b,function(d){var e=new Element("span",{"class":"n"+d}).inject(a)});this.element.xp_current.setStyle("width",0);this.element.xp_gained.setStyle("width",0)}if(this.queue.xp.length>0){this.animateXpBar(this.queue.xp.shift())}}.bind(this))},addXp:function(d){if(!this.ready){this.addToStartupQueue(this.addXp,d);return}this.user.xp+=d;this.animateXpBar(d);var a=new Element("li").inject(this.element.xp_earned,"top"),c=new Element("span",{text:"+ "+d+" XP",styles:{opacity:0}}).inject(a),b=new Fx.Morph(c,{link:"chain",duration:1000}).start({opacity:1}).chain(function(){a.set("morph",{duration:1000});a.morph({"margin-bottom":a.getCoordinates().height*-1,"margin-top":0});this.start({opacity:0})}).chain(function(){a.remove()})},animateXpBar:function(d){if(this.xp_animating){this.queue.xp.push(d);return}this.xp_animating=true;var c,a,b,e;c=this.xpInLevel()-this.convertPxToXp(this.element.xp_current.getCoordinates().width+this.element.xp_gained.getCoordinates().width);a=d<=c?d:c;b=this.convertXpToPx(a);e=b*500/100;this.fx.xp.setOptions({duration:e});this.fx.xp.start({width:this.element.xp_gained.getCoordinates().width+b});if(d-a>0){this.queue.xp.push(d-a)}},levelFromXp:function(a){return Math.floor(Math.sqrt(a/100))+1},xpToLevel:function(a){a=a?a:this.user.level;return 100*Math.pow(a-1,2)},xpInLevel:function(a){a=a?a:this.user.level;return this.xpToLevel(a+1)-this.xpToLevel(a)},convertXpToPx:function(a,b){b=b?b:this.user.level;return a*this.element.xp_bar.getCoordinates().width/this.xpInLevel(b)},convertPxToXp:function(a,b){b=b?b:this.user.level;return a*this.xpInLevel(b)/this.element.xp_bar.getCoordinates().width},getCurrentXp:function(){return this.convertPxToXp(this.element.xp_current.getCoordinates().width)+this.xpToLevel()},initNotifications:function(){this.element.notifications=new Element("div",{id:"notifications",styles:{opacity:0}}).inject(this.element.userbar);this.element.notifications_ul=new Element("ul").inject(this.element.notifications);var a=new Element("a",{text:"x","class":"close"}).inject(this.element.notifications);a.addEvent("click",this.hideNotifications.bindWithEvent(this));this.fx.notifications=new Fx.Tween(this.element.notifications,{property:"opacity"});this.element.notifications.addEvents({mouseenter:this.showNotifications.bind(this).bind(this),mouseleave:function(){this.timer.hide_notifications=setTimeout(this.hideNotifications.bind(this),1000)}.bind(this)});this.fx.notifications.addEvent("complete",function(){if(this.element.notifications.getStyle("opacity")===0){this.element.notifications.setStyle("display","none")}}.bind(this))},addNotification:function(c,b){if(!this.ready){this.addToStartupQueue(this.addNotification,arguments);return}b=b||{};var a=new Element("li",{styles:{opacity:0}}).inject(this.element.notifications_ul,"top");if(b.className){a.addClass(b.className)}if(this.element.notifications_ul.getChildren().length===1){a.addClass("last")}a.adopt(c);a.tween("opacity",1);this.showNotifications();this.timer.hide_notifications=setTimeout(this.hideNotifications.bind(this),10000)},showNotifications:function(a){if(a){(new Event(a)).stop()}if(this.element.notifications_ul.getChildren().length===0){return}$clear(this.timer.hide_notifications);this.element.notifications.setStyle("display","block");this.fx.notifications.cancel().start(1)},hideNotifications:function(a){if(a){(new Event(a)).stop()}this.fx.notifications.cancel().start(0)},addToStartupQueue:function(b,a){this.queue.startup.push({fn:b,args:a})},processStartupQueue:function(){this.queue.startup.each(function(a){a.fn.run(a.args,this)}.bind(this))}});var USERBAR=new VinesUserBar();
