﻿var videoFunctionStack = "";
var isPlayerLoaded = false;
var setNewVideo = true;

//function SetVideo(url) {
//    $('img.itemImg').hide();
//    SetVideoUrl(url);
//    //PlayVideo();
//}
window.onload = function(e) {
    if (document.getElementById('demo')) {
        document.getElementById('demo').style.top = 300 + 'px';
    }
}

window.onscroll = function(e) {
    if (document.getElementById('demo')) {
        document.getElementById('demo').style.top = eval(document.documentElement.scrollTop) + 300 + 'px';
    }
}

function SetVideoUrl(url) {
    if ($('#windowsLiveInAction') != null) {
        $('#windowsLiveInAction').find('a').eq(0).fadeTo("slow", 0.5);
        $('#windowsLiveInAction').find('a').eq(1).fadeTo("slow", 0.5);
    }
    document.getElementById('overDemo').style.visibility = 'visible';
    document.getElementById('demo').style.visibility = 'visible';
    if (isPlayerLoaded) {
        if (setNewVideo) {
            setNewVideo = false;
            var urlsArray = url.split("|");
            setTimeout('document.getElementById("SilverlightHostObject").Content.Page.SetVideo("' + urlsArray[0] + '")', 1000);
            if (urlsArray.length > 1) {
                var urls = '';
                for (i = 1; i < urlsArray.length; i++)
                    urls += urlsArray[i] + '|';
                setTimeout('document.getElementById("SilverlightHostObject").Content.Page.SetRelatedVideos("' + urls + '")', 1000);
            }
            if (arguments.length > 1) {
                var urls = arguments[1];
                for (i = 2; i < arguments.length; i++)
                    urls += '|' + arguments[i];
                setTimeout('document.getElementById("SilverlightHostObject").Content.Page.SetRelatedVideos("' + urls + '")', 1000);
            }
            PlayVideo();
        }
    }
    else {
        videoFunctionStack += "SetVideoUrl('" + url + "'";
        if (arguments.length > 1) {
            for (i = 1; i < arguments.length; i++)
                videoFunctionStack += ", '" + arguments[i] + "'";
        }
        videoFunctionStack += ");";
    }
}

function PlayVideo() {
    document.getElementById('overDemo').style.visibility = 'visible';
    document.getElementById('demo').style.visibility = 'visible';
    if (isPlayerLoaded) {
        setTimeout('document.getElementById("SilverlightHostObject").Content.Page.PlayVideo()', 1500);
    }
    else {
        videoFunctionStack += "PlayVideo();";
    }
}

function StopVideo() {
    if (isPlayerLoaded) {
        setTimeout('document.getElementById("SilverlightHostObject").Content.Page.StopVideo()', 1500);
    }
    else {
        videoFunctionStack += "StopVideo();";
    }
}

function DropVideo() {
    if (isPlayerLoaded) {
        setTimeout('document.getElementById("SilverlightHostObject").Content.Page.DropVideo()', 1000);
        setTimeout('$(\'#windowsLiveInAction\').find(\'a\').eq(0).fadeTo("slow", 1.0)', 1500);
        setTimeout('$(\'#windowsLiveInAction\').find(\'a\').eq(1).fadeTo("slow", 1.0)', 1500);
        setTimeout('setNewVideo=true', 1500);
    }
    document.getElementById('overDemo').style.visibility = 'hidden';
    document.getElementById('demo').style.visibility = 'hidden';
}


function onSilverlightLoad() {
    if (!isPlayerLoaded) {
        isPlayerLoaded = true;
        eval(videoFunctionStack);
        videoFunctionStack = "";
    }
}