var SpeedlinerFramework = new Class({
	initialize: function() {
		this.closetimer = null;
		window.addEvent('domready', this.setupInteractivity.bind(this));
		window.addEvent('load', this.setupEffects.bind(this));
	},
	
	checkCatalogForm: function() {
		$('cSubmitButton').disabled = ($('cName').value == '' || $('cEmail').value == '' || $('cEmail').value.indexOf('@') < 1 || !($('cEmail').value.indexOf('@') < $('cEmail').value.indexOf('.')));
	},
	
	isIe6: function() {
		return isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
	},

	onFeatureIconMouseOver: function(icon) {
		this.onFeatureIconMouseOut(icon);
		
		var icons = $('featureIcons').getElements('img');
		for (var i=0; i<icons.length; i++) {
			if ($(icons[i]) != icon) {
				$(icons[i]).setStyle('opacity', 0.3);
			}
		}
		
		icon.setAttribute('src', icon.getAttribute('src').replace(/\.png/g, '_a.png'));
		var hint = $(document.createElement('div'));
		hint.setAttribute('id', 'featureHint');
		hint.appendChild(document.createTextNode(hintText));
		hint.setStyle('left', (icon.getPosition(icon.getParent('div').getParent('div')).x-5)+'px');
		icon.getParent('div').getParent('div').appendChild(hint);
	},
	
	onFeatureIconMouseOut: function(icon) {
		if ($('featureHint')) {
			icon.setAttribute('src', icon.getAttribute('src').replace(/_a\.png/g, '.png'));
			var icons = $('featureIcons').getElements('img');
			for (var i=0; i<icons.length; i++) {
				if ($(icons[i]) != icon) {
					$(icons[i]).setStyle('opacity', 1);
				}
			}
			$('featureHint').destroy();
		}
	},
	
	setupFeatureHoverEffects: function() {
		if ($('featureIcons')) {
			var icons = $('featureIcons').getElements('img');
			for (var i=0; i<icons.length; i++) {
				var icon = $(icons[i]);
				icon.addEvent('mouseover', function() {
					slm.onFeatureIconMouseOver($(this));
				});
				icon.addEvent('mouseout', function() {
					slm.onFeatureIconMouseOut($(this));
				});
			}
		}
	},
	
	getHintForAddonIcon: function(icon) {
		return icon.getParent('li').getElement('div.addonHint');
	},
	
	onAddonIconMouseOver: function(icon) {
		this.onAddonIconMouseOut(icon);
		
		var icons = $('addonIcons').getElements('img.addonSymbol');
		for (var i=0; i<icons.length; i++) {
			if ($(icons[i]) != icon && $(icons[i]).getParent('li').getElements('div.addonIconsOverlay').length == 0) {
				var overlay = $(document.createElement('div'));
				overlay.addClass('addonIconsOverlay');
				overlay.addClass('addonIconsOverlayDynamic');
				overlay.setStyle('top', '0px');
				$(icons[i]).getParent('li').appendChild(overlay);
			}
		}
		
		var hint = this.getHintForAddonIcon(icon);
		hint.setStyle('display', 'block');
	},
	
	onAddonIconMouseOut: function(icon) {
		var hint = this.getHintForAddonIcon(icon);
		var overlays = $('addonIcons').getElements('div.addonIconsOverlayDynamic');
		for (var i=0; i<overlays.length; i++) {
			$(overlays[i]).destroy();
		}
		hint.setStyle('display', 'none');
	},
	
	setupAddonsHoverEffects: function() {
		if ($('addonIcons')) {
			var icons = $('addonIcons').getElements('li');
			for (var i=0; i<icons.length; i++) {
				var icon = $(icons[i]);
				if (icon.getElements('div.addonIconsOverlay').length == 0) {
					icon.addEvent('mouseover', function() {
						slm.onAddonIconMouseOver($(this).getElement('img'));
					});
					icon.addEvent('mouseout', function() {
						slm.onAddonIconMouseOut($(this).getElement('img'));
					});
				}
			}
		}
	},
	
	setupMenuImgHoverEffect: function(img) {
		if (img.getAttribute('src').indexOf('_a.png') == -1) {
			var src = img.getAttribute('src').replace(/_a\.png/g, '.png');
			var hoverSrc = img.getAttribute('src').replace(/_a\.png/g, '.png').replace(/\.png/g, '_a.png');
			img.addEvent('mouseover', function() {
				$(this).setAttribute('src', hoverSrc);
			});
			img.addEvent('mouseout', function() {
				$(this).setAttribute('src', src);
			});
		}
	},
	
	setupMenuHoverEffects: function() {
		if ('mainNav') {
			var img = $('mainNav').getElements('img');
			for (var i=0; i<img.length; i++) {
				this.setupMenuImgHoverEffect($(img[i]));
			}
		}
	},
	
	setupBikeSelectorItemHoverEffect: function(img, txt) {
		img.addEvent('mouseover', function() {
			txt.addClass('active');
			img.removeClass('inactive');
		});
		txt.addEvent('mouseover', function() {
			txt.addClass('active');
			img.removeClass('inactive');
		});
		img.addEvent('mouseout', function() {
			txt.removeClass('active');
			img.addClass('inactive');
		});
		txt.addEvent('mouseout', function() {
			txt.removeClass('active');
			img.addClass('inactive');
		});
	},
	
	setupBikeSelectorItemHoverEffect2: function(img, txt) {
		txt.addEvent('mouseover', function() {
			txt.addClass('active');
		});
		img.addEvent('mouseover', function() {
			txt.addClass('active');
		});
		txt.addEvent('mouseout', function() {
			txt.removeClass('active');
		});
		img.addEvent('mouseout', function() {
			txt.removeClass('active');
		});
	},
	
	setupBikeSelectorHoverEffects: function() {
		if ($('bikeselector')) {
			var imgList = $('bikeselector').getElements('li');
			var txtList = $('bikenav').getElements('li');
			for (var i=0; i<imgList.length; i++) {
				var img = $(imgList[i]);
				var txt = $(txtList[i]);
				if (img.hasClass('inactive')) {
					this.setupBikeSelectorItemHoverEffect(img, txt);
				} else if (location.href.indexOf('bikes/bikes') > -1) {
					this.setupBikeSelectorItemHoverEffect2(img, txt);
				}
			}
		}
	},
	
	setupInteractivity: function() {
		this.setupMenuHoverEffects();
		this.setupFeatureHoverEffects();
		this.setupAddonsHoverEffects();
		this.setupBikeSelectorHoverEffects();
	},
	
	setupEffects: function() {
		if ($('featureIcons')) {
			var fx = new Fx.Tween($('featureIcons'), {duration: 2000});
			fx.set('opacity', 0);
			fx.set('display', '');
			fx.start('opacity', 0, 1);
		}
	}
});

var slm = new SpeedlinerFramework();
