var pages = new Array();     // of Page objects
var slides = new Array();    // an array to be filled with DOM div elements
var curSlide = 0;
var curPage = 0;
var toolFrameVisible = false;

function full_path(relative_path) {
    if(parent.loader) {
        curFile = parent.document.location.href;
    } else {
        curFile = document.location.href;
    }
    curFile = curFile.replace(/^[^:]*:\/\/\//, "");
    if (relative_path == undefined) relative_path = "";
    curFile = curFile.replace(/[^/]*\/[^/]*$/, relative_path);
    return curFile;
}


//         FORM ELEMENTS         //

elemPages = null;
elemAvail = null;
elemStyles = null;

function elem_seltext() {
    return this.options[this.selectedIndex].text;
}

function elem_append(newText) {
    this.options.length += 1;
    this.options[this.options.length - 1].text = newText;
}

function init() {
    elemPages = document.getElementById('pages');
    
    elemAvail = document.getElementById('avail');
    elemAvail.seltext = elem_seltext;
    elemAvail.append = elem_append;
    
    elemStyles = document.getElementById('styles');
    elemStyles.seltext = elem_seltext;
    elemStyles.append = elem_append;
    
    if(parent.loader) {
        curFile = parent.document.location.href;
        re = /([^/]*).htm/i;
        curshow = curFile.match(re)[1];
    } else {
        today = new Date();
        month = today.getMonth() + 1;
        if (month < 10) {
            month = '0' + month.toString();
        } else {
            month = month.toString();
        }
        day = today.getDate();
        if (day < 10) {
            day = '0' + day.toString();
        } else {
            day = day.toString();
        }
        curshow = today.getFullYear().toString() + '-' + month + '-' + day;
    }
    document.getElementById('slideshowfile').value = curshow;
}

function fill_page_select(index) {
    elemPages.options.length = pages.length;
    for (i = 0; i < pages.length; i++) {
        elemPages.options[i].text = pages[i].repr();
    }
    if (index >= pages.length) {
        index = pages.length - 1;
    }
    if (index < 0) {
        // Add the negative amount.
        index = pages.length + index;
    }
    if (index < 0) index = -1;
    elemPages.selectedIndex = index;
}

function fill_avail(){
    // Fill the 'available slides' select element
    fileObj = new ActiveXObject("Scripting.FileSystemObject");
    
    // Get slides
    dir = fileObj.GetFolder(full_path("slides"));
    filenames = new Array();
    filelist = new Enumerator(dir.Files);
    for (i=0; !filelist.atEnd(); filelist.moveNext()){
        tName = filelist.item().name;
        if (tName.slice(-4) == '.htm'){
            filenames.push(tName.slice(0, -4));
        }
    }
    filenames.sort();
    elemAvail.options.length = 0;
    for (i=0; i < filenames.length; i++){
        elemAvail.append(filenames[i]);
    }
    
    // Get styles
    dir = fileObj.GetFolder(full_path("styles"));
    filenames = new Array();
    filelist = new Enumerator(dir.Files);
    for (i=0; !filelist.atEnd(); filelist.moveNext()){
        tName = filelist.item().name;
        if (tName.slice(-4) == '.css'){
            filenames.push(tName.slice(0, -4));
        }
    }
    filenames.sort();
    elemStyles.options.length = 0;
    for (i=0; i < filenames.length; i++){
        elemStyles.append(filenames[i]);
    }
}

//          COMPOSITION          //

function Page_repr() {
    return this.filename + " (" + this.style + ")"
}

function Page(filename, style) {
    // Page constructor
    this.filename = filename;
    this.style = style;
    this.repr = Page_repr;
}

function add_page(filename, style, index){
    newPage = new Page(filename, style);
    if (index == undefined) {
        pages[pages.length] = newPage;
    } else {
        // bump pages up one slot
        for (i = pages.length; i > index; i--) {
            pages[i] = pages[i-1];
        }
        pages[index] = newPage;
    }
    fill_page_select(-1);
}

function add_available_page(){
    if (elemAvail.selectedIndex >= 0) {
        insert_index = elemPages.selectedIndex;
        if (insert_index > -1) {
            add_page(elemAvail.seltext(), elemStyles.seltext(), insert_index);
        } else {
            add_page(elemAvail.seltext(), elemStyles.seltext());
        }
    }
}

function remove_page(){
    selIndex = elemPages.selectedIndex;
    if (selIndex > -1) {
        pages.splice(selIndex, 1);
        fill_page_select(selIndex);
    }
}

function move_page_up(){
    selIndex = elemPages.selectedIndex;
    if (selIndex > 0 && selIndex < pages.length) {
        // swap this page with the one above it
        tPage = pages[selIndex];
        pages[selIndex] = pages[selIndex - 1];
        pages[selIndex - 1] = tPage;
        fill_page_select(selIndex - 1);
    }
}

function move_page_down(){
    selIndex = elemPages.selectedIndex;
    if (selIndex > -1 && selIndex < pages.length - 1) {
        // swap this page with the one below it
        tPage = pages[selIndex];
        pages[selIndex] = pages[selIndex + 1];
        pages[selIndex + 1] = tPage;
        fill_page_select(selIndex + 1);
    }
}

function save_slideshow(){
    fileObj = new ActiveXObject("Scripting.FileSystemObject");
    showfile = document.getElementById('slideshowfile').value;
    showfile = full_path("shows/" + showfile + ".htm");
    aFile = fileObj.CreateTextFile(showfile, true);
    
    aFile.WriteLine("<html>");
    aFile.WriteLine("<head>");
    aFile.WriteLine("    <title>Lyrica</title>");
    aFile.WriteLine("<script type='text/javascript'>");
    aFile.WriteLine("<!--");
    aFile.WriteLine("");
    aFile.WriteLine("    function setpages() {");
    
    for (i = 0; i < pages.length; i++) {
        aFile.WriteLine("        self.toolbar.add_page('" + pages[i].filename + "', '" + pages[i].style  + "');");
    }
    
    aFile.WriteLine("        self.toolbar.loadcurrentpage(false);");
    aFile.WriteLine("    }");
    aFile.WriteLine("");
    aFile.WriteLine("// -->");
    aFile.WriteLine("</script>");
    aFile.WriteLine("</head>");
    aFile.WriteLine("<frameset id='topframeset' border='0' rows='1, 1*' onLoad='setpages()'>");
    aFile.WriteLine("    <frameset border='1' cols='1, 1*'>");
    aFile.WriteLine("        <frame name='loader' scrolling='no' />");
    aFile.WriteLine("        <frame name='toolbar' scrolling='no' src='../toolbar/toolbar.htm' />");
    aFile.WriteLine("    </frameset>");
    aFile.WriteLine("    <frame name='mainframe' scrolling='no' />");
    aFile.WriteLine("</frameset>");
    aFile.WriteLine("</html>");
    
    aFile.Close();
    alert("Page saved successfully.");
}


function open_slideshow(){
    showfile = document.getElementById('slideshowfile').value;
    showfile = full_path("shows/" + showfile + ".htm");
    if (window.parent) {
        window.parent.location = showfile;
    } else {
        window.location = showfile;
    }
}

//          NAVIGATION          //

function trapKeys() {
    mapKeys(event.keyCode);
}

function mapKeys(key_code) {
    switch(key_code) {
    case 37:   // left arrow
    case 38:   // up arrow
        goprev();
        break;
    case 39:   // right arrow
    case 40:   // down arrow
        gonext();
        break;
    }
}

function trapMenu() {
    topframe = top.document.getElementById('topframeset');
    if(toolFrameVisible){
        topframe.rows = "1, 1*";
    } else {
        topframe.rows = "300, 1*";
    }
    toolFrameVisible = !(toolFrameVisible);
    return false;
}

function goToPage() {
    if(elemPages.options.length > 0) {
        curPage = elemPages.selectedIndex;
        curSlide = 0;
        loadcurrentpage(false);
    }
}

function goprev() {
    if(slides.length > 0 && curSlide > 0) {
        curSlide -= 1;
        loadcurrentslide();
    } else {
        if (curPage > 0) {
            curPage -= 1;
        } else {
            curPage = elemPages.options.length - 1;
        }
        loadcurrentpage(true);
    }
}

function gonext() {
    if(curSlide + 1 < slides.length) {
        curSlide += 1;
        loadcurrentslide();
    } else {
        if (curPage + 1 < elemPages.options.length) {
            curPage += 1;
        } else {
            curPage = 0;
        }
        loadcurrentpage(false);
    }
}

function loadcurrentslide() {
    if(parent.loader) {
        // Show the current slide.
        if(slides.length > 0) {
            for(i=0; i<slides.length; i++) {
                if(i!=curSlide) slides[i].style.display = 'none';
            }
            slides[curSlide].style.display = 'block';
        }
        
        // Show a small arrow or two indicating whether there are
        // previous or further slides included in this page.
        if(slides.length > 1){
            navText = '&lt;&gt;';
            if(curSlide == 0) navText = '&gt;';
            if(curSlide == slides.length - 1) navText = '&lt;';
        } else {
            navText = '';
        }
        parent.mainframe.document.getElementById('slidenav').innerHTML = navText;
    }
}

function loadcurrentpage(loadlastslide) {
    tLen = pages.length;
    if (tLen > 0) {
        // Sanity checks
        if (curPage > tLen - 1) curPage = tLen - 1;
        if (curPage < 0) curPage = 0;
        
        // Highlight the current page in the pages list.
        elemPages.selectedIndex = curPage;
        
        if(parent.loader) {
            // Load the requested page into the loader frame.
            slideFile = full_path("slides/" + pages[curPage].filename + ".htm");
            parent.loader.location = slideFile;
            // A timeout is necessary here, rather than inlining,
            // to give IE time to show parent.loader
            self.setTimeout('fill_main_frame(' + loadlastslide + ')', 100);
        }
    }
}

function fill_main_frame(loadlastslide){
    // Write the HTML for the main frame, incorporating the
    // content of the current page and the specified CSS file
    // reference.
    main = parent.mainframe.document;
    main.open();
    main.writeln("<html>");
    main.writeln("<head>");
    cssFile = "../styles/" + pages[curPage].style + ".css";
    main.writeln("    <link rel='stylesheet' type='text/css' href='" + cssFile + "'>");
    main.writeln("</head>");
    main.writeln("<body>");
    // Drop the loader HTML into the mainframe HTML.
    main.writeln(parent.loader.document.body.innerHTML);
    main.writeln("<p id='slidenav'></p>");
    main.writeln("<script type='text/javascript'>");
    main.writeln("    function trapKeys() {");
    main.writeln("        parent.toolbar.mapKeys(event.keyCode);");
    main.writeln("    }");
    main.writeln("    document.onkeyup = trapKeys;");
    main.writeln("    document.onclick = parent.toolbar.gonext;");
    main.writeln("    document.oncontextmenu = parent.toolbar.trapMenu;");
    main.writeln("</script>");
    main.writeln("</body>");
    main.writeln("</html>");
    
    // Emphasize the first page
    if(curPage == 0){
        titles = main.getElementsByTagName("h1");
        for (i = 0; i < titles.length; i++) {
            titles[i].className = 'firstpage';
        }
    }
    
    // Populate the slides collection.
    slides.length = 0;
    divs = main.getElementsByTagName("div");
    for (i = 0; i < divs.length; i++) {
        if (divs[i].className == 'slide') slides[slides.length]=divs[i];
    }
    
    // Display only the current slide
    if (loadlastslide && slides.length > 0) {
        curSlide = slides.length - 1;
    } else {
        curSlide = 0;
    }
    loadcurrentslide();
    
    parent.mainframe.focus();
}

function show_help() {
    window.open("help.htm", "lyricahelp", "dependent=yes,resizable=yes,scrollbars=yes,width=300,height=500");
}


