
var hcardLoader = $("<div class='loading'/>");
var menuLoader = $("<div class='loading'/>");
var contentLoader = $("<div class='loading'/>");
var pp = '/personprofil/';

$(document).ready(function() {
    // Central AJAX error handling
    $("#error-dialog").ajaxError(function(event, xhr, settings, exception){
        $(this).append("<div>Error requesting page part: " + settings.url + "</div>").append(xhr.responseText);
        $(this).dialog({modal: true, close: function(event, ui) { $(this).html(""); }, width: 500});
    });

    //$("#visitkort").html(hcardLoader);
    $("#submenucontainer .submenucontent").html(menuLoader);
    $("#content").html(contentLoader);

    //$.ajax({url: hcardUrl,
    //    success: function(data) { $("#visitkort").html(data); },
    //    error: function(data) { $("#visitkort").html("Failed to load"); }
    //});

    $.ajax({url: menuUrl,
        success: function(data) { $("#submenucontainer .submenucontent").html(data); },
        error: function(data) { $("#submenucontainer .submenucontent").html("Failed to load"); }
    });

    var profileUrl;
    if(window.location.hash.length < 1 && window.location.href.indexOf(pp) > -1) {
        profileUrl = "/" + pp + getProfileContentUrl(window.location.hash);
    } else {
        profileUrl = getProfileContentUrl(window.location.hash);
    }
    $.ajax({url: profileUrl,
        success: function(data) { $("#content").html(data); },
        error: function(data) { $("#content").html("Failed to load"); }
    });
});

function showProfileContent(anchor) {
    $("#content").html(contentLoader);
    var url = getProfileContentUrl($(anchor).attr("href"));
    $.ajax({url: url,
        success: function(data) { $("#content").html(data); },
        error: function(data) { $("#content").html("Failed to load"); }
    });
}

function getProfileContentUrl(url) {
    url = url.substring(url.indexOf('#'));

    if(url.length == 0 && myPageActive) {
        window.location.hash = "#/minside";
        url = "/minside";
    }

    if(url.indexOf(pp) > -1) {
        url = url.substring(0, url.indexOf(pp) + pp.length) + '/ajax' + url.substring(url.indexOf(pp) + pp.length);
        return url.replace("#", "") + "/" + personId;
    } else {
        return "/ajax" + url.replace("#", "") + "/" + personId;
    }
}


