var tabState = {
	service: null,
	bottom: null,
	home: null
};

$(document).ready(function() {

	// Setup services tabs
	tabState.service = $('#home-services-container div.slide-container.active').attr('id');
	$('#home-services-container-inner div.services-tabs a').click(hSelectService);

	// Setup bottom tabs
	tabState.bottom = $('#home-bottom-tabs-outer div.bottom-slide-container.active').attr('id');
	$('#home-bottom-tabs-outer div.bottom-tabs a').click(hSelectBottom);

	// Setup home tabs
	tabState.home = $('#home-column-right div.home-slide-container.active').attr('id');
	$('#home-column-right-outer .home-mid-tabs a').click(hSelectHome);

	// Start main slideshow
	Slideshow.start({
		style: 'slide'
	});

	// Add newsletter signup handler
	$('#nl-form').submit(function() {

		// Validate
		var email = $(this).find('input[name=email]').val();
		if(!email.match(/^[^@]+@[^\.]+\..+$/)) {
			alert('Please enter a valid email address');
			return false;
		}
		$(this).find('input[name=email]').val('').blur();

		// Post
		$.post('/default.aspx', {email:email}, function(r) {
			$('#nl-form').css({opacity:1}).animate({opacity:0}, 250, function() {
				$(this).css({display:'none'});
				$('#nl-thanks').css({opacity:0, display:'block'}).animate({opacity:1}, 250, function() {
					setTimeout(function() {
						$('#nl-thanks').animate({opacity:0}, 250, function() {
							$(this).css({display:'none'});
							$('#nl-form').css({opacity:0, display:'block'}).animate({opacity:1}, 250);
						});
					}, 5000);
				});
			});
		});
		return false;
	});
});

