var portal_visible = true;
var portal_images = new Array();
var portals = new Array('sf', 'pk', 'p', 'cu', 'mr', 'bs', 'br');
var active_portal = null;
var active_protal_tid = null;
var box_images = new Array();
box_images[0] = new Image();
box_images[0].src = '/images/maximize_icon.gif';
box_images[1] = new Image();
box_images[1].src = '/images/minimize_icon.gif';

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = 'obr-' + name + '=' + escape(value) +
		((expires) ? '; expires=' + expires.toGMTString() : '') +
		((path) ? '; path=' + path : '') +
		((domain) ? '; domain=' + domain : '') +
		((secure) ? '; secure' : '');
	document.cookie = curCookie;
}

function getCookie(name) {
	name = 'obr-' + name;
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) {
			return null;
		}
	} else
		begin += 2;
		var end = document.cookie.indexOf(";", begin);
		if (end == -1) {
			end = dc.length;
		}
	return unescape(dc.substring(begin + prefix.length, end));
}

function $(ele) {
	return document.getElementById(ele);
}

function popUp(page, name, w, h, scroll, resizable, offset_x, offset_y) {
	if (offset_x == null) {
		offset_x = 0;
	}
	if (offset_y == null) {
		offset_y = 0;
	}	
	var winl = ((screen.width - w) / 2) - offset_x;
	var wint = ((screen.height - h) / 2) - offset_y;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
	return window.open(page, name, winprops);
}

function printThis(name) {
	var print_url = window.location.href;
	var pound_sign = print_url.indexOf('#');
	var end_url	 = '';
	if (pound_sign > 0) {
		end_url = print_url.substr(pound_sign, print_url.length);
		print_url = print_url.substr(0, pound_sign);
	}
	if (print_url.indexOf('?') > 0) {
		print_url = print_url + '&.print' + end_url;
	} else {
		print_url = print_url  + '?.print' + end_url;
	}
	popUp(print_url, name, 790, 800, 'yes', 'no');
}

function emailThis(name) {
	var url = '/email.php?page=' + window.location.href;
	popUp(url, '', 300, 170, 'no', 'no');
}

function bookmarkThis(url, title) {
	if (document.all && (parseInt(navigator.appVersion) >= 4)) {
		window.external.AddFavorite(url,title);
	} else if (navigator.appName == "Netscape") {
		window.sidebar.addPanel(title,url,"");
	} else {
		alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
	}
}

function searchThis() {
	/*
	var hdn_ibox = $('hdnbox');
	hdn_ibox.style.width = '190px';
	hdn_ibox.style.height = '50px';	
	hdn_ibox.innerHTML = '<iframe src="/search.php?page=' + window.location.href + '" style="width:190px;height:70px;"></iframe>';
	var coords = pageCoords(190, 50);
	hdn_ibox.style.display = 'block';
	hdn_ibox.style.left = coords[0] + 'px';
	hdn_ibox.style.top = coords[1] + 'px';	
	*/
	var url = '/search.php?page=' + window.location.href;
	popUp(url, '', 200, 30, 'no', 'no');
}

// Initaite selected portal and portal view
function initRegents(portal_view, selected_portal) {
	if (portal_view == null) {
		portal_view = true;	
	}
	active_portal = selected_portal;
	portal_visible = portal_view;
	var expires = new Date();
	expires.setTime(expires.getTime() + 1000 * 60 * 60 * 24);
	var portal = $('portal').style;
	var portal_small = $('portal-small').style;
	setPortalView(active_portal);
	if (!portal_view) {
		portal.display = 'none';
		portal.height = '0px';
		portal_small.display = 'block';
		portal_small.height = '20px';
		hidePortalLinks();
	} else {
		portal.display = 'block';
		portal.height = '165px';
		portal_small.display = 'none';
		portal_small.height = '0px';
		showPortalLinks();
	}
	/*
	var hdn_ibox = document.createElement('div');
	hdn_ibox.setAttribute('id', 'hdnbox');
	hdn_ibox.style.cssText = 'position:absolute;display:none;z-index:99;';
	document.body.appendChild(hdn_ibox);
	*/
}

// Rollover portal links
function changePortalLinks(id) {
	var portal_window = $('portal-window');
	if (id == null) {
		id = 'br';
	}
	if (active_portal != id) {
		active_protal_tid = window.setTimeout("setPortalView('" + id + "')", 170);
	}
}

function cancelPortalLinks() {
	window.clearTimeout(active_protal_tid);
}

function setPortalView(id) {
	active_portal = id;
	for (i = 0; i < portals.length; i++) {
		if (portals[i] != id) {
			$(portals[i] + '-portal').style.visibility = 'hidden';
			$(portals[i] + '-link').className = 'portal-link';
		} else {
			$(portals[i] + '-portal').style.visibility = 'visible';
			$(portals[i] + '-link').className = 'portal-link-hover';
		}
	}
}

function tPortal() {
	var i = 1;
	if (portal_visible) {
		var start_height = 165;
		var current_height = 165;
		while (current_height > 0) {
			current_height-= (start_height - current_height + 2);
			if (current_height < 0) {
				current_height = 0;
			}
			window.setTimeout("$('portal').style.height = '" + current_height + "px';", (i*25));
			i++;
		}
		window.setTimeout("$('portal').style.display = 'none';", (i*25));
		window.setTimeout("$('portal').style.height = '0px';", (i*25));
		i++;
		window.setTimeout("$('portal-small').style.display = 'block';", (i*25));
		current_height = 0;
		while (current_height < 20) {
			current_height+= (current_height + 2);
			if (current_height > 20) {
				current_height = 20;
			}		
			window.setTimeout("$('portal-small').style.height = '" + current_height + "px';", (i*25));
			i++;
		}
		hidePortalLinks();
		portal_visible = false;
	} else {
		var start_height = 20;
		var current_height = 20;
		while (current_height > 0) {
			current_height-= (start_height - current_height + 2);
			if (current_height < 0) {
				current_height = 0;
			}
			window.setTimeout("$('portal-small').style.height = '" + current_height + "px';", (i*25));
			i++;
		}		
		window.setTimeout("$('portal-small').style.display = 'none';", (i*25));
		window.setTimeout("$('portal-small').style.height = '0px';", (i*25));
		i++;
		window.setTimeout("$('portal').style.display = 'block';", (i*25));
		current_height = 0;
		while (current_height < 165) {
			current_height+= (current_height + 2);
			if (current_height > 165) {
				current_height = 165;
			}		
			window.setTimeout("$('portal').style.height = '" + current_height + "px';", (i*25));
			i++;
		}
		showPortalLinks();
		portal_visible = true;
	}
}

function hidePortalLinks() {
	for (var i = 0; i < portals.length; i++) {
		$(portals[i] + '-portal').style.display = 'none';
	}
	with ($('portal-tab-link')) {
		innerHTML = '<img id="portal-tab-icon" src="/images/arrow_down.gif" alt="" /> Show Portal';
		title = 'Show Portal (ALT + T)';
	}
	_event_run = false;
}

function showPortalLinks() {
	for (var i = 0; i < portals.length; i++) {
		$(portals[i] + '-portal').style.display = 'block';
	}
	with ($('portal-tab-link')) {
		innerHTML = '<img id="portal-tab-icon" src="/images/arrow_up.gif" alt="" /> Hide Portal';
		title = 'Hide Portal (ALT + T)';	
	}
	_event_run = false;
}
