var SteamrV8 = function() {
	var me = this;
	var args = arguments;
	
	var self = {
		bgY : 0,
		currentBanner : -1,
		bannerTimeout : 0,
		bannerFrozen : false,
		
		cfg : {
			animateSpeed: 250,
			bannerRotation: 5000,
			addressItems : ".email",
			noscriptItems : ".noscript",
			promoBannersSrc : ['promo.weve_got_a_new_look.png', 'promo.rocket_science.png', 'promo.anything_we_can_offer.png', 'promo.supported_browsers.png'],
			promoBannersAlt : ['We\'ve got a new look!', 'Getting a website isn\'t Rocket Science', 'Anything We Can Offer, For Whatever You Need', 'Our sites are fully compatible with Opera, Internet Explorer, Firefox and Safari'],
			promoBannersRef : ['', '/web_design', '/web_development', '/about/guarantee'],
			imageDir : "/static/8.3/i/"
		}, 

		init : function() {
			//this.initDebug();
			this.hideNoScript();
			this.showEmailAddresses();
			//this.initalizeNavigation();
			//this.animateBackground();
			this.initializeBanner();
			this.initializeBannerRotation();
			this.initializeElements();
			this.initializeCFValidation();
			this.initializeLightBox();
		}, 
		
		initDebug : function() {
			$('<div></div>').attr('id', 'debug').appendTo('#bodyContent');
			$('<h4></h4>').attr('id', 'debugTitle').text('Debug Console (Click to Clear)').appendTo('#debug');
			$('#debugTitle').click(function() {
				$('#debug').remove();
				self.initDebug();
			});
		},
		
		hideNoScript : function() {
			$(self.cfg.noscriptItems).remove();
		},
		
		showEmailAddresses : function() {
			$(self.cfg.addressItems).text('Contact me at leo' + '@' + 'steamr.com').css({'opacity' : 0}).animate({'opacity' : 1});
		},
		
		initalizeNavigation : function() {
			$('#navigation li:last').addClass('last');
			
			$('#navigation li a').css({'margin-top' : '10px'});
			$('#navigation li a').mouseenter(function() {
				$(this).stop().animate({'marginTop' : '7px'}, 150, 'easeOutSine');
				self.debug("Mouse over navigation detected.");
			});
			$('#navigation li a').mouseleave(function() {
				$(this).stop().animate({'marginTop' : '10px'}, 150, 'easeOutSine');
			});
		},
		
		animateBackground : function() {
			if ( ! ($.browser.opera || $.browser.msie || $.browser.mozilla || $.browser.safari)) {
				self.debug("Disabling background scrolling");
				return false;
			}
			
			self.bgY = ((self.bgY + 1) % 750);
			
			setTimeout(function() {
				$('#headWrapper').css({backgroundPosition: self.bgY+'px 0px' });
				$('body').css({backgroundPosition: self.bgY+'px 0px' });
				self.animateBackground();
			}, 200);
		},
		
		initializeBanner : function() {
			self.initializeSelectorEffects();
			
			$('ul#promoBannerSelect li:not(.overlay)').each(function(i) {
				self.debug("Found banner #" + i);
				
				$(this).click(function() {
					self.changeBannerImage(i);
					self.debug("Banner image changed to #" + i);
				});
				
			});
			
			$('<img />').attr('id', 'bannerOverlay').css('opacity', 0).insertBefore('#promoBannerImage');
			
			$('#bannerOverlay').mouseenter(function() {
				self.bannerFrozen = true;
			});
			$('#bannerOverlay').mouseleave(function() {
				self.bannerFrozen = false;
			});
		},
		
		initializeSelectorEffects : function() {
			// Disable effects for the world's most incompetent browser.
			// http://blogs.msdn.com/ie/archive/2005/04/26/412263.aspx
			// This issue STILL REMAINS	in IE8
			if (jQuery.browser.msie) {
				return false;
			}
			
			$('ul#promoBannerSelect li:not(.overlay)').each(function(i) {
				$(this).attr({
					'id' : $(this).attr('id') + '_modified',
					'class' : 'select' + i + ' ' + $(this).attr('class')
				});
				
				$(this).mouseenter(function() {
					self.debug("Detected mouse enter on #" + i);
					$('#select' + i + '_over').stop().animate({opacity: 1}, self.cfg.animateSpeed, 'easeOutSine');
				});
			
				$(this).mouseleave(function() {
					self.debug("Detected mouse leave on #" + i);
					$('#select' + i + '_over').stop().animate({opacity: 0}, self.cfg.animateSpeed, 'easeOutSine');
				});
				
			});
			
			$('ul#promoBannerSelect li').each(function(i) {
				$('<span></span>').attr({
					'id' : 'select' + i + '_over',
					'class' : 'overlay ' + $(this).attr('class')
				}).css({
					top: (i * (38+10)),
					opacity: 0
				}).appendTo(this);
				self.debug("Injected overlay for button #" + i);
			});
		},
		
		changeBannerImage : function(index) {
			if (index > self.cfg.promoBannersSrc.length)
				return false;
			
			self.currentBanner = index;
			self.bannerTimeout = self.cfg.bannerRotation;
			
			$('#promoBannerSelect li').add('#promoBannerSelect li .overlay').removeClass('on').addClass('off');
			$('#select' + index + '_modified').add('#select' + index + '_over').removeClass('off').addClass('on');
			$('#select' + index + '_over').toggleClass('off', 'on');
			
			// Since #selectx might still exist on IE (since effects are disabled, _modified might not have been injected)
			// #selectx will need to be changed too.
			$('#select' + index).add('#select' + index).removeClass('off').addClass('on');
			
			$('#bannerOverlay').attr({
				src : $('#promoBannerImage').attr('src')
			}).css('opacity', 1);
			
			$('#promoBannerImage').attr({
				src : self.cfg.imageDir + self.cfg.promoBannersSrc[index],
				alt : self.cfg.promoBannersAlt[index]
			})
			
			$('#bannerOverlay').animate(
				{'opacity' : 0}, 
				self.cfg.animateSpeed, 
				'easeOutSine'
			).click(function() {
				if (self.cfg.promoBannersRef[self.currentBanner] != '')
					window.location = self.cfg.promoBannersRef[self.currentBanner];
				//alert(self.currentBanner);
			});
		},
		
		initializeBannerRotation : function() {
			if ( ! self.bannerFrozen) 
				self.bannerTimeout -= 1000;
			
			if (self.bannerTimeout <= 0)
				self.changeBannerImage((++self.currentBanner) % self.cfg.promoBannersSrc.length);
			
			setTimeout(self.initializeBannerRotation, 1000);
		},
		
		initializeElements : function() {
			$('.thumbnail:odd').addClass('odd');
		},
		
		initializeCFValidation : function() {
			if ($('#formErrorBox').length == 0) return;
			
			$('#formErrorBox').css({opacity: 0, display: 'none'});
			$('#contactForm input').add('#contactForm textarea').each(function() {
				$(this).blur(function() {
					$(this).addClass('touched');
					self.cfValidate();
				});

					$(this).keyup(function() {
						self.cfValidate();
					});
			});
			
			self.cfValidate();
		},
		
		cfValidate : function() {
			errors = false;
			
			$('#formErrors').empty();
			if ($('#userName').val().length <= 2 && $('#userName').hasClass('touched')) {
				$('<li></li>').text('Your name is too short.').appendTo('#formErrors');
				errors = true;
			}
			if ( ! /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($('#emailAddr').val()) && $('#emailAddr').hasClass('touched')) {
				$('<li></li>').text('Your email is invalid.').appendTo('#formErrors');
				errors = true;
			}
			if ($('#projectInfo').val().length < 15 && $('#projectInfo').hasClass('touched')) {
				$('<li></li>').text('Your project info / details is too short.').appendTo('#formErrors');
				errors = true; 
			}
			
			if ( ! errors) {
				$('#formErrorBox').stop().animate({opacity: 0}).css({display: 'none'});
			} else {
				$('#formErrorBox').css({display: 'block'}).stop().animate({opacity: 1});
			}
		},
		
		initializeLightBox : function() { 
			$(function() {
				$('div.projectThumb a').not('a.nopreview').lightBox();
			});
		
		},
		
		debug : function(message) {
			$('#debug').append(message + "<br />");
		}
		
	};

	self.init.apply(self);
	return self;
};

$(function() {
	var steamr8 = new SteamrV8();
});


