function loadPage(page){
    //$("content").fade({duration:0.2});
    //window.location.hash = page.replace(/\//g, "#");
    if(page == "/index.html" || page.match(/\/pg[0-9]+\.[a-z]{2,5}/) || page == "/" || page == "" ){
        new Ajax.Request(page + "?contentonly=1", {
            method: "get",
            onSuccess: function(transport){
                var c = $("content");var x = $("xmlparser").contentDocument;
                x.body.innerHTML = transport.responseText;
                c.innerHTML = x.getElementById("content").innerHTML;
                x.body.innerHTML = "";
                c.appear({duration:0.5});
                fixAnchors(c);
            }
        });
    }else{
        new Ajax.Request(page, {
            method: "get",
            onSuccess: function(transport){
                var c = $("content");
                c.innerHTML = transport.responseText;
                c.appear({duration:0.5});
                fixAnchors(c);
            }
        });
    }
}
function onLoad(){
    if(window.parent != window)
        return;
    page = window.location.hash.replace(/#/g, "/");
    if(page && page != "#")
        loadPage(page);
    fixAnchors();
}
function fixAnchors(el){
    el = el || document;
    as = el.getElementsByTagName("a");
    for(var i = 0, a; a = as[i++];){
        if(a.fixed)
            continue;
        a.fixed = true;
        try{
            var href = a.getAttribute("href").replace(".xhtml", ".html");
        }catch (e){
            continue;
        }
        //if blank, has onclick event, or is to an external site (.com/)
        if(!href || href == "#" || a.getAttribute("onclick") || href.match(/[.][a-z]{2,5}\//i))
            continue;
        a.setAttribute("href", href.replace(/\//g, "#"));
        a.setAttribute("onclick", "loadPage('" + href + "');");
        
    }
}
function switchFormat(newFormat){
    var page = window.location.hash.replace(/#/g, "/");
    if(page && page != "#"){
        window.location.href = page.replace(".html", "." + newFormat);
    }else{
        window.location.href = "/index.xhtml";
    }
}
function setReply(tag, key, margin){
    var newCommentDiv = $("newcomment");
    //not entirely nessassary in most browsers
    newCommentDiv.parentNode.removeChild(newCommentDiv);
    var inReplyToDiv = tag.parentNode.parentNode;
    inReplyToDiv.parentNode.insertBefore(newCommentDiv, inReplyToDiv.nextSibling);
    //set margin
    newCommentDiv.style.marginLeft = (margin + 20).toString() + "px";
    //set key
    $("inreplyto").value = key;
    newCommentDiv.appear({duration:0.3});
}
function logout(url){
    window.location.href = url;
}
function login(url){
    window.location.href = url;
}
function preview(){
    new Ajax.Request("/new/comment/preview", {
        method: "post",
        parameters: {body: $("body").value },
        onSuccess: function(t){
            $("commenttext").innerHTML = t.responseText;
            $("previewcomment").appear();
        }});
}
