function jumpToPath(context, path) {
    var popup = window.open(context + path,'','width=800,height=650,left=430,top=23');
    popup.focus();
    return popup;
}

function view(url) {
    var prefs = 'width=800,height=650,left=430,top=23';
    try {
        var winTop = parent.document.getElementById("winTop").value;
        var left = parent.document.getElementById("left").value;
        var width = parent.document.getElementById("width").value;
        var height = parent.document.getElementById("height").value;
        if (!(winTop=="0" && left=="0" && width=="0" && height=="0")) {
            //  Load window's size/position preferences.
            var prefs = "top=" + winTop +
                    ",left=" + left +
                    ",width=" + width +
                    ",height=" + height;
        }
    } catch(e) {}
    prefs += ",resizable=1";
    var popup = window.open(url,'',prefs);
    popup.focus();
    return popup;
}

function toggleVisibility(id) {
    var element;
    var visibility;
    if (document.getElementById) { // DOM3 = IE5, NS6
        element = document.getElementById(id);
        visibility = element.style.visibility;
        ;
        if (visibility == 'hidden') {
            element.style.visibility = 'visible';
        }
        else {
            element.style.visibility = 'hidden';
        }
    }
}

function overTreeItem(id) {
    var element = document.getElementById(id);
    element.style.backgroundColor = 'rgb(225, 210, 174)';
}

function outTreeItem(id) {
    var element = document.getElementById(id);
    element.style.backgroundColor = 'transparent';
}

//Open pending updates window
function openPendingUpdates(title, section, appendix, lawsInEffectDate, lawsInEffectException) {
    // Belongs this title to an Appendix?
    if (arguments.length==5) {
        if (appendix=="appendix")
            title+="a";
        openUpdates(title, section, lawsInEffectDate, lawsInEffectException);
    } else {
        // When 3 arguments the real value of 'lawsInEffectData' comes in 'appendix'
        openUpdates(title, section, appendix, lawsInEffectDate);
    }
}

function openUpdates(title, section, lawsInEffectDate, lawsInEffectException) {
    var w = window.open('updates.xhtml?title=' + title + '&section=' + section + "&lawsInEffectDate=" + lawsInEffectDate + "&lawsInEffectException=" + lawsInEffectException, '','width=480,height=280,left=430,top=300,scrollbars=yes');
    w.focus();
    return w;
}

// Set the focus to the last collapsed title.
function setFocus() {

    var calledFrom = top.document.referrer;
    if (calledFrom.indexOf("treeFragment.xhtml")==-1) {
        return;
    }
    try {
        var edition = document.getElementById("edition");
        var currentPath = document.getElementById("currentPath");
        var oldPath = document.getElementById("oldPath");
        var submitBtn = document.getElementById("submitBtn");

        // When a title tree is expanded, store the id.
        if (currentPath.value!="") {
            if (oldPath.value == currentPath.value) return

            //Get just the title in the path, everything before the first '/'.
            var temp=currentPath.value.split("/");
            oldPath.value = temp[0];

            // Store the current Path or title tree using ajax code inside SubmitBtn.
            submitBtn.click();
            return;
        }

        // When a title tree is collapsed, retrieve the id from oldPath.
        if (oldPath.value!="") {
            // Now clear the title. Not required anymore.
            var a = document.getElementById("USC-" + oldPath.value + ":div");
            top.window.scrollTo(0, a.offsetTop);
            oldPath.value = "";
            submitBtn.click();
            return false;
        }
    } catch(e) {}
    return true;
}

/******************************************************************************
 *                         Browse.xhtml functions
 *****************************************************************************/

function resize() {
    var height = parseInt(document.getElementById('treeFrame').contentWindow.document.body.scrollHeight) + 10;
    document.getElementById('treeFrame').height = height +"px";
}

function setAnchorFocus() {
    try {
        if (document.getElementById('treeFrame').contentWindow.document.getElementById("default_anchor") == null) {
            setTimeout("setAnchorFocus()", 250);
        } else {
            var default_anchor = document.getElementById('treeFrame').contentWindow.document.getElementById("default_anchor");
            default_anchor.style.display="block";
            var top = default_anchor.offsetTop;
            default_anchor.style.display="none";
            window.scrollTo(0, top);
        }
    } catch(e) {}
    return true;
}

function changeVersion() {
    document.getElementById("viewVersion").style.display="none";
    document.getElementById("changeVersion").style.display="inline";
}

function setEdition(select) {
    document.getElementById("viewVersion").style.display="inline";
    document.getElementById("changeVersion").style.display="none";

    document.getElementById("currentLabel").innerHTML = select.options[select.selectedIndex].text;

    document.getElementById("edition").value = select.options[select.selectedIndex].value;
    document.getElementById("btnVersions").click();
}
