/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 *
 * The initial developer of the code is Andy Edmonds.
 * Portions created by the Initial Developer are Copyright (C) 2001
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *  Andy Edmonds <andy@uzilla.net>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

// CUSTOMIZABLE VALUES
var multis = "artists,categories";
var isEdit = false; // Using numeric count for number of specified values
var inSequence = true; // Set to false to disable auto-sequential expansion
var autoCollapse = false; // Only allow one container to be expanded at a time

// TRACKING VARIABLES -- do not change
var arMultis = multis.split(",");
var curOpen = false;
var sequenceNum = 0; //Tracker for auto-sequential expansion
var isOpen = -1; // Currently open element for auto-collapse
//,activity,agent,condition";

function initMultiSelect() {
    var isEdit = false;
    var i, viewSummary, viewOptions;

    for(i=0;i<arMultis.length;i++) {
        viewSummary = document.getElementById("summary_" + arMultis[i]);
        viewOptions = document.getElementById("options_" + arMultis[i]);
        // In edit default to all collapsed
        // In new mode, default to editing first element
        // If any checkbox is selected, dont reset edit flag
        if(!isEdit) {
            isEdit = populateSummary(viewSummary, viewOptions, arMultis[i]);
        } else {
            populateSummary(viewSummary, viewOptions, arMultis[i]);
        }
        if(isEdit) inSequence = false;
        showSummary(viewSummary, viewOptions);
    }
    // If it's an edit
//    if(!isEdit && inSequence) {
//        var firstField = arMultis[0];
//        // Show first option panel
//        showOptions(
//            document.getElementById("summary_" + firstField),
//            document.getElementById("options_" + firstField)
//        )
//    }
}

function populateSummary(sum,opt,container_name) {
//    var editString = "<input type='button' value='Edit Selections' onclick='showOptions(this.parentNode,this.parentNode.previousSibling);'>";
    var editString = "<a href='#' onclick='showOptions(this.parentNode,this.parentNode.previousSibling);' class='paging'><img src='images/" + container_name + ".gif'/></a><br>";
    var curNode;
    var numChecked = 0;
    var sumStr = '';
    // Get all checkboxes inside current div
    var inputs = opt.getElementsByTagName("input");
    //alert(inputs.length + " inputs in " + opt.id);
    for(var i=0;i<inputs.length;i++) {
        curNode = inputs.item(i);
        //alert(i);
        if(curNode.nodeName == "INPUT" && curNode.getAttribute("type") == "checkbox") {
            if(curNode.checked) {
//                if(numChecked > 0) sumStr += "; ";
                numChecked++;
//                sumStr += curNode.getAttribute("value") + '';
                sumStr += "<img src='buttons/"+curNode.getAttribute("value")+"_true_sel.jpg' />";

                //alert("At " + i +     " of " + curNode.getAttribute("value"));
            }
        }
    }
    // Options selected? summarize. Otherwise? instruct.
    if(sumStr.length) {
//        sum.innerHTML = sumStr + ".<br/>" + editString;
        sum.innerHTML = editString + sumStr + "</br>";
    } else {
        // No current selectiongs, provide help msg
//        sum.innerHTML = "Click To Select" + ".<br/>" + editString;
        sum.innerHTML = editString;
    }
    // Pass back positive if items are already checked
    return numChecked;
}

function showSummary(sum, opt) {
    sum.style.display = 'inline';
    opt.style.display = 'none';
    isOpen = -1;

}

function showOptions(sum, opt) {
    sum.style.display = 'none';
    opt.style.display = 'inline';
    if(isOpen > -1 && autoCollapse) {
            showSummary(
            document.getElementById("summary_" + arMultis[isOpen]),
            document.getElementById("options_" + arMultis[isOpen])
        )
    }
    isOpen = getOffset(sum.getAttribute("id"));
}

function getOffset(label) {
    for(var i=0;i<arMultis.length;i++) {
        if(label.indexOf(arMultis[i]) > -1) return i;
    }

}

function saveOptions(optNode) {
    var sumNode = document.getElementById("summary_" +optNode.getAttribute("id").split("_")[1]);
    var Nodedesc = optNode.getAttribute("id").split("_")[1];
    populateSummary(sumNode,optNode, Nodedesc);
    showSummary(sumNode,optNode);
    if(inSequence) {
        // check to see if the user is editing sequentialy
        if(optNode.id.indexOf(arMultis[sequenceNum]) > -1) {
            // They're editing in sequence
            sequenceNum++;
            // Is there a next element to show?
            if(sequenceNum < arMultis.length) {
                var nextField = arMultis[sequenceNum];
                showOptions(
                    document.getElementById("summary_" + nextField),
                    document.getElementById("options_" + nextField)
                )
            }
        } else {
            inSequence = false;
        }
    }

}