/*
    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.
*/

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]));
				}
			}
		}
	}
});
