/*
    File:     $HeadURL: file:///D:/Development/subversion/websites/critchleys.co.uk/_misc/common.js $
    Revision: $Rev: 136 $
    Date:     $Date: 2009-09-25 15:18:19 +0100 (Fri, 25 Sep 2009) $
    Author:   $Author: Business $
    
    Copyright © 2009, Datrim Web Design Ltd. All rights reserved.
*/

/*
 Minimized code which breaks the email fraud popup 
 
function addLoadEvent(a){var b=window.onload;if(typeof window.onload!="function"){window.onload=a}else{window.onload=function(){if(b){b()}a()}}}addLoadEvent(function(){var b=document.getElementsByTagName("a");for(var a=0;a<b.length;a++){if(b[a].getAttribute("href")){if(b[a].getAttribute("rel")=="external"){b[a].target="_blank";if(pageTracker){if((b[a].hostname!=location.hostname)||!/(?:\/|.html)$/.test(b[a].pathname)){b[a].onclick=function(){var c=/^[\w]+:\/\/([\w-\.]+(\/[^#]*))#?.*$/;if(this.hostname!=location.hostname){pageTracker._trackPageview("/outbound/"+c.exec(this.href)[1])}else{pageTracker._trackPageview(c.exec(this.href)[2])}}}}}}}});function windowGenerator(a,c,b){return function(i){var g=[],h,d,j;var f=this.target||"_blank";b=b||this.href;if(a!==null){a=(a===0)?screen.availWidth:Math.min(a,screen.availWidth);d=(screen.availWidth-a)/2;g.push("left="+d+",screenX="+d);g.push("width="+a)}if(c!==null){c=(c===0)?screen.availHeight:Math.min(c,screen.availHeight);j=(screen.availHeight-c)/2;g.push("top="+j+",screenY="+j);g.push("height="+c)}g.push("scrollbars=1");g.push("resizable");window.open(b,f,g);this.blur();if(i&&i.preventDefault){i.preventDefault()}else{window.event.returnValue=false}return false}}addLoadEvent(function(){var c,b,a;if(document.getElementById){c=/^.+\/people\/[^\/]+$/;b=document.getElementById("copy-box").getElementsByTagName("a");for(a=0;a<b.length;a++){if(b[a].href){if(location.hostname==b[a].hostname&&c.test(b[a].pathname)){b[a].onclick=windowGenerator(560,380,b[a].href+"?popup=1")}}}}});addLoadEvent(function(){var g,h,f,e,b,a;if(document.getElementById){g=/\bpopup-(\d+)x(\d+)\b/;h=/[\w:\/\.-]+/;f=/\?.+/;e=document.getElementById("copy-box").getElementsByTagName("a");for(b=0;b<e.length;b++){if(e[b].href&&e[b].className){if((a=e[b].className.match(g))!==null){var d=e[b].href.match(h)+"/popup";var c=e[b].href.match(f);if(c!==null){d+=c}e[b].onclick=windowGenerator(a[1],a[2],d)}}}}});addLoadEvent(function(){var g,h,f,e,b,a;if(document.getElementById){g=/\bpopup-(\d+)x(\d+)\b/;h=/[\w:\/\.-]+/;f=/\?.+/;e=document.getElementById("message-box").getElementsByTagName("a");for(b=0;b<e.length;b++){if(e[b].href&&e[b].className){if((a=e[b].className.match(g))!==null){var d=e[b].href.match(h)+"/popup";var c=e[b].href.match(f);if(c!==null){d+=c}e[b].onclick=windowGenerator(a[1],a[2],d)}}}}});addLoadEvent(function(){var e,f,d,c,b;if(document.getElementById){e=/\bimg-(\d+)x(\d+)\b/;f=/([^?]*\/)(.*)$/;d=document.getElementById("copy-box").getElementsByTagName("a");for(c=0;c<d.length;c++){if(d[c].href&&d[c].className){if((b=d[c].className.match(e))!==null){uri=d[c].href.match(f);var a=this.location.href;d[c].onclick=windowGenerator(b[1],b[2],a+"/photos?i="+escape(uri[2]))}}}}});
* 
*/

function addLoadEvent(func) {
    var oldonload = window.onload;
    
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        };
    }
}


