// JavaScript Document
// Load functions on load

function addLoadEvent(func){

	var oldonload = window.onload;

	if(typeof window.onload != 'function'){

		window.onload = func;

	}else{

		window.onload = function(){

			oldonload();

			func();

		}

	}

}


// General browser functions Author: Anthony Booth May 2006 Company: Do Media Ltd

addLoadEvent(prepareCloseWindow);

addLoadEvent(preparePrint);

addLoadEvent(prepareNewWindow);

addLoadEvent(viewAddress);



// Close opened window

function prepareCloseWindow(){

	if(!document.getElementById("close")) return false;

	var closewindow = document.getElementById("close").getElementsByTagName("a");

	closewindow[0].onclick = function(){

		parent.window.close();

		return false;

	}

}

// Print page

function preparePrint(){

	if(!document.getElementById("print")) return false;

	var printpage = document.getElementById("print").getElementsByTagName("a");

	printpage[0].onclick = function(){

		window.print();

		return false;

	}

}

// New window for external links

function prepareNewWindow(){

	if(!document.getElementsByTagName) return false;

	var arrayhref = document.getElementsByTagName("a");

	for(i = 0; i<arrayhref.length; i++){

    	if(arrayhref[i].getAttribute("rel") && arrayhref[i].getAttribute("rel") == "external"){

			arrayhref[i].onclick = function(){

				window.open(this.getAttribute("href"));

				return false;

			}

		}

	}

}

// hide email links from spam

function viewAddress() {

	if (document.getElementsByTagName) {

		var a = document.getElementsByTagName("a")

		var i

		for (i = 0; i < a.length; i++) {

			if (a[i].className && a[i].className == "email") {

				var address_to_replace = a[i].firstChild;

				var real_address = address_to_replace.nodeValue.replace("*atsymbol*", "@");

				address_to_replace.nodeValue = real_address;

				address_to_replace.parentNode.setAttribute("href", "mailto:" + real_address);

			}

		}

	}

}  

// Trigger select change for the directories listings
// see which browser we're working with
if ($.browser.msie) {

// ie doesn't pick up the option.click, so we'll have to use the select.change
$('#edit-role-changer').change(function(){

// get rid of select box
$('#edit-role-changer').hide();

// show the chosen role as static text
// NOTE: FF doesn't pick up [@selected]
$('#role').html($(this).children("[@selected]").text()).show();

// provide a change link
$('#role-change-back').show();

});
}
else {

// provide action for an option change
$('#edit-role-changer option').click(function(){

// get rid of select box
$('#edit-role-changer').hide();

// show the chosen role as static text
$('#role').html($(this).text()).show();

// provide a change link
$('#role-change-back').show();

});
}
