///////////////////////////////
// Coronado Video JavaScript //
///////////////////////////////

// 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");
}

// run the init script on page load
Sys.UI.DomEvent.addHandler(window, "load", playerInit);

// initialize the player
function playerInit()
{   
    docInit();
}

function playerStateChange(ost, nst) {
    var playing = $get("playerStatus");
    if (playing) {
        playing.innerHTML = "Currently " + nst;
    }
}

var currentSecond = -1;
function playerTimeChange(et, nt) {
    var second = parseInt(et);
    if (currentSecond != second) {
        currentSecond = second;
        playerTick(second, parseInt(nt));
    }
}



// initialize the doc
function docInit()
{
    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();
}

// Subscription of the listener
function addListeners() {
    if (ply.view) {
        ply.addListener('STATE', playerStateChange);
        ply.addListener('TIME', playerTimeChange);
    } else {
        setTimeout(addListeners, 100);
    }
};




function agendaItemClick(href)
{
    var item = extractItemNumber(href);
    if(item > -1)
    {
        playerJumpTo(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);
}

// item description display
function updateTextArea(newItem)
{
    var itemDesc = $get("itemDesc");    
    if(currentIndex != -1) {
        var li = document.getElementById("agendaItemList").getElementsByTagName("li");
        itemDesc.innerHTML = li[newItem].getElementsByTagName("a")[0].innerHTML;
    }
}

// update the items playstate display
function updateStatus(status)
{
    var playing = $get("playerStatus");    
    if(playing) {
        playing.innerHTML = "Currently " + status;
    }
}

// jump to a specific index
function playerJumpTo(index)
{
    var indexTime = startTimes[index];
    ply.sendEvent('SCRUB', indexTime);
}

// monitor selected index and "throw events" when there's a change
var currentIndex = -1;
function playerTick(seconds, duration) {

    // counter update
    var counter = $get("counter");
    counter.innerHTML = counterFormater(seconds)

    for (i = 0; i < startTimes.length; i++) {

        var start = startTimes[i];
        
        var end = duration;
        if (i != startTimes.length - 1) {
            end = startTimes[i+1];
        }

        if (seconds >= start && seconds < end) {
            if (currentIndex != i) {
                currentIndex = i;
                itemChanged(i);
            }
            break;
        }
    }
}

function itemChanged(newItem) {
        // highlight the selected item
        highlightSelected(newItem);
         
        // update the index tracker
        currentIndex = newItem;
        
        // download links
        updateDownloadLinks(newItem);

        // update text area
        updateTextArea(newItem);

}


// update the download link
function updateDownloadLinks(item)
{
    if (item < items.length && item > -1)
    {
        var partialLink = $get("ctl00_ContentPlaceHolder1_hlDownloadPartial");
        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";
            }
        }
    }
}

function secondsToTime(secs) {
    var hours = Math.floor(secs / (60 * 60));

    var divisor_for_minutes = secs % (60 * 60);
    var minutes = Math.floor(divisor_for_minutes / 60);

    var divisor_for_seconds = divisor_for_minutes % 60;
    var seconds = Math.ceil(divisor_for_seconds);

    var obj = {
        "h": hours,
        "m": minutes,
        "s": seconds
    };
    return obj;
}

function pad2(number) {

    return (number < 10) ? '0' + number : number;

}

function counterFormater(secs) {

    var obj = secondsToTime(secs);
    return pad2(obj.h) + ":" + pad2(obj.m) + ":" + pad2(obj.s);

}