addLoadEvent(function() {
    var elms = document.getElementsByTagName("a");

    for (var i = 0; i < elms.length; i++) {
/*
        if (elms[i].getAttribute("href") && elms[i].getAttribute("rel") == "external") {
            elms[i].target = "_blank";
        }
*/
        if (elms[i].getAttribute("href")) {
            if (elms[i].getAttribute("rel") == "external") {
                elms[i].target = "_blank";

                if (pageTracker) {
                    if ((elms[i].hostname != location.hostname) || !/(?:\/|.html)$/.test(elms[i].pathname)) {
                        elms[i].onclick = function() {
                            var re = /^[\w]+:\/\/([\w-\.]+(\/[^#]*))#?.*$/;
                            if (this.hostname != location.hostname) {
                                pageTracker._trackPageview("/outbound/" + re.exec(this.href)[1]);
//alert("Tracking: [" + ("/outbound/" + re.exec(this.href)[1]) + "]");
                            }
                            else {
                                pageTracker._trackPageview(re.exec(this.href)[2]);
//alert("Tracking: [" + (re.exec(this.href)[2]) + "]");
                            }
                        };
                    }
                }
            }
        }
    }
});

function windowGenerator(w, h, url)
{
    /*
        Open a popup window.

        w = width

        h = height

            if null, then don't specify any position or size.
            if 0, then use screen width/height
            otherwise, use specified dimensions.

        url = popup url
              if null, uses @href

    */

    return function(e) {
        var features = [], win, x, y;
        var name = this.target || "_blank";
        url = url || this.href;

        if (w !== null) {
            w = (w === 0) ? screen.availWidth : Math.min(w, screen.availWidth);
            x = (screen.availWidth - w) / 2;

            features.push("left=" + x + ",screenX=" + x);
            features.push("width=" + w);
        }

        if (h !== null) {
            h = (h === 0) ? screen.availHeight : Math.min(h, screen.availHeight);
            y = (screen.availHeight - h) / 2;

            features.push("top=" + y + ",screenY=" + y);
            features.push("height=" + h);
        }

        features.push("scrollbars=1");
        features.push("resizable");

//features.push("location");

        window.open(url, name, features);
        this.blur();

        // Prevent default event handler...
        if (e && e.preventDefault) {
            e.preventDefault();                            // W3C method.
        }
        else {
            window.event.returnValue = false;            // MSIE method.
        }

        return false;
    };
}

addLoadEvent(function() {
    // Add onclick popup window to local links containing "/people/"
    var regEx, elem, ii;

    if (document.getElementById) {
        regEx = /^.+\/people\/[^\/]+$/;

        elem = document.getElementById("copy-box").getElementsByTagName("a");

        for (ii = 0; ii < elem.length; ii++) {
            if (elem[ii].href) {
                if (location.hostname == elem[ii].hostname && regEx.test(elem[ii].pathname)) {
                    elem[ii].onclick = windowGenerator(560, 380, elem[ii].href + "?popup=1"); // /popup");
                }
            }
        }
    }
});

addLoadEvent(function() {
    // Add onclick popup window to links with a class name popup-WIDTHxHEIGHT, e.g. "popup-560x380"

    var reCSS, reURI, rePar, elem, i, size;

    if (document.getElementById) {
        reCSS = /\bpopup-(\d+)x(\d+)\b/;
//        reURI = /([^?]*\/)(.*)$/;
//        rePar = /\?\w+=\w+/;
        reURI = /[\w:\/\.-]+/;
        rePar = /\?.+/;

        elem = document.getElementById("copy-box").getElementsByTagName("a");

        for (i=0; i<elem.length; i++) {
            if (elem[i].href && elem[i].className) {
                if ((size = elem[i].className.match(reCSS)) !== null) {
                    
                    var uri = elem[i].href.match(reURI) + '/popup';
                    var par = elem[i].href.match(rePar);
                    
                    if (par !== null) {
                        uri += par;
                    }
                    elem[i].onclick = windowGenerator(size[1], size[2], uri);
                    
                    //uri = elem[i].href.match(rePar) ? '&popup' : '?popup';
                    //elem[i].onclick = windowGenerator(size[1], size[2], elem[i].href + "?popup=1");
                }
            }
        }
    }
});

/* Temporary function for the Email scam message */
addLoadEvent(function() {
    // Add onclick popup window to links with a class name popup-WIDTHxHEIGHT, e.g. "popup-560x380"

    var reCSS, reURI, rePar, elem, i, size;

    if (document.getElementById) {
        reCSS = /\bpopup-(\d+)x(\d+)\b/;
//        reURI = /([^?]*\/)(.*)$/;
//        rePar = /\?\w+=\w+/;
        reURI = /[\w:\/\.-]+/;
        rePar = /\?.+/;

        elem = document.getElementById("message-box").getElementsByTagName("a");

        for (i=0; i<elem.length; i++) {
            if (elem[i].href && elem[i].className) {
                if ((size = elem[i].className.match(reCSS)) !== null) {
                    
                    var uri = elem[i].href.match(reURI) + '/popup';
                    var par = elem[i].href.match(rePar);
                    
                    if (par !== null) {
                        uri += par;
                    }
                    elem[i].onclick = windowGenerator(size[1], size[2], uri);
                }
            }
        }
    }
});


addLoadEvent(function() {
    // Add onclick popup window to links with a class name img-WIDTHxHEIGHT, e.g. "img-560x380"

    var reCSS, reURI, elem, i, size;

    if (document.getElementById) {

        reCSS = /\bimg-(\d+)x(\d+)\b/;
        reURI = /([^?]*\/)(.*)$/;

        elem = document.getElementById("copy-box").getElementsByTagName("a");

        for (i=0; i<elem.length; i++) {
            if (elem[i].href && elem[i].className) {
                if ((size = elem[i].className.match(reCSS)) !== null) {
                    uri = elem[i].href.match(reURI);
                    var href = this.location.href;
                    elem[i].onclick = windowGenerator(size[1], size[2], href + '/photos?i=' + escape(uri[2]));
                }
            }
        }
    }
});

