/*    HTTP Host:  static.ak.fbcdn.net                                          */
/*    Generated:  July 26th 2009 5:08:01 AM PDT                                */
/*      Machine:  10.16.139.102                                                */
/*       Source:  Backing Store                                                */
/*     Location:  js/connect/connect_widget.js h:csuub73j                      */
/*       Locale:  nu_ll                                                        */
/*         Path:  js/connect/connect_widget.js                                 */


function ConnectWidget(viewer_id,profile_id,button_id){copy_properties(this,{button:$(button_id),profile_id:profile_id,viewer_id:viewer_id,status:null,popup:null,busy:false,new_fan:null});this.popup_uri=new URI('/login.php');this.popup_uri.addQueryData({connect_id:this.profile_id,popup:true});this.button=$(button_id);this.button.listen('click',this.connectClickHandler.bind(this));this.fan_hidden=[];this.fan_shown=[];var connections=DOM.scry(document,'div.connections');if(connections.length>0){connections=connections[0];this.new_fan=DOM.find(connections,'div.grid_item_plus');this.fan_shown.push(this.new_fan);this.fan_shown.push(DOM.find(connections,'span.total_plus'));var totals=DOM.scry(connections,'span.total');var grid_items=DOM.scry(connections,'div.grid_item');if(grid_items.length>1){this.fan_hidden.push(grid_items[1]);}
this.fan_hidden.push(totals[totals.length-1]);}
ConnectWidget.instances[button_id]=this;}
ConnectWidget.instances={};ConnectWidget.prototype.setLoggedIn=function(new_user){this.viewer_id=new_user;return this;}
ConnectWidget.prototype.connectClickHandler=function(e){e.kill();if(this.viewer_id!=0){this.connect();}else{this.popUpLogin();}}
ConnectWidget.prototype.connect=function(){if(!this.busy){this.busy=true;fbpage_set_fan_status(this.button,this.profile_id,true,false,this.connectDoneHandler.bind(this),this.connectErrorHandler.bind(this),this.getAsyncData());}}
ConnectWidget.prototype.disconnect=function(){if(!this.busy){this.busy=true;fbpage_set_fan_status(this.button,this.profile_id,false,false,this.connectUndoneHandler.bind(this),function(){},this.getAsyncData());}}
ConnectWidget.prototype.getAsyncData=function(){var data={source:'connect'};if(ge('post_form_id')){data['post_form_id']=$('post_form_id').value;}
return data;}
ConnectWidget.prototype.popUpLogin=function(){window.open(this.popup_uri.toString(),'_blank','toolbar=0,status=0,width=626,height=400');}
ConnectWidget.prototype.connectErrorHandler=function(response){if(response.error==kError_Async_NotLoggedIn){this.viewer_id=0;this.popUpLogin();}else{hide(this.button);if(response.error==kError_ProfileConnect_AlreadyConnected){this.status=$N('span',{className:'connect_span'},_tx("You are a Fan."));}else{this.status=$N('span',{className:'connect_span'},response.getErrorSummary()+': '+
response.getErrorDescription());}
animation(this.status).from('opacity',0).to('opacity',1).go();DOM.insertAfter(this.button,this.status);}
this.busy=false;}
ConnectWidget.prototype.connectDoneHandler=function(response){var payload=response.getPayload();if(!payload||!payload.feedback){return;}
this.status=$N('span',{className:'connect_span'},HTML(payload.feedback));var undo=$N('a',{className:'undo'},_tx("Undo"));undo.listen('click',function(e){e.kill();this.disconnect();}.bind(this));DOM.appendContent(this.status,undo);animation(this.status).from('opacity',0).to('opacity',1).go();DOM.insertAfter(this.button,this.status);hide(this.button);if(this.new_fan!=null&&payload.connect_fan_div){var new_fan=$N('div',{},HTML(payload.connect_fan_div));DOM.setContent(this.new_fan,DOM.find(new_fan,'a'));}
this.fan_shown.forEach(function(element){animation(element).from('opacity',0).to('opacity',1).go();CSS.removeClass(element,'hidden');});this.fan_hidden.forEach(function(element){CSS.addClass(element,'hidden');});this.busy=false;}
ConnectWidget.prototype.connectUndoneHandler=function(response){var payload=response.getPayload();DOM.remove(this.status);animation(this.button).from('opacity',0).to('opacity',1).go();show(this.button);this.fan_shown.forEach(function(element){CSS.addClass(element,'hidden');});this.fan_hidden.forEach(function(element){CSS.removeClass(element,'hidden');});this.busy=false;}
ConnectWidget.loadStream=function(stream_id,dom_id){new AsyncRequest().setMethod('GET').setURI('/ajax/connect/connect_widget.php').setData({id:stream_id}).setReadOnly(true).setErrorHandler(function(){}).setHandler(function(response){payload=response.getPayload();DOM.setContent($(dom_id),HTML(payload['stream_markup']));}.bind(this)).send();}
ConnectWidget.loggedIn=function(new_user,form_user_check){if(ge('post_form_id')){DOM.replace($('post_form_id'),HTML(form_user_check));}else{DOM.appendContent(DOM.find(document,'body'),HTML(form_user_check));}
for(var id in ConnectWidget.instances){ConnectWidget.instances[id].setLoggedIn(new_user);ConnectWidget.instances[id].connect();}}

if (window.Bootloader) { Bootloader.done(["js\/connect\/connect_widget.js"]); }