
FlashProxy.options.swfLocation="/javascripts/lib/FlashProxy/FlashProxy.swf";if(!window.console){var K=function(){};console={log:K,time:K,timeEnd:K}}
function Player(url,elmStart,elmStop,correctSecondDuration,isMovie,opt){var self=this;var opt=this.opt=opt||{};this.correctSecondDuration=correctSecondDuration;this.isMovie=isMovie;this.url=url;this.startTime=opt.startTime;this.stopButton=new Button(elmStop);this.startButton=new Button(elmStart,true);this.loadBar=new Bar({element:getElement('loadbar'),bgColor:'#eee',fgColor:'#ddd',height:15});this.elmVolumeController=opt['elmVolumeController'];this.elmPlayerBar=opt['elmPlayerBar'];this.elmPlayerClock=opt['elmClock'];addElementClass(elmStart,'not-ready');connect(this.startButton,'onClick',dynamicBind('start',this));connect(this.stopButton,'onClick',dynamicBind('stop',this));connect(window,'onbeforeunload',this,'onUnload');}
Player.DEFAULT_INTERVAL=150;Player.RATE_MAX=2.5;Player.RATE_MIN=0.5;State.make(Player,'plugin',{plain:{load:function(start){start=start||false;if(this.url.match(/https?:\/\/www\.youtube\.com/)){this.changeState('plugin','youtube');}
else if(this.url.match(/https?:\/\/www\.nicovideo\.jp/)){this.changeState('plugin','nicovideo');}
else if(this.url.match(/https?:\/\/www\.ustream\.tv/)){this.changeState('plugin','ustream');}
else if(getPluguinVersion('QuickTime')!=0){this.changeState('plugin','quicktime');}
else if(document.createElement('video').canPlayType){this.changeState('plugin','html5media');}
else if(getPluguinVersion('Flash')!=0){this.changeState('plugin','flash');}
else{this.changeState('plugin','noPlugin');}
this.load(start);}},quicktime:{load:function(start){var self=this;self.wait();var timeout_count=0;var timeout_count_warning=40;var ratio_prev=0;var loading=function(qt){var ratio=qt.getLoadedRatio();if(!(ratio>ratio_prev)){timeout_count++;if(timeout_count==timeout_count_warning){if(typeof messagePanel!='undefined'){messagePanel.show(getMessage('warn.mediaFile'),7);}}}
self.loadBar.setRatio(ratio);ratio_rev=ratio;};var opt={loadingInterval:loading,url:self.url,isMovie:self.isMovie,duration:self.correctSecondDuration,element_id:'emb',autoplay:true};var deffered=QT.create(opt);deffered.addCallback(function(sound){if(!self.correctSecondDuration){self.correctSecondDuration=sound.quicktime.GetDuration()/sound.getTimeScale();}
self.sound=sound;self.setupController();self.onSoundReady(start);if(typeof messagePanel!='undefined'){messagePanel.hide();}});deffered.addErrback(function(err){self.changeState('plugin','html5media');self.load(start);});this.changeState('main','loading');}},html5media:{load:function(start){var self=this;self.wait();var opt={url:self.url,isMovie:self.isMovie,duration:self.correctSecondDuration,showControl:self.opt.showControl,element_id:'emb',autoplay:true};var deffered=H5m.create(opt);deffered.addCallback(function(sound){if(!self.correctSecondDuration){self.correctSecondDuration=sound.getDuration();}
self.sound=sound;self.setupController();self.onSoundReady(start);if(typeof messagePanel!='undefined'){messagePanel.hide();}});deffered.addErrback(function(err){console.log(err);self.error();});this.changeState('main','loading');}},youtube:{load:function(start){var self=this;self.wait();var loading=function(qt){self.loadBar.setRatio(qt.getLoadedRatio());};var opt={loadingInterval:loading,url:self.url,isMovie:self.isMovie,duration:self.correctSecondDuration,element_id:'emb',startTime:self.startTime,autoplay:true};if(getPluguinVersion('Flash')!=0){var deffered=YTs.create(opt);}
else{var deffered=YTi.create(opt);}
removeElementClass(self.startButton.elmButton,'not-ready');self.startButton.disable();deffered.addCallback(function(sound){if(!self.correctSecondDuration){self.correctSecondDuration=sound.getDuration()/1000;}
self.sound=sound;self.setupController();self.onSoundReady(start);if(typeof messagePanel!='undefined'){messagePanel.hide();}});deffered.addErrback(function(err){console.log('E:',err);console.log(err);self.error();});this.changeState('main','loading');}},nicovideo:{load:function(start){var self=this;self.wait();if(typeof messagePanel!='undefined'){messagePanel.show(getMessage('info.push_play_button'),true);}
removeElementClass(self.startButton.elmButton,'not-ready');self.startButton.disable();var opt={id:'external_nico_0',duration:self.correctSecondDuration,height:'240',width:'320'};var deffered=Nicovideo.create(opt);deffered.addCallback(function(sound){if(!self.correctSecondDuration){self.correctSecondDuration=sound.getDuration();}
self.volumeController=new VolumeController(sound,self.elmVolumeController);self.playerBar=new UI.Slider(self.elmPlayerBar,0,self.correctSecondDuration);self.playerClock=new PlayerClock(self.correctSecondDuration,self.elmPlayerClock);var r=sound.getLoadedRatio();self.loadBar.setRatio(r);var refresh=function(pos){var pos=pos||sound.getPosition()/1000;self.playerBar.setValue(pos,true);self.playerClock.update(pos);if(r<1){r=sound.getLoadedRatio();self.loadBar.setRatio(sound.getLoadedRatio());}};self.refresh=refresh;connect(self,'onStart',function(){if(!self.playerBar.timer){self.playerBar.timer=setInterval(function(){refresh();},500);}});connect(self,'onStop',function(){if(self.playerBar.timer){clearTimeout(self.playerBar.timer);self.playerBar.timer=null;}});connect(self.playerBar,'valueChanged',function(val){self.playerClock.update(val);editPage.player.movePosition(val);editPage.getCurrentPanel().atInterval(editPage.player);});self.sound=sound;self.onSoundReady(start);if(typeof messagePanel!='undefined'){messagePanel.hide();}});deffered.addErrback(function(err){self.error();console.log('E:',err);});this.changeState('main','loading');}},ustream:{load:function(start){var self=this;self.wait();var loading=function(qt){self.loadBar.setRatio(qt.getLoadedRatio());};var opt={loadingInterval:loading,url:self.url,isMovie:self.isMovie,duration:self.correctSecondDuration,element_id:'emb',autoplay:true,debug:false,swfPath:'/javascripts/lib/Common/ust/'};var deffered=UST.create(opt);deffered.addCallback(function(sound){if(!self.correctSecondDuration){self.correctSecondDuration=sound.quicktime.getDuration();}
self.volumeController=new VolumeController(sound,self.elmVolumeController);self.playerBar=new UI.Slider(self.elmPlayerBar,0,self.correctSecondDuration);self.playerClock=new PlayerClock(self.correctSecondDuration,self.elmPlayerClock);if(!self.correctSecondDuration){setTimeout(function(){var du=self.sound.getDuration();if(du){self.playerClock=new PlayerClock(du?du/1000:null,self.elmPlayerClock);}},1000);}
var r=sound.getLoadedRatio();self.loadBar.setRatio(r);var refresh=function(pos){var pos=pos||sound.getPosition()/1000;self.playerBar.setValue(pos,true);if(self.playerClock){self.playerClock.update(pos);}
if(r<1){r=sound.getLoadedRatio();self.loadBar.setRatio(sound.getLoadedRatio());}};self.refresh=refresh;connect(self,'onStart',function(){if(!self.playerBar.timer){self.playerBar.timer=setInterval(function(){refresh();},500);}});connect(self,'onStop',function(){if(self.playerBar.timer){clearTimeout(self.playerBar.timer);self.playerBar.timer=null;}});connect(self.playerBar,'valueChanged',function(val){self.playerClock.update(val);editPage.player.movePosition(val);editPage.getCurrentPanel().atInterval(editPage.player);});self.sound=sound;self.onSoundReady(start);if(typeof messagePanel!='undefined'){messagePanel.hide();}});deffered.addErrback(function(err){self.error();console.log('E:',err);});this.changeState('main','loading');}},flash:{load:function(start){var self=this;if(!Cookie.get('alertPlugin')){Cookie.set('alertPlugin',true);alert(getMessage('info.useFlash'));}
FlashProxy.getFirstInstance({callback:function(){setTimeout(function(){var sound=self.sound=new FlashProxy.Library.Sound();sound.loadSound(self.url,true);sound.stop();self.onSoundReady(start);},500);}});this.changeState('main','loading');}},noPlugin:{load:function(start){if(!Cookie.get('alertPlugin')){Cookie.set('alertPlugin',true);alert(getMessage('info.noPlugin'));}}}},'plain');State.make(Player,'main',{notLoaded:{start:function(){var self=this;this.load(true);}},loading:{onSoundReady:function(start){start=start||false;var self=this;var sound=this.sound;self.wait();var elmButton=this.startButton.elmButton;var error_count=0;var interval=300;var timeout=30*1000;var id=setInterval(function(){var duration=sound.getDuration();var loaded=sound.getBytesLoaded();var total=sound.getBytesTotal();if(sound.flash&&duration==0&&loaded==0&&!total){error_count++;if(error_count>(timeout/interval)){clearInterval(id);self.error();}
return;}
if(duration>self.offset||total==loaded){self.changeState('main','stopped');clearInterval(id);removeElementClass(elmButton,'not-ready');elmButton.style.cursor='';elmButton.innerHTML=getMessage('resource.play');self.startButton.enable();if(start){self.start();}}else{toggleElementClass('not-ready',elmButton);elmButton.innerHTML=Math.round(duration/self.offset*100)+'%';}},interval);}},stopped:{start:function(){if(this.sound.getDuration()<this.offset){this.changeState('main','loading');this.onSoundReady(true);return;}
signal(this,'onStart',this);this.setInterval();this.changeState('main','playing');this.sound.start(this.offset/1000);this.startButton.disable();this.stopButton.enable();},setInterval:function(){var self=this;this.intervalId=setInterval(function(){signal(self,'atInterval',self);},this.interval);}},playing:{stop:function(){this.clearInterval();this.changeState('main','stopped');this.sound.stop();this.setSecondOffset(this.getSecondPosition());signal(this,'onStop',this);this.startButton.enable();this.stopButton.disable();},clearInterval:function(){if(!this.intervalId)return;clearInterval(this.intervalId);this.intervalId=null;}}},'notLoaded');MochiKit.Base.update(Player.prototype,{url:'',sound:null,startButton:null,stopButton:null,offset:0,intervalId:null,interval:Player.DEFAULT_INTERVAL,delay:0,onUnload:function(){if(!this.sound)return;this.stop();},setSecondPosition:function(second){if(this.sound){return this.sound.setPosition(second*1000);}},getSecondPosition:function(){return this.sound.getPosition()/1000;},setSecondOffset:function(secondOffset){this.offset=secondOffset*1000;},isPlaying:function(){return this.state['main']=='playing';},toggle:function(){this.isPlaying()?this.stop():this.start();},error:function(){signal(this,'onError');},wait:function(){var elmButton=this.startButton.elmButton;elmButton.style.cursor='wait';},setDelay:function(delay){var diff=this.delay-delay;var sec=this.getSecondPosition();this.delay=delay;var s=(sec-diff<0)?0:sec-diff;this.setSecondPosition(s);this.setSecondOffset(s);},getDelay:function(){return this.delay;},movePosition:function(pos){this.setSecondPosition(pos);this.setSecondOffset(pos);},hideMovie:function(){if(this.isMovie&&this.sound&&this.sound.hideMovie){this.sound.hideMovie();}},showMovie:function(){if(this.isMovie&&this.sound&&this.sound.showMovie){this.sound.showMovie();}},shiftRate:function(up){var rate=this.sound.getRate();this.sound.setRate(up?Math.min(rate+0.1,Player.RATE_MAX):Math.max(rate-0.1,Player.RATE_MIN));},setRate:function(rate){this.sound.setRate(rate);},setupController:function(){var self=this;var sound=self.sound;self.volumeController=new VolumeController(sound,self.elmVolumeController);self.playerBar=new UI.Slider(self.elmPlayerBar,0,self.correctSecondDuration);self.playerClock=new PlayerClock(self.correctSecondDuration||self.getDuration(),self.elmPlayerClock);var r=sound.getLoadedRatio();var refresh=function(pos){if(typeof pos=='undefined'){pos=sound.getPosition()/1000;}
self.playerBar.setValue(pos,true);self.playerClock.update(pos);if(r<1){r=sound.getLoadedRatio();self.loadBar.setRatio(sound.getLoadedRatio());}};self.refresh=refresh;connect(self,'onStart',function(){if(!self.playerBar.timer){self.playerBar.timer=setInterval(function(){refresh();},500);}});connect(self,'onStop',function(){if(self.playerBar.timer){clearTimeout(self.playerBar.timer);self.playerBar.timer=null;}});connect(self.playerBar,'valueChanged',function(val){self.playerClock.update(val);editPage.player.movePosition(val);editPage.getCurrentPanel().atInterval(editPage.player);});}});function VolumeController(sound,elm){this.element=elm?elm:document.createElement('div');this.sound=sound;this.volImg=createDOM('img',{src:'/images/sound.png'});this.volStatus=createDOM('span',{'class':'vol_status'},'');this.element.appendChild(this.volImg);this.element.appendChild(this.volStatus);var self=this;this.displayVolume();connect(this.element,'onclick',function(){self.down();});};VolumeController.prototype.displayVolume=function(){var s='';var v=this.sound.getVolume();for(var i=0;i<Math.round(v/25);i++){s+='|';}
this.volStatus.innerHTML=s;};VolumeController.prototype.down=function(){var v=this.sound.getVolume();var vv=(Math.round(v/25)+4)%5;this.sound.setVolume(vv*25);this.displayVolume();};function PlayerClock(duration,elm){if(!duration){return;}
this.element=elm?elm:document.createElement('div');this.hasHour=duration>=60*60;this.elemCurrent=createDOM('span',{'class':'clock_duration'},ppsec(0,this.hasHour));this.elemDuration=createDOM('span',{'class':'clock_duration'},ppsec(duration));this.element.appendChild(this.elemCurrent);this.element.appendChild(createDOM('span',{},'/'));this.element.appendChild(this.elemDuration);}
PlayerClock.prototype.update=function(sec){var s=ppsec(sec,this.hasHour);if(this.elemCurrent&&s!=this.elemCurrent.innerHTML){this.elemCurrent.innerHTML=s;}};function ppsec(sec,isDisplayHour){var s=typeof(sec)=='number'?sec:0;var format=function(num){var s=String(num);return s.length==1?'0'+s:s;};var hour=Math.floor(s/(60*60));var minu=Math.floor((s-60*60*hour)/60);var seco=Math.floor(s%60);if(isDisplayHour||hour>0){return[format(hour),format(minu),format(seco)].join(':');}
else{return[format(minu),format(seco)].join(':');}}
function Bar(opt){var opt=opt?opt:{};this.width=opt.width||200;this.height=opt.height||20;this.onUpdate=opt.onUpdate;this.ratio=opt.defaultRatio||0;this.element=opt['element']?opt['element']:document.createElement('div');this.element.style.width=this.width+'px';this.element.style.height=this.height+'px';this.element.style.backgroundColor=opt.bgColor||'#ccc';this.element.style.padding=0;var inner=document.createElement('div');inner.style.width=(opt.defaultRatio||0)+'px';inner.style.height=this.height+'px';inner.style.backgroundColor=opt.fgColor||'#999';inner.style.padding=0;inner.style.margin=0;this.element.appendChild(inner);this.inner=inner;}
Bar.prototype.setRatio=function(ratio){var r=ratio>1?1:ratio;this.ratio=r;this.update();};Bar.prototype.addRatio=function(ratio){var r=this.ratio+ratio>1?1:this.ratio+ratio;this.ratio=r;this.update();};Bar.prototype.update=function(){this.inner.style.width=this.width*this.ratio+'px';};function NextPage(baseURL){if(getFirstElementByTagAndClassName('a','next_page')){this.baseURL=baseURL||location.pathname;var q=this.query=parseQueryString(location.search);q.page||(q.page=1);var pe=new PageExpnader(bind('expand',this));pe.onScroll();}}
MochiKit.Base.update(NextPage.prototype,{query:null,expand:function(end,terminate){var self=this;showElement('indicator');this.query.page++;doXHR(this.baseURL+queryString(this.query)).addCallback(function(res){var elmPage=SPAN();elmPage.innerHTML=res.responseText;insertSiblingNodesBefore('indicator',elmPage);signal(document.body,'onexpand');if(!getElementsByTagAndClassName('a','next_page',elmPage).length){terminate();}}).addErrback(function(err){terminate();}).addBoth(function(){hideElement('indicator');end();});}})
