var PageController=GawkerWidget.extend({widgets:{},fn_queue:{},stop_periodicals:false,gwIds:{},auth:null,document_ready:false,options:{onInit:function(){Logger.debugLog('PageController onInit');},onLogin:function(args){Logger.debugLog('PageController onLogin');args.pc.auth.setUser(args.userdata.user);var hiders=['login'];for(var i=0,l=hiders.length;i<l;i++)args.pc.doStateTransition(hiders[i],function(it){it.hideWidget();});var showers=['logout','profile','replycount'];for(var i=0,l=showers.length;i<l;i++)args.pc.doStateTransition(showers[i],function(it){it.showWidget();});},onLogout:function(args){Logger.debugLog('PageController onLogout');args.pc.auth.setUser(null);var hiders=['logout','profile','replycount'];for(var i=0,l=hiders.length;i<l;i++)args.pc.doStateTransition(hiders[i],function(it){it.hideWidget();});var showers=['login'];for(var i=0,l=showers.length;i<l;i++)args.pc.doStateTransition(showers[i],function(it){it.showWidget();});},onAuthSuccess:function(args){Logger.debugLog('PageController onAuthSuccess');var hiders=['login'];for(var i=0,l=hiders.length;i<l;i++)args.pc.doStateTransition(hiders[i],function(it){it.hideWidget();});var showers=['logout','profile','replycount','hybridlink','poststatustoggler'];for(var i=0,l=showers.length;i<l;i++)args.pc.doStateTransition(showers[i],function(it){it.showWidget();});},onAuthFailure:function(args){Logger.debugLog('PageController onAuthFailure');var hiders=['logout','profile','replycount','poststatustoggler'];for(var i=0,l=hiders.length;i<l;i++)args.pc.doStateTransition(hiders[i],function(it){it.hideWidget();});var showers=['login','hybridlink'];for(var i=0,l=showers.length;i<l;i++)args.pc.doStateTransition(showers[i],function(it){it.showWidget();});},onGetUserState:function(){},onCommentListUpdated:function(args){Logger.debugLog('PageController::onCommentListUpdated event fires');}},initialize:function(options){this.setOptions(options);this.busy_flags={};this.setupWidgets();this.initAuth();this.fireEvent('onInit');},initAuth:function(){var authOptions=this.options.auth_options.options;jQuery.extend(authOptions,this.options.auth_options.eventHandlerBuilder(this));jQuery().GawkerAuth(authOptions);this.auth=jQuery().data('GawkerAuth');this.auth.getUserState();},setupWidgets:function(){this.last_count=0;for(var widgettype in this.options.widgets){if(this.options.widgets.hasOwnProperty(widgettype)){this.widgets[widgettype]=[];this.fn_queue[widgettype]=[];}}
this.periodical=window.setInterval(this.goSetupWidgets.bind(this),this.options.scan_interval);return true;},goSetupWidgets:function(){var containers=jQuery('.gawkerwidget');for(var i=0,l=containers.length;i<l;i++){var actual_container=jQuery(containers[i]);for(var widgettype in this.options.widgets){if(this.options.widgets.hasOwnProperty(widgettype)){if(!this.widgets[widgettype]){this.widgets[widgettype]=[];this.fn_queue[widgettype]=[];}
if(actual_container.hasClass('gawkerWidget_'+widgettype)){continue;}
for(var selector in this.options.widgets[widgettype].byselector){if(this.options.widgets[widgettype].byselector.hasOwnProperty(selector)){var act_class=selector.substring(1);if(actual_container.hasClass(act_class)){var terminating_el=jQuery(this.options.widgets[widgettype].byselector[selector]['terminating_el'],containers[i]);if(terminating_el.length>0){this.last_count=i;Logger.debugLog('found new widget '+widgettype+'['+selector+']');actual_container.addClass('gawkerWidget_'+widgettype);actual_container.addClass('gwId_'+this.gwId());var myoptions=jQuery.extend({},this.options.widgets[widgettype].byselector[selector].options);if(this.options.widgets[widgettype].byselector[selector].eventHandlerBuilder){jQuery.extend(myoptions,this.options.widgets[widgettype].byselector[selector].eventHandlerBuilder(this));}else{jQuery.extend(myoptions,this.options.widgets[widgettype].eventHandlerBuilder(this));}
jQuery.extend(myoptions,{'pagecontroller_instance':this});try{actual_container[this.options.widgets[widgettype]['klass']](myoptions);var brandnew_widget=actual_container.data(this.options.widgets[widgettype]['klass']);this.widgets[widgettype].push(brandnew_widget);for(var ii=0,ll=this.fn_queue[widgettype].length;ii<ll;ii++){this.fn_queue[widgettype][ii](brandnew_widget);}}catch(e){Logger.debugLog('Error while initializing widget:');Logger.debugLog(e);}
myoptions={};}}}}}}}},cleanWidgets:function(){Logger.debugLog('checking widgets');for(var widgettype in this.options.widgets){oldWidgets=[];if(this.widgets[widgettype]&&this.widgets[widgettype].length>0){for(var i=0,l=this.widgets[widgettype].length;i<l;i++){var gwId=this.widgets[widgettype][i].getParam('gwId');if(jQuery('.gwId_'+gwId).length>0){oldWidgets.push(this.widgets[widgettype][i]);}}
this.widgets[widgettype]=oldWidgets;}}},finishSetup:function(){Logger.debugLog('finish setup fires');window.clearInterval(this.periodical);this.goSetupWidgets();this.document_ready=true;document.write=function(t){Logger.debugLog('document.write called after dom ready');if(jQuery('#canvas').length==0){var c=document.createElement('div');jQuery(c).attr('id','canvas').hide().appendTo('body');}
var canvas=jQuery('#canvas');var parts=t.split('<script');var id=0;for(var i=0,l=parts.length;i<l;i++){if(i%2==1)parts[i]='<script'+parts[i];canvas.append(parts[i]);}}},doStateTransition:function(widgettype,fn){if(this.widgets[widgettype])
{for(var i=0,l=this.widgets[widgettype].length;i<l;i++){fn(this.widgets[widgettype][i]);}
this.fn_queue[widgettype].push(fn);}},setupWidgetGroup:function(widgetoptions,widgettype){if(!this.busy_flags[widgettype]){this.busy_flags[widgettype]=true;for(var selector in widgetoptions.byselector){if(!jQuery(selector)[widgetoptions['klass']]){Logger.debugLog('Failed to load '+widgetoptions['klass']+' on selector: '+selector);continue;}
if(widgetoptions.byselector.hasOwnProperty(selector)){var containers=jQuery(selector);for(var i=0,l=containers.length;i<l;i++){var actual_container=jQuery(containers[i]);if(actual_container.hasClass('gawkerWidget_'+widgettype)){continue;}
var terminating_el=jQuery(widgetoptions.byselector[selector]['terminating_el'],containers[i]);if(terminating_el.length>0){Logger.debugLog('found new widget '+widgettype+'['+selector+']');actual_container.addClass('gawkerWidget_'+widgettype);actual_container.addClass('gwId_'+this.gwId());var myoptions=jQuery.extend({},widgetoptions.byselector[selector].options);if(widgetoptions.byselector[selector].eventHandlerBuilder){jQuery.extend(myoptions,widgetoptions.byselector[selector].eventHandlerBuilder(this));}else{jQuery.extend(myoptions,widgetoptions.eventHandlerBuilder(this));}
jQuery.extend(myoptions,{'pagecontroller_instance':this});try{actual_container[widgetoptions['klass']](myoptions);var brandnew_widget=actual_container.data(widgetoptions['klass']);}catch(e){Logger.debugLog('Error while initializing widget:');Logger.debugLog(e);}
this.widgets[widgettype].push(brandnew_widget);for(var ii=0,ll=this.fn_queue[widgettype].length;ii<ll;ii++){this.fn_queue[widgettype][ii](brandnew_widget);}
myoptions={};}}}}
if(jQuery('body'))
{}
if(this.stop_periodicals==true)
{window.clearInterval(this.periodicals[widgettype]);}
this.busy_flags[widgettype]=false;}},getCommenterpageUsername:function(){var parts=window.location.pathname.split('?')[0].split('/');return parts[2];},gwId:function(){var gwId=0;while(gwId==0||this.gwIds[gwId]==true){gwId=Math.floor(Math.random()*10000);}
this.gwIds[gwId]=true;return gwId;},writeGeoHTML:function(geoip,rawhtml){if(this.auth&&this.auth.geoip&&this.auth.geoip===geoip){document.write(rawhtml);}}});var special_handlers={onInit:function(){Logger.debugLog('commenterpage controller onInit');args.pc.doStateTransition('hybrid',function(it){it.loadMe();});},onAuthSuccess:function(args){Logger.debugLog('commenterpage controller onAuthSuccess');var hiders=['login'];for(var i=0,l=hiders.length;i<l;i++)args.pc.doStateTransition(hiders[i],function(it){it.hideWidget();});var showers=['logout','profile','replycount','statusupdate','buddytoggle','postclip','hybridlink','commentadmin'];for(var i=0,l=showers.length;i<l;i++)args.pc.doStateTransition(showers[i],function(it){it.showWidget();});args.pc.doStateTransition('profileeditor',function(it){it.setUser(args.pc.auth.user);});},onAuthFailure:function(args){Logger.debugLog('commenterpage controller onAuthFailure');var hiders=['logout','profile','replycount','statusupdate','commentadmin'];for(var i=0,l=hiders.length;i<l;i++)args.pc.doStateTransition(hiders[i],function(it){it.hideWidget();});var showers=['login','hybridlink'];for(var i=0,l=showers.length;i<l;i++)args.pc.doStateTransition(showers[i],function(it){it.showWidget();});args.pc.doStateTransition('profileeditor',function(it){it.setUser(args.pc.auth.user);});},fbFriendsUpdated:function(args){args.pc.goSetupWidgets();args.pc.doStateTransition('buddytoggle',function(it){it.showWidget();});}};pcc=new PageController(jQuery.extend(settings,special_handlers));jQuery(document).ready(function(){pcc.finishSetup();});

