 // JavaScript Document

var _DOMAIN = 'www.jcsqualityfoods.com.au';

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') {
				// Pre-load state 2 images
				tmp_image = new Image();
				tmp_image.src = link.childNodes[0].src.replace(/(\.[^.]+)$/, "_f2$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("_f2.") < 0) img_tag.src = img_tag.src.replace(/(\.[^.]+)$/, "_f2$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(/_f2(\.[^.]+)$/, "$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 '';
	}
}

window.onload = initRollovers;
