﻿$(document).ready(function() {
    var overlay = $('#overlay');

    if ($("a[rel='video']").length > 0) {
        $('body').append('<object id="video" class="skipOverlay" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="640" height="360"><param name="source" value="/virtualearth/ClientBin/VE_HomePagePlayer.xap" /><param name="background" value="transparent" /><param name="windowless" value="true" /><param name="initParams" value="initialVideo=,fadeInTime=400,fadeOutTime=400" /><param name="minRuntimeVersion" value="2.0.31005.0" /><param name="autoUpgrade" value="true" /></object>');
    }
    if ($("a[rel='photosynth']").length > 0) {
        $('body').append('<iframe id="photosynth" class="skipOverlay" frameborder="0" src="" width="670" height="365"></iframe>');
    }

    $("a[rel='video']").click(function() {
        var video = document.getElementById("video");
        if (video && video.content) {
            $('#overlay').data('currentItem', 'video');
            video.content.Page.ChangeVideoUrl(this.href);
            showOverlay();
            return false;
        } else {
            return true;
        }
    });
    $("a[rel='photosynth']").click(function() {
        $('#overlay').data('currentItem', 'photosynth');
        $('#photosynth').attr('src', this.href.replace('view.aspx', 'embed.aspx') + '&delayLoad=false&slideShowPlaying=false');
        showOverlay();
        dcsMultiTrack('DCS.dcsuri', '/virtualearth/link/Photosynth/Default.aspx', 'WT.ti', 'View Photosynth Popup');
        return false;
    });
    overlay.bind('preShow', function() {
        var sizes = getPageSize();
        var scroll = getPageScroll();
        var item = $('#' + $('#overlay').data('currentItem'));
        item.css({
            left: scroll.x + Math.floor((sizes.windowWidth - item.outerWidth()) / 2),
            top: scroll.y + Math.floor((sizes.windowHeight - item.outerHeight()) / 2)
        });

        if ($('#overlay').data('currentItem') == 'video') {
            var video = document.getElementById("video");
            if (video && video.content) {
                video.content.Page.StartVideoPlayer();
            }
        }
    });
    overlay.bind('preHide', function() {
        if ($('#overlay').data('currentItem') == 'video') {
            var video = document.getElementById("video");
            if (video && video.content) {
                video.content.Page.EndVideoPlayer();
            }
        }
    });
    overlay.bind('hide', function() {
        $('#video').css({ left: -99999, top: 0 });
        $('#photosynth').css({ left: -99999, top: 0 });
    });
});