/* ************************************************************************************* */
/* manages relocation, history entries, doc outline updates, event hooks */
/* ************************************************************************************* */

function handlePageMouse()
{
	top.handleMouseEx(self, 1);
}

function handlePageKeys()
{
	top.handleKeysEx(self, 1);
}

var sTitle = "";
function handlePageMouseOver()
{
	if(top.getBrowserInfo().isIe()){
		sTitle = event.srcElement.title;
		setTimeout("window.status = sTitle;", 50);
	}
}

function handlePageLoaded(wnd, iSubLevel)
{
    if(isInFrameset(wnd)){
        return handlePageLoadedFrameset(wnd, iSubLevel);
    }else{
        return handlePageLoadedNonFrameset(wnd, iSubLevel);
    }
}

function handlePageLoadedFrameset(wnd, iSubLevel)
{
    // disable sidebar
    hideSidebar(wnd);
    // standard frameset
	if(top.name == "Ober"){
        // error handler for main frame
        top.registerErrorHandler(self);
        // event mapping
        wnd.document.onmousedown = handlePageMouse;
        wnd.document.onmouseup = handlePageMouse;
        wnd.document.onclick = handlePageMouse;
        wnd.document.onkeydown = handlePageKeys;
        wnd.document.onkeyup = handlePageKeys;
        /*wnd.document.onmouseover = handlePageMouseOver;*/
        // history, outliner
        var sOutliner = getOutliner();
        parent.addToHistory(wnd.document.title, wnd.location.href, sOutliner);
        parent.notifyPageLoaded(sOutliner);
        // page mucking
        if(isMsIe()){
            var fStartPage = isStartPage(wnd);
            var fPrefsPage = isPreferencesPage(wnd);
            // auto hyperlinking
            if(false == fStartPage && false == fPrefsPage){
            	if(top.getUseAutoLinks()){
            		top.createPageLinks(wnd);
            	}
            }
            // page collapsing
            if(false == fStartPage && false == fPrefsPage){
            	if(top.getUsePageCollapsing()){
            		top.collapsePage(wnd);
            	}
            }
            // page styling
            if(false == fStartPage && false == fPrefsPage){
            	top.applyPageStyles(wnd);
            }
        } // if IE
        // log info
        /*parent.addInfo("page loaded: " + wnd.document.title);*/
    }
    // any frameset
    if(top.name == "Ober"
    || top.name == "haupt"){
    	// colors (scrollbars)
    	top.refreshColor();
    }
    return true;
}

function handlePageLoadedNonFrameset(wnd, iSubLevel)
{
    var sidebar = findSidebar(wnd);
    if(sidebar != null){
        var sHref = wnd.location.href;
        if(sHref.indexOf("dotnet") >= 0
        || sHref.indexOf("webedit.net") >= 0){
            sidebar.style.borderLeftColor = "green";
        }else if(sHref.indexOf("200") >= 0
              || sHref.indexOf("journal") >= 0){
            sidebar.style.borderLeftColor = "orange";
        }else if(sHref.indexOf("vb") >= 0
              || sHref.indexOf("webedit") >= 0){
            sidebar.style.borderLeftColor = "maroon";
        }
    }
    return false;
}

function isMsIe()
{
    return (navigator && navigator.appName.indexOf("Microsoft") >= 0);
}

function isInFrameset(wnd)
{
    if(wnd != top
    &&(wnd.name == "notes" || wnd.name == "haupt")){
        return true;
    }
    return false;
}

function shouldRefer(wnd)
{
    if(wnd == top                                   /* page deeply linked    */
    || wnd.parent != null && top != wnd.parent      /* index in foe frameset */
    || wnd.name != "notes" && wnd.name != "haupt"){ /* page in foe frameset  */
        var s = wnd.location.search;
        return (s != "?s"); // true;
    }
    return false;
}

function referToRoot(wnd, iSubLevel)
{
	var sTop = "";
	for(var i = 0; i < iSubLevel; i++){
		sTop += "../";
	}
	sTop += "indexie.html";
	sTop += "?" + wnd.location.href;
	top.location.replace(sTop);
}

function getOutliner()
{
	return (getLocationBase() + "outliner.htm");
}

