// JavaScript Document
var navLinks = {	
	wineBrandID: 'wineBrand',
	wineBrandCloseID: 'wineBrandClose',
	
	wineTypeID: 'wineType',
	wineTypeCloseID: 'wineTypeClose',
	
	wineBrandDivID: 'search_by_brand',
	wineTypeDivID: 'search_by_type',
	
	wineBrandContID: 'wineBrandCont',
	wineTypeContID: 'wineTypeCont',
	
	wineTypeBeforeIMGOBJ: '',
	wineBrandBeforeIMGOBJ: '',
	
	wineTypeAfterIMGOBJ: '',
	wineBrandAfterIMGOBJ: '',
	
	imgRestoreARR: new Array(),
	
	flashHomeContID: 'flash_home_container',
	
	init: function() {			
		
		if(!document.getElementById || !document.getElementsByTagName)
			return;
		
		//add brand events
		if(document.getElementById(navLinks.wineBrandID)) {	
			navLinks.toggleImgClickEvents(navLinks.wineBrandID,true);			
		}	
		
		//add type events
		if(document.getElementById(navLinks.wineTypeID)) {
			navLinks.toggleImgClickEvents(navLinks.wineTypeID,true);			
		}	
		
		//add mouseover/out's for imgs
		if(document.getElementById(navLinks.wineTypeID + "IMG")) {
			navLinks.toggleImgOverEvents(navLinks.wineTypeID + "IMG",true);			
		}
		
		if(document.getElementById(navLinks.wineBrandID + "IMG")) {
			navLinks.toggleImgOverEvents(navLinks.wineBrandID + "IMG",true);			
		}
		
	},
	
	imgRollOver: function(e) {		
		
		var local_target = findTarget(e);
		
		//current state is not over
		if(!local_target.src.match(/_over/)) {			
			local_target.src = local_target.src.replace(/.gif/,'_over.gif');			
		} else {
			local_target.src = local_target.src.replace(/_over/,'');			
		}
		
	},
	
	toggleBrand: function(e) {
		
		var local_target = findTarget(e);
		
		//traverse to get to link node (Safari issue)
		local_target = linkTraverse(local_target);			
		
		if(local_target.nodeName.toLowerCase() == 'body') {
			return;	
		}		
		
		if(dU.getStyle(navLinks.wineBrandDivID,'display') == 'none' || $(navLinks.wineBrandDivID).style.display == 'none') {			
			//hide flash
			Element.hide(navLinks.flashHomeContID);			
				
			//hide type			
			Element.hide(navLinks.wineTypeDivID);			
			
			//show info
			Element.show(navLinks.wineBrandDivID);			
			
			//change img to over			
			navLinks.toggleImgNEW(navLinks.wineBrandID + "IMG", true);
		} else {			
			//hide info
			Element.hide(navLinks.wineBrandDivID);
			
			//show flash
			Element.show(navLinks.flashHomeContID);
			
			//change img to not over			
			navLinks.toggleImgNEW(navLinks.wineBrandID + "IMG", false);
		}
		
	},
	
	toggleType: function(e) {
		
		var local_target = findTarget(e);
		
		//traverse to get to link node (Safari issue)
		local_target = linkTraverse(local_target);		
		
		if(local_target.nodeName.toLowerCase() == 'body') {
			return;	
		}		
		
		if(dU.getStyle(navLinks.wineTypeDivID,'display') == 'none' || $(navLinks.wineTypeDivID).style.display == 'none') {
			//hide flash
			Element.hide(navLinks.flashHomeContID);
			
			//hide brand
			Element.hide(navLinks.wineBrandDivID);
			
			//show type
			Element.show(navLinks.wineTypeDivID);
			
			//change img to over			
			navLinks.toggleImgNEW(navLinks.wineTypeID + "IMG", true);
			
		} else {
			//hide info
			Element.hide(navLinks.wineTypeDivID);
			
			//show flash
			Element.show(navLinks.flashHomeContID);
			
			//change img to over			
			navLinks.toggleImgNEW(navLinks.wineTypeID + "IMG", false);
		}
		
	},
	
	toggleImgOverEvents: function(imgID, addEventsBOOL) {
		if(addEventsBOOL == true) {			
			$(imgID).stopObserving('mouseover', navLinks.imgRollOver);
			$(imgID).stopObserving('mouseout', navLinks.imgRollOver);			
			$(imgID).observe('mouseover', navLinks.imgRollOver);
			$(imgID).observe('mouseout', navLinks.imgRollOver);
		} else {			
			$(imgID).stopObserving('mouseover', navLinks.imgRollOver);
			$(imgID).stopObserving('mouseout', navLinks.imgRollOver);						
		}
	},
	
	
	toggleImgClickEvents: function(imgLinkID, addEventsBOOL) {
		if(addEventsBOOL == true) {
			if(imgLinkID.match(/Brand/)) {
				//remove events for IE 6
				$(imgLinkID).stopObserving('click',navLinks.toggleBrand);							
				$(imgLinkID + 'Close').stopObserving('click',navLinks.toggleBrand);
				
				$(imgLinkID).observe('click',navLinks.toggleBrand);
				cancelClick($(imgLinkID));	
				$(imgLinkID).onclick = cancelClickSafari;
				
				$(imgLinkID + 'Close').observe('click',navLinks.toggleBrand);
				cancelClick($(imgLinkID + 'Close'));
				$(imgLinkID + 'Close').onclick = cancelClickSafari;
			} else {				
				//remove events for IE 6
				$(imgLinkID).stopObserving('click',navLinks.toggleType);							
				$(imgLinkID + 'Close').stopObserving('click',navLinks.toggleType);				
				
				$(imgLinkID).observe('click',navLinks.toggleType);
				cancelClick($(imgLinkID));	
				$(imgLinkID).onclick = cancelClickSafari;
				
				$(imgLinkID + 'Close').observe('click',navLinks.toggleType);
				cancelClick($(imgLinkID + 'Close'));
				$(imgLinkID + 'Close').onclick = cancelClickSafari;
			}
			
		} else {
			if(imgLinkID.match(/Brand/)) {
				$(imgLinkID).stopObserving('click',navLinks.toggleBrand);												
			} else {
				$(imgLinkID).stopObserving('click',navLinks.toggleType);														
			}			
		}
	},
	
	toggleImgNEW: function(imgID, overBOOL) {
		
		//over present, strip it
		if(overBOOL == false) {			
			//add mouseevents
			navLinks.toggleImgOverEvents(imgID,true);	
			
			//add click events			
			navLinks.toggleImgClickEvents(imgID.replace(/IMG/,''), true);
			
			//swap img to be '' (not over)
			$(imgID).src = $(imgID).src.replace(/_over/,'');
			
		} 
		//add over to img src
		else {		
			
			//swap img to be '' (not over)
			if(!$(imgID).src.match(/_over/)) {				
				$(imgID).src = $(imgID).src.replace(/.gif/,'_over.gif');				
		  	}
			
			//remove mouseevents
			navLinks.toggleImgOverEvents(imgID,false);
			
			//remove click events
			navLinks.toggleImgClickEvents(imgID.replace(/IMG/,''), false);			
			
			//check if mouseovers/out's exist on other tab and add if necessary			
			if(imgID.match(/Brand/) && !$(navLinks.wineTypeID + 'IMG').onmouseover) {
				//add mouseevents				
				navLinks.toggleImgOverEvents(navLinks.wineTypeID + 'IMG',true);	
				
				//add click events
				navLinks.toggleImgClickEvents(navLinks.wineTypeID, true);
			
			} else if(!$(navLinks.wineBrandID + 'IMG').onmouseover) {				
				//add mouseevents				
				navLinks.toggleImgOverEvents(navLinks.wineBrandID + 'IMG',true);
				
				//add click events
				navLinks.toggleImgClickEvents(navLinks.wineBrandID, true);
			}					
		}
		
		//swap other img to be not over (if already over)		
		if(imgID.match(/Brand/)) {			
			$(navLinks.wineTypeID + 'IMG').src = $(navLinks.wineTypeID + 'IMG').src.replace(/_over/,'');
		} else {
			$(navLinks.wineBrandID + 'IMG').src = $(navLinks.wineBrandID + 'IMG').src.replace(/_over/,'');
		}			
	}	
}


