/* Copyright 2004-2005 Derek Poon.  All rights reserved. */

function setTarget(id, target) {
    if (document.getElementById) {
        var el = document.getElementById(id);
        if (el) {
            el.target = target;
            return true;
        }
    }
    return false;
}

function useLinkRel() {
    if (document.getElementsByTagName) {
        var anchors = document.getElementsByTagName('a');
        for (var i = 0; i < anchors.length; i++) {
            var anchor = anchors[i];
            var rel = anchor.getAttribute('rel');
            if (!rel) {
                continue;
            }

            var colon = 1 + rel.indexOf(':');
            var type = colon ? rel.substring(0, colon - 1)
                             : rel;

            if (anchor.getAttribute('href') &&
                  (type == 'external' ||
                   type == 'child' ||
                   type == 'offspring' ||
                   type == 'blank')) {

                if (type == 'child') {
                    // Open a child window slightly smaller than its parent
                    anchor.onclick = function() {
                        var colon = 1 + this.rel.indexOf(':');
                        var trgt = colon ? this.rel.substring(colon) : '_blank';
                        var x = window.innerWidth ? window.innerWidth :
                                document.body.clientWidth ? document.body.clientWidth :
                                500;
                        var y = window.innerHeight ? window.innerHeight :
                                document.body.clientHeight ? document.body.clientHeight :
                                400;
                        x = 0.8 * x; y = 0.8 * y;
                        var hash = this.href.indexOf('#');
                        var noNav = (this.href.indexOf('?') < 0) ?
                            '?navbar=no' :
                            '&navbar=no';
                        var url = (hash < 0) ?
                            this.href + noNav :
                            this.href.substring(0, hash) + noNav + this.href.substring(hash);
                        window.open(url,
                                    trgt,
                                    'resizable=yes,scrollbars=yes,width=' + x
                                        + ',height=' + y);
                        return false;
                    };
                } else if (type == 'offspring') {
                    // Open a much smaller child window out of the way of its parent
                    anchor.onclick = function() {
                        var colon = 1 + this.rel.indexOf(':');
                        var trgt = colon ? this.rel.substring(colon) : '_blank';
                        var hash = this.href.indexOf('#');
                        var noNav = (this.href.indexOf('?') < 0) ?
                            '?navbar=no' :
                            '&navbar=no';
                        var url = (hash < 0) ?
                            this.href + noNav :
                            this.href.substring(0, hash) + noNav + this.href.substring(hash);
                        window.open(url,
                                    trgt,
                                    'resizable=yes,scrollbars=yes,'
                                        + 'width=150,height=150,'
                                        + 'screenX=0,left=0,screenY=0,top=0');
                        return false;
                    };
                } else {
                    var colon = 1 + anchor.rel.indexOf(':');
                    var trgt = colon ? anchor.rel.substring(colon) : '_blank';
                    anchor.target = trgt;
                }

            }
        }
    }
}

window.onload = useLinkRel;
