// JavaScript Document
function addEvent(elm, evType, fn, useCapture) {
	// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
	// By Scott Andrew
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}
function getNode(e) {
	//this function gets the node that triggered the event
	if (e) { 
		var node;
		if (e.srcElement) {			//IE
			node = e.srcElement; 
		} else if (e.target) {		//W3C and DOM specs
			node = e.target;
		}
		return (node);
	}
}
function getDestination(e) {
	if (e) {
		var destination; //this is the element that the mouse moved into when the function was called
		if (e.toElement && e.toElement != null) {				//IE
			destination = e.toElement;
		} else if (e.relatedTarget && e.relatedTarget != null) {	//W3C and DOM specs
			destination = e.relatedTarget;
		}
		return (destination);
	}
}
function initialize(e) {
	if (!document.getElementsByTagName || !document.getElementById) return;	
	var links = document.getElementById('wineSubNav').getElementsByTagName('img');
	for (i=0;i<links.length;i++) {
		if (links[i].getAttribute('rel') != '' && links[i].getAttribute('rel') != null) {  //IE detects '' FF (maybe others) detect null
			addEvent(links[i], 'mouseover', winesTertiary, false);
		}
	}
	addEvent(document.getElementById('vineyards'), 'mouseover', winemakingTertiary, false);	
	addEvent(document.getElementById('philosophy'), 'mouseover', winemakingTertiaryHide, false);	
	
	addEvent(document.getElementById('wines'), 'mouseover', subShow, false);
	addEvent(document.getElementById('wines'), 'mouseout', winesHide, false);
	
	addEvent(document.getElementById('winemaking'), 'mouseover', subShow, false);
	addEvent(document.getElementById('winemaking'), 'mouseout', winemakingHide, false);
	
	addEvent(document.getElementById('purchase'), 'mouseover', subShow, false);
	addEvent(document.getElementById('purchase'), 'mouseout', purchaseHide, false);
	
	addEvent(document.getElementById('wineSubNav'), 'mouseout', winesHide, false);
	addEvent(document.getElementById('winemakingSubNav'), 'mouseout', winemakingHide, false);
	addEvent(document.getElementById('purchaseSubNav'), 'mouseout', purchaseHide, false);
	
	//addEvent(document.getElementById('purchaseSubNav'), 'load', navRolls, false);
	
	
}
function subShow(e) {
	node = getNode(e);
	//node.src = node.src.replace('.jpg','_roll.jpg');
	switch (node.getAttribute('id')){
		case 'wines': 
			document.getElementById('wineSubNav').style.display = 'block';
			break;
		case 'winemaking': 
			document.getElementById('winemakingSubNav').style.display = 'block';
			break;
		case 'purchase': 
			document.getElementById('purchaseSubNav').style.display = 'block';
			break;	
		//default : statement;
	}
}
function winesHide(e) {
	var destination = getDestination(e);
	if (destination.getAttribute('rel') != 'wineSubNav' && destination.parentNode.getAttribute('rel') != 'wineSubNav' && destination.parentNode.parentNode.getAttribute('rel') != 'wineSubNav') {
		document.getElementById('wineSubNav').style.display = 'none';
		document.getElementById('wineSubNav2').style.display = 'none';
	}
}
function winemakingHide(e) {
	var destination = getDestination(e);
	if (destination.getAttribute('rel') != 'winemakingSubNav' && destination.parentNode.getAttribute('rel') != 'winemakingSubNav' && destination.parentNode.parentNode.getAttribute('rel') != 'winemakingSubNav') {
		document.getElementById('winemakingSubNav').style.display = 'none';
		document.getElementById('winemakingSubNav2').style.display = 'none';
	}
}
function purchaseHide(e) {
	var destination = getDestination(e);
	if (destination.getAttribute('rel') != 'purchaseSubNav' && destination.parentNode.getAttribute('rel') != 'purchaseSubNav' && destination.parentNode.parentNode.getAttribute('rel') != 'purchaseSubNav') {
		document.getElementById('purchaseSubNav').style.display = 'none';
	}
}
function winesTertiary(e) {
	var node = getNode(e);
	var source = node.src;
	//source = source;
	var nav2 = document.getElementById('wineSubNav2').getElementsByTagName('div');
	for (i=0; i < nav2.length; i++) {
		nav2[i].style.display = 'none';
	}
	document.getElementById('wineSubNav2').style.display = 'block';
	document.getElementById(node.getAttribute('rel')).style.display = 'block';
}
function winemakingTertiary(e) {
	document.getElementById('winemakingSubNav2').style.display = 'block';
}
function winemakingTertiaryHide(e) {
	document.getElementById('winemakingSubNav2').style.display = 'none';
}
addEvent(window, 'load', initialize, false);