var typeList = {	
	typeListID: 'types',
	subtypeListID: 'subtypes',
	
	typeListOBJ: '',
	subtypeListOBJ: '',	
	subtypeListContentARR: new Array(),
	
	selectedTypeID: '',
	selectedClass: "selected",
	arrowImgID: 'arrowImg',
	arrowImg: '<img src="/images/arrow_right.gif" alt="arrow" id="arrowImg" style="margin: 0 0 0 8px;" />',	
	arrowImgOBJ: '',
	
	init: function() {			
		
		if(!document.getElementById || !document.getElementsByTagName)
			return;
		
		//assign list objects
		typeList.typeListOBJ = $(typeList.typeListID);
		typeList.subtypeListOBJ = $(typeList.subtypeListID);
		
		//attach clicks to <a>'s of type list
		if(document.getElementById(typeList.typeListID)) {	
			for(i=0;i < $(typeList.typeListID).childNodes.length; i++) {
				//alert($(typeList.typeListID).childNodes[i].nodeName);
				if($(typeList.typeListID).childNodes[i].nodeName == 'LI' && $(typeList.typeListID).childNodes[i].firstChild.nodeName == 'A') {					
					typeList.attachLinkEvents($(typeList.typeListID).childNodes[i].firstChild);				
				}
			}
		}			
		
	},
	
	attachLinkEvents: function(linkOBJ) {
		addEvent(linkOBJ,'click',typeList.updateSubList,false);
		addEvent(linkOBJ,'click',cancelClick,false);
		linkOBJ.onclick = cancelClickSafari;		
	},
	
	cacheList: function(updatedHTML) {		
		typeList.subtypeListContentARR[typeList.selectedTypeID] = updatedHTML;	
	},	
	
	getParentID: function(link_target) {
		//alert(link_target.parentNode.id);
		return link_target.parentNode.id.substr(link_target.parentNode.id.lastIndexOf("_") + 1);	
	},
	
	updateSubList: function(e) {	
		var local_target = findTarget(e);
		
		//traverse to get to link node (Safari issue)
		local_target = linkTraverse(local_target);		
		
		if(local_target.nodeName.toLowerCase() == 'body') {
			return;	
		}
		
		var qs = '';
		
		typeList.selectedTypeID = typeList.getParentID(local_target);		
		
		//build POST query string
		qs = 'ParentID=' + typeList.selectedTypeID;
		
		//if HTML content is not cached, make call to server to retrieve list HTML
		if(!typeList.subtypeListContentARR[typeList.selectedTypeID] || typeList.subtypeListContentARR[typeList.selectedTypeID] == '') {			
			new Ajax.Request('/typeAJAX.cfm', {method:'post', postBody:qs, onSuccess:typeList.postAjaxUpdate, onFailure:typeList.errFunc, asynchronous:true});		
		} else {
			typeList.postUpdateSL();	
		}
		
	},
	
	postAjaxUpdate: function(t) {			
		typeList.cacheList(t.responseText);
		typeList.postUpdateSL();		
	},	
	
	postUpdateSL: function() {
		var localA;
		var localText;
		var selectedListElement;
		
		//update list w/HTML from Array
		Element.update($(typeList.subtypeListID),typeList.subtypeListContentARR[typeList.selectedTypeID]);		
		
		//previous selected type actions		
		selectedListElement = $(typeList.typeListID).getElementsByClassName(typeList.selectedClass)[0];		
		
		// -> remove 'selected' class on <li>
		selectedListElement.removeClassName(typeList.selectedClass);
		
		// -> remove 'img' of arrow
		if($(typeList.arrowImgID)) {
			typeList.arrowImgOBJ = selectedListElement.removeChild($(typeList.arrowImgID));
		}
		
		//clone and save current text to local var				
		localText = selectedListElement.firstChild.cloneNode(false);
		
		// -> create and append <a>
		localA = document.createElement('a');
		localA.title = localText.nodeValue;
		localA.href= "#";
		localA.appendChild(localText);	
		
		// -> add <a> tag node (replace text)
		selectedListElement.replaceChild(localA,selectedListElement.firstChild);		
	
		// -> attach onclick handlers to <a> tag
		typeList.attachLinkEvents(localA);		
		
		//clicked type actions
		// -> add 'selected' class to <li>			
		$('type_' + typeList.selectedTypeID).addClassName(typeList.selectedClass);
		
		// -> remove <a> tag around text
		newLocalText = $('type_' + typeList.selectedTypeID).firstChild.firstChild.cloneNode(false);
		$('type_' + typeList.selectedTypeID).replaceChild(newLocalText,$('type_' + typeList.selectedTypeID).firstChild);		
		
		// -> add 'img' of arrow
		$('type_' + typeList.selectedTypeID).appendChild(typeList.arrowImgOBJ);		
		
	}	
}

var dU = {
	
	getStyle: function(el,styleProp) {
		var x = document.getElementById(el);
		if (x.currentStyle)
			var y = x.currentStyle[styleProp];
		else if (window.getComputedStyle)
			var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);		
		return y;
	},
	
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	getPageSize: function() {
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}
}

//addEvent(window, 'load', navLinks.init, false);
addEvent(window, 'load', typeList.init, false);