/*
 * 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(a){function L(){}function K(){t.fp_play(0)}function J(){var a=["abort","canplay","canplaythrough","durationchange","emptied","ended","error","loadeddata","loadedmetadata","loadstart","pause","play","playing","progress","ratechange","seeked","seeking","stalled","suspend","volumechange","waiting"];var b=function(a){w("Got event "+a.type,a)};for(var j=0;j<a.length;j++)t.addEventListener(a[j],b,false);var p=function(a){w("got onBufferEmpty event "+a.type);A(e);$f.fireEvent(i.id(),"onBufferEmpty",n)};t.addEventListener("emptied",p,false);t.addEventListener("waiting",p,false);var s=function(a){if(r==d||r==e){}else{w("Restoring old state "+x(r));A(r)}$f.fireEvent(i.id(),"onBufferFull",n)};t.addEventListener("canplay",s,false);t.addEventListener("canplaythrough",s,false);var u=function(a){t.fp_updateClip({duration:t.duration,metaData:{duration:t.duration}},n);o[n].duration=t.duration;$f.fireEvent(i.id(),"onMetaData",n,o[n])};t.addEventListener("loadedmetadata",u,false);t.addEventListener("durationchange",u,false);var B=function(a){if(q==g){if(!y("Resume")){w("Resume disallowed, pausing");t.fp_pause();return z(a)}$f.fireEvent(i.id(),"onResume",n)}A(f);if(!k){k=true;$f.fireEvent(i.id(),"onStart",n)}};t.addEventListener("playing",B,false);var C=function(a){if(!y("Finish")){if(o.length==1){w("Active playlist only has one clip, onBeforeFinish returned false. Replaying");G()}else if(n!=o.length-1){w("Not the last clip in the playlist, but onBeforeFinish returned false. Returning to the beginning of current clip");t.fp_seek(0)}else{w("Last clip in playlist, but onBeforeFinish returned false, start again from the beginning");t.fp_play(0)}return z(a)}A(h);$f.fireEvent(i.id(),"onFinish",n);if(o.length>1&&n<o.length-1){w("Not last clip in the playlist, moving to next one");t.fp_play(++n,false,true)}};t.addEventListener("ended",C,false);var D=function(a){A(c,true);$f.fireEvent(i.id(),"onError",n,201);if(v.onFail&&v.onFail instanceof Function)v.onFail.apply(i,[])};t.addEventListener("error",D,false);var E=function(a){w("got pause event from player"+i.id());if(l)return;if(q==e&&r==d){w("forcing play");setTimeout(function(){t.play()},0);return}if(!y("Pause")){t.fp_resume();return z(a)}A(g);$f.fireEvent(i.id(),"onPause",n)};t.addEventListener("pause",E,false);var F=function(a){$f.fireEvent(i.id(),"onBeforeSeek",n)};t.addEventListener("seeking",F,false);var H=function(a){if(l){l=false;$f.fireEvent(i.id(),"onStop",n)}else $f.fireEvent(i.id(),"onSeek",n);w("seek done, currentState",x(q));if(m){m=false;t.fp_play()}else if(q!=f)t.fp_pause()};t.addEventListener("seeked",H,false);var I=function(a){$f.fireEvent(i.id(),"onVolume",t.fp_getVolume())};t.addEventListener("volumechange",I,false)}function I(){function a(a){var b={};u(b,p);u(b,i.getCommonClip());u(b,a);if(b.ipadUrl)url=decodeURIComponent(b.ipadUrl);else if(b.url)url=b.url;if(url&&url.indexOf("://")==-1&&b.baseUrl)url=b.baseUrl+"/"+url;b.originalUrl=b.url;b.completeUrl=url;b.extension=b.completeUrl.substr(b.completeUrl.lastIndexOf("."));b.type="video";delete b.index;w("fixed clip",b);return b}t.fp_play=function(a,b,c){var d=null;var f=true;var g=true;w("Calling play() "+a,a);if(b){w("ERROR: inStream clips not yet supported");return}if(a!==undefined){if(typeof a=="number"){if(n>=o.length)return;n=a;a=o[n]}else{if(typeof a=="string"){a={url:a}}t.fp_setPlaylist(a.length!==undefined?a:[a])}if(o[n].url.substr(-4)==".jpg"){if(o.length>1&&n<o.length-1){w("Not last clip in the playlist, moving to next one");t.fp_play(++n,false,true)}else{w("Last clip.  Nothing to do.")}return}a=o[n];d=a.completeUrl;if(a.autoBuffering!==undefined&&a.autoBuffering===false)f=false;if(a.autoPlay===undefined||a.autoPlay===true||c===true){f=true;g=true}else{g=false}}else{w("clip was not given, simply calling video.play, if not already buffering");if(q!=e)t.play();return}w("about to play "+d,f,g);B();if(d){w("Changing SRC attribute"+d);t.setAttribute("src",d)}if(f){if(!y("Begin"))return false;$f.fireEvent(i.id(),"onBegin",n);w("calling video.load()");t.load()}if(g){w("calling video.play()");t.play()}};t.fp_pause=function(){w("pause called");if(!y("Pause"))return false;t.pause()};t.fp_resume=function(){w("resume called");if(!y("Resume"))return false;t.play()};t.fp_stop=function(){w("stop called");if(!y("Stop"))return false;l=true;t.pause();try{t.currentTime=0}catch(a){}};t.fp_seek=function(a){w("seek called "+a);if(!y("Seek"))return false;var b=0;var a=a+"";if(a.charAt(a.length-1)=="%"){var c=parseInt(a.substr(0,a.length-1))/100;var d=t.duration;b=d*c}else{b=a}try{t.currentTime=b}catch(e){w("Wrong seek time")}};t.fp_getTime=function(){return t.currentTime};t.fp_mute=function(){w("mute called");if(!y("Mute"))return false;j=t.volume;t.volume=0};t.fp_unmute=function(){if(!y("Unmute"))return false;t.volume=j};t.fp_getVolume=function(){return t.volume*100};t.fp_setVolume=function(a){if(!y("Volume"))return false;t.volume=a/100};t.fp_toggle=function(){w("toggle called");if(i.getState()==h){G();return}if(t.paused)t.fp_play();else t.fp_pause()};t.fp_isPaused=function(){return t.paused};t.fp_isPlaying=function(){return!t.paused};t.fp_getPlugin=function(a){if(a=="canvas"||a=="controls"){var b=i.getConfig();return b["plugins"]&&b["plugins"][a]?b["plugins"][a]:null}w("ERROR: no support for "+a+" plugin on iDevices");return null};t.fp_close=function(){A(b);t.parentNode.removeChild(t);t=null};t.fp_getStatus=function(){var a=0;var b=0;try{a=t.buffered.start();b=t.buffered.end()}catch(c){}return{bufferStart:a,bufferEnd:b,state:q,time:t.fp_getTime(),muted:t.muted,volume:t.fp_getVolume()}};t.fp_getState=function(){return q};t.fp_startBuffering=function(){if(q==d)t.load()};t.fp_setPlaylist=function(b){w("Setting playlist");n=0;for(var c=0;c<b.length;c++)b[c]=a(b[c]);o=b;$f.fireEvent(i.id(),"onPlaylistReplace",b)};t.fp_addClip=function(b,c){b=a(b);o.splice(c,0,b);$f.fireEvent(i.id(),"onClipAdd",b,c)};t.fp_updateClip=function(a,b){u(o[b],a);return o[b]};t.fp_getVersion=function(){return"3.2.3"};t.fp_isFullscreen=function(){return false};t.fp_toggleFullscreen=function(){if(t.fp_isFullscreen())t.webkitExitFullscreen();else t.webkitEnterFullscreen()};t.fp_addCuepoints=function(a,b,c){var d=b==-1?i.getCommonClip():o[b];d._cuepoints=d._cuepoints||{};a=a instanceof Array?a:[a];for(var e=0;e<a.length;e++){var f=typeof a[e]=="object"?a[e]["time"]||null:a[e];if(f==null)continue;f=Math.floor(f/100)*100;var g=f;if(typeof a[e]=="object"){g=u({},a[e],false);if(g["time"]!=undefined)delete g["time"];if(g["parameters"]!=undefined){u(g,g["parameters"],false);delete g["parameters"]}}d._cuepoints[f]=d._cuepoints[f]||[];d._cuepoints[f].push({fnId:c,lastTimeFired:-1,parameters:g})}};$f.each("toggleFullscreen,stopBuffering,reset,playFeed,setKeyboardShortcutsEnabled,isKeyboardShortcutsEnabled,css,animate,showPlugin,hidePlugin,togglePlugin,fadeTo,invoke,loadPlugin".split(","),function(){var a=this;t["fp_"+a]=function(){w("ERROR: unsupported API on iDevices "+a);return false}})}function H(a){}function G(){B();m=true;t.fp_seek(0)}function F(){function d(d,e){d=d>=0?d:b-Math.abs(d);for(var f=0;f<e.length;f++){if(e[f].lastTimeFired>c){e[f].lastTimeFired=-1}else if(e[f].lastTimeFired+500>c){continue}else{if(d==a||a-500<d&&a>d){e[f].lastTimeFired=c;$f.fireEvent(i.id(),"onCuepoint",n,e[f].fnId,e[f].parameters)}}}}var a=Math.floor(t.fp_getTime()*10)*100;var b=Math.floor(t.duration*10)*100;var c=(new Date).time;$f.each(i.getCommonClip()._cuepoints,d);$f.each(o[n]._cuepoints,d)}function E(){clearInterval(C);C=null}function D(){if(C)return;console.log("starting tracker");C=setInterval(F,100);F()}function B(){t.fp_stop();k=false;l=false;m=false;A(d);A(d)}function A(a,c){if(q==b&&!c)return;r=q;q=a;E();if(a==f)D();w(x(a))}function z(a){a.stopPropagation();a.preventDefault();return false}function y(a){var b=$f.fireEvent(i.id(),"onBefore"+a,n);return b!==false}function x(a){switch(a){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 w(){if(v.debug){if(s){var a=[].splice.call(arguments,0).join(", ");console.log.apply(console,[a])}else{console.log.apply(console,arguments)}}}function u(a,b,c){if(b){for(key in b){if(key){if(b[key]&&typeof b[key]=="function"&&!c)continue;if(b[key]&&typeof b[key]=="object"&&b[key].length==undefined){var d={};u(d,b[key]);a[key]=d}else{a[key]=b[key]}}}}return a}var b=-1;var c=0;var d=1;var e=2;var f=3;var g=4;var h=5;var i=this;var j=1;var k=false;var l=false;var m=false;var n=0;var o=[];var p={accelerated:false,autoBuffering:false,autoPlay:true,baseUrl:null,bufferLength:3,connectionProvider:null,cuepointMultiplier:1e3,cuepoints:[],controls:{},duration:0,extension:"",fadeInSpeed:1e3,fadeOutSpeed:1e3,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 q=b;var r=b;var s=/iPad|iPhone|iPod/i.test(navigator.userAgent);var t=null;var v={simulateiDevice:false,controlsSizeRatio:1.5,controls:true,debug:false,validExtensions:/mov|m4v|mp4|avi/gi};u(v,a);var C=null;if(s||v.simulateiDevice){if(!window.flashembed.__replaced){var M=window.flashembed;window.flashembed=function(a,b,c){if(typeof a=="string"){a=document.getElementById(a.replace("#",""))}if(!a){return}var d=window.getComputedStyle(a,null);var e=parseInt(d.width);var f=parseInt(d.height);while(a.firstChild)a.removeChild(a.firstChild);var g=document.createElement("div");var h=document.createElement("video");g.appendChild(h);a.appendChild(g);g.style.height=f+"px";g.style.width=e+"px";g.style.display="block";g.style.position="relative";g.style.background="-webkit-gradient(linear, left top, left bottom, from(rgba(0, 0, 0, 0.5)), to(rgba(0, 0, 0, 0.7)))";g.style.cursor="default";g.style.webkitUserDrag="none";h.style.height="100%";h.style.width="100%";h.style.display="block";h.id=b.id;h.name=b.id;h.style.cursor="pointer";h.style.webkitUserDrag="none";h.type="video/mp4";h.playerConfig=c.config;$f.fireEvent(c.config.playerId,"onLoad","player")};flashembed.getVersion=M.getVersion;flashembed.asString=M.asString;flashembed.isSupported=function(){return true};flashembed.__replaced=true}var N=i._fireEvent;i._fireEvent=function(a){if(a[0]=="onLoad"&&a[1]=="player"){t=i.getParent().querySelector("video");if(v.controls)t.controls="controls";I();J();A(c,true);t.fp_setPlaylist(t.playerConfig.playlist);K();N.apply(i,[a])}var d=q!=b;if(q==b&&typeof a=="string")d=true;if(d)return N.apply(i,[a])};i._swfHeight=function(){return parseInt(t.style.height)};i.hasiPadSupport=function(){return true}}return i})
