var udlList = new Array();
var locList = new Array();
var currUDLConnNum = 0;

// Find all UDL Connection irems on the page
function getUDLConn() {
    var j = 0;
    var spanList = document.getElementById('maincontent').getElementsByTagName("SPAN");
    var ulList = document.getElementById('maincontent').getElementsByTagName("UL");
    for (var i=0; i < spanList.length; i++) {
        if (spanList[i].id.match(new RegExp("^udlitm_", "i"))) {
            var tempNum = spanList[i].id.substring(7);
            udlList[tempNum-1] = spanList[i];
        }
    }
    for (var i=0; i < ulList.length; i++) {
        if (ulList[i].id.match(new RegExp("^udlitm_", "i"))) {
            var tempNum = ulList[i].id.substring(7);
            udlList[tempNum-1] = ulList[i];
        }
    }
}

// Horizontal align 'UDL Connections' buttons with text to be highlighted
function alignUDLConn() {
    // Reset arrays
    udlList = new Array();
    locList = new Array();

    // Build UDL Connection List
    getUDLConn();
    for (var i=0; i < udlList.length; i++) {
        if (udlList[i].id.match(new RegExp("^udlitm_", "i"))) {
            // Find top coord
            tempHeight = udlList[i].offsetTop;
            // If IE add header height
            this.isIE = (document.all) ? true : false;
            if (this.isIE) {
                tempHeight += document.getElementById('header').offsetHeight;
            }
            // Set top for matching button
            tempNum = udlList[i].id.substring(7);
            //Check to see if this interferes/overlaps with the previous button
            if (i > 0) {
                if ((locList[i-1] <= tempHeight) && (tempHeight <= (locList[i-1] + 24))) {
                    // Move button down a bit below previous one
                    tempHeight = locList[i-1] + 25;
                }
            }
            // Align button on page
            document.getElementById("udlconn_" + tempNum).style.top = tempHeight + "px";
            // Save button location
            locList[i] = tempHeight;
        }
    }
}

// Hide all 'UDL Connections' buttons
function hideAllUDLConn(num) {
    for (var i=0; i < udlList.length; i++) {
        if (udlList[i].id.match(new RegExp("^udlitm_", "i"))) {
            tempNum = udlList[i].id.substring(7);
            if (tempNum == num) {
                // show this item
                document.getElementById("udlconn_" + tempNum).style.display = "block"
            } else {
                // Hide this item
                document.getElementById("udlconn_" + tempNum).style.display = "none"
            }
        }
    }
}

// Show all 'UDL Connections' buttons
function showAllUDLConn() {
    // Find UDL Connection items
    for (var i=0; i < udlList.length; i++) {
        if (udlList[i].id.match(new RegExp("^udlitm_", "i"))) {
            tempNum = udlList[i].id.substring(7);
            // Show this item
            document.getElementById("udlconn_" + tempNum).style.display = "block";
        }
    }
}

// Open the specified UDL Connection item
function openUDLConn(num) {
    // Hide all 'UDL Connections' items excpet specified one
    hideAllUDLConn(num);
    // Highlight the text
    document.getElementById("udlitm_" + num).className = "udlitemhigh";
    // Hide the specified button
    document.getElementById("udlbtn_" + num).style.display = "none";
    // Show the specified content
    document.getElementById("udltop_" + num).style.display = "block";
    document.getElementById("udltxt_" + num).style.display = "block";
}

// Close the specified UDL Connection item
function closeUDLConn(num) {
    // Remove highlight from the text
    document.getElementById("udlitm_" + num).className = "";
    // Hide audio item
    hideAudio(num);
    // Hide agent item
    hideAgentInline(num);
    // Hide the specified content
    document.getElementById("udltop_" + num).style.display = "none";
    document.getElementById("udltxt_" + num).style.display = "none";
    // Show the specified button
    document.getElementById("udlbtn_" + num).style.display = "block";
    // Show all 'UDL Connections' buttons
    showAllUDLConn();
}

function showAudio(num, filename) {
    // Hide Listen button
    document.getElementById("udllistenaudio_" + num).style.display = "none";
    // Build Flash OBJECT item
    var FO = { movie:"./flash/mp3_player/mp3player.swf", width:"175", height:"20", majorversion:"7", build:"0", flashvars:"file=./audio/" + filename + "&config=./flash/mp3_player/config.xml&mywidth=175&myheight=20", id:"mp3player_" + num, allowscriptaccess:"always" };
    UFO.create(FO, 'udlaudio_' + num);
    // Delay for audio clip reload
    setTimeout('playAudio("' + num + '", "' + filename + '")', 1000);
}

function playAudio(num, filename)  {
    thisMovie =  document.getElementById("mp3player_" + num);
    thisMovie.jsControl('load',"./audio/" + filename);
}

function hideAudio(num) {
    if (document.getElementById("udlaudio_" + num) == null) {
        return;
    }
    // Delete the children of DOM node for the audio item
    objNode = document.getElementById("udlaudio_" + num);
    // Walk DOM tree backwards
    while(objNode.hasChildNodes()) {
        objNode.removeChild(objNode.lastChild);
    }

    // Resize the div
    document.getElementById('udlaudio_' + num).style.width = "0px";
    document.getElementById('udlaudio_' + num).style.height = "0px";

    // Show Listen buttons
    document.getElementById("udllistenaudio_" + num).style.display = "block";
}

function showAgentInline(num) {
    // Hide Listen button
    document.getElementById("udllistenagent_" + num).style.display = "none";
    // Update agent settings (sequence file)
    if (AgentItem[1] == null) {
        createAgent(sequenceFile[num]);
    } else {
        AgentItem[1].seqPath = sequenceFile[num];
        AgentItem[1].updateSettings();
    }
    // Insert into agent container
    document.getElementById('udlagent_' + num).style.marginTop = "-60px";
    AgentItem[1].insertTag('udlagent_' + num);
    // Set hide on play end
    currUDLConnNum = num;
    AgentItem[1].setOnPlayStop( function() { hideAgentInline(currUDLConnNum); } );
    // Start playing
    AgentItem[1].playSequence('01');
}

function hideAgentInline(num) {
    if (document.getElementById("udlagent_" + num) == null) {
        return;
    }

    // Reset holder
    currUDLConnNum = 0;

    // Delete the children of DOM node for the audio item
    objNode = document.getElementById("udlagent_" + num);
    // Walk DOM tree backwards
    while(objNode.hasChildNodes()) {
        objNode.removeChild(objNode.lastChild);
    }

    // Resize the div
    document.getElementById('udlagent_' + num).style.marginTop = "0px";

    // Show Listen buttons
    document.getElementById("udllistenagent_" + num).style.display = "block";
}

function createAgent(sequenceFile) {
    // sequenceFile(s) -- defined in model lesson plan template -- e.g. [app_root]/model_plans/butterflies_1.tpl

    // Create agent object
	var agent = new AgentChar(1, 'http://agents.cast.org/v2/', 'http://agents.cast.org/shared/', sequenceFile);
	agent.autoplay = true;
	agent.setSize(175, 175);
}

addEvent(window, 'load', alignUDLConn);
addEvent(window, 'resize', alignUDLConn);