//autoPopups.js
//created 2007 Nov 09 by SLY

var autoPdfWin1 = null;
var autoPopWin1 = null;

function closeWin(){
	if (autoPopWin1 != null){
		if(!autoPopWin1.closed)
			autoPopWin1.close();
	}
}

function popupLinks() {
	//create anchors array of anchor tags
	var anchors = document.getElementsByTagName('a');

	//add onclick event handler to each item in the array
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if(anchor.href){
		//add behavior to anchors with "popup" in the rel attribute, or with ".pdf" but not "javascript:" in the href 
			if(((anchor.rel) && (anchor.rel.indexOf('popup')!=-1)) || ((anchor.href.search(/\.pdf/i)!=-1) && (anchor.href.search(/javascript:/i)==-1))){	
				anchor.onclick = doPop;
				anchor.title += ' (Will open in a new window)';	
				//optional styling of popup links:	
				//anchor.style.backgroundImage = "url(pop-up.gif)";
				//anchor.style.backgroundPosition = "right center";
				//anchor.style.backgroundRepeat = "no-repeat";
				//anchor.style.paddingRight = "15px";	
			}
			
			//external links need redirector (added 080530 by SLY)
			else if((anchor.href.search(/http/i)!=-1) && (anchor.href.search(/ghc\.org/i)==-1) && (anchor.href.search(/javascript:/i)==-1)){
				var redirector = '';
				if(window.location.hostname.indexOf('www')==-1){redirector = '/public';}//not public site, go to /public
				redirector += '/redirector.jhtml?url='+ encodeURIComponent(anchor.href);
				anchor.href = redirector;
				anchor.onclick = doPop;
				anchor.title += ' (External link)';	
			}
			
		}
	}
}


function doPop(e){
	// the following mode, width, and height will be used as default values
	var m = 'standard';
	var w = '780';
	var h = '580';
	//extract customized mode, width, and height from the rel attribute of the anchor element
	fVars = this.rel.split(' ');
	if (fVars[1]!=null) {m = fVars[1];}
	if (fVars[2]!=null) {w = fVars[2];}
	if (fVars[3]!=null) {h = fVars[3];}
	newPop(this.href,m,w,h);
	//stop propogation/bubbling
	if (window.event){
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	} 
	else if (e){
		e.stopPropagation();
		e.preventDefault();
	}
}


function newPop(url, mode, strWidth, strHeight){
	if(url.indexOf('.pdf')!=-1){
		//pdf windows do not have choice of width and height, and always use the same features
		autoPdfWin1 = window.open(url, '_blank', 'resizable,location,width=550,height=700,top=30,left=30');
		autoPdfWin1.focus();
	}
	else{
		closeWin();
		mode = mode.toLowerCase();
		if (mode == "fullscreen"){
			strWidth = screen.availWidth;
			strHeight = screen.availHeight;
		}
		var features="";
		if (mode == "console") {features = "resizable,width="+strWidth+",height="+strHeight+",left=30,top=30";}
		if (mode == "fullscreen") {features = "resizable,width="+strWidth+",height="+strHeight+",left=0,top=0";}
		if (mode == "standard") {features = "resizable,toolbar,location,scrollbars,menubar,width="+strWidth+",height="+strHeight+",top=30,left=30";}
		autoPopWin1 = window.open(url, 'popWin', features);
		autoPopWin1.focus();
	}
}

function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	}
}

addEvent(window, 'load', popupLinks);