﻿window.onload = init;
window.onresize = setPageContentMinHeight;
function init() {
	setLinkTargets();
	setPageContentMinHeight();
	}
function initFellows() {
	replaceTagWithImage( 'div', 'class', 'source', '/lib/img/fellows/publications/' );
	setLinkTargets();
	setPageContentMinHeight();
	}

function setPageContentMinHeight() {
	var pageContentHeight = document.getElementById('page_content').offsetHeight;
	var otherContentHeight = document.getElementById('site_header').offsetHeight; // + document.getElementById('site-navigation-footer').offsetHeight;
	var contentHeight = pageContentHeight + otherContentHeight;
	document.getElementById('page_content').style.minHeight = getWindowHeight() - otherContentHeight - 18 + 'px';
	}

function openWindow(theURI, newWinWidth){
	if (!newWinWidth) {newWinWidth=800}
	window.open(theURI, "publication", 'location=0, status=0, toolbar=0, scrollbars=1, width=800')
	}
function openWindowViaRel( the_uri, height, width, rel, parms ) {
	if( !width ) { width = 800; }
	if( !height ) { height = 1; }
	if( height <= 1 ) { height = Math.floor(screen.height * height); }
	if( width <= 1 ) { width = Math.floor(screen.width * width); }
	target = ( rel != "external" ) ? rel : "";
	toggle_all_bars = ( rel != "external" ) ? "0" : "1";
	if( toggle_all_bars ) { 
		toggle_location = toggle_all_bars; 
		toggle_status = toggle_all_bars; 
		toggle_toolbar = toggle_all_bars; 
		}
	var attr = 'width = ' + width + ', height = ' + height + ', location = ' + toggle_location + ', status = ' + toggle_status + ', toolbar = ' + toggle_toolbar + ', scrollbars = 1, resizable = yes';
	t = window.open(the_uri, target, attr );
	if( window.focus ) { t.focus(); }
	return false;
	}

function setLinkTargets() {
	var x = document.getElementsByTagName( 'a' );
	for( var i = 0; i < x.length; i++ ) {
		var rel = x[i].getAttribute( 'rel' );
		if( rel ) {
			switch( rel ) {
				case( "external" ) : 
					x[i].onclick = function () { return openWindowViaRel( this.href, 1, 800, this.rel ); }
					break;
				case( "fellow_focus" ) : 
//					x[i].style.border = '1px solid blue';
					x[i].onclick = function () { return openWindowViaRel( this.href, 1, 800, this.rel ); }
					break;
				case( "slideshow" ) : 
					x[i].onclick = function () { return openWindowViaRel( this.href, 1, screen.height, this.rel ); }
					break;
				case( "audioplay" ) : 
					x[i].onclick = function () { return activateAudioPlayer( this ); }
					break;
				}
			}
		}
	}

function getWindowHeight() {
	var windowHeight = 0;
	if ( typeof ( window.innerHeight ) == 'number' ) { windowHeight = window.innerHeight; } 
	else { if ( document.documentElement && document.documentElement.clientHeight ) { 
		windowHeight = document.documentElement.clientHeight; } 
		else { if (document.body && document.body.clientHeight) { windowHeight = document.body.clientHeight; } }
		}
	return windowHeight;
	}

function replaceTagWithImage( elem, attr, val, jir_img_path, jir_img_suffix ) { 
	if ( !document.getElementsByTagName( 'div' ) ) return;
	var x = document.getElementsByTagName( elem );
	for ( var i = 0; i < x.length; i++ ) {
//		var attr_val = x[i].getAttribute( attr );
//		if ( !attr_val ) { attr_val = x[i].className };
//		if ( attr_val == val ) {
		if ( x[i].getAttribute( attr ) == val ) {
			var jir_img = new Image();
			jir_img_suffix = jir_img_suffix ? jir_img_suffix : '.gif';
			jir_img.src = jir_img_path + x[i].innerHTML.toLowerCase().replace( /[^\w\s] /g, '- ' ).replace( / /g, '_' ) + jir_img_suffix;
			jir_img.onload = JIR( x[i], jir_img.src, jir_img.height, jir_img.width ); 
			}
		}
	}
function JIR( target, img_url, height, width ) {
	height = ( height == 0 ) ? 30 : height;
	target.style.height = '0px';
	target.style.paddingTop = height + 'px';
	target.style.overflow = 'hidden';
	target.style.backgroundImage = 'url(' + img_url + ')';
	target.style.backgroundRepeat = 'no-repeat';
	target.style.backgroundPosition = '0 0';			
	return 'called';
	}