/**
* Select a service tab.
*
* @param Event
* @return void
*/
function hSelectService(ev) {
	var slideId = $(this).attr('href').replace(/^#/, '');
	if(tabState.service==slideId) {
		return false;
	}
	var slide = $('#'+slideId);
	$('#home-services-container-inner div.services-tabs').removeClass('active');
	$(this).parents('div.services-tabs').addClass('active');
	$('#'+tabState.service).stop().animate({opacity:0}, 100, function() {
		$(this).css({display: 'none'});
		slide.stop().css({opacity:0, display:'block'}).animate({opacity:1}, 100);
	});
	tabState.service = slideId;
	$(this).blur();
	return false;
}

/**
* Select a bottom tab.
*
* @param Event
* @return void
*/
function hSelectBottom(ev) {
	var slideId = $(this).attr('href').replace(/^#/, '');
	if(tabState.bottom==slideId) {
		return false;
	}
	var slide = $('#'+slideId);
	$('#home-bottom-tabs-outer div.bottom-tabs a').removeClass('active');
	$(this).addClass('active');
	$('#'+tabState.bottom).stop().animate({opacity:0}, 100, function() {
		$(this).css({display: 'none'});
		slide.stop().css({opacity:0, display:'block'}).animate({opacity:1}, 100);
	});
	tabState.bottom = slideId;
	$(this).blur();
	return false;
}

/**
* Select a home tab.
*
* @param Event
* @return void
*/
function hSelectHome(ev) {
	var slideId = $(this).attr('href').replace(/^#/, '');
	if(tabState.home==slideId) {
		return false;
	}
	var slide = $('#'+slideId);
	$('#home-column-right-outer .home-mid-tabs a').removeClass('active');
	$(this).addClass('active');
	$('#'+tabState.home).stop().animate({opacity:0}, 100, function() {
		$(this).css({display: 'none'});
		slide.stop().css({opacity:0, display:'block'}).animate({opacity:1}, 100);
	});
	tabState.home = slideId;
	$(this).blur();
	return false;
}

/**
* Slideshow function library
*/
var Slideshow = {

	/**
	* @var HtmlElement Element containing the slideshow elements
	*/
	container: null,

	/**
	* @var int Timer
	*/
	timer: null,

	/**
	* @var int Currently active slide
	*/
	current: 0,

	/**
	* @var array Slides in the show
	*/
	slides: 0,

	/**
	* @var HtmlElement The element containing the slides
	*/
	slidesWrapper: null,

	/**
	* @var int Slide width
	*/
	slideWidth: 0,

	/**
	* @var array Holds references to slide buttons (if present)
	*/
	buttons: null,

	/**
	* @var string Style to use.
	*/
	slideStyle: 'slide',

	/**
	* Start the slideshow running
	*
	* @return void
	*/
	start: function(options) {

		if(options && options.style) {
			Slideshow.slideStyle = options.style;
		}

		// Get slideshow container
		Slideshow.container = $('#home-banner-area-inner');
		if(Slideshow.container.size()==0) {
			return;
		}
		Slideshow.slidesWrapper = $('#home-banner-container');
		Slideshow.slides = Slideshow.slidesWrapper.children('.home-banner');
		Slideshow.slideWidth = $(Slideshow.slides.get(0)).width();
		Slideshow.buttons = Slideshow.container.find('.slider-control ul li');

		// Handle handlers to elements
		Slideshow.buttons.each(function(i, el) {
			$(el).data('index', i);
		}).click(Slideshow.hButtonClick);

		// Setup styles for the chosen style
		if(Slideshow.slideStyle=='burn') {
			Slideshow.slides.css({display:'block', position:'absolute', left:0, top:0, 'z-index':1, opacity:0});
		}
		else {
			Slideshow.slides.css({display:'block'});
		}

		// Slide to the first slide in the show
		Slideshow.slideTo(0);

		// Start timer
		if(Slideshow.slides.length>1) {
			Slideshow.resetTimer();
		}
	},

	/**
	* Slides to the next slide in the show.
	*
	* @return void
	*/
	slideToNext: function() {
		var nextIndex = (Slideshow.current+1)%Slideshow.slides.size();
		Slideshow.slideTo(nextIndex);
	},

	/**
	* Slide to the specified slide.
	*
	* @param int Slide index
	* @return void
	*/
	slideTo: function(index, style) {

		// Style: slide
		style = style || Slideshow.slideStyle;
		if(style=='burn') {
			Slideshow.slidesWrapper.css({left:0});
			var ci = Slideshow.current;
			$(Slideshow.slides).each(function(i, el) {
				if(i!=index) $(el).css({opacity:0});
			});
			$(Slideshow.slides.get(Slideshow.current)).css({position:'absolute', left:0, top:0, 'z-index':1, opacity:1});
			$(Slideshow.slides.get(index)).css({position:'absolute', left:0, top:0, 'z-index':2, opacity:0}).stop().animate({opacity:1}, 1000, function() {
				if(ci!=index) {
					$(Slideshow.slides.get(ci)).css({opacity:0});
				}
			});
			Slideshow.buttons.removeClass('selected-control');
			$(Slideshow.buttons.get(index)).addClass('selected-control');
			Slideshow.current = index;
		}

		// Style: slide
		else {
			Slideshow.slidesWrapper.css({left:-Slideshow.current*960});
			for(var i=0; i<Slideshow.slides.size(); i++) {
				$(Slideshow.slides.get(i)).css({display: 'block', opacity:1, top:0, left:i*960});
			}
			Slideshow.slidesWrapper.stop().animate({left:-index*Slideshow.slideWidth});
			Slideshow.buttons.removeClass('selected-control');
			$(Slideshow.buttons.get(index)).addClass('selected-control');
			Slideshow.current = index;
		}
	},

	/**
	* Handle buttons clicks.
	*
	* @param Event
	* @return void
	*/
	hButtonClick: function(ev) {
		var index = $(this).data('index');
		if(index==Slideshow.current) {
			return false;
		}
		Slideshow.slideTo(index, 'slide');
		Slideshow.resetTimer();
		return false;
	},

	/**
	* Reset the slide timer.
	*
	* @return void
	*/
	resetTimer: function() {
		clearInterval(Slideshow.timer);
		Slideshow.timer = setInterval(Slideshow.slideToNext, 5000);
	}
};
