/** flowplayer plugin: flowplayer.ipad-3.2.2.js **/
//$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});
/** flowplayer plugin **/
var mediaHolder;
function embedVideo(holder,video,title,autoplay,width,height,bgImg,autoBuffering){
        if(isIOS()){
            
            mediaHolder = holder;
            embedhtml5Tag(holder,video,title,autoplay,width,height,bgImg,autoBuffering);
           
        }else{
            embedVod(holder,video,title,autoplay,width,height,bgImg,autoBuffering);
        }
}



function embedVod(holder,video,title,autoplay,width,height,bgImg,autoBuffering){
           
	if(video==null){
		return false;
	}
	if(autoplay==null){
		autoplay=false;
	}
	if(title==null){
		title="";
	}
	if(width!=null){
		$('#'+holder).width(width);
	}
	if(height!=null){
		$('#'+holder).height(height);
	}
        if(autoBuffering == null){
            autoBuffering = true;
        }
       
        var myVideo = null;
        if(bgImg){
            isPhoto = true;
            myVideo = [bgImg,video];
            startPlay = true;
        }else{
            isPhoto = false;
            myVideo = [video];
            startPlay = false;
        }
        
	$f(holder, {src:"http://www.omy.sg/global/common/swf/flowplayer.commercial-3.2.1.swf",wmode: "transparent"}, {
		key:'#$ce13e49366aff115c46',
		playlist: myVideo,
		clip:{scaling: 'fit',
                      autoPlay: startPlay,
                      autoBuffering: autoBuffering,               
                      onBeforeFinish: function() {         
				if(!clickTime && bgImg){
                                    clickTime = true;
                                    return false;
                                }                
                      },
                      onFinish: function(){
				if(isPhoto){
                        		this.getPlugin("play").hide();
				}else{
					this.getPlugin("play").show();
				}
                                $f().stop();
                                //alert('should stop');
                      }
                },
		plugins: {
			controls: {
				autoHide:'fullscreen',time:false
			},
			audio: {
				url: 'http://www.omy.sg/global/common/swf/flowplayer.audio-3.2.0.swf'
			}
		}
	});
        
       
        if(!isPhoto && autoplay){
            $f().play();
        }
}


function isIOS(){
    var ua = navigator.userAgent;
    var checker = {
        iphone: ua.match(/(iPhone|iPod|iPad)/),
        blackberry: ua.match(/BlackBerry/),
        android: ua.match(/Android/)
    }
    return checker.iphone;
}

function isMP4(filename){
   
    var dot = filename.lastIndexOf(".");
    if( dot == -1 ) return "";
    var extension = filename.substr(dot,filename.length).toLowerCase();
  
    return (extension == '.mp4');
}

function isMP3(filename){

    var dot = filename.lastIndexOf(".");
    if( dot == -1 ) return "";
    var extension = filename.substr(dot,filename.length).toLowerCase();
    return (extension == '.mp3');
}

function embedhtml5Tag(holder,video,title,autoplay,width,height,bgImg,autoBuffering){

    if(isMP4(video)){
        var html = '<div id="pholder" style="width:'+width+'px;height:'+height+'px;position:relative;"><div id="cholder" style="width:50%;margin:auto;position:absolute;top:50%;left:50%;margin-left:-120px;">';
        html = html + '</div><div id="vholder"><video id="omyvideo" width="'+width+'" height="'+height+'" ';
        if(autoplay){
            html = html + 'autoplay';
        }
        html = html + ' poster="'+bgImg+'" src="'+video+'"  controls="controls" style="background:rgb(255,255,255);"></video></div></div>';
        $('#' + holder).css('width',width+'px');
        $('#' + holder).css('height',height+'px');
        $('#' + holder).html(html);
    }else if(isMP3(video)){
        var html = '<div id="pholder" style="height:100%;width:100%;position:relative;">';
        html = html + '<div id="cholder" style="background:url('+bgImg+') no-repeat;height:100%;background-size:auto '+(height - 16)+'px;">&nbsp;';
        html = html + '</div><div id="vholder"><audio id="omyaudio" style="width:'+width+'px;position:absolute;bottom:0px;z-index:1;" ';
        if(autoplay){
            html = html + 'autoplay';
        }
        html = html + ' src="'+video+'" controls="controls"></audio></div></div>';
        $('#' + holder).css('width',width+'px');
        $('#' + holder).css('height',height+'px');
        $('#' + holder).html(html);
    }else{
        var html = '<div id="pholder" style="height:100%;width:100%;position:relative;">';
        html = html + '<div id="cholder" style="width:100%;margin:auto;position:absolute;top:50%;text-align:center;"></div>';
        html = html + '</div>';
        $('#' + holder).css('width',width+'px');
        $('#' + holder).css('height',height+'px');
        $('#' + holder).html(html);
        $('#cholder' ).html('This video is not supported by this device.');
    }
    
}

