/*
 * links.js
 *
 * Activates special behaviour for specifically-named link elements.
 *
 * (c) Copyright 2006 Future Medium Pty Ltd - http://www.futuremedium.com.au/
 *
 * @author scampbell
 */


/* Default attributes */
var defaultAttribs = { directories: "yes", scrollbars: "yes", resizable: "yes", location: "yes", toolbar: "yes", menubar: "yes", status: "yes" }

/* Custom attributes */
var popups = {
	external: {
		// use all defaults
	},
	gallery: {
		id: "gallery",
		attribs: { width: "800", height: "575", directories: "no", scrollbars: "no", resizable: "yes", location: "no", toolbar: "no", menubar: "no", status: "no" }
	},
	print: {
		id: "print",
		attribs: { width: "600", height: "600", directories: "no", scrollbars: "yes", resizable: "yes", location: "no", toolbar: "no", /*menubar: "no", */status: "no" }
	}
}


// preserve existing onload code
var _links_js_wOnload = window.onload;
window.onload = function() {
	if (_links_js_wOnload != null) {
		_links_js_wOnload();
	}
	init_popups();
}


/**
 * Adds popup behaviour for uniquely-name links.
 *
 * Each link element with a rel attribute is examined. If the value
 * of rel has an entry in the popupAttribs object/associative array,
 * the onclick handler is attached.
 */
function init_popups() {
 	var links = document.getElementsByTagName('a');

 	for (var i = 0; i < links.length; i++) {
		var link = links[i];

		// check that this link has the attribute we are looking for
		if (link.getAttribute("rel") == null) {
			continue;
		}

		var key = link.getAttribute("rel");

		// check if "rel" matches an entry in popupAttribs
		if (popups[key] != null) {
			link.onclick = function() {
				_popup(this); return false;
			}
		}
	}
}


/**
 * Creates a popup window using the attributes defined in popupAttribs.
 *
 * @param link The source of the popup call.
 */
function _popup(link) {
	var definedAttribs = popups[link.rel].attribs;
	var winAttribs = {};

	// first get the default attributes
	for (var key in defaultAttribs) {
		winAttribs[key] = defaultAttribs[key];
	}
	// now override with any extra attributes
	for (var key in definedAttribs) {
		winAttribs[key] = definedAttribs[key];
	}

	// construct the attributes string
	var winAttribStr = "";
	for (var key in winAttribs) {
		winAttribStr += key + "=" + winAttribs[key] + ",";	//@FIXME: broken in IE
	}
	if (winAttribStr != "") {
		winAttribStr = winAttribStr.substr(0, winAttribStr.length-1);
	}

	var winId;
	if (popups[link.rel].id != null) {
		winId = popups[link.rel].id;
	} else {
		// create a unique ID for the window
		var date = new Date();
		var winId = "Popup_" + date.getTime();
	}

	window.open(link.href, winId, winAttribStr);
	return false;
}