﻿jQuery(document).ready(function() {
    VideoPlayer.Initialise();
});

var VideoPlayer = {

    /* properties */
    _this: null,
    _PlayerInitialised: false,
    PlayerHtml: '',
    CurrentMovieId: '',
    CurrentMovie: null,

    Initialise: function () {
        // setup properties and player html
        _this = VideoPlayer;
        _this.BuildPlayerHtml();

        // get all dom elements that have class of modalVideo
        // var videoLinks = jQuery(".modalVideo");
        // unbind/bind click events incase already initialised elsewhere
        jQuery(".modalVideo").unbind('click');
        //jQuery(".modalVideo").live('click', VideoPlayer.OnLinkClick);
        
        
        
        jQuery(".modalVideo").live('click', function (e) {
            if (jQuery(this).attr('tracking') == 'True') {
                googleTracking.TrackEvent(null, jQuery(this).attr('doctype'), jQuery(this).attr('title'));
            }
            VideoPlayer.OnLinkClick(this);
            e.preventDefault();
        });

        $(".modalVideo").each(function (index, value) {
            var searchResult = jsonPath(videoColl, "$.[?(@.ID == '" + this.id + "')]");
            if (searchResult[0].RunOnStart == "1") {
                VideoPlayer.OnLinkClick(this);
            }
        });
        
    },

    /* Event Handlers */
    OnLinkClick: function (clickedObj) {
        _this.SetCurrentMovieId(clickedObj);
        _this.PlayMovie(_this.CurrentMovieId);
    },

    /* Methods */
    SetCurrentMovieId: function (clickedObj) {
        _this.CurrentMovieId = clickedObj.id;
    },

    PlayMovie: function (movieId) {
        // check if the player has already been initialised
        if (!_this._PlayerInitialised)
            _this.Initialise();

        _this.SetCurrentMovie(movieId);
        _this.SetOrientation(_this.CurrentMovie.Orientation);
        
        // setup flash params/attributes for player
        var flashVars = { video_path: _this.CurrentMovie.VideoUrl };

        var params = {
            allowScriptAccess: "sameDomain",
            allowFullScreen: "true",
            menu: "false",
            scale: "noscale",
            base: "",
            wmode: "transparent"
        };

        var attributes = {
            id: "flashObject",
            name: "flashObject"
        };

        // embed the flash and show
        swfobject.embedSWF("/swf/AFP_FloatingVideo.swf", "flashVideo", "100%", "100%", "9.0.28", false, flashVars, params, attributes);
        jQuery("#flashContainer").show();
    },

    SetCurrentMovie: function (movieId) {
        // check if we have any videos otherwise we won't do anything
        if (typeof videoColl === 'undefined') {
            return null;
        }
        else {
            // search the collection for the movie object based on the id
            var searchResult = jsonPath(videoColl, "$.[?(@.ID == '" + movieId + "')]");
            _this.CurrentMovie = searchResult[0];
            _this.CurrentMovieId = searchResult[0].ID;
        }
    },

    GetMovieUrl: function (movieId) {
        if (_this.CurrentMovie == null)
            _this.SetCurrentMovie(movieId);

        return _this.CurrentMovie.VideoUrl;
    },

    // Define the orientation of the video which is set in CMS
    SetOrientation: function (orientation) {

        var player = jQuery("#flashContainer");

        switch (orientation) {
            case "Bottom Left":
                player.css("position", "fixed");
                player.css("bottom", "-1px");
                player.css("left", "0px");
                break;
            case "Bottom Right":
                player.css("position", "fixed");
                player.css("bottom", "-1px");
                player.css("right", "0px");
                break;
            case "Top Left":
                player.css("position", "fixed");
                player.css("top", "0px");
                player.css("left", "0px");
                break;
            case "Top Right":
                player.css("position", "fixed");
                player.css("top", "0px");
                player.css("right", "0px");
                break;
        }
    },

    BuildPlayerHtml: function () {
        if (jQuery("#flashContainer").length > 0)
            jQuery("#flashContainer").remove();

        _this.PlayerHtml = '<div id="flashContainer" style="z-index:1000000; display:none; width:400px; height: 225px; position:fixed;"><div id="flashVideo"><div class="noFlashBlock" align="center"><h2>Flash Player Required</h2><p>Flash is required to view this website and we have detected that you don\'t have it installed on your computer.</p><div class="downloadFlash"><p><a href="http://get.adobe.com/flashplayer" title="Download Flash Player"><strong>Download Flash Player</strong></a></p><p><a href="?detectflash=false" title="I already have the latest Flash Player">I already have the lastest Flash Player</a></p></div></div></div></div>';
        jQuery(document.body).append(_this.PlayerHtml);
        _this._PlayerInitialised = true;
    },

    Close: function () {
        _this._PlayerInitialised = false;
        jQuery("#flashContainer").hide();
    }
}
