﻿$(document).ready(function() {
    $("#quote").click(function() { getQuote(); });
    $("#contentlegend").html("<h1>".concat($("#contentlegend").text(), "</h1>"));
    $("#accordionmenu").accordion({ collapsible: true, autoHeight: false, active: false });

    $(".ajaxmenu, .ajaxlink").click(function(e) {
        loadContent($(this).attr("id"), $(this).html());
        e.preventDefault();
        return false;
    });
    if (self.document.location.hash != "") {
        var hash = unescape(self.document.location.hash);
        if (hash[1] == ".")
            loadContent(hash.substring(2), "");
        else
            loadContent(hash.substring(1), "");
    }
    $("#viewsource").viewSource();
});

function getQuote() {
    $.get("Home/GetQuote", function(data) {
        var q = $("#quote");
        $(q).fadeOut(100, function() {
            $(q).html(data);
            $(q).fadeIn(100);
        });
    });
}

function fixHeader() {
    var content = $("#content");
    $("#contentlegend").html($(content).find("h1,h2,h3,h4,h5").first());
    document.title = $("#contentlegend").text() + " — DukeLupus' scripts and programs";
}

function loadContent(itemId, itemText) {
    getQuote();
    if (self.document.location.hash == "#.".concat(itemId) && itemText != "")
        return;

    $("#contentlegend").html("<h1>".concat(itemText, "</h1>"));

    self.document.location.hash = ".".concat(itemId);

    var content = $("#content");
    $(content).html("<div id='ajaximageholder'><img src='/Content/ajaxloader.gif' alt='loading' /></div>");

    $.ajax({ url: "Home/GetContent", data: { pageId: itemId }, dataType: "html", type: "POST",
        success: function(data) {
            $(content).fadeOut(20, function() {
                $(content).html(data);
                $("#contentlegend").html($(content).find("h1,h2,h3,h4,h5").first());
                document.title = $("#contentlegend").text() + " — DukeLupus' scripts and programs";          
                $(content).fadeIn(200);
            });

        },
        complete: function(XMLHttpRequest, textStatus) {
            if (textStatus != "success")
                $(content).html("<div class='error'>Error: failed to load the page</div>");
        }

    });
}

    
    
