/* globalutils.js: Global Utilities
/* Dan Smith, 19/07/05 */
/* added multiple onload events handler and 
/* table row class modifiers, 11/04/06, DBS */

addEvent(window, 'load', formatTable);
addEvent(window, 'load', extLinks);

/* Set the style from cookie, if there is one */
if (document.cookie) {
	var dc = getCookie("soton_style");
	if (dc != null && dc != "") {
		var split_dc = dc.split(":");
		var the_style = split_dc[1]; 
		var face = the_style.charAt(0);
		var size = the_style.charAt(1);
		var col = the_style.charAt(2);
		document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/style/stylepick/face" + face + ".css\" media=\"all\">\n"
		+ "<link rel=\"stylesheet\" type=\"text/css\" href=\"/style/stylepick/size" + size + ".css\" media=\"all\">\n"
		+ "<link rel=\"stylesheet\" type=\"text/css\" href=\"/style/stylepick/col" + col + ".css\" media=\"screen\">");
	}
}
/* End of style setting */

/* add multiple onload events */
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

/* open pop up window, making the popup larger according to the  */
/* font-size currently selected by user */
var popwin = null;
function popOpen(URL, frompage) {
	var strOptions;
	var size = 1;
	var popwidth = 594;
	var popheight = 540;

	if (popwin != null && !popwin.closed)
	popwin.close();

	var dc = getCookie("soton_style");
	if (dc != null && dc != "") {
		var split_dc = dc.split(":");
		var the_style = split_dc[1];
		size = the_style.charAt(1);

		if (size == 0) {
			popwidth = 554;
			popheight = 520;
		}
		else if (size == 2) {
			popwidth = 720;
			popheight = 550;
		}
		else if (size == 3) {
			popwidth = 764;
			popheight = 570;
		}
	}
	
	strOptions = "scrollbars,resize,width=" + popwidth + ",height=" + popheight;

	popwin = window.open(URL, 'popwin', strOptions);
	popwin.focus();
}

/* open links to binary documents and non-southampton.ac.uk pages in new window */
function extLinks() {
	if ((!document.getElementsByTagName) || (!document.getElementById)) return;
	var clnks = document.getElementsByTagName("a");
	for (var i = 0; i < clnks.length; i++) {
		var trgt = clnks[i].getAttribute("href");
		var any_action = clnks[i].getAttribute("onclick");
		check_action = new RegExp("^javascript", "i");
		int_link = new RegExp("^[/|#].*");
		doc_link = new RegExp("^.*\.(doc|ppt|pps|pdf|xls|mp3)$");
		if ((trgt != null) && (trgt != "") && (any_action == null) && (!trgt.match(check_action))) {
			if ((trgt.match("soton.ac.uk")) || (trgt.match("southampton.ac.uk")) || (trgt.match(int_link))) {
				if (trgt.match(doc_link)) {
					clnks[i].target = "_blank"; 
				} else {
					clnks[i].target = "_self"; 
				}
			} else {
				clnks[i].target = "_blank";
			}
		}
	}
}

/* helper function to avoid M$'s new cookie security (from http://www.mach5.com/ -Ta!)	*/
function getCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

/* large rotating banner selector */
function lrgImageBanner(target, image_file, alt_txt) {
	if (typeof document.write != "undefined") {
		var image_count = image_file.length;
		var i = Math.random() * image_count;
		i = Math.floor(i)
		document.write("<a href=\"" + target[i] + "\" title=\"" + alt_txt[i] + "\" rel=\"section\"><img src=\"" + image_file[i] + "\" alt=\"" + alt_txt[i] + "\" /></a>");
	}
}

/* full-width rotating banner selector */
function fullWidthBanner(image_file, h1_text, bg_attach, text_place, target, alt_txt) {
	if (typeof document.write != "undefined") {
		var image_count = image_file.length;
		var i = Math.random() * image_count;
		i = Math.floor(i)
		document.write("<div id=\"fullbanner\" class=\"" + bg_attach[i] + "\" style=\"background-image:url(" + image_file[i] + ");\">");
		document.write("<a href=\"" + target[i] + "\" title=\"" + alt_txt[i] + "\" class=\"" + text_place[i] + "\" rel=\"section\"><h1 class=\"" + text_place[i] + "\">" + h1_text[i] + "</h1></a></div>");
	}
}

/* text and image campaign */
function textImageCampaign(h1_text, target, ctext, image_file, alt_txt) {
	if (typeof document.write != "undefined") {
		var image_count = image_file.length;
		var i = Math.random() * image_count;
		i = Math.floor(i)
		document.write("<a href=\"" + target[i] + "\" title=\"" + alt_txt[i] + "\" rel=\"section\"><img src=\"" + image_file[i] + "\" alt=\"" + alt_txt[i] + "\" /></a><h1><a href=\"" + target[i] + "\" title=\"" + alt_txt[i] + "\" rel=\"section\">" + h1_text[i] + "</a></h1>" + ctext[i]);
	}
}

/* Fancy table formatting */
function formatTable() {
	var tbl = document.getElementsByTagName("table");
	for (var i = 0; i < tbl.length; i++) {
		var tblbdy = tbl[i].getElementsByTagName("tbody");
		var trows = tblbdy[0].getElementsByTagName("tr");
		for (var x = 0; x < trows.length; x++)	{
			if (x % 2) {
				trows[x].className = "even";
			} else {
				trows[x].className = "odd";
			}
		}
	}
}