// visibilty toggle & store
var IE=(navigator.userAgent.indexOf("MSIE")>0);
var OPERA=(navigator.userAgent.indexOf("MSIE")>0);
var visibilitySetting={};
var setVisibilityCookie = function(id, state) {
    if (document.cookie!=null) {
        var s=document.cookie.indexOf("sitebuilder_visibility")
        var states = "";
        if (s>-1) {
            s+=23;
            var e=document.cookie.indexOf(";",s)-s;
            if (e<1) e=document.cookie.length-s;
            if (e>-1) {
                states = document.cookie.substr(s,e);
                if ((s=states.indexOf(":"+id+":"))>-1) {
                    method=states.substr(s+id.length+2,1);
                    states=states.substr(0,s)+states.substr(s+id.length+4);
                }
            }
        }
        document.cookie = "sitebuilder_visibility="+states+":"+id+":p"+((state) ? "t" : "f")+"; path=/";
    }
}
// Highlight Fade Appear Puff BlindUp BlindDown SwitchOff DropOut Shake SlideDown SlideUp Squish Grow Shrink Pulsate Fold
var visibilityStyles = {fade:{show:"Appear",hide:"Fade"}};
var addVisibilityStyle = function (style, show, hide) {
    if ((style==null) ||(show==null) ||(show==null)) return false;
    visibilityStyles[style]={show:show,hide:hide};
    return true;
}
var setVisibility = function(id, state, store, style) {
    if (document.cookie!=null) {
        var s=document.cookie.indexOf("sitebuilder_visibility")
        var states = "";
        if (s>-1) {
            s+=23;
            var e=document.cookie.indexOf(";",s)-s;
            if (e<1) e=document.cookie.length-s;
            if (e>-1) {
                states = document.cookie.substr(s,e);
                if ((s=states.indexOf(":"+id+":"))>-1) {
                    method=states.substr(s+id.length+2,1);
                    states=states.substr(0,s)+states.substr(s+id.length+4);
                }
            }
        }
    }
    var chosenStyle=visibilityStyles[style];
    var scAvail=(!OPERA) && (chosenStyle!=null) && (typeof(Effect) != "undefined");
    if (scAvail) {
        scAvail=(typeof(Effect[chosenStyle.show])!="undefined") && (typeof(Effect[chosenStyle.hide])!="undefined");
    }
    var method;
    if (!state) {
        // hiding
        method = (scAvail) ? "s" : "p";// we can use either since we are hiding
    } else {
        // revealing
        method = ((scAvail) && (method!=null)) ? method : "p";// if scr is avail then use what we used last time otherwise use plain mode
    }
    visibilitySetting[id]=state;
    if (method=="s") {
        // if scriptaculous is available then use it
        method="s";
        if (!state) {
            Effect[chosenStyle.hide](id);
        } else {
            Effect[chosenStyle.show](id);
        }
    } else {
        // simply toggle the visibility of the children div now that we've already fetched it
        method="p";
        var panelDiv = document.getElementById(id);
        if (panelDiv!=null) {
          if (!state) {
              if (IE) {
                  panelDiv.style.display = 'none';
              } else {
                  panelDiv.style.visibility = "collapse";
                  panelDiv.style.display = 'none';
              }
          } else {
              if (IE) {
                  panelDiv.style.display = 'block';
              } else {
                  panelDiv.style.visibility = "visible";
                  panelDiv.style.display = null;
              }
          }
        }
    }
    if (store) {
        document.cookie = "sitebuilder_visibility="+states+":"+id+":"+method+((state) ? "t" : "f")+"; path=/";
    }
    return state;
}
var toggleVisibility = function (id,store,style) {
    visibilitySetting[id] = visibilitySetting[id]==null || visibilitySetting[id]; 
    return setVisibility(id,!visibilitySetting[id],store,style);
}
var restoreVisibility = function (id,def,store,style) {
    if (document.cookie!=null) {
        var s=document.cookie.indexOf("sitebuilder_visibility")
        if (s>-1) {
            s+=23;
            var e=document.cookie.indexOf(";",s)-s;
            if (e<1) e=document.cookie.length-s;
            if (e>-1) {
                var states = document.cookie.substr(s,e);
                if ((s=states.indexOf(":"+id+":"))>-1) {
                    return setVisibility(id,(states.substr(s+id.length+3,1)=="t"),store,style);
                }
            }
        }
    }
    return setVisibility(id,def,store,style);
}
var toggleSidebar = function (id) {
    toggleVisibility(id,true,"fade");
    var arrow=document.getElementById(id+"arrow");
    if (arrow!=null) {
        if (arrow.src.substr(-5,1)=="1") {
            arrow.src=contextPath+"/download/resources/com.adaptavist.confluence.themes.sitebuilder:sitebuilder/icons/arrow-black-0.gif";
        } else {
            arrow.src=contextPath+"/download/resources/com.adaptavist.confluence.themes.sitebuilder:sitebuilder/icons/arrow-black-1.gif";
        }
    }
}
var restoreSidebar = function (id,collapse) {
    var state=restoreVisibility(id,!collapse,true,"fade");
    var arrow=document.getElementById(id+"arrow");
    if (arrow!=null) {
        if (id=="leftSidebar") state=!state;
        if (state) {
            arrow.src=contextPath+"/download/resources/com.adaptavist.confluence.themes.sitebuilder:sitebuilder/icons/arrow-black-1.gif";
        } else {
            arrow.src=contextPath+"/download/resources/com.adaptavist.confluence.themes.sitebuilder:sitebuilder/icons/arrow-black-0.gif";
        }
    }
}
var unloadListeners = [];
var onPageUnload = function () {
    var i=unloadListeners.length;
    while (-1<--i) if (typeof(unloadListeners[i])=="function") {
        if (!(unloadListeners[i]())) return false;
    } else {
        if (!eval(unloadListeners[i]())) return false;
    }
    return true;
}
var addUnloadListener = function (listener) {
    if ((typeof(listener)=="function") || (typeof(listener)=="string")) return unloadListeners.push(listener)>0;
    return false;
}
var onloadListeners = [];
var pageLoaded=false;
var onPageLoad = function () {
    pageLoaded=true;
    var i=onloadListeners.length;
    var rv;
    while (-1<--i) if (typeof(onloadListeners[i])=="function") {
        rv = onloadListeners[i]();
        if (rv == false) return false;
    } else {
        rv = eval(onloadListeners[i]);
        if (rv == false) return false;
    }
    return true;
}
var addOnloadListener = function (listener) {
    if ((typeof(listener)=="function") || (typeof(listener)=="string")) return onloadListeners.push(listener)>0;
    return false;
}
function showBreadcrumbsEllipsis() {
    document.getElementById('breadcrumbsEllipsis').style.display = 'none';
    document.getElementById('breadcrumbsExpansion').style.display = 'inline';
}
var copyTasks = [];
var defermentTasks={};
var checkCopyTasks = (function () {
    var intervalID;
    var i;
    var source;
    var target;
    var task;
    var lid;
    return function () {
        clearInterval(intervalID);
        i=copyTasks.length;
        while (-1<--i) {
            task=copyTasks[i];
            source=document.getElementById(task.source);
            target=document.getElementById(task.target);
            if ((source!=null) && (target!=null)) {
                target.innerHTML = source.innerHTML;
                source.innerHTML = "";
                for (lid in task.listeners) task.listeners[lid]();
                copyTasks.splice(i,1);
                task.complete=true;
            }
        }
        if (copyTasks.length) {
            intervalID = setInterval("checkCopyTasks();",333);
        }
    }
})();
function addCopyTask(task) {
    if (defermentTasks[task.target]==null) {
        task.complete=false;
        if (task.listeners==null) task.listeners={};
        defermentTasks[task.target]=task;
        copyTasks.push(task);
        if (pageLoaded || !IE) checkCopyTasks();
        return true;
    }
    return false;
}
if (IE) addOnloadListener(checkCopyTasks);
function addDefermentListener(target,listener,listenerID) {
    if (defermentTasks[target]!=null) {
        if (defermentTasks[target].listeners[listenerID]!=null) return
        if (!defermentTasks[target].complete) {
            defermentTasks[target].listeners[listenerID]=listener;
            return;
        }
    }
    if (navigator.userAgent.indexOf("MSIE")<0) {
        listener();
    } else {
        window.attachEvent("onload", listener);
    }
}
