
var player;
var playerId;
var partialLinkId = "hlDownloadPartial";
var itemDescId = "itemDesc";

// application loaded event
function ApplicationLoaded() {
    player = document.getElementById(playerId);
    setInterval("timer_Tick()", 200);
    agendaInitialize();
    
}


function Silverlight_Loaded(sender) {
    playerId = sender.get_id();
    ApplicationLoaded();
}


// timer initialize
var currentIndex = -1;
function timer_Tick() {
    if (player) {
        var itemId = player.content.Bridge.GetActiveIndex();
        if (items[currentIndex] != itemId) {
            for (var i = 0; i < items.length; i++) {
                if (items[i] == itemId) {
                    itemChanged(i);
                    break;
                }
            }
        }
    }
    else {
        player = document.getElementById(playerId);
    }
}

function itemChanged(newItem) {
    // highlight the selected item
    highlightSelected(newItem);

    // update the index tracker
    currentIndex = newItem;

    // download links
    updateDownloadLink(newItem);

    // update text area
    updateTextArea(newItem);

}

// item description display
function updateTextArea(newItem) {
    var itemDesc = document.getElementById(itemDescId);
    if (currentIndex != -1) {
        var li = document.getElementById("agendaItemList").getElementsByTagName("li");
        itemDesc.innerHTML = li[newItem].getElementsByTagName("a")[0].innerHTML;
    }
}



// update the download link
function updateDownloadLink(item) {
    if (item < items.length && item > -1) {
        var partialLink = document.getElementById(partialLinkId);
        partialLink.setAttribute("href", "javascript:downloadPartial('" + items[item] + "')");
    }
}



// highlight selected item
function highlightSelected(newIndex) {
    var li = document.getElementById("agendaItemList").getElementsByTagName("li");

    if (li && newIndex != -1) {
        for (var i = 0; i < li.length; i++) {
            if (i == newIndex) {
                li[i].className = "activeAgendaListItem";
                li[i].getElementsByTagName("a")[0].className = "activeAgendaListLink";
            }
            else {
                li[i].className = "unactiveAgendaListItem";
                li[i].getElementsByTagName("a")[0].className = "unactiveAgendaListLink";
            }
        }
    }
}



// initialize the doc
function agendaInitialize() {
    var agendaList = $get("agendaItemList");
    var links = agendaList.getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
        links[i].onclick = function() { agendaItemClick(this.href); return false; }
    }
    // addListeners();
}

// Set the active index
function agendaItemClick(href) {
    var item = extractItemNumber(href);
    if (item > -1) {
        player.content.Bridge.SetActiveIndex(item);
    }
}

// pull the item number value out of a URL
function extractItemNumber(href) {
    var index = 0;
    if (href.match("&Index=")) {
        var chunks = href.split("&", 10);
        if (chunks.length > 0) {
            for (i = 0; i < chunks.length; i++) {
                if (chunks[i].match("Index=")) {
                    var indexArray = chunks[i].split("=");
                    index = indexArray[1];
                    break;
                }
            }
        }
    }
    return parseInt(index);
}

// download popup function
function downloadPartial(id) {
    var url = "Download.aspx?Type=Partial&Id=" + id;
    window.open(url, "Download", "width=450,height=200,scrollbars=no");
}

// download popup function
function downloadFull(url) {
    window.open(url, "Download", "width=450,height=200,scrollbars=no");
}


