// --------------------------------------------------------------------------------------------------------------
// set up variables

var isSafari = false;
var isSafari3 = false;
var isIE = false;
var isIE7 = false;
var isIE6 = false;
var isMozilla = false;
var isMac = false;

// --------------------------------------------------------------------------------------------------------------
// what to do on DOM ready
document.observe("dom:loaded", runOnDOMready);
// what to do when body loaded
Event.observe(window, 'load', function() { runOnLoaded(); });

function runOnDOMready() {
	/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ some simple browser testing */
	if (navigator.appVersion.indexOf("Mac")!=-1) {
		isMac = true;
		$$('body').invoke('addClassName', 'isMac');
	}
	// Safari versioning
	isSafari = Prototype.Browser.WebKit;
	// are we on safari 3? add a class (it over anti aliases everything which causes padding issues)
	// http://www.hedgerwow.com/360/dhtml/detect-safari3-by-js-css.html
	if( window.devicePixelRatio && window.getMatchedCSSRules && !window.Opera){
		isSafari3 = !!window.getMatchedCSSRules(document.documentElement,'');
	}
	if (isSafari) {	
		$$('body').invoke('addClassName', 'isSafari');
		if (isSafari3) { $$('body').invoke('addClassName', 'isSafari3'); }
	}
	// (ok so theres an assumption that any Safari thats not Safari3 is Safari2 here)		
	// IE versioning
	isIE = Prototype.Browser.IE;
	if (typeof document.body.style.maxHeight != "undefined") { if (!isSafari) { isIE7 = true; } }
	if (isIE) {	
		$$('body').invoke('addClassName', 'isIE');
		if (isIE7) {
			$$('body').invoke('addClassName', 'isIE7');
		} else {
			isIE6 = true;
			$$('body').invoke('addClassName', 'isIE6');
		}
	}
	/* http://gmatter.wordpress.com/2006/10/20/detecting-ie7-in-javascript/ */
	// ### ok so theres an assumption that any IE thats not IE7 is IE6 here	
	// alittle mozilla testing too
	isMozilla = Prototype.Browser.Gecko;
	if (isMozilla) {
		$$('body').invoke('addClassName', 'isMozilla');
		if(navigator.userAgent.search(/3.0/) != -1) {
			$$('body').invoke('addClassName', 'isFF3');
		} else {
			$$('body').invoke('addClassName', 'isFF2');
		}
	}
	
	/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ set up some adding of classes to things */	
	
	setUpClasses();	
	setupCalendarPopup();
	
	/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ do some roll over events */
	
	
	$$('input.submit').each(function(el){
		el.observe('mouseover', function(event) {
			var element = Event.element(event);
			element.addClassName('submitHover');
		});
		el.observe('mouseout', function(event) {
			var element = Event.element(event);
			element.removeClassName('submitHover');
		});
	});
	
	$$('div#navigation ul')[0].childElements().each(function(el){
		el.observe('mouseover', function(event) {
			el.addClassName('hover');
		
			if (isIE) {
				$$('img#welcome', 'img#map', 'div#local-map', 'div#login', 'div#calendar', 'div#content').each(function(div) {
					div.setStyle({ zIndex: -1 });
				});
				
				if (isIE6) {
					$$('select').each(function(select) {
						select.setStyle({
							visibility: 'hidden'
						})
					});
				}
			}
		});
		
		el.observe('mouseout', function(event) {
			if (isIE) {
				$$('img#welcome', 'img#map', 'div#local-map', 'div#login', 'div#calendar', 'div#content').each(function(div) {
					div.setStyle({ zIndex: 1 });
				});
				
				if (isIE6) {
					$$('select').each(function(select) {
						select.setStyle({
							visibility: 'visible'
						})
					});
				}
			}
			
			el.select('> li').invoke('removeClassName', 'hover');
			el.removeClassName('hover');
		});
	});
	
	
	$$('div#navigation ul > li > ul > li').each(function(el) {
		el.observe('mouseover', function(e) {
			el.addClassName('hover');
		});
	});
	
	$$('tr').each(function(el) {
		el.observe('mouseover', function(event) {
			el.addClassName('hover');
		})
		el.observe('mouseout', function(event) {
			el.removeClassName('hover');
		})		
	})
	$$('tr th').each(function(el) {
		el.observe('mouseover', function(event) {
			if(this.childElements().length == 2) {
				this.childElements()[1].style.display = "inline";
			}
		})
		el.observe('mouseout', function(event) {
			if(this.childElements().length == 2 && !this.hasClassName("sorted")) {
				this.childElements()[1].style.display = "none";
			}
		})		
	})
	
	$$('div.optionGroup').each(function(el) {
		el.down().observe('click', function(event) {
			if(this.next().hasClassName('hide')) {
				this.next().removeClassName('hide');
				this.down().addClassName('open');
			} else {
				this.next().addClassName('hide');				
				this.down().removeClassName('open');				
			}
			if($$('div.optionGroup h3 a.open').length > 0) {
				$('applyFilters').removeClassName('hide');
			} else {
				$('applyFilters').addClassName('hide');				
			}
			Event.stop(event);			
		})
		if(el.childElements()[1]) {
			if(!el.down().down().hasClassName('open')) {
			el.childElements()[1].addClassName('hide');
			}
		}
	})
	if($('pagination-jump')) {
		$('pagination-jump').observe('change', function(event) {
			alert('changed!');
		})
	}
		
	$('q').observe('focus', function(e) {
		el = Event.element(e);
		if (el.value == 'SEARCH')
		{
			el.writeAttribute('value', '');
		}
	});
	
	$('q').observe('blur', function(e) {
		if (el.value == '')
		{
			el.writeAttribute('value', 'SEARCH');
		}
	});
	
	// add hover state to sidebar nav
	$$('div#sidenav ul li').each(function(el) {
		el.observe('mouseover', function(event) {
			class_name = el.className.split(' ')[0];
			el.addClassName(class_name + '-hover');
		});
		
		el.observe('mouseout', function(event) {
			class_name = el.className.split(' ')[0];
			el.removeClassName(class_name + '-hover');
		});
	});
}

