/*
 * Media Element jQuery plugin
 * http://mediaelementjs.com/
 *
 * Creates a controller bar for HTML5 <video> add <audio> tags
 * using jQuery and MediaElement.js
 *
 * Copyright 2010, John Dyer
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Version: 1.1.0
 */
(function(a){html5.MepDefaults={defaultVideoWidth:480,defaultVideoHeight:270,videoWidth:-1,videoHeight:-1,audioWidth:300,audioHeight:30,messages:{start:"Click to Start",loading:"Loading",paused:"Paused",error:"Error",ended:"Ended"},controls:{playpause:true,timerail:true,duration:true,volume:true,captions:true,fullscreen:true},loop:false};html5.mepIndex=0;html5.MediaElementPlayer=function(d,e){var b=this,c=html5.MediaFeatures;b.$media=a(d);b.options=a.extend(true,{},html5.MepDefaults,e);b.isVideo=(b.$media[0].tagName.toLowerCase()=="video");if(c.isiPad||c.isiPhone){b.$media.attr("controls","controls");b.$media.removeAttr("poster");if(c.isiPad&&b.$media[0].getAttribute("autoplay")!==null){b.$media[0].load();b.$media[0].play()}return}else{if(c.isAndroid&&b.isVideo){b.$media.attr("controls","controls");return}else{b.$media.removeAttr("controls")}}b.createPlayer()};html5.MediaElementPlayer.prototype={createPlayer:function(){var b=this,c=a.extend(true,{},b.options,{success:function(d,e){b.setupPlayer(d,e)},error:function(d){b.handleError(d)}});b.id="mep_"+html5.mepIndex++;b.$media.before(a('<div id="'+b.id+'" class="mep-container"><div class="mep-mediaelement"></div><div class="mep-captions"><img /></div><div class="mep-poster"><img /></div><div class="mep-overlay"><div class="mep-overlay-message"></div></div><div class="mep-controls"><div class="mep-playpause-button mep-play"><span></span></div><div class="mep-time-rail"><span class="mep-time-total"><span class="mep-time-loaded"></span><span class="mep-time-current"></span><span class="mep-time-handle"></span></span></div><div class="mep-time"><span class="mep-currenttime"></span> <span> | </span> <span class="mep-duration"></span></div><div class="mep-captions-button"><span></span></div><div class="mep-volume-button mep-mute"><span></span><div class="mep-volume-slider"><div class="mep-volume-rail"><div class="mep-volume-handle"></div></div></div></div><div class="mep-fullscreen-button"><span></span></div></div><div class="mep-clear"></div></div>'));b.container=a("#"+this.id);b.container.addClass(b.$media[0].className).find(".mep-mediaelement").append(b.$media);if(b.isVideo){b.width=(b.options.videoWidth>0)?b.options.videoWidth:(b.$media[0].getAttribute("width")!==null)?b.$media.attr("width"):b.options.defaultVideoWidth;b.height=(b.options.videoHeight>0)?b.options.videoHeight:(b.$media[0].getAttribute("height")!==null)?b.$media.attr("height"):b.options.defaultVideoHeight}else{b.width=b.options.audioWidth;b.height=b.options.audioHeight}b.buildPoster();b.buildOverlay();b.setPlayerSize(b.width,b.height);b.buildControls();c.pluginWidth=b.height;c.pluginHeight=b.width;html5.MediaElement(b.$media[0],c)},setupPlayer:function(b,d){var c=this;c.mediaElement=b;c.domNode=d;c.buildControlBar();c.buildPlayPause();c.buildTimeRail();c.buildVolumeControls();c.buildFullscreen();c.mediaElement.addEventListener("click",function(f){if(c.mediaElement.paused){c.mediaElement.play()}},true);c.mediaElement.addEventListener("playing",function(f){c.poster.hide();c.playpause.removeClass("mep-play").addClass("mep-pause");c.hideMessage()},true);c.mediaElement.addEventListener("pause",function(f){c.playpause.removeClass("mep-pause").addClass("mep-play");c.showMessage(c.options.messages.paused)},true);c.mediaElement.addEventListener("ended",function(f){if(c.options.loop){c.mediaElement.setCurrentTime(0);c.setTimePosition();c.mediaElement.play()}else{c.poster.show();c.playpause.removeClass("mep-pause").addClass("mep-play");c.showMessage(c.options.messages.ended)}},true);c.mediaElement.addEventListener("loadedmetadata",function(f){if(c.isVideo&&c.options.videoHeight<=0&&c.$media[0].getAttribute("height")===null&&!isNaN(f.target.videoHeight)){c.setPlayerSize(f.target.videoWidth,f.target.videoHeight);c.setRailSize();c.mediaElement.setVideoSize(f.target.videoWidth,f.target.videoHeight)}},true);setTimeout(function(){c.setRailSize()},50);if(c.options.success){c.options.success(c.mediaElement,c.domNode)}this.loadTracks()},loadTracks:function(){var b=this,c=b.$media.find("track[kind=subtitles]");b.tracks=[];b.trackToLoad=-1;c.each(function(){b.tracks.push({srclang:a(this).attr("srclang"),src:a(this).attr("src"),entries:[],isLoaded:false})});if(b.tracks.length>0){b.loadNextSubtitle()}else{b.captions.remove();b.setRailSize()}},loadNextSubtitle:function(){var b=this;b.trackToLoad++;if(b.trackToLoad<b.tracks.length){b.loadSubtitles(b.trackToLoad)}},loadSubtitles:function(b){var c=this,d=c.tracks[b];a.ajax({url:d.src,success:function(e){d.entries=SrtParser.parse(e);c.loadNextSubtitle()},error:function(){c.loadNextSubtitle()}})},buildPoster:function(){var b=this;b.poster=b.container.find(".mep-poster");b.posterImg=b.poster.find("img");b.posterUrl=b.$media.attr("poster");b.posterUrl=(b.posterUrl===null||b.posterUrl==undefined)?"":b.posterUrl;if(b.posterUrl!==""){b.posterImg.attr("src",b.posterUrl)}else{b.poster.hide()}},buildOverlay:function(){var b=this;b.overlay=b.container.find(".mep-overlay");b.overlayMessage=b.container.find(".mep-overlay-message");if(b.$media[0].getAttribute("autoplay")!==null){b.showMessage(b.options.messages.loading)}else{b.showMessage(b.options.messages.start)}b.overlay.bind("click",function(c){if(b.mediaElement.paused){b.mediaElement.play()}},true)},showMessage:function(b){if(this.isVideo){this.overlayMessage.html(b);this.overlay.css("visibility","visible")}},hideMessage:function(){this.overlay.css("visibility","hidden")},buildControls:function(){var b=this;b.controls=b.container.find(".mep-controls");b.isControlsVisible=true;b.playpause=b.controls.find(".mep-playpause-button");b.fullscreen=b.controls.find(".mep-fullscreen-button");if(!b.isVideo){b.fullscreen.remove()}b.time=b.controls.find(".mep-time");b.currentTime=b.controls.find(".mep-currenttime").html("00:00");b.duration=b.controls.find(".mep-duration").html("00:00");b.captions=b.controls.find(".mep-captions-button");b.mute=b.controls.find(".mep-volume-button");b.volumeSlider=b.controls.find(".mep-volume-slider");b.volumeRail=b.controls.find(".mep-volume-rail");b.volumeHandle=b.controls.find(".mep-volume-handle");b.timeRail=b.controls.find(".mep-time-rail");b.timeCurrent=b.timeRail.find(".mep-time-current").width(0);b.timeLoaded=b.timeRail.find(".mep-time-loaded").width(0);b.timeTotal=b.timeRail.find(".mep-time-total");b.timeHandle=b.controls.find(".mep-time-handle");b.controls.show();b.setRailSize();if(!b.options.controls.playpause){b.playpause.remove()}if(!b.options.controls.timerail){b.timeRail.remove()}if(!b.options.controls.duration){b.time.remove()}if(!b.options.controls.volume){b.mute.remove()}if(!b.options.controls.fullscreen){b.fullscreen.remove()}},buildControlBar:function(){var b=this;if(b.isVideo){b.container.bind("mouseenter",function(){b.controls.fadeIn(200);b.setRailSize();b.isControlsVisible=true}).bind("mouseleave",function(){b.controls.fadeOut(200);b.isControlsVisible=false})}},buildPlayPause:function(){var b=this;b.playpause.bind("click",function(){if(b.playpause.hasClass("mep-play")){b.mediaElement.play();b.playpause.removeClass("mep-play").addClass("mep-pause")}else{b.mediaElement.pause();b.playpause.removeClass("mep-pause").addClass("mep-play")}})},buildTimeRail:function(){var b=this;b.timeRail.delegate("span","click",function(h){var c=h.pageX,i=b.timeTotal.offset(),f=b.timeTotal.outerWidth(),d=((c-i.left)/f),g=d*b.mediaElement.duration;b.mediaElement.setCurrentTime(g)});b.mediaElement.addEventListener("timeupdate",function(c){if(!b.isControlsVisible){return}b.setTimePosition();b.setTimeLoaded(c.target)},true);b.mediaElement.addEventListener("progress",function(c){b.setTimeLoaded(c.target)},true)},setTimePosition:function(){var b=this,d,c;if(b.mediaElement.currentTime&&b.mediaElement.duration){b.currentTime.html(html5.Utility.secondsToTimeCode(b.mediaElement.currentTime));if(b.mediaElement.duration){b.duration.html(html5.Utility.secondsToTimeCode(b.mediaElement.duration))}d=b.timeTotal.width()*b.mediaElement.currentTime/b.mediaElement.duration;c=d-(b.timeHandle.width()/2);b.timeCurrent.width(d);b.timeHandle.css("left",c)}},setTimeLoaded:function(d){var b=this,c=null;if(d&&d.bytesTotal!=undefined&&d.bytesTotal>0&&d.bufferedBytes!=undefined){c=d.bufferedBytes/d.bytesTotal}else{if(d&&d.buffered&&d.buffered.length>0&&d.buffered.end&&d.duration){c=d.buffered.end(0)/d.duration}}if(c!==null){b.timeLoaded.width(b.timeTotal.width()*c)}},setRailSize:function(){var b=this,c=b.playpause.outerWidth(true)+b.time.outerWidth(true)+b.mute.outerWidth(true)+b.captions.outerWidth(true)+((b.isVideo)?b.fullscreen.outerWidth(true):0),d=b.controls.width()-c-(b.timeRail.outerWidth(true)-b.timeRail.outerWidth(false));b.timeRail.width(d);b.timeTotal.width(d-(b.timeTotal.outerWidth(true)-b.timeTotal.width()))},setPlayerSize:function(d,b){var c=this;c.width=parseInt(d,10);c.height=parseInt(b,10);c.container.width(c.width).height(c.height);c.overlay.width(c.width).height(c.height);c.posterImg.height(c.height).width(c.width)},handleError:function(c){var b=this;b.$media.hide();b.overlay.hide();b.controls.hide();b.poster.hide()},buildFullscreen:function(){var b=this;b.isFullScreen=false;b.normalHeight=0;b.normalWidth=0;b.fullscreen.bind("click",function(){b.setFullScreen(!b.isFullScreen)})},setFullScreen:function(b){var c=this;switch(c.mediaElement.pluginType){case"flash":c.mediaElement.setFullscreen(b);break;case"silverlight":c.mediaElement.setFullscreen(b);break;case"native":if(html5.MediaFeatures.hasNativeFullScreen){if(b){c.mediaElement.webkitEnterFullScreen()}else{c.mediaElement.webkitExitFullScreen()}}else{if(b){c.normalHeight=c.$media.height();c.normalWidth=c.$media.width();c.container.addClass("mep-container-fullscreen").width("100%").height("100%").css("z-index",1000);c.$media.width("100%").height("100%");c.overlay.width("100%").height("100%");c.posterImg.width("100%").height("auto");c.fullscreen.removeClass("mep-fullscreen").addClass("mep-unfullscreen");c.setRailSize();c.escB=function(d){c.escListener(d)};c.resB=function(d){c.resizeListener(d)};a(document).bind("keydown",c.escB);a(window).bind("resize",c.resB)}else{c.container.removeClass("mep-container-fullscreen").width(c.normalWidth).height(c.normalHeight).css("z-index",1);c.$media.width(c.normalWidth).height(c.normalHeight);c.posterImg.width(c.normalWidth).height(c.normalHeight);c.fullscreen.removeClass("mep-unfullscreen").addClass("mep-fullscreen");c.setRailSize();a(document).unbind("keydown",c.escB);a(window).unbind("resize",c.resB)}}}c.isFullScreen=b},escListener:function(b){if(b.keyCode==27){this.setFullScreen(false)}},resizeListener:function(b){this.setRailSize()},buildVolumeControls:function(){var b=this;b.vmmB=function(c){b.volumeMove(c)};b.vrmB=function(c){b.removeMouseMove(c)};b.volumeSlider.bind("mousedown",function(c){b.volumeMove(c);a(document).bind("mousemove",b.vmmB).bind("mouseup",b.vrmB)});b.mute.find("span").bind("click",function(){if(b.mediaElement.muted){b.mediaElement.setMuted(false);b.mute.removeClass("mep-unmute").addClass("mep-mute");b.positionVolumeHandle(1)}else{b.mediaElement.setMuted(true);b.mute.removeClass("mep-mute").addClass("mep-unmute");b.positionVolumeHandle(0)}})},volumeMove:function(g){var c=this,b=c.volumeRail.height(),f=g.pageY-c.volumeRail.offset().top,d=(b-f)/b;if(f<0){f=0}else{if(f>b){f=b}}c.volumeHandle.css("top",f-(c.volumeHandle.height()/2));if(d==0){c.mediaElement.setMuted(true);c.mute.removeClass("mep-mute").addClass("mep-unmute")}else{c.mediaElement.setMuted(false);c.mute.removeClass("mep-unmute").addClass("mep-mute")}c.mediaElement.setVolume(d)},positionVolumeHandle:function(c){var b=this;b.volumeHandle.css("top",b.volumeRail.height()-(b.volumeRail.height()*c)-(b.volumeHandle.height()/2))},removeMouseMove:function(){var b=this;a(document).unbind("mousemove",b.vmmB).unbind("mouseup",b.vrmB)}};html5.SrtParser={timecodeToSeconds:function(b){var c=b.split(":");return c[0]*60*60+c[1]*60+parseFloat(c[2].replace(",","."))},parse:function(e){var g=/^[0-9]+$/,h=/^([0-9]{2}:[0-9]{2}:[0-9]{2}(,[0-9]{1,3})?) --\> ([0-9]{2}:[0-9]{2}:[0-9]{2}(,[0-9]{3})?)(.*)$/,f=0,d=e.split(/\r?\n/),c={text:[],times:[]},b,j;for(;f<d.length;f++){if(g.exec(d[f])){f++;b=h.exec(d[f]);if(b&&f<d.length){f++;j=d[f];f++;while(d[f]!==""&&f<d.length){j=j+"\n"+d[f];f++}c.text.push(j);c.times.push({start:this.timecodeToSeconds(b[1]),stop:this.timecodeToSeconds(b[3]),settings:b[5]})}}}return c},canTranslate:function(){return !(typeof(google)=="undefined"||typeof(google.language)=="undefined")},translate:function(d,j,f,h){if(!this.canTranslate()){h(null);return}var g=d.text.join(" <span></span>"),b={text:[],times:[]},c,e;google.language.translate(g,j,f,function(i){c=i.translation.split("<span></span>");for(e=0;e<d.text.length;e++){b.text[e]=c[e];b.times[e]={start:d.times[e].start,stop:d.times[e].stop,settings:d.times[e].settings}}h(b)})}};jQuery.fn.mediaelementplayer=function(b){return this.each(function(){return new MediaElementPlayer(a(this),b)})};window.MediaElementPlayer=html5.MediaElementPlayer})(jQuery);
