﻿var video1 = {};
var video2 = {};
var video3 = {};
var flashvars1 = {};
var flashvars2 = {};
var flashvars3 = {};
var params = {};
var attributes = {};
var showFrames = true;
var attributesInvisible = {};

initVideoObject(video1, 1);
initVideoObject(video2, 2);
initVideoObject(video3, 3);

if (typeof SetVariablesHTC == 'function')
{ SetVariablesHTC(); } //is set in VideosHTC.js if it has been included (if HTC device detected)
else
{ SetVariables(); }

swfobject.embedSWF("/vioPlayer.swf", "vioPlayer1", "460", "376", "9.0.115", "/expressInstall.swf", flashvars1, params, attributes);
swfobject.embedSWF("/vioPlayer.swf", "vioPlayer2", "460", "376", "9.0.115", "/expressInstall.swf", flashvars2, params, attributesInvisible);
swfobject.embedSWF("/vioPlayer.swf", "vioPlayer3", "460", "376", "9.0.115", "/expressInstall.swf", flashvars3, params, attributesInvisible);

function SetVariables() {
    flashvars1.configURL = "/config1.xml";
    flashvars1.scaleToFit = true;
    flashvars1.ShowSplash = true;
    flashvars2.configURL = "/config2.xml";
    flashvars2.scaleToFit = true;
    flashvars3.configURL = "/config3.xml";
    flashvars3.scaleToFit = true;
    params.allowfullscreen = "true";
    params.allowscriptaccess = "always";
    params.wmode = "transparent";
    attributes.wmode = "opaque";
    attributes.VALUE = "transparent";
    attributesInvisible.wmode = "opaque";
    attributesInvisible.VALUE = "transparent";
    attributesInvisible.style = "display: none";
}

function initVideoObject(videoObj, index) {
    videoObj.originalPosition = index;
    videoObj.currentPosition = index;
    videoObj.currentFrame = 1;
    videoObj.videoEnded = false;
    videoObj.imageFileOn = 'images/SmallPreview' + index + '_on.jpg';
    videoObj.imageFileOff = 'images/SmallPreview' + index + '_off.jpg';
    videoObj.logoFile = 'images/Logo' + index + '.gif';
    videoObj.endImageFile = 'images/EndImage' + index + '.jpg';
    videoObj.swfVideoObject = null;
    videoObj.playVideo = false;
    videoObj.ready = false;
    videoObj.endFrame1 = 'images/Frame1-' + index + '.jpg';
    videoObj.endFrame2 = 'images/Frame2-' + index + '.jpg';
    videoObj.endFrame3 = 'images/Frame3-' + index + '.jpg';
    videoObj.runStartGAScript = function() {
        var videoId = "'Video" + this.originalPosition + "'";
        _gaq.push(['_trackEvent', 'Videos', 'Play', videoId]);
    }
    videoObj.runEndGAScript = function() {
        var videoId = "'Video" + this.originalPosition + "'";
        _gaq.push(['_trackEvent', 'Videos', 'Finish', videoId]);
    }
    videoObj.runCurrentFrameGAScript = function() {
        var videoId = "'Video" + this.originalPosition + "'";
        var frameId = "'Frame" + this.currentFrame + "'";
        _gaq.push(['_trackEvent', 'Videos', frameId, videoId]);
    }
    videoObj.runReplayGAScript = function() {
        var videoId = "'Video" + this.originalPosition + "'";
        _gaq.push(['_trackEvent', 'Videos', 'Replay', videoId]);
    }
    videoObj.runShareToWinGAScript = function() {
        var videoId = "'Video" + this.originalPosition + "'";
        _gaq.push(['_trackEvent', 'Videos', 'ShareToWin', videoId]);
    }
    videoObj.runRewindFromEndGAScript = function() {
        var videoId = "'Video" + this.originalPosition + "'";
        _gaq.push(['_trackEvent', 'Videos', 'RewindFromEnd', videoId]);
    }
}

function load() {
    try {
        initVideo(video1);
        initVideo(video2);
        initVideo(video3);
        video1.playVideo = true;
    }
    catch (err)
    { }
}

function vioPlayerReady() {
    var objVideo = null;
    objVideo = (video1.currentPosition == 1 ? video1 : (video2.currentPosition == 1 ? video2 : (video3.currentPosition == 1 ? video3 : null)));
    if (objVideo != null) {
        objVideo.swfVideoObject = document["vioPlayer" + objVideo.originalPosition];
        objVideo.ready = true;
        attachStateEvent(objVideo);
        if (objVideo.playVideo)
            replayVideo(objVideo);
    }
}

function attachStateEvent(objVideo) {
    try {
        if (objVideo.swfVideoObject.addEventListener != null)
            objVideo.swfVideoObject.addEventListener("stateEvent", "onState");
    }
    catch (err)
    { }
}

function initVideo(objVideo) {
    objVideo.swfVideoObject = document["vioPlayer" + objVideo.originalPosition];
}

