// --------------------------------------------------------------------------------------------------------------
// 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 (isIE) {	
		if (typeof document.body.style.maxHeight != "undefined") { if (!isSafari) { isIE7 = true; } }
		$$('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();
	if (!isIE) { bacTableFunctions(); }
	
	/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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', 'table#bacTable div', 'div.intro', 'div#map').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', 'table#bacTable div').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');
		});
	});
	
	$('purple').insert($('front-news').show());
	
	var blue_height = $('blue').getHeight();
	var turquoise_height = $('turquoise').getHeight();
	var new_height = Math.max(blue_height, turquoise_height);
	$('blue').setStyle({ height: new_height + 'px' });
	$('turquoise').setStyle({ height: new_height + 'px' });
	
	var news_height = new_height + $('green').getHeight() + 12;
	$('purple').setStyle({ height: news_height + 'px' });
}

function runOnLoaded() {
	//alert("isIE: "+isIE+", isIE6: "+isIE6+", isIE7: "+isIE7);
	if (isIE && isIE6) {
		bacTableFunctions();
	} else if (isIE && isIE7) {
		setTimeout(bacTableFunctions,1000);
	}
}

// --------------------------------------------------------------------------------------------------------------
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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');
	
	// 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');
	
	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');
		}
	}
	
	if ($('navigation')) {
		$$("div#navigation ul li a").each(function(a){
			var aT = a.innerHTML;
			if(aT.length > 12) {				
				if (aT.include("<br>") || aT.include("<BR>") || aT.include("<br />") ) {
					// nowt
				} else {
					aT = aT.sub(" ", " <br />", 1);
					a.innerHTML = aT;
				}
				a.addClassName("long");
			}
		});
	}
}

function bacTableFunctions() {
	if ($('bacSectionTable')) {
		$$("table#bacSectionTable h3").each(function(h3){
			var h3T = h3.innerHTML;
			if(h3T.length > 12) {				
				if (h3T.include("<br>") || h3T.include("<BR>") || h3T.include("<br />") ) {
					// nowt
				} else {
					h3T = h3T.sub(" ", " <br />", 1);
					h3.innerHTML = h3T;
				}
				h3.addClassName("alt");
			}
		});
	}

	if ($('bacTable')) {
		$$('table#bacTable tr td').each(function(td) {
			var a, img, h4, p;
			
			// Create Tooltip
			if (td.select('a').length > 0) {
				a = td.select('a')[0];
				h4 = a.readAttribute('title');
				
				if (td.select('img').length > 0) {
					img = td.select('img')[0];
					p = img.readAttribute('alt');
					
					var div = new Element('div');
					a.wrap(div);
					div.insert('<div class="tip hide"><h4>' + h4 + '</h4><p>' + p + '</p></div>');
					var tip = div.select('div.tip')[0];
					
					// Remove default tips
					a.writeAttribute('title', '');
					img.writeAttribute('alt', '');
					
					// Observe
					a.observe('mouseover', function(event) {
						tip.removeClassName('hide');
						
						if (isIE6) {
							$$('#bacTable td').invoke('addClassName', 'zindex-fix');
							td.removeClassName('zindex-fix');
							td.style.zIndex = 9999;
						}
						if (isIE7) {
							$$('#bacTable td').invoke('addClassName', 'zindex-fix');
							td.removeClassName('zindex-fix');
							td.style.zIndex = 999;
						}
					});
					
					a.observe('mouseout', function(event) {
						tip.addClassName('hide');
						if (isIE6) {
							$$('#bacTable td').invoke('removeClassName', 'zindex-fix');
							td.style.zIndex = 9;
						}
						if (isIE7) {
							$$('#bacTable td').invoke('removeClassName', 'zindex-fix');
							td.style.zIndex = 9;
						}
					});
					
					tip.observe('mouseover', function(event) {
						tip.removeClassName('hide')
						if (isIE6) {
							$$('#bacTable td').invoke('addClassName', 'zindex-fix');
							td.removeClassName('zindex-fix');
							td.style.zIndex = 9999;
						}
						if (isIE7) {
							$$('#bacTable td').invoke('addClassName', 'zindex-fix');
							td.removeClassName('zindex-fix');
							td.style.zIndex = 999;
						}
					});
					
					tip.observe('mouseout', function(event) {
						tip.addClassName('hide');
						if (isIE6) {
							$$('#bacTable td').invoke('removeClassName', 'zindex-fix');
							td.style.zIndex = 9;
						}
						if (isIE7) {
							$$('#bacTable td').invoke('removeClassName', 'zindex-fix');
							td.style.zIndex = 9;
						}
					});
				}
			}
		});
	}
}

function setupCalendarPopup() {
	$$('div#calendar div.keys').each(function(el) {
		el.observe('click', function(event) {
			// 1312
			if (Prototype.Browser.IE) { $$("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;
	});
}