﻿/*
Requires jQuery, jquery.tools.1.2.5 (expose)
*/
var activeOverlay = null;
var activeVideo = null;
var html5MediaOverlayDebugMode = false;

function getOverlayHeight(overlayid) {
    if (typeof (overlayid) == "undefined") {
        if ($("div.overlay").length > 0) {
            overlayid = $("div.overlay:first");
        } else {
            return defaultPlayerHeight;
        }
    }
    $(overlayid).css("visibility", "hidden").css("display", "block");
    var dimension = $(overlayid).outerHeight();
    $(overlayid).css("display", "none").css("visibility", "visible");
    return dimension;
}
function getOverlayWidth(overlayid) {
    if (typeof (overlayid) == "undefined") {
        if ($("div.overlay").length > 0) {
            overlayid = $("div.overlay:first");
        } else {
            return defaultPlayerWidth;
        }
    }
    $(overlayid).css("visibility", "hidden").css("display", "block");
    var dimension = $(overlayid).outerWidth();
    $(overlayid).css("display", "none").css("visibility", "visible");
    return dimension;
}

function videoPosition(overlayid) {
    this.playerWidth = getOverlayWidth(overlayid);
    this.playerHeight = getOverlayHeight(overlayid);
    this.clientHeight = document.documentElement.clientHeight;
    this.clientWidth = document.documentElement.clientWidth;
    
    this.clientScrollTop = $(document).scrollTop();
    this.clientScrollLeft = $(document).scrollLeft();
    this.displayX = 0;
    this.displayY = 0;
    if (this.playerHeight <= this.clientHeight) {
        var tempValue = this.clientHeight - this.playerHeight;
        this.displayY = Math.floor(tempValue / 2);
        if (this.clientScrollTop > 0) {
            this.displayY += this.clientScrollTop;
        }
    }
    if (this.playerWidth <= this.clientWidth) {
        var tempValue = this.clientWidth - this.playerWidth;
        this.displayX = Math.floor(tempValue / 2);
        if (this.clientScrollLeft > 0) {
            this.displayX += this.clientScrollLeft;
        }
    }
}
function currentPosition(overlayid) {
    var posObj = new videoPosition(overlayid);
    if (typeof (overlayid) != "undefined") {
        if (html5MediaOverlayDebugMode) {
            testCurrentPosition(posObj);
        }
        $(overlayid).css("left", posObj.displayX + "px").css("top", posObj.displayY + "px");
    }
    return false;
}

function testCurrentPosition(posObj) {
    var dimensionOutput = "<ul><li>Client: " + posObj.clientWidth + " X " + posObj.clientHeight + "</li>";
    dimensionOutput += "<li>Scroll: " + posObj.clientScrollLeft + " X " + posObj.clientScrollTop + "</li>";
    dimensionOutput += "<li>Player: " + posObj.playerWidth + " X " + posObj.playerHeight + "</li>";
    dimensionOutput += "<li>PlayerPos: " + posObj.displayX + " X " + posObj.displayY + "</li></ul>";
    videoMediaOverlayDebug(dimensionOutput);
}

function closeVideo() {
    $.mask.close();
    return false;
}
function runVideo(clickItem) {
    var overlayid = $(clickItem).attr("VideoOverlayID");
    overlayid = "#" + overlayid;
    if ($(overlayid).length == 0) {
        return true;
    }
    var videoid = $(overlayid).attr("VideoDisplayID");
    videoid = "#" + videoid;
    activeOverlay = overlayid;
    activeVideo = videoid;
    currentPosition(overlayid);
    $(overlayid).show().expose({
        color: overlayBGColor,
        opacity: overlayBGOpacity,
        closeSpeed: overlayCloseSpeed,
        closeOnClick: overlayCloseOnClick,
        onBeforeClose: function() {
            if (isHTML5ok) {
                $(activeVideo).get(0).pause();
            } else {
                $(activeVideo).flowplayer(0).pause();
            }
            if (typeof (activeOverlay) != "undefined") {
                $(activeOverlay).hide();
                activeOverlay = null;
                activeVideo = null;
            }
        },
        onBeforeLoad: function() {
            if (isHTML5ok) {
                $(activeVideo).get(0).play();
            } else {
                if (!$(activeVideo).flowplayer(0).isLoaded()) {
                    $(activeVideo).flowplayer(0).load(function() {
                        startVideoPlaybackDelay();
                    });
                } else {
                    $(activeVideo).flowplayer(0).play();
                }
            }
        }
    });
    return false;
}

var vidPlaybackTimeout = null;
function startVideoPlaybackDelay() {
    if (vidPlaybackTimeout != null) { clearTimeout(vidPlaybackTimeout); }

    var currentVideoState = $(activeVideo).flowplayer(0).getState();
    if (currentVideoState == 1) {
        vidPlaybackTimeout = setTimeout("startVideoPlaybackDelay()", 100);
    } else if (currentVideoState == 2 || currentVideoState == 4) {
        $(activeVideo).flowplayer(0).play();
    }
    if (html5MediaOverlayDebugMode) {
        videoMediaOverlayDebug("State = " + videoMediaOverlayStateString(currentVideoState));
    }
}

function videoMediaOverlayStateString(state) {
    if (state == -1) {
        return "unloaded";
    }
    if (state == 0) {
        return "loaded";
    }
    if (state == 1) {
        return "unstarted";
    }
    if (state == 2) {
        return "buffering";
    }
    if (state == 3) {
        return "playing";
    }
    if (state == 4) {
        return "paused";
    }
    if (state == 5) {
        return "ended";
    }
    return "unknown";
}
function videoMediaOverlayDebug(msg) {
    if ($("#videodisplaydebug").length == 0) {
        $("body").append("<div id=\"videodisplaydebug\"></div>");
    }
    var msgDiv = "<div>" + getVideoMediaOverlayDebugMsgTime() + " - " + msg + "</div>";
    $("#videodisplaydebug").append(msgDiv);
}
function getVideoMediaOverlayDebugMsgTime() {
    var currentTime = new Date();
    var timeString = new String();
    if (currentTime.getHours() < 10) { timeString += "0"; }
    timeString += currentTime.getHours() + ":";
    if (currentTime.getMinutes() < 10) { timeString += "0"; }
    timeString += currentTime.getMinutes() + ":";
    if (currentTime.getSeconds() < 10) { timeString += "0"; }
    timeString += currentTime.getSeconds() + ".";
    if (currentTime.getMilliseconds() < 100) { timeString += "0"; }
    if (currentTime.getMilliseconds() < 10) { timeString += "0"; }
    timeString += currentTime.getMilliseconds();
    return timeString;
}
