var BrowserFixes = function() {
	// Internet Explorer
	if (Browser.Engine.trident) {
		// Fix white space under images
		$each($(document.body).getElements('img'), function(item){
			item.addEvent('load', function(item) { this.set('alt', '');}).setStyle('display', 'block').set('GALLERYIMG', 'no');
		});
		
		// IE6
		if (Browser.Engine.version==4) {
			// Rainbow resizing
			window.addEvent('resize', function() {
				if (this.getSize().x.toInt() < 962)
					$(document.body).getElement('div.rainbow').setStyle('width', 960);
				else $(document.body).getElement('div.rainbow').setStyle('width', '100%');
			});
			
			// Fix Buttons Spaces
			//$(document.body).getElements('a.button').each(function(item){ item.set('html', item.get('html').replace(/ /g, '&nbsp;')); });
		}
	}
	
	// Firefox
	if (Browser.Engine.gecko) {
		// Auto fill input text
/*
		$each($(document.body).getElements('input[type=text]'), function(item){
			if (!item.getAttribute('value')) item.set('value', '');
			item.addEvent('focus', function(e) { e.stop() });
		});
*/
		// Fix Legends
		$each($(document.body).getElements('legend'), function(item){
			new Element('div', {'class': 'legend', 'html': item.get('html'), 'styles': {'top': '-40px', 'left': '-10px'}}).inject(item, 'after');
			item.destroy();
		}); 
	}
	
	// PC (but not Opera)
	if (Browser.Platform.win && !Browser.Engine.presto)
		// Fix inputs
		$each($(document.body).getElements('input[type=text]'), function(item){
			item.setStyles({'padding-top': 5, 'height': 21});
		});
}

var rollovers = function() {
	// Speaker navigation
	items = new Array();
	if ($chk($(document.body).getElement('div.speakernav'))) {
		$each($(document.body).getElement('div.speakernav').getElements('div'), function(div) { items.extend(div.getElements('a')); });
		// Add events
		$each(items, function(item) {
			if (!item.hasClass("category")) {
				var myFx2 = new Fx.Morph(item, {duration: 100, link: 'cancel'});
				var originalcolor = item.getStyle('color');
				item.addEvents({
					'mouseenter': function() { if (!item.hasClass('focus')) myFx2.start({'padding-left': 2, 'color': '#'+colour}); },
					'mouseleave': function() { if (!item.hasClass('focus')) myFx2.start({'padding-left': 0, 'color': originalcolor}); }
				});
			}
		});
	}
	// Footer
/*
	if ($chk($(document.body).getElement('div.footer').getElement('div.link')))
		if ($chk($(document.body).getElement('div.footer').getElement('div.link').getElement('h4'))) {
			var linkcolor = $(document.body).getElement('div.footer').getElement('div.link').getElement('h4').getStyle('color');
			$(document.body).getElement('div.footer').getElement('div.link').addEvents({
				'mouseenter': function() { this.getElement('h4').setStyle('color', '#'+colour); },
				'mouseleave': function() { this.getElement('h4').setStyle('color', linkcolor); }
			});
		}
*/
	
	// Home navigation
	if ($chk($(document.body).getElement('div.homenav'))) {
		var colors = new Array('519036', '64065f', '0c80b1');
		$each($(document.body).getElements('div.homenav'), function(nav, index) {
			if (index<3) {
				nav.addEvents({
					'mouseenter': function() { nav.getElement('p').setStyle('color', '#'+colors[index]); },
					'mouseleave': function() { nav.getElement('p').setStyle('color', '#333'); }
				});
			}
		});
	}
	
	// Support
	if ($chk($(document.body).getElement('ul.support')))
		$each($(document.body).getElements('ul.support li'), function(li) {
			var originalcolor = li.getElement('h3').getStyle('color');
			li.addEvents({
				'mouseenter': function() { li.getElement('h3').setStyle('color', '#'+colour); },
				'mouseleave': function() { li.getElement('h3').setStyle('color', originalcolor); }
			});
		});
	// Support: FAQ
	if ($chk($(document.body).getElement('ul.faq')))
		$each($(document.body).getElement('ul.faq').getElements('a'), function(a) {
			a.store('open', 'false');
			a.addEvents({
				'mouseenter': function() {
					if (this.retrieve('open')=='false')
						this.setStyle('color', '#000');
				},
				'mouseleave': function() {
					if (this.retrieve('open')=='false')
						this.setStyle('color', '#'+colour);
				},
				'click': function() {
					if (this.retrieve('open')=='false') {
						this.store('open', 'true');
						this.setStyle('color', '#000');
					} else {
						this.store('open', 'false');
						this.setStyle('color', '#'+colour);
					}
					$each($(document.body).getElement('ul.faq').getElements('a'), function(a2) {
						if (a!=a2) {
							a2.store('open', 'false');
							a2.setStyle('color', '#'+colour);
						}
					});
				}
			});
		});
	
	// Sitemap
	if ($chk($(document.body).getElement('div.sitemap')))
		$each($(document.body).getElements('div.sitemap'), function(div) {
			$each(div.getElements('a'), function(a) {
				var originalcolor = a.getStyle('color');
				a.addEvents({
					'mouseenter': function() { a.setStyle('color', '#'+colour); },
					'mouseleave': function() { a.setStyle('color', originalcolor); }
				});
			});
		});
		
	// Labels
/*
	if ($chk($(document.body).getElement('ul.labels')))
		$each($(document.body).getElement('ul.labels').getElements('li'), function(li) {
			var color = li.getElement('p').getStyle('color');
			li.getElement('p').setStyle('color', '');
			li.getElement('a').addEvents({
				'mouseenter': function() { this.getPrevious('p').setStyle('color', color) },
				'mouseleave': function() { this.getPrevious('p').setStyle('color', '') }
			});
		});
*/
}

