// JavaScript Document

var _DOMAIN = 'dangermouse/web/Archdiocese_Perth_v1_1';

function initRollovers() {
   var all_links;
   var link;
   var idx;
	var tmp_image;
	var section_id = getSectionId();	

   if (!document.getElementsByTagName) {
      return;   
   }
   
   all_links = document.getElementsByTagName("a");
   
   for (idx = 0; idx < all_links.length; idx++) {
      link = all_links[idx];
	  
		if (link.className && (' ' + link.className + ' ').indexOf(' rollover ') != -1) {
			if (link.childNodes && link.childNodes.length == 1 && (link.childNodes[0].nodeName.toLowerCase() == 'img' || link.childNodes[0].nodeName.toLowerCase() == 'input')) {
			
			// Pre-load state 2 images
			tmp_image = new Image();
			tmp_image.src = link.childNodes[0].src.replace(/(\.[^.]+)$/, "_f1$1");
			
			if (link.childNodes[0].id && link.childNodes[0].id == section_id) {
				// Don't attach a listener. Instead, set rollover state permanently
				link.childNodes[0].src = link.childNodes[0].src.replace(/(\.[^.]+)$/, "_f2$1");
			} else {								
				// Attach listeners
				link.onmouseover = mouseover;
				link.onmouseout = mouseout;
				link.onfocus = mouseover;
				link.onblur = mouseout;
			}
		 }
		}	
   }
}

function findTarget(e) {
	var target;
	
	if (window.event && window.event.srcElement) {
		target = window.event.srcElement;
	}
	else if (e && e.target) {
		target = e.target;	   
	}
	
	if (!target) return null;
	
	while (target != document.body && target.nodeName.toLowerCase() != "a") {
		target = target.parentNode;   
	}
	
	if (target.nodeName.toLowerCase() != "a") return null;
	
	return target;
}

function mouseover(e) {
	var target = findTarget(e);
	var img_tag;
	
	if (!target) return;
	
	img_tag = target.childNodes[0];
	
	if (img_tag.src.indexOf("_f1.") < 0) img_tag.src = img_tag.src.replace(/(\.[^.]+)$/, "_f1$1"); 
}

function mouseout(e) {
	var target = findTarget(e);
	var img_tag;
	
	if (!target) return;
	
	img_tag = target.childNodes[0];
	img_tag.src = img_tag.src.replace(/_f1(\.[^.]+)$/, "$1");   
}

function getSectionId() {
	var current_url = location.href;
	var pattern = new RegExp("^http:\\/\\/" + _DOMAIN + "\\/((\\w|-)+)");
	var regexp_results = pattern.exec(current_url);
	
	if (RegExp.$1) {
		return RegExp.$1;
	} else {
		return '';
	}
}

/* 
Methods for resizing the flash stage at runtime.

setFlashWidth(divid, newW)
divid: id of the div containing the flash movie.
newW: new width for flash movie

setFlashWidth(divid, newH)
divid: id of the div containing the flash movie.
newH: new height for flash movie

setFlashSize(divid, newW, newH)
divid: id of the div containing the flash movie.
newW: new width for flash movie
newH: new height for flash movie

canResizeFlash()
returns true if browser supports resizing flash, false if not. 
*/
function setFlashWidth(divid, newW){
	document.getElementById(divid).style.width = newW+"px";
}
function setFlashHeight(divid, newH){
	document.getElementById(divid).style.height = newH+"px";		
}
function setFlashSize(divid, newW, newH){
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}
function canResizeFlash(){
	var ua = navigator.userAgent.toLowerCase();
	var opera = ua.indexOf("opera");
	if( document.getElementById ){
		if(opera == -1) return true;
		else if(parseInt(ua.substr(opera+6, 1)) >= 7) return true;
	}
	return false;
}

function initPage() 
{
	initRollovers();
}

function popWindow(theURL,winName,features) {
	window.open(theURL,winName,features);
}

window.onload = initPage;
