var VodpodRibbon={};VodpodRibbon.NTVGuide=new Class({pod_id:'newteevee',api_key:'67314c99874a76b6',url_base:'http://api.vodpod.com/api/pod/videos.js',videos:[],total_videos:0,current_page:1,active_video:null,videos_node:null,rating_widget:null,vodpod_widget:null,per_page:7,badge:true,pending_data:0,panes:[],paneAddedCallback:null,lastPageReq:0,cacheRequestThreshold:3,cachePPR:6,cacheImageThreshold:2,dir:0,initialize:function(pod_id,url_base,tag,mash,badge,hide_stars){if(pod_id)
this.pod_id=pod_id
if(url_base)
this.url_base=url_base
if(tag)
this.tag=tag
if(mash)
this.mash=mash
if(badge!='hide'&&badge)
this.badge=true
if(hide_stars)
this.hide_stars=true
window.addEvent('domready',function(){this.videos_node=$('video_thumbnails')
this.vodpod_widget=$('vodpod_widget')
$('vodpodWindowCloseRibbon').addEvent('click',function(e){e.stop()
$('video_embed').innerHTML=''
$('video_pane').setStyle('display','none')}.bindWithEvent(this))
$(document.body).adopt($('video_pane'))
this.paneAddedCallback=function(){this.paneAddedCallback=null
this.displayPage(1)}.bind(this)
this.requestVideos({'per_page':this.cachePPR*this.per_page})}.bind(this))},requestVideos:function(options){this.pending_data++
var opts=(options)?options:{}
if(!opts.category_id)
opts.category_id=null
if(!opts.page)
opts.page=1
if(!opts.sort)
opts.sort='date-desc'
if(!opts.per_page)
opts.per_page=this.cachePPR*this.per_page
if(!opts.tag)
if(this.tag)
opts.tag=this.tag
if(!opts.mash)
if(this.mash)
opts.mash=this.mash
this.lastPageReq++
url=this.url_base+'?pod_id='+this.pod_id+"&api_key="+this.api_key+"&callback=vodpod_ribbon.addVideos&per_page="+opts.per_page
if(opts.category_id)
url+="&category_id="+opts.category_id
if(opts.page)
url+="&page="+opts.page
if(opts.sort)
url+="&sort="+opts.sort
if(opts.tag)
url+='&tag_id='+opts.tag
if(opts.mash)
url+='&mash='+opts.mash
console.log(url)
$(document.body).adopt(new Element('script',{'src':url}))},displayPage:function(page){var styleArrows=function(){if(this.total_videos>this.current_page*this.per_page)
$('video_arrow_right').removeClass('inactive')
else
$('video_arrow_right').addClass('inactive')
if(this.current_page>1)
$('video_arrow_left').removeClass('inactive')
else
$('video_arrow_left').addClass('inactive')}.bind(this)
if(this.videos.length==1){this.vodpod_widget.setStyle('height',this.vodpod_widget.getSize().size.y-$('selector_pane').getSize().size.y)
$('selector_pane').setStyle('display','none')}
if(!this.videos_node.getFirst()){if(page!=1||this.videos.length==0||this.current_page!=1)
return
for(var i=0;i<this.cacheImageThreshold&&i<this.panes.length;i++)
this.panes[i].loadThumbs()
this.videos_node.adopt(this.panes[0].element)
this.panes[0].element.setStyle('display','block')
styleArrows()
}else{var current_pane=this.panes[this.current_page-1].element
console.log(this.current_page+' : '+this.panes.length)
if(this.current_page>=this.panes.length){var overlay=new Element('div').setStyles({'position':'absolute','top':0,'left':0,'background-color':'#333','z-index':20,'width':'100%','height':'100%','opacity':.9}).setHTML('loading, please wait...').injectTop($('selector_pane'))
this.paneAddedCallback=function(){this.paneAddedCallback=null
overlay.remove()
this.loadNext()}.bind(this)
return}
this.panes[page-1].element.setStyles({'left':this.dir*current_pane.getSize().size.x,'top':0,'height':current_pane.getSize().size.y,'width':current_pane.getSize().size.x})
this.videos_node.adopt(this.panes[page-1].element)
this.panes[page-1].loadThumbs()
this.panes[page-1].element.setStyle('display','block')
new Fx.Elements([current_pane,this.panes[page-1].element],{onComplete:function(){current_pane.setStyle('display','none')
styleArrows()}.bind(this),duration:500,transition:Fx.Transitions.linear}).start({'0':{left:[0,this.dir*-1*current_pane.getSize().size.x]},'1':{left:[this.dir*current_pane.getSize().size.x,0]}})}
this.current_page=page},addVideos:function(json){console.log(json)
if(json.videos.items.length<1||json.videos.total<1){if(this.videos.length<1)
$('vodpod_widget').setStyle('display','none')
return}
this.total_videos=json.videos.total
var pane=null
json.videos.items.each(function(item,i){if(i%this.per_page==0){pane=new VodpodRibbon.VideoPane(this)
this.panes.push(pane)}
var v=new VodpodRibbon.Video(item.video,this)
this.videos.push(v)
pane.addVideo(v)}.bind(this))
this.pending_data--
if(this.paneAddedCallback)
this.paneAddedCallback()},loadNext:function(){$('video_arrow_right').addClass('inactive')
$('video_arrow_left').addClass('inactive')
this.dir=1
this.displayPage(this.current_page+1)
for(var i=this.current_page+1;i<this.current_page+this.cacheImageThreshold;i++){if(i<this.panes.length)
this.panes[i].loadThumbs()}
if(((this.current_page+this.cacheRequestThreshold)*this.per_page)/(this.cachePPR*this.per_page)>=this.lastPageReq)
this.requestVideos({'page':this.lastPageReq+1})
return false},loadPrevious:function(){$('video_arrow_right').addClass('inactive')
$('video_arrow_left').addClass('inactive')
this.dir=-1
this.displayPage(this.current_page-1)
return false}})
VodpodRibbon.VideoPane=new Class({gallery:null,videos:[],element:null,thumbsLoaded:false,initialize:function(gallery){this.gallery=gallery
this.element=new Element('div').setStyles({'position':'absolute','display':'none'}).injectInside(document.body)},addVideo:function(video){this.videos.push(video)
this.element.adopt(video.createElement())},loadThumbs:function(){if(!this.thumbsLoaded){this.thumbsLoaded=true
this.videos.each(function(video){video.loadThumb()})}}
})
VodpodRibbon.Video=new Class({id:null,pod_id:null,title:null,description:null,tags:null,created_at:null,link:null,embed_tag:null,autoplay_embed_tag:null,thumbnail_small:null,thumbnail_medium:null,total_views:null,weekly_views:null,num_collectors:1,node:null,gallery:null,initialize:function(video,gallery){this.gallery=gallery
this.id=video.video_id
this.pod_id=video.pod_id
this.title=video.title
this.description=video.description
this.tags=video.tags
this.created_at=new Date(video.created_at)
this.updated_at=new Date(video.updated_at)
this.link=video.link
this.embed_tag=video.embed_tag
this.autoplay_embed_tag=video.autoplay_embed_tag
this.thumbnail_small=video.thumbnails.small
this.thumbnail_medium=video.thumbnails.medium
this.total_views=video.stats.total_views
this.weekly_views=video.stats.weekly_views
this.num_collectors=video.num_collectors
this.user_rating=video.user_rating
if(video.edit_rating)
this.edit_rating=video.edit_rating
if(video.permalink)
this.permalink=video.permalink
if(video.featured)
this.featured=1
else
this.featured=0
},createElement:function(container){var video_date=this.formattedDate()
var thumb_class='video_thumb'
var thumb=new Element('img',{'class':'thumbnail','src_c':this.thumbnail_medium.substr(0,this.thumbnail_medium.length-7)+'100.jpg','events':{click:this.loadVideo.bindWithEvent(this,true)}})
this.node=new Element('div',{'id':'vp_video_'+this.id,'class':thumb_class,'video_id':this.id})
.adopt(thumb)
if(!this.previousVideo())
this.node.adopt(new Element('span',{'class':'ourPick'}))
this.node.adopt(new Element('div',{'class':'video_title','events':{click:this.loadVideo.bindWithEvent(this,true)}}).setHTML(((this.tags.match('nsfw'))?'<span class="nsfw">(NSFW)</span> ':'')+this.title))
this.loadThumb=function(){if(!thumb.getProperty('src')){thumb.setProperty('src',thumb.getProperty('src_c'))
thumb.removeProperty('src_c')}}
return this.node},loadVideo:function(e,autoplay){var embed=(autoplay==true)?this.autoplay_embed_tag:this.embed_tag
$('video_embed').innerHTML=embed
if(this.featured&&this.permalink)
$('video_title').innerHTML='<a href="'+this.permalink+'">'+this.title+'</a>'
else
$('video_title').innerHTML=this.title
$('video_description').innerHTML=this.description
if(this.featured)
new Element('span').addClass('bigOurPick').setHTML('&nbsp;').injectAfter($('video_embed'))
else
if($('video_embed').getNext().getTag()=='span'){$('video_embed').getNext().remove()}
if(this.featured&&this.permalink)
$('video_readmore').innerHTML='<a class="title" href="'+this.permalink+'">Read more&nbsp&raquo;</a>'
else
$('video_readmore').innerHTML=''
$('video_ntv_extras').innerHTML=''
$('video_pane').setStyle('visibility','hidden')
$('video_pane').setStyle('display','block')
$('video_pane').setStyle('top',Window.getScrollTop()+50)
$('video_pane').setStyle('left',(Window.getScrollWidth()/2)-($('video_pane').getSize().size.x/2))
$('video_pane').setStyle('visibility','visible')
this.gallery.active_video=this},previousVideo:function(){var index=this.gallery.videos.indexOf(this)
if(index==0)
return null
return this.gallery.videos[index-1]},nextVideo:function(){var index=this.gallery.videos.indexOf(this)
if(index==4)
return null
return this.gallery.videos[index+1]},formattedDate:function(){var months=['January','February','March','April','May','June','July','August','September','October','November','December']
return months[this.updated_at.getMonth()]+' '+this.updated_at.getDate()},staticStars:function(rating){return'<span class="rating"><span class="current" style="width:'+(rating/5*100)+'%"></span></span>'}})