﻿WL.registerOnloadEvent(function() {
    VideoUtil.InitVideoModals();
    VideoUtil.ShowInitialVideo($('#InitialVideoUrl').val());
});

var VideoUtil = {};

VideoUtil.ShowVideo = function(url, onready, ondismiss, subclass) {
    if (url) {
        var dlg = new Modal(url, 'wlVideoModal ' + subclass);
        dlg.PageViewTracked = false;
        dlg.OnReady = function() {
            window.AllowVideoTrack = true;
            if (onready) onready.call(this);
            VideoUtil.AdjustVideoFrameHeight(this);
        };
        dlg.OnDismiss = function() {
            window.AllowVideoTrack = false;
            if (window.s && s.__wlHostPageName) s.pageName = s.__wlHostPageName;
            if (ondismiss) ondismiss.call(this);
        };
        dlg.ClassName = 'wlVideoModal ' + subclass;
        (Modal.Requested = dlg).Show();
    }
};

VideoUtil.SetupVideoLink = function(selector, url, onready, ondismiss, subclass, pageName) {
    var ready = function() {
        window.AllowVideoTrack = true;
        if (onready) onready.call(this);
        VideoUtil.AdjustVideoFrameHeight(this);
    };
    var dismiss = function() {
        window.AllowVideoTrack = false;
        if (window.s && s.__wlHostPageName) s.pageName = s.__wlHostPageName;
        if (ondismiss) ondismiss.call(this);
    };
    var o = {
        ClassName: 'wlVideoModal',
        OnReady: ready,
        OnDismiss: dismiss
    };
    if (subclass) o.ClassName += ' ' + subclass;
    if (pageName) o.PageName = pageName;
    Modal.Setup($(selector), url, o);
};

VideoUtil.ShowInitialVideo = function(url, onready, ondismiss, subclass) {
    VideoUtil.ShowVideo(url,
                        function() {
                            this.SuppressTrackView = true;
                            this.SuppressTrackExit = false;
                            if (onready) onready.call(this);
                        },
                        function() {
                            if (ondismiss) ondismiss.call(this);
                        },
                        subclass);
};

VideoUtil.GetAjaxUrl = function (href) {
    var lowerHref = href.toLowerCase(),
        index = lowerHref.indexOf('/desktop');
    if (index < 0) index = lowerHref.indexOf('/online');
    if (index < 0) index = lowerHref.indexOf('/experience');
    if (index < 0) index = lowerHref.indexOf('/campaign');
    return '/Ajax' + href.substring(index);
};

VideoUtil.AdjustVideoFrameHeight = function(modal) {
    var $videoFrame = modal.Self.find('#silverlightVideoHost');
    if ($videoFrame.length > 0) {
        var hasSilverlight = false;

        if (navigator.plugins && navigator.plugins.length > 0) {
            for (var i = navigator.plugins.length - 1; i > -1; i--) {
                hasSilverlight = navigator.plugins[i].name.toLowerCase().indexOf('silverlight') > -1
                if (hasSilverlight) break;
            }
        }
        else if (window.ActiveXObject) {
            try {
                var control = new ActiveXObject('AgControl.AgControl');
                if (control) hasSilverlight = true;
                delete control;
            }
            catch (e) { }
        }

        if (!hasSilverlight) {
            $videoFrame.add('.wlVideoModal .modalDetail .content').css('height', '480px');
        }
    }

};

VideoUtil.InitVideoModals = function() {
    $('#wlHome #RediscoverHotmail .promotion a, #wlHome #OnlineUpdates .promotion a, #productInDepth #heroSidebar a.videoCta, #productInDepth #heroSidebar a.videoCtaThumb, a.video-link').each(function() {
        var $this = $(this),
            url = VideoUtil.GetAjaxUrl(this.href);    
        VideoUtil.SetupVideoLink($this, url, null, null, null, $this.attr('title'));
    });
};