function isLocal() 
{
	return (top.location.href.indexOf("Internet") >= 0);
}

function getLocationBase()
{
	if(top.location.href.indexOf("peisker.de") != -1){
		return "http://peisker.de/";
	}
	else if(top.location.href.indexOf("www.peisker.de") != -1){
		return "http://www.peisker.de/";
	}
	else if(top.location.href.indexOf("peisker.net") != -1){
		return "http://peisker.net/";
	}
	else if(top.location.href.indexOf("www.peisker.net") != -1){
		return "http://www.peisker.net/";
	}
	else if(top.location.href.indexOf("www.gregor-peisker.de") != -1){
		return "http://www.gregor-peisker.de/";
	}
	else if(top.location.href.indexOf("localhost") != -1){
		return "http://localhost/gregor/";
	}
	else{
		return "file://D:/Internet/peisker/";
	}
}

function getRelativePath(sFullUrl)
{
	var iPde = sFullUrl.indexOf("peisker.de", 0);
	if(iPde > -1){
		return sFullUrl.substring(iPde + 11);
	}
	var iPnt = sFullUrl.indexOf("peisker.net", 0);
	if(iPnt > -1){
		return sFullUrl.substring(iPnt + 12);
	}
	var iLh = sFullUrl.indexOf("localhost/gregor", 0);
	if(iLh > -1){
		return sFullUrl.substring(iLh + 17);
	}
	var iPlc = sFullUrl.indexOf("peisker");
	if(iPlc > -1){
		return sFullUrl.substring(iPlc + 8);
	}
	return "";
}

function isStartPage(/* Window */ wnd)
{
    return (wnd.location.href.indexOf("note11") >= 0);
}

function isPreferencesPage(/* Window */ wnd)
{
    return (wnd.location.href.indexOf("optionen.htm") >= 0);
}

function hideSidebar(/* Window */ wnd)
{
    var sidebar = findSidebar(wnd);
    var content = findContent(wnd);
    if(sidebar != null && content != null){
        sidebar.style.display = "none";
        content.style.left = "8px";
        content.style.width = "100%";
    }
}

function findSidebar(wnd)
{
    return findPageElementById(wnd, "sidebar");
}

function findContent(wnd)
{
    return findPageElementById(wnd, "content");
}

function goToHeading(wnd, iHeading)
{
    var heading = findHeading(wnd, iHeading);
    if(heading != null){
        heading.scrollIntoView();
        wnd.scrollBy(-15, -2);
    }
}

function findHeading(wnd, iHeading)
{
    var content = findContent(wnd);
    if(content != null){
        function isHeading(elem)
        {
            return (elem.tagName == "H1" || elem.tagName == "H2"
                 || elem.tagName == "H3" || elem.tagName == "H4"
                 || elem.tagName == "H5" || elem.tagName == "H6");
        }
        var heading = findElementByIndex(content, iHeading, isHeading);
        return heading;
    }
    return null;
}

function findPageElementById(wnd, sId)
{
    if(isMsIe()){
        return wnd.document.all[sId];
    }else{
        var body = findBody(wnd);
        if(body != null){
            var elem = findElementById(body, sId);
            return elem;
        }
    }
    return null;
}

function findBody(wnd)
{
    var html = findElementByTagName(wnd.document, "HTML");
    if(html != null){
        var body = findElementByTagName(html, "BODY");
        return body;
    }
    return null;
}

function findElementByTagName(parentElement, sTagName)
{
    var kids = parentElement.childNodes;
    for(i = 0; i < kids.length; i++){
        var elem = kids[i];
        if(elem.tagName == sTagName){
            return elem;
        }
    }
    return null;
}

function findElementById(parentElement, sId)
{
    var kids = parentElement.childNodes;
    for(i = 0; i < kids.length; i++){
        var elem = kids[i];
        if(elem.id == sId){
            return elem;
        }
    }
    return null;
}

function findElementByIndex(parentElement, index, filter)
{
    var kids = parentElement.childNodes;
    var idx = 0;
    for(i = 0; i < kids.length; i++){
        var elem = kids[i];
        if(filter(elem)){
            if(idx == index){
                return elem;
            }
            idx++;
        }
    }
    return null;
}