function loadVideo(video,poster){
    if(isIOS()){
        if(isMP4(video)){
            $('#vholder').show();
            $('#cholder').html('');
            $('#omyvideo').attr('src', video);
            $('#omyvideo').attr('poster', 'http://www.omy.sg/global/common/js/video_play_bg.png');
            $('#omyvideo')[0].play();
        }else{
            $('#omyvideo')[0].pause();
            $('#vholder').hide();
            $('#cholder' ).html('This video is not supported by this device.');
        }
    }else{
        $f().play([video]);
    }

}



function embedPlayer(media_holder,width,height,cssFile,key){

	if(media_holder==null){
		alert('invalid media holder');
		return false;
	}
	if(width!=null){
		$('#'+media_holder).width(width);
	}

	if(height!=null){
		$('#'+media_holder).height(height);
	}
	if(cssFile == null){
		cssFile = 'http://fun.omy.sg/common/media/popup/omymedia_r2.css';
	}
	if(key == null){
		key = '#$ce13e49366aff115c46';
	}

	$f(media_holder,
			"http://www.omy.sg/global/common/swf/flowplayer.commercial-3.2.1.swf",
			{
				key: key,
				onLoad : function(){
					activateTab();
				},

				clip:{
					scaling: 'fit',
					autoBuffering: true,

					onStart: function(clip) {
						if(contentF){
							this.getPlugin("content").setHtml(makeCaption(contentF));
							contentF = null;
						}
						if(isPhoto){
							this.getControls().setWidgets({all: false,fullscreen: true});

						}else{
							this.getControls().setWidgets({
								all: false,
								play: true,
								volume: true,
								mute: true,
								time: true,
								fullscreen: true,
								scrubber: true

							});
						}
					},
					onBeforeFinish: function() {
						if(isPhoto){
							return false;
						}
					},
					onFinish: function(){
						if(isPhoto){
							this.getPlugin("play").hide();
						}else{
							this.getPlugin("play").show();
						}
					}



				},
				canvas: {
							backgroundColor:'#000000',
							backgroundGradient: 'none',
							opacity:0.0
					},
				plugins: {
					viral: {

						url: 'http://www.omy.sg/global/common/swf/flowplayer.viralvideos-3.2.0.swf',
						email: {
							script: 'share_email.php'
						}


					},
					content: {
						url:'http://www.omy.sg/global/common/swf/flowplayer.content-3.2.0.swf',
						bottom: 10,
						height:40,
						backgroundColor: 'transparent',
						backgroundGradient: 'none',
						border: 0,
			            textDecoration: 'outline',
						stylesheet: cssFile,
						html: '<span class="caption">&nbsp;&nbsp;</span>'

					},
					controls: {
						autoHide:'fullscreen'
					},
					audio: {
					url: 'http://www.omy.sg/global/common/swf/flowplayer.audio-3.2.0.swf'
				}
				}
			}
	);


}


function showVod(media_url, caption)
{

	isVod = true;
	isPod = false;
	isPhoto = false;

	$f().play([media_url]);
	$f().getPlugin("content").setHtml(makeCaption(caption));



}

function showPod(media_url, caption, img)
{
	isVod = false;
	isPod = true;
	isPhoto = false;

	 if(img == null){
		 pod_img = 'http://www.omy.sg/global/common/imgs/pod.JPG';
	 }else{
		 pod_img = img;
	 }




	 $f().play([pod_img,media_url]);

	 $f().getPlugin("content").setHtml(makeCaption(caption));



}


function showPhoto(media_url, caption)
{
	isVod = false;
	isPod = false;
	isPhoto = true;

	$f().play([media_url,'http://www.omy.sg/global/common/mp3/abc.mp3']);

	$f().getPlugin("content").setHtml(makeCaption(caption));






}


function makeCaption(caption){

	  if(caption!=''){
	    caps="<span class=\"caption\">" + caption + "</span>";
	    return caps;
	  }else return "";

}

var isVod = false;
var isPod = false;
var isPhoto = false;
var contentF;
var clickTime = false;

















