﻿$(document).ready(function() {
    var overlay = $('#overlay');
    var hasMedia = false;

    // Prepare tags for rich content
    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="/maps/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>');
    }
    $("a[rel='youtube']").each(function() {
        var id = this.href.match(/\?v=([^&]*)/i)[1];
        var src = "http://www.youtube.com/v/" + id + "&enablejsapi=1&hd=1&showinfo=0&rel=0&autoplay=0&loop=0&hl=en&color1=0x006699&color2=0x54abd6";
        $('body').append('<object type="application/x-shockwave-flash" data="' + src + '" width="670" height="365" id="youtube_' + id + '" class="youtube skipOverlay"><param name="movie" value="' + src + '" /><param name="allowscriptaccess" value="always" /></object>');
        hasMedia = true;
    });
    if ($("a[rel='photosynth']").length > 0) {
        $('body').append('<iframe id="photosynth" class="skipOverlay" frameBorder="0" src="" width="670" height="365"></iframe>');
        hasMedia = true;
    }
    if ($("a[rel='iframe']").length > 0) {
        $('body').append('<iframe id="iframe" class="skipOverlay" frameBorder="0" src="" width="870" height="565"></iframe>');
        hasMedia = true;
    }

    // Insert close button
    if (hasMedia) {
        $("body").append('<a href="#close" id="overlayClose"><img src="/maps/ClientBin/Images/closeButton.png" width="13" height="13" /></a>');
        $("a[href='#close']").click(function() {
            hideOverlay();
            return false;
        });
    }

    // Wire up click event handlers
    $("a[rel='video']").click(function() {
        var video = document.getElementById("video");
        if (video && video.content) {
            $('#overlay').data('currentItem', 'video');
            video.content.Page.ChangeVideoUrl(this.href);
            if (this.id && this.id.length > 0) {
                document.location.hash = "#_" + this.id;
            }
            showOverlay();
            return false;
        } else {
            return true;
        }
    });
    $("a[rel='youtube']").click(function() {
        var id = "youtube_" + this.href.match(/\?v=([^&]*)/i)[1];
        var video = document.getElementById(id);
        if (video && video.playVideo) {
            $('#overlay').data('currentItem', id);
            if (this.id.length > 0) {
                document.location.hash = "#_" + this.id;
            }
            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');
        if (this.id && this.id.length > 0) {
            document.location.hash = "#_" + this.id;
        }
        showOverlay();
        dcsMultiTrack('DCS.dcsuri', '/maps/link/Photosynth/Default.aspx', 'WT.ti', 'View Photosynth Popup');
        return false;
    });
    $("a[rel='iframe']").click(function() {
        $('#overlay').data('currentItem', 'iframe');
        $('#iframe').attr('src', this.href);
        if (this.id && this.id.length > 0) {
            document.location.hash = "#_" + this.id;
        }
        showOverlay();
        dcsMultiTrack('DCS.dcsuri', '/maps/link/iframe/Default.aspx', 'WT.ti', 'View iframe Popup');
        return false;
    });

    // Overlay event handlers
    overlay.bind('preShow', function() {
        var sizes = getPageSize();
        var scroll = getPageScroll();
        var item = $('#' + $('#overlay').data('currentItem'));
        var closeButton = $('#overlayClose');
        var itemPos = { left: scroll.x + Math.floor((sizes.windowWidth - item.outerWidth()) / 2),
            top: scroll.y + Math.floor((sizes.windowHeight - item.outerHeight()) / 2)
        };
        item.css(itemPos);

        // display close button if this isnt using the silverlight player that has it built in.
        if ($('#overlay').data('currentItem') != 'video') {
            closeButton.css({ left: itemPos.left + item.outerWidth() - closeButton.outerWidth(),
                top: itemPos.top - closeButton.outerHeight() - 2
            });
        }

        if ($('#overlay').data('currentItem') == 'video') {
            var video = document.getElementById("video");
            if (video && video.content) {
                video.content.Page.StartVideoPlayer();
            }
        } else if ($('#overlay').data('currentItem').indexOf("youtube_") != -1) {
            item.each(function() {
                this.playVideo();
            });
        }
    });
    overlay.bind('preHide', function() {
        if ($('#overlay').data('currentItem') == 'video') {
            var video = document.getElementById("video");
            if (video && video.content) {
                video.content.Page.EndVideoPlayer();
            }
        } else if ($('#overlay').data('currentItem').indexOf("youtube_") != -1) {
            $("#" + $('#overlay').data('currentItem')).each(function() {
                this.pauseVideo();
            });
        }
    });
    overlay.bind('hide', function() {
        document.location.hash = "";
        $('#video').css({ left: -99999, top: 0 });
        $('#photosynth').css({ left: -99999, top: 0 });
        $('#iframe').css({ left: -99999, top: 0 });
        $('.youtube').css({ left: -99999, top: 0 });
        $('#overlayClose').css({ left: -99999, top: 0 });
    });

    // Code to auto-open layers
    if (document.location.hash.length > 1) {
        var link = $(document.location.hash.replace('_', ''));
        link.click();
    }
});
