
tm.kyte={};tm.kyte.DEBUG_LEVEL=3;tm.kyte.showServiceURL='/_services/ajax/ajaxController.js?event=kyteShows';tm.kyte.rateServiceURL='/_services/ajax/ajaxController.js?event=rateKyteShow';tm.kyte.RATING_IMAGE_FULL_LRG='ratingAct-lrg.png';tm.kyte.RATING_IMAGE_HALF_LRG='ratingAct_half-lrg.png';tm.kyte.deDupeShows=function(tabCel,elCel,visibleShowCount,extraClassNameExpr){var allShows=$(tabCel+' '+elCel);tm.logger.debug(['DeDuping',tabCel+' '+elCel,allShows],tm.kyte.DEBUG_LEVEL);extraClassNameExpr=extraClassNameExpr?extraClassNameExpr:RegExp('^cell c[0-9]+[^a-z0-9\-]+');allShows.each(function(){var cName=this.className.replace(extraClassNameExpr,'').trim();tm.logger.debug(['DeDuping','Checking class "'+cName+'"'],tm.kyte.DEBUG_LEVEL);var dups=$(tabCel+' .'+cName);if(dups.length>1){tm.logger.debug(['Removing Duplicate',this,dups],tm.kyte.DEBUG_LEVEL);var hiddenDups=dups.filter(':hidden');if(hiddenDups.length==dups.length){hiddenDups.not(':eq(0)').remove();}else{hiddenDups.remove();}
dups.filter(':visible').not(':eq(0)').remove();}});allShows=$(tabCel+' '+elCel);visibleShowCount=visibleShowCount?visibleShowCount:allShows.length;for(var s=0;s<allShows.length;s++){allShows[s].className=allShows[s].className.replace(/ c[0-9]+[^~]*$/,'')+' c'+(s+1);allShows[s].style.display=s<visibleShowCount?'':'none';}}
tm.kyte.pagination=function(pageInfo,contentInfo,htmlInfo,paginationObjectVarName){this._pageInfo=pageInfo;this._pageInfo.deDupeExtras=this._pageInfo.deDupeExtras?this._pageInfo.deDupeExtras:0;this._contentInfo=contentInfo;this._htmlInfo=htmlInfo;this._paginationObjectVarName=paginationObjectVarName;this.showsSkipped={'1':pageInfo.skippedShowCounts};this._updatePaginationHTML=function(pEl,p,tP,pON){pEl.html('<span class="prev'+(p>1?' lnk':'')+'">'+
(p>1?'<a href="javascript:'+pON+'.previousPage();">':'')+'&lt;&nbsp;'+
(p>1?'</a>':'')+'</span>'+'<span class="pageIndex">Page '+p+' of '+tP+'</span>'+'<span class="next'+(p<tP?' lnk':'')+'">'+
(p<tP?'<a href="javascript:'+pON+'.nextPage();">':'')+'&gt;'+
(p<tP?'</a>':'')+'</span>');}
this._showStatusMessage=function(msg,pElS){var pEl=0;for(var p=0;p<pElS.length;p++){pEl=$(pElS[p]);$('.kyte-ajax-status',pEl).remove();if(msg){pEl.append('<div class="kyte-ajax-status"><p class="msg">'+msg+'</p></div>');}}}
this._updatePaginationHTML($(this._htmlInfo.paginationSel),this._pageInfo.currentPage,this._pageInfo.numPages,this._paginationObjectVarName);this._appendItemHTML=function(pEl,i,p,style,siteLifeSpanId){pEl.append('<div class="cell c'+p+' '+i.uniqueClassName+'"'+
(style?' style="'+style+'"':'')+'>'+'<div class="thumb">'+'<span class="corner'+i.channelTitle.toLowerCase()+'icon">&nbsp;</span>'+'<a href="'+i.multimediaHREF+'">'+'<img title="'+i.title+'" alt="'+i.title+'" src="'+i.customThumbURL+'">'+'</a>'+'</div>'+'<div class="meta">'+'<div class="metaTitle">'+'<a href="'+i.multimediaHREF+'">'+i.title+'</a>'+'</div>'+'<div class="metaTime">'+i.showdate+'</div>'+'<div class="metaViews"><span>Views:&nbsp;</span>'+i.totalWatches+'</div>'+'<div class="metaRating">'+tm.kyte.ratingStars.add({"frozenRating":i.rating,"showURI":i.showURI},true)+'</div>'+'<div class="metaComments"><span id="'+siteLifeSpanId+'"></span>'+'</div>'+'</div>'+'</div>');}
this._showComments=function(showUniqueId,siteLifeSpanId){tm.siteLife.daapi.getArticle(showUniqueId,function(article){tm.siteLife.display.displayCommentRecommendCountR2(article,siteLifeSpanId,false,false);});}
this._showItems=function(items,tabSels,ords){var tEl=0;var siteLifeSpanId='sitelife';for(var t=0;t<tabSels.length;t++){tEl=$(tabSels[t]);$('.cell',tEl).remove();if(items[ords[t]]){tm.logger.info(items[ords[t]],tm.kyte.DEBUG_LEVEL);for(var tI=0;tI<items[ords[t]].length;tI++){siteLifeSpanId=tm.siteLife.display.getUniqueId();this._appendItemHTML(tEl,items[ords[t]][tI],tI+1,tI>=this._pageInfo.itemsPerPage?'display:none':'',siteLifeSpanId);this._showComments(items[ords[t]][tI].siteLifeUniqueId,siteLifeSpanId);}
tm.kyte.deDupeShows(tabSels[t],'.cell',this._pageInfo.itemsPerPage);}
if(typeof tm.siteLife!='undefined'){tm.siteLife.daapi.post();}}}
this._getPage=function(pNo){function _getPageAJAXReturn(data){var ords=this._contentInfo.itemOrder.split(',');this.items=data;if(data[ords[0]]){this._pageInfo.currentPage=this.pNo;this.showsSkipped[this.pNo]=data.showsSkipped;this._showItems(data,this._htmlInfo.itemsSel,ords);if(data[ords[0]].length<this._pageInfo.itemsPerPage){this._pageInfo.numPages=this._pageInfo.currentPage;}}else{this._pageInfo.numPages=this._pageInfo.currentPage;}
this._showStatusMessage('',this._htmlInfo.itemsSel);this._updatePaginationHTML($(this._htmlInfo.paginationSel),this._pageInfo.currentPage,this._pageInfo.numPages,this._paginationObjectVarName);}
if(this._pageInfo.currentPage==pNo){return;}
var paginationObject=eval(this._paginationObjectVarName);paginationObject.pNo=pNo;$.ajax({type:'GET',url:tm.kyte.showServiceURL+'&sitename='+tm.kyte.sitename+'&o='+this._contentInfo.itemOrder+'&uri='+this._contentInfo.accountURI+'&x='+this._contentInfo.excludeURI+'&ct='+this._contentInfo.contentType+'&p='+(pNo)+'&c='+this._pageInfo.itemsPerPage+'&ddx='+this._pageInfo.deDupeExtras+'&xc='+(this.showsSkipped[pNo-1]?this.showsSkipped[pNo-1]:'')+'&tk='+this._contentInfo.showThumbKey,context:paginationObject,success:_getPageAJAXReturn,dataType:'json',error:function(){this._showStatusMessage('Error loading page',this._htmlInfo.itemsSel);}});this._showStatusMessage('Loading next page. Please wait...',this._htmlInfo.itemsSel);}
this.nextPage=function(){if(this._pageInfo.currentPage<this._pageInfo.numPages){this._getPage(this._pageInfo.currentPage+1);}else{this._updatePaginationHTML($(this._htmlInfo.paginationSel),this._pageInfo.currentPage,this._pageInfo.numPages,paginationObjectVarName);}}
this.previousPage=function(){if(this._pageInfo.currentPage>1){this._getPage(this._pageInfo.currentPage-1);}else{this._updatePaginationHTML($(this._htmlInfo.paginationSel),this._pageInfo.currentPage,this._pageInfo.numPages,paginationObjectVarName);}}
this.reset=function(){this._getPage(1);}}
tm.kyte.starRate=function(rateElSel,curRating,showURI,objectId,varName){this.setCurrentRating=function(rating){this._curRating=Math.ceil(rating*2)/2;}
this._rateElSel=rateElSel;this._showURI=showURI;this._starRateVarName=varName;this._objectId=objectId;this.setCurrentRating(curRating);this.userRating=typeof getCookieVal!='undefined'?getCookieVal(showURI):'';this.ratingsCount=0;this._stars=$(rateElSel+' img');this.reset=function(){for(var s=0;s<this._curRating;s++){this._stars[s].src=this._stars[s].src.replace(/rating.*$/,(Math.ceil(this._curRating)-1==s&&this._curRating%1==0.5?tm.kyte.RATING_IMAGE_HALF_LRG:tm.kyte.RATING_IMAGE_FULL_LRG));}
for(s;s<5;s++){this._stars[s].src=this._stars[s].src.replace(/rating.*$/,'ratingDeact-lrg.png');}
if(this.userRating){this.active(this.userRating,true,true);}
$(rateElSel+' .rate-count').html(this.ratingsCount<2?'':'Based on '+this.ratingsCount+' ratings');}
this.rate=function(rating){function _rateAJAXReturn(data){tm.logger.info(['rating ',data],tm.kyte.DEBUG_LEVEL);this.setCurrentRating(data.rating);this.userRating=data.userRating;this.ratingsCount=data.ratingCount;if(this.userRating){this._stars.attr('title',data.rating+' - You rated '+
this.userRating+'');}
this.reset();if(typeof setCookieVal!='undefined'){setCookieVal(data.showURI,data.userRating,1000000,'/');}}
if(!this.userRating||!rating){$.ajax({type:'GET',url:tm.kyte.rateServiceURL+'&sitename='+tm.kyte.sitename+
(rating?'&r='+rating:'')+'&u='+this._showURI,context:eval(this._starRateVarName),success:_rateAJAXReturn,dataType:'json',error:function(){tm.logger.error(['Error rating ',this,this.response],tm.kyte.DEBUG_LEVEL);}});}}
this.active=function(starNo,selected,force){if(!force&&this.userRating){return;}
if(selected&&this._stars.length>starNo-1){this._stars[starNo-1].src=this._stars[starNo-1].src.replace(/rating.*$/,'ratingSel-lrg.png');}else{this.reset();}}}
tm.kyte.RatingStars=function(stars){this.ratingEls=[];this.stars=stars;this.add=function(data,needHTML){if(data.frozenRating){var elId='rating_'+this.ratingEls.length;var h='<span id="'+elId+'">'+this._getHTML(data.frozenRating)+'</span>';if(!needHTML){document.write(h);var el=$('#'+elId)[0];el.rating=data;this.ratingEls[this.ratingEls.length]=el;}else{return h;}}}
this.reload=function(){function _reloadReturn(data){var e=0;var els=tm.kyte.ratingStars.ratingEls;for(var r=0;r<data.length;r++){while(data[r].showURI!=els[e].rating.showURI&&e<=els.length){e++;}
if(e==els.length){tm.logger.error(['Error: unable to reload rating for some shows',data],tm.kyte.DEBUG_LEVEL);break;}
$(els[e]).html(tm.kyte.ratingStars._getHTML(data[r].rating));e++;}}
if(this.ratingEls){$.ajax({type:'GET',url:tm.kyte.rateServiceURL+'&q=1&sitename='+tm.kyte.sitename+'&u='+this._getShowURIs(),success:_reloadReturn,dataType:'json',error:function(){tm.logger.error(['Error loading ratings ',this.response],tm.kyte.DEBUG_LEVEL);}});}}
this._getHTML=function(rating){var r=Math.ceil(rating*2)/2;var h='';for(var s=0;s<r;s++){h+='<img src="'+(Math.ceil(r)-1==s&&r%1==0.5?this.stars.half:this.stars.full)+'" alt="'+rating+'" title="'+rating+'"/>';}
for(s;s<5;s++){h+='<img src="'+this.stars.none+'" alt="'+rating+'" title="'+rating+'"/>';}
return h;}
this._getShowURIs=function(){var u='';for(var s=0;s<this.ratingEls.length;s++){u+=(s!=0?',':'')+this.ratingEls[s].rating.showURI;}
return u;}}
tm.kyte.ratingStars=new tm.kyte.RatingStars({"full":tm.imageServer+"design/r2/ratingAct.png","half":tm.imageServer+"design/r2/ratingAct_half.png","none":tm.imageServer+"design/r2/ratingDeact.png"});$(function(){if(tm.kyte.ratingStars.ratingEls.length){tm.kyte.ratingStars.reload();}});
