
Application.Page=new Class({Extends:APE.Client,Implements:[Application.Modules.AchievementOverlay,Application.Modules.IUnlockedStar,Application.Modules.ITimer,Application.Tools.Shortcut,Application.Tools.RandomStringGenerator,Application.Modules.ITopbar,Application.Modules.ISocialize,Application.Tools.ScoreUpdater,Application.Tools.StarUpdater,Application.Modules.IVideo,Application.Modules.IPlaxo,Application.Modules.ISheet],lowerPriceTriggers:null,apeIsReady:false,apeChannels:['*main'],initialize:function(){this.lazyloadImages();this.joinLimit=20;this.joinTracker=new Application.Tools.Tracker();this.setupAchievementOverlay();this.initializeUnlockedStar();this.facebookRoutines=Facebook.getSingleton();this.lowerPriceTriggers=new Application.Modules.LowerPriceTriggerBehavior();if(!this.isGuestUser()){this.initializeTopbar();this.lowerPriceTriggers.addEvent('itemReport',this.UserCoins.CoinReloader.execute.bind(this.UserCoins.CoinReloader));this
this.lowerPriceOverlay=new Application.Modules.LowerPriceOverlay(this);}
this.formatTimers();this.initializeSocialize();this.setupLinks();this.setupFooter();this.initPlaxo();this.initializeSheet();var t=new Application.Tools.Touch($('#layout-container'));this.loginWidget=new Application.Modules.ILogin();this.registerWidget=new Application.Modules.IRegister();this.initializeApe();this.initializeBrowserFixes();this.initializeNavScrollable();this.performGoogleAnalytics();this.initBasicTooltips();},isGuestUser:function(){return($_YOTTA_.User.uid=='guest');},initBasicTooltips:function(){$targets=$('#featured-event').find('a[title]').add('div[title]');Application.Tools.BasicTooltip.initTooltips($targets);},initializeNavScrollable:function(){$('.nav-scroll').each(function(){new Application.Modules.NavScroller($(this),{});});},initializeBrowserFixes:function(){$('.tab-stop').keypress(function(e){if(e.keyCode==9){return false;}});$('#side-nav a, #side-nav button, #side-nav .nav-group > li, #side-nav .nav-list-box h4').live({mouseover:function(){$(this).addClass('hover');},mouseout:function(){$(this).removeClass('hover');}});},initializeApe:function(){this.addEvent('load',this.start);this.addEvent('ready',this.onReady);this.addEvent('init',this.onInit);this.onRaw('Debug',this.debug);this.onRaw('ident',this.onIdent);this.addEvent('onRaw',this.genericRawHandler);this.onCmd('CHECK',this.checkChannels);this.addEvent('multiPipeCreate',this.createPipe);this.addEvent('userJoin',this.createUser);},lazyloadImages:function(){$('img.ll').each(function(){var $img=$(this);var newsrc=$img.data('real-src');if(newsrc==null){newsrc=$img.attr('rel');}
$img.attr('src',newsrc);$img.removeClass('ll');});},performGoogleAnalytics:function(){if($_YOTTA_.GoogleAnalytics&&$_YOTTA_.GoogleAnalytics._trackPageview){Application.Tools.GoogleAnalytics.trackPageView($_YOTTA_.GoogleAnalytics._trackPageview);}},initializeBrowserFixes:function(){$('.tab-stop').keypress(function(e){if(e.keyCode==9){console.log('tab detected');return false;}});$('#side-nav .nav-group > li, #side-nav .nav-list-box h4').bind('mouseover',function(){$(this).addClass('hover')}).bind('mouseout',function(){$(this).removeClass('hover');});},initializeApe:function(){this.addEvent('load',this.start);this.addEvent('ready',this.onReady);this.addEvent('init',this.onInit);this.onRaw('Debug',this.debug);this.onRaw('ident',this.onIdent);this.addEvent('onRaw',this.genericRawHandler);this.onCmd('CHECK',this.checkChannels);this.addEvent('multiPipeCreate',this.createPipe);this.addEvent('userJoin',this.createUser);this.onError('004',function(){console.log("bad session caught");this.core.clearSession();this.load({identifier:'soshopping',channel:this.apeChannels});});if(window.location.protocol!='https:'){this.load({identifier:'soshopping',channel:this.apeChannels});}},initializeTicker:function(){this.ticker=new Application.Modules.ITicker(this);},checkChannels:function(){var currentChannels=this.currentChannels();var toJoin=[];var iterator=function(requiredChannel){if(!currentChannels.contains(requiredChannel)){if(this.joinTracker.track(requiredChannel)<this.joinLimit){toJoin.push(requiredChannel);}}};this.apeChannels.each(iterator,this);if(toJoin.length<=0){return;}
this.addChannel(toJoin);},currentChannels:function(){var currentChannels=[];this.core.pipes.each(function(channel){currentChannels.push(channel.pipe.properties.name);},this);return currentChannels;},genericRawHandler:function(param){if(param.data.User){this.SetScore(param.data.User.Score);if($_YOTTA_.User.StarCount!==param.data.User.StarCount){this.SetStarCount(param.data.User.StarCount);}
if(param.data.User.UnlockedVideos){param.data.User.UnlockedVideos.each(function(item){$('[data-video='+item+']').closest('.video').removeClass('locked');});}}
if(param.data.Message_Id){this.markMessageAsRead(param.data.Message_Id);}},setupFooter:function(){this.$footer=$('#layout-footer-container');var onMouseOver=function(){$(this).stop(true,false).animate({height:150},1000,'easeOutElastic');};var onMouseOut=function(){$(this).stop(true,false).animate({height:40,bottom:0},1000,'easeOutBounce');};this.$footer.hoverIntent({over:onMouseOver,timeout:500,out:onMouseOut});Application.Tools.fixTabs([this.$footer]);},debug:function(param){},checkMessages:function(){var ajax=new Application.Tools.AjaxRequest({url:'?action=message_notifier.Unread'});ajax.execute();},markMessageAsRead:function(messageId){var request=new Application.Tools.AjaxRequest({url:'?action=message.markasread',data:'messageId='+messageId,type:'post'});request.execute();},removeChannel:function(channelName){if(this.core===null){return;}
var channelNames;if($.isArray(channelName)){channelNames=channelName;}else{channelNames=[channelName];}
if(channelName.length<=0){return;}
this.core.pipes.each(function(channel){if(channelNames.contains(channel.pipe.properties.name)){this.apeChannels.erase(channel.pipe.properties.name);channel.left();}},this);},addChannel:function(channelName){var channelNames;if(!$.isArray(channelName)){channelNames=[channelName];}else{channelNames=channelName;}
channelNames.each(function(channelName){if(!this.apeChannels.contains(channelName)){this.apeChannels.push(channelName);}},this);if(this.apeIsReady&&channelNames.length>0){channelNames.each(function(channel){this.core.join(channel);},this);}},createPipe:function(pipe,options){if($.inArray(pipe.name,this.apeChannels)<0){pipe.left();}},createUser:function(user,pipe){},getCore:function(){return this.core;},start:function(core){var name;if($_YOTTA_.User.uid!=='guest'){name=''+$_YOTTA_.User.uid
+this.generateRandomString(5);this.core.start({'name':name});}else{name='g'+$time().toString();this.core.start({'name':name});}},onInit:function(){},onIdent:function(raw){var apeId=this.apeUserId(raw.data.user.properties.name);if(apeId!==null&&apeId!=$_YOTTA_.User.uid){console.log('bad match. this.apeUserId = "'+apeId
+'", $_YOTTA_.User.uid = "'+$_YOTTA_.User.uid
+'"');this.core.quit();this.load({identifier:'soshopping',channel:this.apeChannels});return false;}},onReady:function(){console.log("calling ready");if($_YOTTA_.messages>0){setTimeout(this.checkMessages,2000);}
this.apeIsReady=true;},setupLinks:function(){Facebook.getSingleton().ready(function(){console.log('all set');$('#layout-header-links a.sign-out').click(function(){if(FB.getSession()!=null){console.log('calling fb.logout');FB.logout();return false;}});});},apeUserId:function(name){if(name===null){return null;}
if(null!==(matches=name.match(/^([\d]+)/))){return matches[1];}else{return"guest";}},facebookRoutine:null,_:{isFunction:function(obj){return!!(obj&&obj.constructor&&obj.call&&obj.apply);}}});Application.Tools.Tracker=new Class({initialize:function(){this.clear();},track:function(name){if(this.counter[name]===null){this.counter[name]=0;}
return this.counter[name]++;},clear:function(){this.counter={};}});Application.Start=function(initClass){$(document).ready(function(){if($splat===null){console.log('splat is not defined, waiting');setInterval(function(){Application.Start(initClass);},500);return;}else{console.log('splat found',$splat);page=new initClass();}});};Element.implement({closest:function(selector){var matches=$$(selector);var cur=this;while(cur&&!matches.contains(cur)){cur=cur.getParent();}
return cur;}});Application.Modules.NavScroller=new Class({Implements:[Options,Events],loaded:[true],options:{items:'div.nav-items',keyboard:false,circular:false,vertical:true,scrollbar:true},initialize:function($element,options){this.$scrollTarget=$element;this.setOptions(options);this.options.onBeforeSeek=this.onBeforeSeek.bind(this);this.options.onSeek=this.onSeek.bind(this);this.options.onAddItem=this.onAddItem.bind(this);this.$scrollTarget.scrollable(this.options);this.$downButton=this.$scrollTarget.prev('h4').find('a.down');this.$upButton=this.$scrollTarget.prev('h4').find('a.up');this.$downButton.click(function(){this.getScrollable().next();}.bind(this));this.$upButton.click(function(){this.getScrollable().prev();}.bind(this));console.log(this.$downButton);this.browserCompat();},browserCompat:function(){$('#side-nav a, #side-nav .nav-group > li, #side-nav .nav-list-box h4').not('.nav-browser-compat').each(function(){$(this).bind({mouseover:function(){$(this).addClass('hover');},mouseout:function(){$(this).removeClass('hover');}}).addClass('nav-browser-compat');});},getURL:function(){return this.$scrollTarget.data('source');},getScrollable:function(){return this.$scrollTarget.scrollable();},onBeforeSeek:function(event,pageNo,undef){if(pageNo<0&&!this.fullyLoaded){return false;}
if(this.fullyLoaded===true){return true;}
if(this.loaded[pageNo]===true){return true;}else if(this.loaded[pageNo]==='loading'){return false;}else{this.loaded[pageNo]='loading';var $targetButton;if(this.getScrollable().getIndex()<=pageNo){$targetButton=this.$downButton;}else{$targetButton=this.$upButton;}
$targetButton.addClass('loading');var request=new Application.Tools.AjaxRequest({url:this.getURL(),async:true,data:{page:pageNo},success:function(result){$targetButton.removeClass('loading');this.onFetchSuccess(result,pageNo);}.bind(this)});request.execute();return false;}},onSeek:function(event,pageNo){console.log(this,pageNo,this.loaded.length,this.fullyLoaded);if(pageNo==0){this.$upButton.addClass('disabled');}else{this.$upButton.removeClass('disabled');}
if(pageNo+2==this.loaded.length&&this.fullyLoaded){this.$downButton.addClass('disabled');}else{this.$downButton.removeClass('disabled');}},onAddItem:function(event){this.browserCompat();},onFetchSuccess:function(html,pageNo){if(html=='done'){this.fullyLoaded=true;this.loaded[pageNo]="end";this.getScrollable().prev(0).end();}else{this.getScrollable().addItem($(html));this.loaded[pageNo]=true;this.getScrollable().seekTo(pageNo);}}});
