﻿
$(document).ready(function () {
    var basePath = window.location.protocol + '//' + window.location.host;
    SetAnchorTargetAttributes(basePath);
});

function SetAnchorTargetAttributes(basePath) {

    $('a').each(function () {
        var href = $(this).attr("href");

        if (IsURLWithProtocall(href)) {

            if (IsLocalPath(basePath, href)) {
                $(this).attr({ target: "_self", title: "Read More" });
            }
            else {
                $(this).attr({ target: "_blank", title: "Read More" });
            }
        }
    });
}

function IsLocalPath(basePath, href) {
    var path = RemoveTrailingSlash(basePath);  //Should need length check here, cant see a uri less than 3 chars....

    //If href is the first instance of itself within basePath we are using a local path
    return (href.toLowerCase().indexOf(path) == 0);
}

function RemoveTrailingSlash(basePath) {
    return basePath.substring(0, basePath.length - 1).toLowerCase();
}

function IsURLWithProtocall(basePath) {
    return ((basePath.toLowerCase().indexOf("http") == 0) || (basePath.toLowerCase().indexOf("https") == 0));
}

