var Nav = Class.create();
Nav.prototype = {
	initialize: function(container) {
		this.container = $('nav');
		this.togglers  = this.container.childElements('li');
		this.tabs      = this.container.childElements('div.callout');
		this.setup();
	},
	setup: function() {
		this.togglers.each(function(el, i) {
			if (el.down('a.tab')) {
				Event.observe(el, 'mouseenter', this.open.bindAsEventListener(this, i));
				Event.observe(el, 'mouseleave', this.hide.bindAsEventListener(this, i));
			}
		}.bind(this));
	},
	open: function(event, i) {
		this.timeout = setTimeout(this.show.bind(this,i), 100);
		Event.stop(event);
	},
	show: function(i) {
		for(var j=0, l=this.togglers.length; j<l; j++){
			this.togglers[j].style.zIndex = 1;
		}
		this.togglers[i].style.zIndex = 5;
		this.togglers[i].className = 'active';
		clearTimeout(this.timeout);
		
	},
	hide: function(event, i) {
		if (this.timeout) {
			clearTimeout(this.timeout);
		}
		this.togglers[i].className = '';
		Event.stop(event);
	}
}

Event.onDOMReady(function(){
	new Nav('nav');
});


/* ------------------------------------------------------------------------*/
/* son of suckerfish http://www.htmldog.com/articles/suckerfish/dropdowns/ */
/* ------------------------------------------------------------------------*/

sfHover = function() {
	if (document.getElementById("nav")) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



/* ------------------------------------------------------------------------*/
/* alternate images                                                        */
/* ------------------------------------------------------------------------*/

function switchimage(newimage) {
	document.getElementById('mainimage').src = newimage;
}




/* ------------------------------------------------------------------------*/
/* prevent double click on OPAY screen                                     */
/* ------------------------------------------------------------------------*/

var opaySubmitted = false;
function submitOnce(theform) {
	if (opaySubmitted) { 
		return false;
	}
//	document.getElementById('btnContinue').className='btnOrangeDisabled';
	document.getElementById('btnContinue').style.backgroundImage='url(/images/buttons/button_orange_disabled.gif)';
	opaySubmitted = true;
	return true;
}


/* ------------------------------------------------------------------------*/
/* large image format viewer (php script)                                  */
/* ------------------------------------------------------------------------*/

function popupimage(filename) {
	doPopUpWindow = window.open("/popup_larger.php?file="+filename,"popupImage","scrollbars=yes,resizable=yes,left=50,top=50,screenX=50,screenY=50,width=600,height=600");
}




/* ------------------------------------------------------------------------*/
/*  all other popups from http://www.alistapart.com/articles/popuplinks/   */
/* ------------------------------------------------------------------------*/

//var _POPUP_FEATURES = 'location=0, statusbar=0, menubar=0, width=400, height=300';
var _POPUP_FEATURES = '';

function raw_popup(url, target, features) {
	if (isUndefined(features)) {
		features = _POPUP_FEATURES;
	}
	if (isUndefined(target)) {
		target = '_blank';
	}
	var theWindow = window.open(url, target, features);
	theWindow.focus();
	return theWindow;
}

function link_popup(src, features) {
	return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function isUndefined(v) {
	var undef;
	return v===undef;
}


/* ------------------------------------------------------------------------*/
/*  shows and hides the faceting divs on the search results pages          */
/* ------------------------------------------------------------------------*/

function showhide(div_name) {
	if (document.getElementById(div_name + "_link").innerHTML == "more")	{
		document.getElementById(div_name + "_div").style.display = "block";
		document.getElementById(div_name + "_link").innerHTML = "hide";
	}
	else {
		document.getElementById(div_name + "_div").style.display = "none";
		document.getElementById(div_name + "_link").innerHTML = "more";
	}
}

/* ------------------------------------------------------------------------*/
/*  apply filters from facet dropdowns on category pages                   */
/* ------------------------------------------------------------------------*/

function applyFilter(dropdown) {
	if (dropdown.value) {
		location.href=dropdown.value;
	}
}

