﻿var Actor = null;
var flash = false, win = null, doc = null, links = null, timer = null;

$(function () {
    Objects({
        hidden: true,
        layers: true,
        objects: true
    });
    
    if (flash) {
        Resize();
        
        win.bind("resize", Resize);
        
        timer = window.setTimeout(function () {
            window.clearTimeout(timer);
        
            LoadActor();
        }, 2000);
    ;}
});

var Objects = function (o) {
    /// <summary>Utility function to get jQuery instances of required elements.</summary>
    /// <param name="o">JSON object containing conditional variables specifying which elements need to have a jQuery instance created of.</param>
    /// <returns>void</returns>

    if (typeof(o) !== "undefined") {
        if (o.hidden) {
            flash = Boolean(parseInt($("#Flash").val()));
        };
        
        if (o.layers) {
            Actor = $("#Actor");
        };
        
        if (o.objects) {
            win = $(window);
            doc = $(document);
            links = $(document).find("a");
        };
    };
};

var Resize = function () {
    /// <summary>Utility function to update the positioning of elements during browser window resize.</summary>
    /// <returns>void</returns>

    Actor.css({
        left: parseInt((win.width() - 250) / 2)
    });
};

var Remove = function (o) {
    /// <summary>Removes a specific element from display. Will execute an optional callback function if specified.</summary>
    /// <param name="o">JSON object containing the list of elements to remove.</param>
    /// <returns>void</returns>

    if (typeof(o) !== "undefined") {
        for (var i = 0; i < o.objects.length; i++) {
            $(o.objects[i]).css({
                display: "none"
            });
        };
        
        if (typeof(o.callback) === "function") {
            o.callback();
        };
    };
};

var LoadActor = function () {
    /// <summary>Embeds and plays a Flash video into a specified jQuery object.</summary>
    /// <returns>void</returns>
    
    links.each(function () {
        $(this).css({
            cursor: "default"
        }).bind("click", function (e) {
            e.preventDefault();
        });
    });
    
    Actor.css({
        display: "block"
    }).flashembed({
        src: "http://cms.usoag.com/Shared/Flash/Player.swf",
        width: 250,
        height: 425,
        play: true,
        loop: false,
        quality: "low",
        wmode: "transparent",
        allowscriptaccess: "always"
    }, {
        videoPath: "http://cms.usoag.com/Comparison/Flash/Heather.flv",
        videoW: 250,
        videoH: 425,
        callback: "UnloadActor",
        autoplay: true,
        autorewind: true,
        hoursDelayInterval: 0,
        daysDelayInterval: 0,
        weeksDelayInterval: 0,
        closewhendone: true
    });
};

var UnloadActor = function () {
    /// <summary>Callback function after the video has finished playing to dispose of the video.</summary>
    /// <returns>void</summary>
    
    Remove({
        objects: [ Actor ],
        callback: function () {
            links.each(function () {
                $(this).css({
                    cursor: "pointer"
                }).unbind("click");
            });
        }
    });
};