$(document).ready(function() {

    var fp_isiPad      = navigator.userAgent.match(/iPad/i) != null;
    var fp_isiPhone    = navigator.userAgent.match(/iPhone/i) != null;
    var fp_isiPod      = navigator.userAgent.match(/iPod/i) != null;
    var fp_isios       = false;
    if (fp_isiPad || fp_isiPhone || fp_isiPod){
        fp_isios       = true;
    }

    $('a.fp_video_player').each(function(){

        $(this).click(function(ev){

            ev.preventDefault();

            var thisId = "fp_vp_" + Math.floor(Math.random()*11);
            var video = $(this).attr('href');
            var videoDiv = "<div id='" + thisId + "' style='display:block;height:480px;width:640px;'></div>";

            // Create and show the Dialog pop-up
            this.myDialog = new dijit.Dialog({
                title: "Abbott Molecular",
                content: videoDiv
            }).show();

            dojo.connect(this.myDialog,"hide",function(){
                clearInterval(everySecond);
            });

            this.flowplayer = flowplayer(thisId, {
                    src:"/static/flash/flowplayer-3.2.7.swf",
                    wmode: "transparent"
                }, {
                    clip: {
                        url:video,
                        autoPlay:true,
                        autoBuffering:true,
                        onStart: function(clip){
                            videoPlaying(clip.metaData.duration,this,video);
                        },
                        onPause: function() {
                            videoStopped();
                        },
                        onResume: function(clip) {
                            videoPlaying(clip.metaData.duration,this,video);
                        },
                        onFinish: function() {
                            videoStopped();
                            videoFinished(video);
                        }
                    }
                }
            );

            if(fp_isios){
                this.flowplayer.ipad();
            }

            setTimeout(function() {
                console.info($(thisId));
            }, 500);

            // Don't follow the link
            return false;

        });
    });

    var everySecond;

    // =========================================================================
    function videoPlaying(duration,player,title){
        s.linkTrackVars = "prop1,prop2,prop3,prop45,eVar13,events";
        s.linkTrackEvents = "event14,event23";
        s.events = "event14,event23";
        s.prop2 = s.eVar2 = "Tier 2: Video";
        s.prop1 = s.prop3 = "";
        s.eVar13 = s.prop45 = title;
        s.tl(this,'o',"Video Play");

        everySecond = setInterval(function(){

            var position = player.getTime();
//            console.warn('TITLE: ' + title + ' | POSITION: '+ position + ' | DURATION: ' + duration);

            var minutes = Math.floor(position/60);
            var remainder = position%60;

            // calculate percentage watched
            var percentageViewed = (position / duration) * 100;

            // PERCENTAGES *
            if (percentageViewed > 99) {
                s.current7 = "100%";
            }
            else if (percentageViewed > 74.99){
                s.current7 = "75%";
            }
            else if (percentageViewed > 49.99 && percentageViewed <= 74.99 ){
                s.current7 = "50%";
            }
            else if (percentageViewed > 24.99 && percentageViewed <= 49.99) {
                s.current7 = "25%";
            }
            else {
                s.current7 = "0%";
            }

            if(s.current7 != s.previousProp7){
                s.prop7 = s.previousProp7 = s.current7;
                s.prop8 = "";
                setEVar7();
                videoCallToOmnit(title);
//                console.warn('setting new prop 7 to ' + s.prop7 + " eVar 7 " + s.eVar7);
            }

            if (remainder < 30){
                //s.prop8 = minutes + ' min 00 sec';
                s.current8 = minutes + ' min 00 sec';
            } else {
                //s.prop8 = minutes + ' min 30 sec';
                s.current8 = minutes + ' min 30 sec';
            }

            if (s.current8 != s.previousProp8){
                s.prop8 = s.previousProp8 = s.current8;
                s.prop7 = "";
                setEVar8();
                videoCallToOmnit(title);
//                console.warn('setting new prop 8 to ' + s.prop8 + " eVar 8 " + s.eVar8);
            }

            function setEVar8(){
                if(s.previous8){
                    if(s.previous8 < position){
                        s.eVar8 = s.prop8;
                        s.previous8 = position;
                    } else {
                        // Do nothing
                    }
                }else{
                    s.eVar8 = s.prop8;
                    s.previous8 = position;
                }
            }

            function setEVar7() {
                if(s.previous7){
                    if(s.previous7 < percentageViewed){
                        s.eVar7 = s.prop7;
                        s.previous7 = percentageViewed;
                    }else{
                        // Do nothing
                    }
                }else{
                    s.eVar7 = s.prop7;
                    s.previous7 = percentageViewed;
                }
            }

        },1000);
    }

    // =============================================================================
    function videoStopped(){
        clearInterval(everySecond);
    }

    // =============================================================================
    function videoFinished(title){
        if(s.prop7 != "100%"){
            s.eVar7 = s.prop7 = "100%";
            videoCallToOmnit(title);
//            console.warn('setting new prop 7 to ' + s.prop7 + " eVar 7 " + s.eVar7);
        }
    }

    // =============================================================================
    function videoCallToOmnit(title){
        s.linkTrackVars = "prop7,prop8,prop45,eVar7,eVar8,eVar13";
        s.eVar13 = s.prop45 = title;
//        console.warn('** CALLING OMNI **' + '\n' + 'TITLE: ' + s.prop45);
        s.tl(this,'o',"Video Duration");
    }

});