// Custom functions for certain pages
var Custom = function() {
	// Comments
	if ($chk($('formComment'))) {
		if (!$chk($('formComment').getElement('span.success'))) {
			$('topic').addEvent('change', function(select) {
				if (this.get('value')==1)
					this.getParent('div').getParent('div').getNext('div').setStyle('display', 'block');
				else
					this.getParent('div').getParent('div').getNext('div').setStyle('display', 'none');
			});
		} else {
			// Success page
			if (!window.location.href.contains('labs.totem')) _gaq.push(['_trackPageview', '/comments/success/']);
		}
	}
	// Manuals
	if ($chk($('manuals')))
		$('manuals').addEvent('change', function() {
			if (this.get('value')!=0)
				setTimeout("window.location = '"+this.get('value')+"';", 100); 
		});
	// Track promos
	if ($chk($(document.body).getElement('div.promos a[href!=#]'))) {
		$(document.body).getElement('div.promos a[href!=#]').addEvent('click', function(e) {
			e.stop();
			var anchor = this;
			var name = this.get('href').split('/');
			name = name[name.length-2];
			var url = '/promos/'+name+'/'+'?from='+window.location.href.split('totemacoustic.com')[1];
			// Track fictive page for goals
			if (!window.location.href.contains('labs.totem')) _gaq.push(['_trackPageview', url]);
			// Delay change of location (http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55527)
			(function() { window.location = anchor.get('href') }).delay(200);
		});
	}
}

var map, language, colour, colourIndex, colourSelection;
var timeStart = new Date();
window.addEvent('domready', function() {

	// Set global variables
	language = $(document.html).get('lang');
	// Set color
	var colourSelection = {
		'jazzberry-jam': 'b10c4a', 
		'blaze-orange': 'ff6c00', 
		'apple': '519036', 
		'blue-chill': '0c80b1', 
		'celery': '8f9709', 
		'seance': '8621a3', 
		'boston-blue': '37b1cb', 
		'tamarillo': '9E1919', 
		'buddha-gold': 'c29200'
	}
	colourIndex = $(document.body).getElement('div.container').get('id');
	colour = colourSelection[colourIndex];
	// Initialize
	BrowserFixes.run();
	// Rollovers
	rollovers.run();
	// Custom scripts for particular pages
	Custom.run();
	// Background header
	if (!(Browser.Engine.trident && Browser.Engine.version==4))
		window.addEvent('resize', function() {
			if (this.getSize().x.toInt() < 962)
				$(document.body).getElement('div.fadebg').setStyle('width', 960);
			else $(document.body).getElement('div.fadebg').setStyle('width', '100%');
		});
		
	// Geolocation (if needed to be set)
/*
	if ($(document.body).getElement('div.container').hasClass('findgeo')) {
		$(document.body).getElement('div.container').removeClass('findgeo');
		new Request({url: '/sync/?findgeo=true&lang='+language, onSuccess: function(responseText) {
			// Homepage: show form location
			if ((responseText=='CA' || responseText=='US') && $chk($('formLocation'))) {
				$('formLocation').setStyle('visibility', 'visible');
				$('formLocation').getPrevious('a').destroy();
			}
			// Products page
			if ($chk($(document.body).getElement('div.price'))) {
				if (responseText=='CA') {
					$(document.body).getElement('div.price').setStyle('display', 'block');
					$(document.body).getElement('div.price').getNext('a').setStyle('display', 'block');
				} else {
					$(document.body).getElement('div.price').getNext('a').destroy();
					$(document.body).getElement('div.price').destroy();
				}
			}
		}}).send();
	};
*/
});
