// Makes the given function run at page load without having
// to worry about clobbering another onload event.
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

// Just a shorthand for referring to an IDed page element
function $(id) { return(document.getElementById(id)); }

// Return an XMLHTTP request, cross-browser
function getRequest() {
    var request;
    if(window.XMLHttpRequest)
        request=new XMLHttpRequest();
    else if(window.ActiveXObject)
        request=new ActiveXObject("Microsoft.XMLHTTP");
    return request;
} 

// Remove all child nodes from a given element
function clearChildNodes(id) {
    var elem=$(id);
    for(i=0;i<elem.childNodes.length;i++)
        elem.removeChild(elem.childNodes[i]);
}

// Shorthand for hiding and showing elements
function hide(id) { $(id).style.display="none"; }
function show(id) { $(id).style.display="block"; }

function enableNext() {
    $("next").className="";
    $("next").disabled=false;
    $("next").onclick=function() { document.location=$("next").href; };
}

function disableNext() {
    $("next").className+=" disabled";
    $("next").disabled=true;
    $("next").onclick=function() { return(false); };
}

// Renders a list of errors into <ul id="error-list">
function showErrors(errors) {
    clearChildNodes("error-list");

    // Apparently all the children do actually get removed sometimes!
    if(!$("error-list").childNodes.length)
        $("error-list").appendChild(document.createElement("li"));

    // Lists always (almost; see above) have one child, so use that...
    $("error-list").childNodes[0].innerHTML=errors[0];
    // ...then start with the second one if more errors
    for(i=1;i<errors.length;i++)
    {
        var li=document.createElement("li");
        li.innerHTML=errors[i];
        $("error-list").appendChild(li);
    }
    show("error");
}

// Prints a document by loading the document in an iframe and then
// opening the print dialog box. A status message also pops up.
function hiddenPrint(url) {
    //document.location=url; return(false); // Uncomment to do inline preview (debugging)
    var printFrame=document.createElement("iframe");
    printFrame.id="printFrame";
    printFrame.onload=function() {
        $("printFrame").style.display='block';
        // Wait to print until done loading
        setTimeout("window.frames[0].print();", 4000);
    }
    printFrame.src=url;
    $("footer").appendChild(printFrame);

    // If this is IE, start a timer since onload isn't supported
    if(document.readyState) setTimeout(iePrint, 1000);

    // Create a little message to indicate that printing will start soon
    var statusSpan=document.createElement("span");
    statusSpan.id="statusSpan";
    statusSpan.innerHTML="Please wait while your document prints.";
    $("slide-content").appendChild(statusSpan);
    setTimeout("$('statusSpan').style.display='none';", 6000);

    // Count down until print with a growing ellipsis
    setTimeout("ellipsis(5)", 1000);
}

// IE-specific code to print the printFrame
function iePrint() {
    $("printFrame").style.display='block';
    if($("printFrame").readyState=='complete')
    {
        $("printFrame").contentWindow.focus();
        $("printFrame").contentWindow.print();
    }
    else setTimeout(iePrint, 1000);
}

// Just a quick little indication of progress
function ellipsis(countdown) {
    $("statusSpan").innerHTML+=".";
    if(countdown>0) setTimeout("ellipsis("+(countdown--)+");", 1000);
}

// Play a single sound file immediately
// See documentation at: http://www.schillmania.com/projects/soundmanager2/
function playSound(file) {
    soundManager.stopAll();
    soundManager.play("sound"+file,"sounds/"+file+".mp3");
}

// Handler for toggling sound on and off
addLoadEvent(function() {
    $("sound").onclick=function() {
        var request=getRequest();
        var url="components/User.cfc?method=ToggleSound";
        request.open("GET", url, true);
        request.onreadystatechange=function() {
            if(request.readyState==4)
            {
                var enabled;
                if(request.responseText.match(/value='true'/)) enabled=true;
                else enabled=false;
                
                if(enabled)
                {
                    $("sound").src="images/soundon.gif";
                    if(soundManager.soundIDs.length)
                        soundManager.play(soundManager.soundIDs[0]);
                }
                else
                {
                    $("sound").src="images/soundoff.gif";
                    soundManager.stopAll();
                }
            }
        };
        request.send(null);
    };
});

// Set up sound manager
// See documentation at: http://www.schillmania.com/projects/soundmanager2/
soundManager.debugMode=false;
soundManager.useConsole=false;
soundManager.url="scripts/soundmanager/soundmanager2.swf";

// Can only start to play sounds after soundManager has loaded.
soundManager.onload = function() {
    // Add calls to soundManager.createSound(), etc., here.
};

