﻿//path to the video to play
var videoToPlay;
//number of plays, only important for IE vs other browsers
var playerLoadCount = 0;

var videos = [];
videos[0] = "mms://msvcatalog-3.wmod.llnwd.net/a2249/e1/ft/share15/edee/0/769c3cc0-926b-4770-a17e-5ffc2d2edb58.wmv";
videos[1] = "mms://msnvideo.wmod.llnwd.net/a392/d1/cmg/WPC/DWPC_Event/AdditionalWPCVideos/WPC04-EventWrapUpVideo.wmv";
videos[2] = "mms://msnvidweb.wmod.msecnd.net/a10026/e1/ft/share3/f6ae/0/Connect_with_Microsoft_at_WPC09_mbr.wmv";

function playVideo(source) {
    var displayval = $("#SilverlightPlayer").css("display");
    if ("none" != displayval) {
        var element = document.getElementById('SilverlightObject');
        var player = element.content.findName('mediaPlayer');
        player.Source = source;
        player.AutoPlay = true;
        
        player.Width = 561;
        player.Height= 318;
    }
}

function stopVideo() {
    var displayval = $("#SilverlightPlayer").css("display");
    if ("none" != displayval) {
        var element = document.getElementById('SilverlightObject');
        if (element != null){
            var player = element.content.findName('mediaPlayer');
            player.Source = null;
            player.AutoPlay = true;
        } 
    }
}

function playerHide() {
    stopVideo();
    $("#SilverlightPlayer").hide();
}

function playerShow() {
    if ($.browser.msie) {
        $("#SilverlightPlayer").show();
        if (playerLoadCount > 0)
            playerLoaded(null)
    }
    else {
        $("#SilverlightPlayer").show();
    }
}
//video player onload callback
function playerLoaded(source) {
    playerLoadCount = playerLoadCount + 1;
    if (videoToPlay != null)
        playVideo(videoToPlay);
}

function updatePlayerThumbnail(day) {
    hideThumbnails();
    $("#promo" + day).show();
}

function hideThumbnails() {
    $("#promo1").hide();
    $("#promo2").hide();
    $("#promo3").hide();
    $("#promo4").hide();
}
function changePromoPage(currDay, useFadeTransition) {
    // clear active anchor
    $("#VideoTabs li span").removeClass("activeVideoTab");

    // set background of video player
    updatePlayerThumbnail(currDay);
    
    // set anchor to active
    $("#VideoTabs li span#videoTab" + currDay).addClass("activeVideoTab");
}    

var currentDay = 1;
var executeCycle;

function rotateDay() {
  currentDay++
  if (currentDay > 3) {
    currentDay = 1;
  }
  changePromoPage(currentDay, true);
}

function cycleDays(delay) {
  executeCycle = setInterval(rotateDay, delay);
}

var prevDay = 1;

function isSilverlightInstalled() {
    var isSilverlightInstalled = false;
    try {
        //check on IE
        try {
            var slControl = new ActiveXObject('AgControl.AgControl');
            isSilverlightInstalled = true;
        }
        catch (e) {
            //either not installed or not IE. Check Firefox
            if (navigator.plugins["Silverlight Plug-In"]) {
                isSilverlightInstalled = true;
            }
        }
    }
    catch (e) {}
    return isSilverlightInstalled;
}

//we should not call methods on player before it's loaded
$(document).ready(function() {
//    if (!isSilverlightInstalled()) {
//        //hide thumnails and nav
//        $("#LargePromoContent").hide();
//        $("#VideoTabs").hide();
//        $("#SilverlightPlayer").show();
//        return;
//    }

    changePromoPage(currentDay, false);
    cycleDays(7000);

    $("#VideoTabs li span").click(function(span) {
        var id = span.currentTarget.id.charAt(8);
        clearInterval(executeCycle);
        //playerHide();
        changePromoPage(id);
    });


//    $("#Videos div img").click(function(imageClicked) {
//        //stop animation
//        if (executeCycle != null)
//            clearInterval(executeCycle);

//        //find id of video
//        var divId = imageClicked.currentTarget.parentNode.id;
//        var index = divId.indexOf("promo", 0);
//        var day = divId.substring(index + 5, index + 6);

//        //disable thumbnail
//        hideThumbnails();

//        //load video
//        videoToPlay = videos[parseInt(day) - 1];

//        playerShow();
//    });
});
