
var rootDir='';var ytplayer;var ytconstants;var ytelements;var ytplaying;function formatTime(secs){var times=new Array(60,1);var time='';var tmp;for(var i=0;i<times.length;i++){tmp=Math.floor(secs/times[i]);if(tmp<1){tmp='00';}
else if(tmp<10){tmp='0'+tmp;}
time+=tmp;if(i<1){time+=':';}
secs=secs%times[i];}
return time;}
function ytplayerReadyCheck(url,autostart,constants,elements){var ready=!!ytplayer;if(!ready){window.setTimeout(function(){ytplayerReadyCheck(url,autostart,constants,elements);},50);}
else{ytelements=elements;ytconstants=constants;if(autostart){ytplayer.loadVideoByUrl(url);}
else{ytplayer.cueVideoByUrl(url);}
ytelements.current.html(ytGetCurrentTimer());ytelements.duration.html(ytGetDuration());}}
function ytOnPlayerStateChange(newState){switch(newState){case 1:ytelements.imgPlay.attr('src',ytconstants.yt_pause);ytplaying=setInterval(function(){ytelements.current.html(ytGetCurrentTimer());ytelements.duration.html(ytGetDuration());ytUpdateSlider();},10);break;default:ytelements.imgPlay.attr('src',ytconstants.yt_play);ytplaying=null;break;}}
function ytLoadVideo(url){ytplayer.loadVideoByUrl(url);}
function ytUpdateSlider(){progress=ytplayer.getCurrentTime()/ytplayer.getDuration();maxWidth=ytelements.slider.width();currentWidth=Math.floor(progress*maxWidth);ytelements.currentSlider.css({'width':currentWidth+'px'})}
function ytGetCurrentTimer(){time=formatTime(ytplayer.getCurrentTime());return time;}
function ytGetDuration(){time=formatTime(ytplayer.getDuration());return time;}
function ytOnClickPlay(button){ytplayer.playVideo();}
function ytOnClickPause(button){ytplayer.pauseVideo();}
function ytGetPlayerState(){return ytplayer.getPlayerState();}
function onYouTubePlayerReady(playerId){ytplayer=document.getElementById("myytplayer");ytplayer.addEventListener("onStateChange","ytOnPlayerStateChange");}
function ytIsMuted(){return ytplayer.isMuted();}
function ytOnClickVolumeMute(){ytplayer.mute();}
function ytOnClickVolumeUnMute(){ytplayer.unMute();}
(function($){$.fn.ytPlay=function(options){return this.each(function(el){container=$(this);container.click(function(){thisLink=$(this);if(ytplayer){ytLoadVideo(thisLink.attr('href'));}
return false;});});}
$.fn.ytPlayer=function(options){var settings=$.extend({url:'',width:'',height:'',autostart:false},options);var constants={yt_player:'http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=myytplayer&hd=1&showsearch=showsearch=0&showinfo=0&cc_load_policy=0&iv_load_policy=0&version=3',yt_dot:rootDir+'/js/chromelessYouTube/dot.png',yt_play:rootDir+'/js/chromelessYouTube/play.jpg',yt_pause:rootDir+'/js/chromelessYouTube/pause.jpg',yt_volume:rootDir+'/js/chromelessYouTube/volume.jpg',yt_volumeMute:rootDir+'/js/chromelessYouTube/volume-mute.jpg'}
return this.each(function(){container=$(this);if(swfobject){var embed=$("<div />").attr('id',container.attr('id')+'-embed').appendTo(container);var flashvars={};var params={'allowScriptAccess':'always','wmode':'transparent'};var attributes={'id':'myytplayer'};swfobject.embedSWF(constants.yt_player,container.attr('id')+'-embed',settings.width,settings.height,"9.0.0","expressInstall.swf",flashvars,params,attributes);}
else{var object=$("<object />").attr('id','myytplayer').attr('width',settings.width).attr('height',settings.height).attr('type','application/x-shockwave-flash').attr('data',constants.yt_player).appendTo(container);var param1=$("<param />").attr('name','allowScriptAccess').attr('value','always').appendTo(object);var param2=$("<param />").attr('name','movie').attr('value',constants.yt_player).appendTo(object);var param3=$("<param />").attr('name','quality').attr('value','high').appendTo(object);var param4=$("<param />").attr('name','wmode').attr('value','transparent').appendTo(object);var embed=$("<embed />").attr('src',constants.yt_player).attr('quality','high').attr('width',settings.width).attr('height',settings.height).attr('allowScriptAccess','always').attr('type','application/x-shockwave-flash').appendTo(object);}
var controller=$("<div />").attr('id',container.attr('id')+'-controller').css({'height':'28px','margin-top':'2px'}).appendTo(container);var play=$("<div />").attr('id',controller.attr('id')+'-play').css({'float':'left','width':'29px','height':'28px'}).appendTo(controller);var imgPlay=$("<img />").attr('src',constants.yt_play).attr('width','29').attr('height','28').click(function(){switch(ytGetPlayerState()){case 1:ytOnClickPause();break;default:ytOnClickPlay();break;}}).appendTo(play);var bar=$("<div />").attr('id',controller.attr('id')+'-bar').css({'float':'left','width':(settings.width-62)+'px','height':'28px','margin-left':'2px','background-color':'#eaeced'}).appendTo(controller);var slider=$("<div />").attr('id',bar.attr('id')+'-slider').css({'float':'left','width':(settings.width-141)+'px','height':'5px','margin-left':'4px','margin-top':'11px','background-color':'#edc8b1'}).appendTo(bar);var currentSlider=$("<div />").attr('id',slider.attr('id')+'-current').css({'width':'0px','height':'5px','background-color':'#f47320','text-align':'right'}).appendTo(slider);var dot=$("<img />").attr('id',slider.attr('id')+'-dot').attr('src',constants.yt_dot).attr('width','12').attr('height','12').css({'margin-top':'-3px'}).appendTo(currentSlider);var timer=$("<div />").attr('id',bar.attr('id')+'-timer').css({'float':'right','width':'65px','height':'28px','font-size':'10px','margin-left':'6px','margin-right':'4px','line-height':'28px','text-align':'right','color':'#f47320'}).appendTo(bar);var current=$("<div />").attr('id',timer.attr('id')+'-current').css({'float':'left'}).html("00:00").appendTo(timer);var slash=$("<div />").attr('id',timer.attr('id')+'-slash').css({'float':'left','margin-left':'2px','margin-right':'2px'}).html("/").appendTo(timer);var duration=$("<div />").attr('id',timer.attr('id')+'-duration').css({'float':'left'}).html("00:00").appendTo(timer);var volume=$("<div />").attr('id',controller.attr('id')+'-volume').css({'float':'left','width':'29px','height':'28px','margin-left':'2px'}).appendTo(controller);var imgVolume=$("<img />").attr('src',constants.yt_volume).attr('width','29').attr('height','28').click(function(){if(ytIsMuted()){ytOnClickVolumeUnMute();$(this).attr('src',constants.yt_volume);}
else{ytOnClickVolumeMute();$(this).attr('src',constants.yt_volumeMute);}}).appendTo(volume);var elements={play:play,imgPlay:imgPlay,bar:bar,slider:slider,currentSlider:currentSlider,timer:timer,current:current,duration:duration,volume:volume,imgVolume:imgVolume};ytplayerReadyCheck(settings.url,settings.autostart,constants,elements);});};})(jQuery);