function runOnLoaded() {

}

// --------------------------------------------------------------------------------------------------------------
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ application wide functions */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (remember to test if the elements you are messing with exist) */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ( eg. if (($$('body.homepage'))!=""){ } ) */

function setUpClasses() {
	$$('input[type=submit]').invoke('addClassName', 'submit');
	$$('input[type=radio]').invoke('addClassName', 'radio');
	$$('input[type=checkbox]').invoke('addClassName', 'checkBox');
	$$('input[type=image]').invoke('addClassName', 'image');
	$$('input[type=file]').invoke('addClassName', 'file');
	$$('tr:nth-child(even)').invoke('addClassName', 'even');
	$$('li:nth-child(even)').invoke('addClassName', 'even');	
	$$('li:first-child').invoke('addClassName', 'first');
	$$('li:last-child').invoke('addClassName', 'last');		
	$$('tr td:last-child').invoke('addClassName', 'last');	
	$$('tr th:last-child').invoke('addClassName', 'last');	
	$$('select[multiple=multiple]').invoke('addClassName', 'multiple');
	
	// Navigation
	$$('div#navigation > ul > li:nth-child(1)').invoke('addClassName', 'blue');
	$$('div#navigation > ul > li:nth-child(2)').invoke('addClassName', 'purple');
	$$('div#navigation > ul > li:nth-child(3)').invoke('addClassName', 'pink');
	$$('div#navigation > ul > li:nth-child(4)').invoke('addClassName', 'red');
	$$('div#navigation > ul > li:nth-child(5)').invoke('addClassName', 'orange');
	$$('div#navigation > ul > li:nth-child(6)').invoke('addClassName', 'green');
	$$('div#navigation > ul > li:nth-child(7)').invoke('addClassName', 'aqua');
	$$('div#navigation > ul > li:nth-child(8)').invoke('addClassName', 'turquoise');
	
	$$('div#navigation > ul > li:nth-child(2) ul').invoke('remove');
	$$('div#navigation > ul > li:nth-child(3) ul').invoke('remove');
	
	// Sitemap
	$$('div#sitemap li:nth-child(1)').invoke('addClassName', 'blue');
	$$('div#sitemap li:nth-child(2)').invoke('addClassName', 'purple');
	$$('div#sitemap li:nth-child(3)').invoke('addClassName', 'pink');
	$$('div#sitemap li:nth-child(4)').invoke('addClassName', 'red');
	$$('div#sitemap li:nth-child(5)').invoke('addClassName', 'orange');
	$$('div#sitemap li:nth-child(6)').invoke('addClassName', 'green');
	$$('div#sitemap li:nth-child(7)').invoke('addClassName', 'aqua');
	$$('div#sitemap li:nth-child(8)').invoke('addClassName', 'turquoise');
	
	// Calendar
	$$('div#calendar table tr:first-child td:first-child').invoke('addClassName', 'tl');
	$$('div#calendar table tr:first-child td:last-child').invoke('addClassName', 'tr');
	
	// Side bar
	$$('div#sidenav ul li:nth-child(2)').invoke('addClassName', 'second');
	$$('div#sidenav ul li:nth-child(3)').invoke('addClassName', 'third');
	$$('div#sidenav ul li:nth-child(4)').invoke('addClassName', 'fourth');
	$$('div#sidenav ul li:nth-child(5)').invoke('addClassName', 'fifth');
	$$('div#sidenav ul li:nth-child(6)').invoke('addClassName', 'sixth');
	
	if ($$('div#calendar'))
	{
		td = $$('div#calendar table td').last();
		if (td) 
		{
			td.addClassName('br');
			td.up().select('td:first-child').invoke('addClassName', 'bl');
		}

		if (last_row = $$('div#calendar table tr').last())
		{
			if (last_row.childElements().length == 0)
			{
				last_row.remove();
			}
		}
	
		if ($$('div#calendar table tr').length == 8)
		{
			$$('div#calendar table').invoke('addClassName', 'long');
		}
	}

}

function setupCalendarPopup() {
	$$('div#calendar div.keys').each(function(el) {
		el.observe('click', function(event) {
			// 1312
			if (Prototype.Browser.IE) { $$("input","select").invoke("addClassName","hideForms"); }
			// 1312
			$$('div.popup').invoke('hide');
			element = Event.element(event);
			popup = element.up().up().next();
			popup.toggle();
			Event.stop(event);
		});
	});
	
	$$('div.popup a.close').each(function(el) {
		el.observe('click', function(event) {
			// 1312
			if (Prototype.Browser.IE) { $$("input","select").invoke("removeClassName","hideForms"); }
			// 1312
			element = Event.element(event);
			element.up().hide();
			Event.stop(event);
		});
	});
}

// this function search and replaces a string and returns the new string
// http://www.daveshuck.com/blog/index.cfm/2006/12/13/Javascript-examples--removeElement-and-replaceAll
function replaceAll( str, searchTerm, replaceWith, ignoreCase )   {
   var regex = "/"+searchTerm+"/g";
   if( ignoreCase ) regex += "i";
   return str.replace( eval(regex), replaceWith );
}

// this function copies the invoice address to the delivery address fields on the checkout.
function copyAddress()
{
	$$('fieldset#invoice input[type=text]').each( function(el) {
		$(el.id.gsub('invoice', 'delivery')).value = el.value;
	});
}