/*
 * ipad.js 3.2.2. The Flowplayer ipad/iphone fallback.
 *
 * Copyright 2010, 2011 Flowplayer Oy
 * By Thomas Dubois <thomas@flowplayer.org>
 *
 * This file is part of Flowplayer.
 *
 * Flowplayer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Flowplayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Flowplayer.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Date: 2011-01-10 07:50:57 -0500 (Mon, 10 Jan 2011)
 * Revision: 4901
 */
$f.addPlugin("ipad",function(t){var L=-1;var u=0;var v=1;var J=2;var z=3;var F=4;var g=5;var f=this;var N=1;var M=false;var C=false;var q=false;var o=0;var K=[];var c={accelerated:false,autoBuffering:false,autoPlay:true,baseUrl:null,bufferLength:3,connectionProvider:null,cuepointMultiplier:1000,cuepoints:[],controls:{},duration:0,extension:"",fadeInSpeed:1000,fadeOutSpeed:1000,image:false,linkUrl:null,linkWindow:"_self",live:false,metaData:{},originalUrl:null,position:0,playlist:[],provider:"http",scaling:"scale",seekableOnBegin:false,start:0,url:null,urlResolvers:[]};var s=L;var m=L;var p=/iPad|iPhone|iPod/i.test(navigator.userAgent);var b=null;function j(R,Q,O){if(Q){for(key in Q){if(key){if(Q[key]&&typeof Q[key]=="function"&&!O){continue}if(Q[key]&&typeof Q[key]=="object"&&Q[key].length==undefined){var P={};j(P,Q[key]);R[key]=P}else{R[key]=Q[key]}}}}return R}var w={simulateiDevice:false,controlsSizeRatio:1.5,controls:true,debug:false,validExtensions:/mov|m4v|mp4|avi/gi};j(w,t);function e(){if(w.debug){if(p){var O=[].splice.call(arguments,0).join(", ");console.log.apply(console,[O])}else{console.log.apply(console,arguments)}}}function i(O){switch(O){case -1:return"UNLOADED";case 0:return"LOADED";case 1:return"UNSTARTED";case 2:return"BUFFERING";case 3:return"PLAYING";case 4:return"PAUSED";case 5:return"ENDED"}return"UNKOWN"}function D(O){var P=$f.fireEvent(f.id(),"onBefore"+O,o);return P!==false}function H(O){O.stopPropagation();O.preventDefault();return false}function G(P,O){if(s==L&&!O){return}m=s;s=P;y();if(P==z){l()}e(i(P))}function x(){b.fp_stop();M=false;C=false;q=false;G(v);G(v)}var d=null;function l(){if(d){return}console.log("starting tracker");d=setInterval(A,100);A()}function y(){clearInterval(d);d=null}function A(){var P=Math.floor(b.fp_getTime()*10)*100;var Q=Math.floor(b.duration*10)*100;var R=(new Date()).time;function O(U,S){U=U>=0?U:Q-Math.abs(U);for(var T=0;T<S.length;T++){if(S[T].lastTimeFired>R){S[T].lastTimeFired=-1}else{if(S[T].lastTimeFired+500>R){continue}else{if(U==P||(P-500<U&&P>U)){S[T].lastTimeFired=R;$f.fireEvent(f.id(),"onCuepoint",o,S[T].fnId,S[T].parameters)}}}}}$f.each(f.getCommonClip()._cuepoints,O);$f.each(K[o]._cuepoints,O)}function B(){x();q=true;b.fp_seek(0)}function I(O){}function n(){function O(Q){var P={};j(P,c);j(P,f.getCommonClip());j(P,Q);if(P.ipadUrl){url=decodeURIComponent(P.ipadUrl)}else{if(P.url){url=P.url}}if(url&&url.indexOf("://")==-1&&P.baseUrl){url=P.baseUrl+"/"+url}P.originalUrl=P.url;P.completeUrl=url;P.extension=P.completeUrl.substr(P.completeUrl.lastIndexOf("."));P.type="video";delete P.index;e("fixed clip",P);return P}b.fp_play=function(S,Q,U){var P=null;var T=true;var R=true;e("Calling play() "+S,S);if(Q){e("ERROR: inStream clips not yet supported");return}if(S!==undefined){if(typeof S=="number"){if(o>=K.length){return}o=S;S=K[o]}else{if(typeof S=="string"){S={url:S}}b.fp_setPlaylist(S.length!==undefined?S:[S])}if(!w.validExtensions.test(K[o].extension)){if(K.length>1&&o<(K.length-1)){e("Not last clip in the playlist, moving to next one");b.fp_play(++o,false,true)}return}S=K[o];P=S.completeUrl;if(S.autoBuffering!==undefined&&S.autoBuffering===false){T=false}if(S.autoPlay===undefined||S.autoPlay===true||U===true){T=true;R=true}else{R=false}}else{e("clip was not given, simply calling video.play, if not already buffering");if(s!=J){b.play()}return}e("about to play "+P,T,R);x();if(P){e("Changing SRC attribute"+P);b.setAttribute("src",P)}if(T){if(!D("Begin")){return false}$f.fireEvent(f.id(),"onBegin",o);e("calling video.load()");b.load()}if(R){e("calling video.play()");b.play()}};b.fp_pause=function(){e("pause called");if(!D("Pause")){return false}b.pause()};b.fp_resume=function(){e("resume called");if(!D("Resume")){return false}b.play()};b.fp_stop=function(){e("stop called");if(!D("Stop")){return false}C=true;b.pause();try{b.currentTime=0}catch(P){}};b.fp_seek=function(P){e("seek called "+P);if(!D("Seek")){return false}var T=0;var P=P+"";if(P.charAt(P.length-1)=="%"){var Q=parseInt(P.substr(0,P.length-1))/100;var S=b.duration;T=S*Q}else{T=P}try{b.currentTime=T}catch(R){e("Wrong seek time")}};b.fp_getTime=function(){return b.currentTime};b.fp_mute=function(){e("mute called");if(!D("Mute")){return false}N=b.volume;b.volume=0};b.fp_unmute=function(){if(!D("Unmute")){return false}b.volume=N};b.fp_getVolume=function(){return b.volume*100};b.fp_setVolume=function(P){if(!D("Volume")){return false}b.volume=P/100};b.fp_toggle=function(){e("toggle called");if(f.getState()==g){B();return}if(b.paused){b.fp_play()}else{b.fp_pause()}};b.fp_isPaused=function(){return b.paused};b.fp_isPlaying=function(){return !b.paused};b.fp_getPlugin=function(Q){if(Q=="canvas"||Q=="controls"){var P=f.getConfig();return P.plugins&&P.plugins[Q]?P.plugins[Q]:null}e("ERROR: no support for "+Q+" plugin on iDevices");return null};b.fp_close=function(){G(L);b.parentNode.removeChild(b);b=null};b.fp_getStatus=function(){var Q=0;var R=0;try{Q=b.buffered.start();R=b.buffered.end()}catch(P){}return{bufferStart:Q,bufferEnd:R,state:s,time:b.fp_getTime(),muted:b.muted,volume:b.fp_getVolume()}};b.fp_getState=function(){return s};b.fp_startBuffering=function(){if(s==v){b.load()}};b.fp_setPlaylist=function(Q){e("Setting playlist");o=0;for(var P=0;P<Q.length;P++){Q[P]=O(Q[P])}K=Q;$f.fireEvent(f.id(),"onPlaylistReplace",Q)};b.fp_addClip=function(Q,P){Q=O(Q);K.splice(P,0,Q);$f.fireEvent(f.id(),"onClipAdd",Q,P)};b.fp_updateClip=function(Q,P){j(K[P],Q);return K[P]};b.fp_getVersion=function(){return"3.2.3"};b.fp_isFullscreen=function(){return false};b.fp_toggleFullscreen=function(){if(b.fp_isFullscreen()){b.webkitExitFullscreen()}else{b.webkitEnterFullscreen()}};b.fp_addCuepoints=function(S,Q,P){var U=Q==-1?f.getCommonClip():K[Q];U._cuepoints=U._cuepoints||{};S=S instanceof Array?S:[S];for(var R=0;R<S.length;R++){var V=typeof S[R]=="object"?(S[R]["time"]||null):S[R];if(V==null){continue}V=Math.floor(V/100)*100;var T=V;if(typeof S[R]=="object"){T=j({},S[R],false);if(T.time!=undefined){delete T.time}if(T.parameters!=undefined){j(T,T.parameters,false);delete T.parameters}}U._cuepoints[V]=U._cuepoints[V]||[];U._cuepoints[V].push({fnId:P,lastTimeFired:-1,parameters:T})}};$f.each(("toggleFullscreen,stopBuffering,reset,playFeed,setKeyboardShortcutsEnabled,isKeyboardShortcutsEnabled,css,animate,showPlugin,hidePlugin,togglePlugin,fadeTo,invoke,loadPlugin").split(","),function(){var P=this;b["fp_"+P]=function(){e("ERROR: unsupported API on iDevices "+P);return false}})}function E(){var Z=["abort","canplay","canplaythrough","durationchange","emptied","ended","error","loadeddata","loadedmetadata","loadstart","pause","play","playing","progress","ratechange","seeked","seeking","stalled","suspend","volumechange","waiting"];var R=function(ab){e("Got event "+ab.type,ab)};for(var T=0;T<Z.length;T++){b.addEventListener(Z[T],R,false)}var O=function(ab){e("got onBufferEmpty event "+ab.type);G(J);$f.fireEvent(f.id(),"onBufferEmpty",o)};b.addEventListener("emptied",O,false);b.addEventListener("waiting",O,false);var Q=function(ab){if(m==v||m==J){}else{e("Restoring old state "+i(m));G(m)}$f.fireEvent(f.id(),"onBufferFull",o)};b.addEventListener("canplay",Q,false);b.addEventListener("canplaythrough",Q,false);var P=function(ab){b.fp_updateClip({duration:b.duration,metaData:{duration:b.duration}},o);K[o].duration=b.duration;$f.fireEvent(f.id(),"onMetaData",o,K[o])};b.addEventListener("loadedmetadata",P,false);b.addEventListener("durationchange",P,false);var Y=function(ab){if(s==F){if(!D("Resume")){e("Resume disallowed, pausing");b.fp_pause();return H(ab)}$f.fireEvent(f.id(),"onResume",o)}G(z);if(!M){M=true;$f.fireEvent(f.id(),"onStart",o)}};b.addEventListener("playing",Y,false);var V=function(ab){if(!D("Finish")){if(K.length==1){e("Active playlist only has one clip, onBeforeFinish returned false. Replaying");B()}else{if(o!=(K.length-1)){e("Not the last clip in the playlist, but onBeforeFinish returned false. Returning to the beginning of current clip");b.fp_seek(0)}else{e("Last clip in playlist, but onBeforeFinish returned false, start again from the beginning");b.fp_play(0)}}return H(ab)}G(g);$f.fireEvent(f.id(),"onFinish",o);if(K.length>1&&o<(K.length-1)){e("Not last clip in the playlist, moving to next one");b.fp_play(++o,false,true)}};b.addEventListener("ended",V,false);var U=function(ab){G(u,true);$f.fireEvent(f.id(),"onError",o,201);if(w.onFail&&w.onFail instanceof Function){w.onFail.apply(f,[])}};b.addEventListener("error",U,false);var X=function(ab){e("got pause event from player"+f.id());if(C){return}if(s==J&&m==v){e("forcing play");setTimeout(function(){b.play()},0);return}if(!D("Pause")){b.fp_resume();return H(ab)}G(F);$f.fireEvent(f.id(),"onPause",o)};b.addEventListener("pause",X,false);var aa=function(ab){$f.fireEvent(f.id(),"onBeforeSeek",o)};b.addEventListener("seeking",aa,false);var S=function(ab){if(C){C=false;$f.fireEvent(f.id(),"onStop",o)}else{$f.fireEvent(f.id(),"onSeek",o)}e("seek done, currentState",i(s));if(q){q=false;b.fp_play()}else{if(s!=z){b.fp_pause()}}};b.addEventListener("seeked",S,false);var W=function(ab){$f.fireEvent(f.id(),"onVolume",b.fp_getVolume())};b.addEventListener("volumechange",W,false)}function k(){b.fp_play(0)}function r(){}if(p||w.simulateiDevice){if(!window.flashembed.__replaced){var h=window.flashembed;window.flashembed=function(Q,V,R){if(typeof Q=="string"){Q=document.getElementById(Q.replace("#",""))}if(!Q){return}var U=window.getComputedStyle(Q,null);var T=parseInt(U.width);var O=parseInt(U.height);while(Q.firstChild){Q.removeChild(Q.firstChild)}var P=document.createElement("div");var S=document.createElement("video");P.appendChild(S);Q.appendChild(P);P.style.height=O+"px";P.style.width=T+"px";P.style.display="block";P.style.position="relative";P.style.background="-webkit-gradient(linear, left top, left bottom, from(rgba(0, 0, 0, 0.5)), to(rgba(0, 0, 0, 0.7)))";P.style.cursor="default";P.style.webkitUserDrag="none";S.style.height="100%";S.style.width="100%";S.style.display="block";S.id=V.id;S.name=V.id;S.style.cursor="pointer";S.style.webkitUserDrag="none";S.type="video/mp4";S.playerConfig=R.config;$f.fireEvent(R.config.playerId,"onLoad","player")};flashembed.getVersion=h.getVersion;flashembed.asString=h.asString;flashembed.isSupported=function(){return true};flashembed.__replaced=true}var a=f._fireEvent;f._fireEvent=function(O){if(O[0]=="onLoad"&&O[1]=="player"){b=f.getParent().querySelector("video");if(w.controls){b.controls="controls"}n();E();G(u,true);b.fp_setPlaylist(b.playerConfig.playlist);k();a.apply(f,[O])}var P=s!=L;if(s==L&&typeof O=="string"){P=true}if(P){return a.apply(f,[O])}};f._swfHeight=function(){return parseInt(b.style.height)};f.hasiPadSupport=function(){return true}}return f});
