function MM_findObj(n, d) { //v4.01
 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
 d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
 if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
 for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
 if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function toggle(objName,x,theProp,theValue) { 
/* toggle divs with id of parameter
find the object
if none set to theValue
if theValue set to none
*/
	var targetBlock="showArticle"+objName;
	var targetShowImg="showArt"+objName;
	var objBlock = MM_findObj(targetBlock);
	var objShowImg = MM_findObj(targetShowImg);
	if(objBlock.style.display == "none"){
		/*objShowImg.src='../images/layout/button_closed.gif';*/
		objBlock.style.display=theValue;
	}else{
		/*objShowImg.src='../images/layout/button_open.gif';*/
		objBlock.style.display="none";
	}
}

function externalLinks(){ 
/* rewrite links to enable javascript popups in valid xhtml pages 
valid values of rel are external or popup+parameters space delimited
examples of use
if rel='external' set target='_blank'
if rel='popup career 300 400' set onclick to popup in a window named career 300 wide and 400 high
*/
  if (!document.getElementsByTagName) {
		return; 
	}
	var defW = 640;
	var defH = 480;
	/* todo - check for missing or invalid width and height settings and set a default value for the popup*/
	var popupTitle = 'popup';
	var scrW = screen.availWidth;
    var scrH = screen.availHeight;
    var anchors = document.getElementsByTagName("a"); 
    var anchor, i, target, relIndex, relSplit, linkDest; 
    for (i=0;i<anchors.length;i++){
		anchor=anchors[i];
		/* does this anchor have a rel attribute set? */
		if( anchor.getAttribute("href") && anchor.getAttribute("rel") != null){
			linkDest = anchor.getAttribute("href"); 
			relIndex = anchor.getAttribute("rel"); 
			relSplit = relIndex.split(" ");
			switch(relSplit[0]) {
				case 'stream' :
					/* anchor.className+=anchor.className?' popup':'popup';*/
					/* if popup parameters missing fill in with defaults */
					if(relSplit[1]==null){relSplit[1]='popup';}
					if(relSplit[2]==null){relSplit[2]=defW;}
					if(relSplit[3]==null){relSplit[3]=defH;}
					anchor.setAttribute("rel" , relSplit[0] + ' ' + relSplit[1] + ' ' + relSplit[2] + ' ' + relSplit[3]);
					anchor.title+='Stream this recording';
					popupW=relSplit[2];
					popupH=relSplit[3];
					anchor.onclick=function(){newWin=window.open(this.href,this.rel.split(" ")[1],'location=no,menubar=no,status=no,resizable=yes,toolbar=no,scrollbars=yes,width=' + this.rel.split(" ")[2] + ',height=' + this.rel.split(" ")[3]);if(window.focus){newWin.focus()} return false;}
				break;
				case 'download' :
					anchor.title+='Download this recording';
				break;
				case 'itunes' :
					anchor.title+='Open this podcast in iTunes';
				break;
				case 'getiTunes' :
					anchor.title+='Download iTunes';
				break;
				case 'subscribe' :
					anchor.title+='Subscribe to these podcasts in iTunes';
				break;
			}
		}
	}
}

function linkStyler(parentID) {
	
	var parentContainer, contA, aCount;
	// if no parentID is declared, use <body> as the parent container
	if(!parentID) {
		parentContainer = document.getElementsByTagName('body')[0];
	} else {
		parentContainer = document.getElementById(parentID);
	}
	contA = parentContainer.getElementsByTagName('a');
	aCount = contA.length;
	
	// Applies mimetype styles to hrefs in content div
	function addDocLinks() {
		if(parentContainer != null){
	
			for(i=0; i<aCount; i++) {
				var aExt = contA[i].href.slice(-3);
				switch(aExt) {
					case 'rtf' :
						contA[i].className = 'rtf';
						break;
					case 'doc' :
						contA[i].className = 'doc';
						break;	
					case 'pdf' :
						contA[i].className = 'pdf';
						break;
					case 'xls' :
						contA[i].className = 'xls';
						break;
					case 'ppt' :
						contA[i].className = 'ppt';
						break;
				}
			}
		}
	}
	
	// more functions can be added to this routine by calling them here -- they will also have access to the 3 link related variables.
	addDocLinks();
}

/* Image Selector */
function MM_changeProp(objName,x,theProp,theValue) { //v6.0
 var obj = MM_findObj(objName);
 if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
 if (theValue == true || theValue == false)
 eval("obj."+theProp+"="+theValue);
 else eval("obj."+theProp+"='"+theValue+"'");
 }
}

function showImg(objName,x,theProp,theValue) { 
/* hide divs with id="showImg0" to "showImg99" and show div with matching id 
onclick="showImg('showHide0','','style.display','block','DIV')"
IE bug alert: showImg = showimg as IE ignores case
*/
 var i,noneValue='none';
  for (i=0;i<=99;i++) 
	{
		var obj = MM_findObj(objName.substring(0,7)+i);
		if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
			if (noneValue == true || noneValue == false)
		      eval("obj."+theProp+"="+noneValue);
			else eval("obj."+theProp+"='"+noneValue+"'");
		}
		var obj2 = MM_findObj(objName);
		if (obj2 && (theProp.indexOf("style.")==-1 || obj2.style)){
			if (theValue == true || theValue == false)
		      eval("obj2."+theProp+"="+theValue);
			else eval("obj2."+theProp+"='"+theValue+"'");
		}
	}
}

/* Progressive Layout by Alessandra Fulciniti */
function MakeLayout(id,minr,maxw,maxr){
if(document.getElementById){
    SetWidth(id,minr,maxw,maxr);
    window.onresize=function(){ SetWidth(id,minr,maxw,maxr);}
    }
}

function SetWidth(id,a,b,c){
	var w=getBrowserWidth();
	if(w==0) return;
	var el=document.getElementById(id);
	el.style.margin="0 auto";
	var d=el.style;
	if(w<=a) d.width=a+"px";
	else if(w>=c) d.width=b+"px";
	else {
    var m=(b-a)/(c-a);
    d.width=parseInt(m*w+a*(1-m))+"px";
	}
}

function getBrowserWidth(){
	if (window.innerWidth) return window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth!=0)
		return document.documentElement.clientWidth;
	else if (document.body) return document.body.clientWidth;
	return 0;
}

window.onload = function() {
	externalLinks();
	hide();
	linkStyler('content');
}
