﻿
function addLinkPopups() {
    var links = document.getElementsByTagName('a');
    var hostname = location.hostname;

    for (var i = 0; i != links.length; i++) {
        var url = links[i].href.toLowerCase();
        if (url.indexOf('file.axd?') >= 0) {
            addResourcePopup(links[i]);
        } else if (url.indexOf('http://') == 0
         && url.indexOf('http://' + hostname) < 0
		 && url.indexOf('correspond.com.au') < 0
         && url.indexOf('www.thewebcompany.com.au') < 0) {
            addExternalPopup(links[i]);
        }
    }
}

function addResourcePopup(link) {
    var found = false;
    
    for (var i=0; i < LinkPopupIDList.length; i++)
    {
        if (link.href.indexOf(LinkPopupIDList[i]) > 0)
            found = true;
    }

    if (!found)
        link.onclick = function() { showPopup('popupResource', this); return false; };
}

function addExternalPopup(link) {
    link.onclick = function() { showPopup('popupExternal', this); return false; };
}

function showPopup(id, link) {
    var shroud = document.getElementById('shroud');

    if (shroud) {
        shroud.style.display = 'block';
    }
    
    var dlg = document.getElementById(id);

    if (dlg) {
        dlg.className = 'displayDialog';
        dlg.style.left = ((document.body.offsetWidth - dlg.offsetWidth) / 2) + 'px';
        dlg.style.top = ((getWindowHeight() - dlg.offsetHeight) / 2) + 'px';
        var buttons = dlg.getElementsByTagName('input');

        for (var i = 0; i != buttons.length; i++) {
            if (buttons[i].id.indexOf('Yes') > 0) {
                if (link.target == "_blank") {
                    buttons[i].onclick = function() { hidePopup(id); window.open(link.href); };
                }
                else
                {
                    buttons[i].onclick = function() { hidePopup(id); location.href = link.href; };
                }
                break;
            }
        }
    }

    window.scrollTo(0, 0);
}

function hidePopup(id) {
    var dlg = document.getElementById(id);

    if (dlg) {
        dlg.className = '';
    }

    var shroud = document.getElementById('shroud');

    if (shroud) {
        shroud.style.display = 'none';
    }
}

function getWindowHeight() {
    var windowHeight = 0;
    if (typeof (window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    }
    else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        }
        else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}


$(document).ready(function() { addLinkPopups(); });