(function(b){var a={init:function(c){this.each(function(){var e=b(this);var d=e.data("glhunger");if(!d){d={started:true,hungry:false,friends:{},chats:{},parts:{}};e.find(".btn_im_hungry").click(b.proxy(a.toggleHungry,this));e.data("glhunger",d);b.comet.callbacks.hungry=b.proxy(a.update,e);if(e.attr("gulu:hungry")){b.comet.onReady(function(){e.glhunger("startHungry","get_hungry")})}}});return this},destroy:function(){return this.each(function(){})},update:function(c){this.glhunger("update_"+c.type,c.data);return this},update_start:function(c){this.glhunger("_addFriends",c)},update_stop:function(d){var e=this;var c=this.data("glhunger")["friends"];b.each(d,function(g,f){if(!(g in d)){return}delete c[g];e.glhunger("_removeFriendBox",g)})},update_showchat:function(c){this.glhunger("_addChats",[c])},update_hidechat:function(c){this.glhunger("_removeChat",c)},update_joinchat:function(c){this.glhunger("_addChats",[c])},update_expire:function(c){this.glhunger("_doStop")},chat_update:function(d){var c=d.key;if(d.type=="participant"){if(d.is_gone){this.glhunger("_removeParticipant",c,d)}else{this.glhunger("_addParticipant",c,d)}}else{if(d.type=="message"){this.glhunger("_addNotify",c)}}},toggleHungry:function(c){c.preventDefault();var d=b(this);if(d.data("glhunger")["hungry"]){d.glhunger("stopHungry")}else{d.glhunger("startHungry")}},startHungry:function(h){var g=this.find(".hungry_str").html();var f=this.find(".hungry_btn_str").html();var e=this.find(".stop_hungry_str").html();var c=this.find(".stop_hungry_btn_str").html();this.data("glhunger")["hungry"]=true;this.find("h3").html(g);this.find(".btn_im_hungry").text(f).addClass("red");var d=this;if(!h){h="start_hungry"}b.comet.call({method:h,data:{},success:function(i){d.glhunger("_addFriends",i.friends);d.glhunger("_addChats",i.chats,true);d.find(".solo-friends").show()}})},stopHungry:function(){this.glhunger("_doStop");b.comet.call({method:"stop_hungry",data:{}})},friend_click:function(c){var d=this;b.comet.call({method:"create_chat",data:{f:c},success:function(e){d.glhunger("_addChats",[e.chat]);d.glhunger("_showChat",e.chat["chat"])}})},chat_click:function(c){var f=b(c);var d=f.attr("gulu:chat");if(f.hasClass("active")){this.glhunger("_showChat",d);return}var e=this;b.comet.call({method:"join_chat",data:{chat:d},success:function(g){e.glhunger("_addChats",[g.chat]);e.glhunger("_showChat",g.chat["chat"])}})},_doStop:function(){var c=this.data("glhunger");var g=this.find(".hungry_str").html();var f=this.find(".hungry_btn_str").html();var e=this.find(".stop_hungry_str").html();var d=this.find(".stop_hungry_btn_str").html();c.hungry=false;c.friends={};c.chats={};this.find("h3").html(e);this.find(".btn_im_hungry").text(d).removeClass("red");this.find(".solo-friends, .chats").hide().find("ul").remove();b("#chat-content").empty();b("#page-content").show()},_addFriends:function(c){var e=this;var d=this.data("glhunger")["friends"];b.each(c,function(g,h){if(g in d){return}d[g]=h;var f=b("<ul><li /></ul>");f.find("li").html(h.nickname);f.attr("gulu:uid",g);f.click(function(){e.glhunger("friend_click",g)});e.find(".solo-friends").append(f)})},_addChats:function(f,d){var e=this;var c=this.data("glhunger")["chats"];b.each(f,function(l,o){var m=o.chat;if(m in c&&c[m].hasClass("active")){return}var n="messages" in o;var p=gCHAT_TEMPLATE.clone().hide();p.attr("gulu:glchat",m);var h=b('<ul><div class="notify">0</div></ul>');if(n){h.addClass("active")}h.attr("gulu:chat",m);h.click(function(){e.glhunger("chat_click",this)});if(m in c){e.find(".chats ul").each(function(){if(b(this).attr("gulu:chat")==m){b(this).replaceWith(h)}})}else{e.find(".chats").append(h)}c[m]=p;e.data("glhunger")["parts"][m]=o.participants;e.glhunger("_updateParticipants",m);e.find(".chats").show();if(n){p.glchat();p.addClass("active");b("#chat-content").append(p);var k="update-"+m+".glchat";b("#chat-content").trigger(k,[{type:"init",data:o}]);b.comet.callbacks[m]=b.proxy(a.chat_update,e)}else{if(d){for(var g=0;g<o.participants.length;g++){if(o.participants[g].user_id==gUSER_ID&&!o.participants[g]["is_gone"]){b.comet.call({method:"join_chat",data:{chat:m},success:function(i){e.glhunger("_addChats",[i.chat])}})}}}}})},_removeChat:function(c){var d=c.chat;var e=this;b.each(this.data("glhunger")["parts"][d],function(g,f){e.glhunger("_removeParticipant",d,f)});this.find(".chats ul").each(function(f,g){if(b(g).attr("gulu:chat")==d){b(g).remove()}});this.data("glhunger")["chats"][d].remove();delete this.data("glhunger")["chats"][d]},_addParticipant:function(d,c){var e=this.data("glhunger")["parts"][d];if(!e){return}var f=true;b.each(e,function(g,h){if(h.user_id==c.user_id){h.gone=false;f=false}});if(f){e.push(c)}this.glhunger("_updateParticipants",d)},_removeParticipant:function(f,d){var c=this.data("glhunger")["parts"];var j=c[f];if(!j){return}for(var e=0;e<j.length;e++){var h=j[e];if(h.user_id==d.user_id){h.gone=true;break}}this.glhunger("_updateParticipants",f);var g=false;var k=this;this.find(".chats ul").each(function(m,l){var n=b(l).attr("gulu:chat");if(d.user_id in c[n]){g=true}});if(!g){this.glhunger("_showFriendBox",d.user_id,true)}},_updateParticipants:function(c){var d=this.data("glhunger")["parts"][c];if(!d){return}var e=this;this.find(".chats ul").each(function(){var f=b(this);if(f.attr("gulu:chat")==c){f.find("li").remove();var g=[];b.each(d,function(k,j){if(!j.gone){g.push(j)}});var h=g.length-1;b.each(g,function(k,j){var l=b("<li />").text(j.display_name);if(k<h){l.append(", ")}f.append(l);e.glhunger("_showFriendBox",j.user_id,false)})}})},_showChat:function(c){b("#page-content").hide();b("#chat-content .glchat").each(function(){var d=b(this);if(d.attr("gulu:glchat")==c){d.show()}else{d.hide()}});this.find(".chats ul").each(function(){var d=b(this);if(d.attr("gulu:chat")==c){d.addClass("current");d.find(".notify").html("0").hide()}else{d.removeClass("current")}})},_showFriendBox:function(d,c){this.find(".solo-friends ul").each(function(){if(b(this).attr("gulu:uid")==d){if(c){b(this).show()}else{b(this).hide()}}})},_removeFriendBox:function(d,c){this.find(".solo-friends ul").each(function(){if(b(this).attr("gulu:uid")==d){b(this).remove()}})},_addNotify:function(c){this.find(".chats ul").each(function(){var f=b(this);if(f.attr("gulu:chat")==c){if(f.hasClass("current")){return}var e=f.find(".notify");var d=parseInt(e.text(),10);e.text(d+1).show()}})}};b.fn.glhunger=function(c){if(a[c]){return a[c].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof c==="object"||!c){return a.init.apply(this,arguments)}else{b.error("Method "+c+" does not exist on jQuery.glhunger")}}return this}})(jQuery);$(function(){$(".im_hungry").glhunger()});
