﻿var flashvars = {};
var params = {};
params.wmode = "transparent";
var attributes = {};

var videos = new Array();
var videoId;

$(document).ready(function() {
    $(".Video").each(function() {
        var galleryid = videos.length;

        videos[galleryid] = new Object();

        $(this).find(".Image").each(function() {
            videos[videos.length - 1].alt = this.alt;
        });

        $(this).find(".Link").click(function() {
            ShowVideoPopup(galleryid);
        });

        $(this).find("a").each(function() {
            videos[videos.length - 1].src = this.rel;
        });
    });

    $("#PopupVideo .Close").each(function() {
        this.onclick = function() {
            $('#PopupVideo').animate({ height: 'hide', opacity: 'hide' }, 'slow', null, function() {
                swfobject.embedSWF("Content/Flash/Video.swf", "PopupVideoFlash", "348", "298", "8.0.0", "Content/Flash/expressInstall.swf", flashvars, params, attributes);
            });
        }
    });

    $("#PopupVideo .Next").each(function() {
        this.onclick = function() {
            ShowVideo(videoId + 1);
        }
    });

    $("#PopupVideo .Back").each(function() {
        this.onclick = function() {
            ShowVideo(videoId - 1);
        }
    });
})

function ShowVideoPopup(id) {
    /* out of range */
    if (id < 0 || id >= videos.length) return;

    if ($('#PopupImage').css("position") != "fixed")
        scroll(0, 0);

    videoId = id;

    UpdateVideo(videoId);

    $('#PopupVideo').animate({ height: 'show', opacity: 'show' }, 'slow');
}

function UpdateVideo(id) {
    swfobject.embedSWF(videos[id].src, "PopupVideoFlash", "348", "298", "8.0.0", "Content/Flash/expressInstall.swf", flashvars, params, attributes);

    $('#PopupVideo .Text').each(function() {
        this.firstChild.data = videos[id].alt;
    });
}

function ShowVideo(id) {
    /* out of range */
    if (id < 0 || id >= videos.length) return;

    videoId = id;

    $('#PopupVideo .Media').animate({ opacity: 'hide' }, 'slow', function() {
        UpdateVideo(videoId);

        $('#PopupVideo .Media').animate({ opacity: 'show' }, 'slow');
    });
}