function showHideVideo(objVideo) {
    try {
        if (objVideo != null) {
            var visible = objVideo.currentPosition == 1;
            objVideo.swfVideoObject.style.display = (visible ? 'block' : 'none');
            objVideo.playVideo = visible;
            if (objVideo.currentPosition == 2)
                document.getElementById('image2').src = objVideo.imageFileOff;
            if (objVideo.currentPosition == 3)
                document.getElementById('image3').src = objVideo.imageFileOff;
            if (visible) {
                document.getElementById('logo').src = objVideo.logoFile;
                if (navigator.appName.indexOf("Microsoft") > -1) {
                    attachStateEvent(objVideo);
                }
                replayVideo(objVideo);
            }
        }
    }
    catch (err)
    { }
}

function onState(json) {
    if (json == "ended")
        videoEnded();
    else if (json == "seeking") {
        var objVideo = getCurrentVideo();
        if (objVideo.videoEnded == true) {
            objVideo.swfVideoObject.mediaPlay();
            objVideo.videoEnded = false;
            showFrames = false;
        } 
    }

    else
        document.getElementById('vioPlayerButtons').style.display = 'none';
}

function shareToWinClicked() {
    var objVideo = getCurrentVideo();
    objVideo.runShareToWinGAScript();
}

function replay() {
    var objVideo = getCurrentVideo();
    objVideo.runReplayGAScript();
    showHideButtons(false);
    showHideVideos();
}

function getVideo(currentPosition) {
    var video = null;
    video = (video1.currentPosition == currentPosition ? video1 : (video2.currentPosition == currentPosition ? video2 : video3));
    return video;
}
function getCurrentVideo() {
    var video = null;
    if (video1.currentPosition == 1)
        video = video1;
    if (video2.currentPosition == 1)
        video = video2;
    if (video3.currentPosition == 1)
        video = video3;

    return video;
}

function replayVideo(objVideo) {
    try {
        if (objVideo.ready) {
            objVideo.videoEnded = false;
            objVideo.swfVideoObject.seek(0);
            objVideo.swfVideoObject.mediaPlay();
            objVideo.runStartGAScript();
        }
    }
    catch (err)
    { }
}

function videoEnded() {
    showFrames = true;
    showHideButtons(true);
    var objVideo = getCurrentVideo();
    if (objVideo != null) {
        objVideo.runEndGAScript();
        objVideo.videoEnded = true;
    }
    hideHoldingImage();
    document.getElementById('replayImage').style.display = 'none';
    document.getElementById('shareToWinImage').style.display = 'none';
    document.getElementById('divImage2').style.display = 'block';
    document.getElementById('divImage3').style.display = 'block';
    document.getElementById('imgEndingFrame').src = '';
    ShowFrame1();
}

function ShowFrame1() {
    if (showFrames) {
        var objVideo = getCurrentVideo();
        document.getElementById('imgEndingFrame').src = objVideo.endFrame1;
        objVideo.currentFrame = 1;
        objVideo.runCurrentFrameGAScript();
        setTimeout(ShowFrame2, 1700);
    }
}

function ShowFrame2() {
    if (showFrames) {
        var objVideo = getCurrentVideo();
        document.getElementById('imgEndingFrame').src = objVideo.endFrame2;
        objVideo.currentFrame = 2;
        objVideo.runCurrentFrameGAScript();
        setTimeout(ShowFrame3, 3400);
    }
}

function ShowFrame3() {
    if (showFrames) {
        var objVideo = getCurrentVideo();
        document.getElementById('imgEndingFrame').src = objVideo.endFrame3;
        objVideo.currentFrame = 3;
        objVideo.runCurrentFrameGAScript();
        setTimeout(ShowFrame4, 600);
    }
}

function ShowFrame4() {
    if (showFrames) {
        var objVideo = getCurrentVideo();
        document.getElementById('replayImage').style.display = 'block';
        document.getElementById('shareToWinImage').style.display = 'block';
        objVideo.currentFrame = 4;
        objVideo.runCurrentFrameGAScript();
    }
}

function showHideButtons(isVisible) {
    document.getElementById('vioPlayerButtons').style.display = (isVisible ? 'block' : 'none');
}

function hideHoldingImage() {
    document.getElementById('divHoldingImage').style.display = 'none';
}

function showHideVideos() {
    showHideVideo(video1);
    showHideVideo(video2);
    showHideVideo(video3);
}

function swapVideo(imageIndex) {
    showFrames = false;
    video1.currentPosition = (video1.currentPosition == imageIndex ? 1 : (video1.currentPosition == 1 ? imageIndex : video1.currentPosition));
    video2.currentPosition = (video2.currentPosition == imageIndex ? 1 : (video2.currentPosition == 1 ? imageIndex : video2.currentPosition));
    video3.currentPosition = (video3.currentPosition == imageIndex ? 1 : (video3.currentPosition == 1 ? imageIndex : video3.currentPosition));
    showHideVideos();
    hideHoldingImage();
    showHideButtons(false);
}

