

var SlideShow = Class.create({
	initialize : function (initial, container, contents, delay, options) {
		this.initial = initial;
		this.container = container;
		this.contents = contents;
		this.delay = 9;
		this.fadeDuration = 0.8;
		this.showOnly(this.contents[this.initial]);
		this.current = initial;
		this.show = null;
		this.hide = null;
		
		this.options = Object.extend({
			prevToggler: '.prev',
			nextToggler: '.next',
			btnToggler: '.controls li'
		}, options || {});
		
		if (this.container.select(this.options.prevToggler).length > 0 && this.container.select(this.options.nextToggler).length > 0) {
			this.prevBtn = this.container.select('.prev')[0].addClassName('active');
			this.nextBtn = this.container.select('.next')[0].addClassName('active');
			this.prevBtn.observe('click', this.back.bindAsEventListener(this));
			this.nextBtn.observe('click', this.forward.bindAsEventListener(this));
			
			//handle ie6's lack of hover support on non-anchor elements
			if (Prototype.Browser.IE6) {
				this.prevBtn.observe('mouseenter', function () {
					this.addClassName('hover_prev');
				});
				this.prevBtn.observe('mouseleave', function () {
					this.removeClassName('hover_prev');
				});
			}
		}
		else if (this.container.select(this.options.btnToggler).length > 0) {
			this.buttons = this.container.select(this.options.btnToggler);
			this.buttons.each(function (el, i) {
				el.observe('click', function (ev) {
					ev.stop();
					this.pause();
					this.goTo(i, 0.5);
				}.bind(this));
			}.bind(this));
			
			this.buttons[this.current].addClassName('active');
			
		}
		else { 
			return;
		}
		
		document.observe('pop:active',  this.pause.bind(this));
		document.observe('pop:inactive',  this.start.bind(this));
		
		this.start();
	},
	start : function () {
		if (this.timer) { 
			return;
		}
		this.timer = setTimeout(this.next.bind(this), this.delay * 1000);
	},
	showOnly : function (el) {
		this.contents.invoke('hide');
		el.show();
	},
	pause : function () {
		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = false;
		}
		if (this.hide) {
			this.hide.cancel();
		}
		else {
			return;
		}
	},
	back: function (ev) {
		if (this.show) {
			this.show.cancel();
			return;
		}	
		if (this.hide) {
			this.hide.cancel();
			return;
		}	

		this.pause(); 
		if (this.current === 0) {
			this.goTo(this.contents.length - 1);
		}
		else {
			this.goTo(this.current - 1);
		}
		ev.stop(); 
	},
	forward: function (ev) {
		if (this.show) {
			this.show.cancel();
			return;
		}	
		if (this.hide) {
			this.hide.cancel();
			return;
		}	
		
		this.pause();
		this.next();
		ev.stop(); 
	}, 
	next: function () {
		this.timer = false;
		if (this.current === this.contents.length - 1) {
			this.goTo(0);
		}
		else {
			this.goTo(this.current + 1);
		}
		this.start();
	},	
	goTo: function (idx, dur) {
		if (idx === this.current) { 
			return;
		}
		this.hide = new Effect.Fade(this.contents[this.current], { duration: this.fadeDuration || 2, afterFinish: function () {
			this.hide = null;
		}.bind(this)});
		
		if (this.buttons) {
			this.buttons[this.current].removeClassName('active');
		}
		
		this.current = idx;
		this.show = new Effect.Appear(this.contents[this.current], { duration: this.fadeDuration || 2, afterFinish: function () {
			this.show = null; 
		}.bind(this)});
		
		if (this.buttons) {
			this.buttons[this.current].addClassName('active');
			this.start();
		}
	}
});

/*--------------------------------------------------------------------------*/

var BizFilingsHomepage = {
	BusinessTabs: function () {
		new AdvancedTabs($('business_services'));
	},
	BizSlideShow: function () {
		new SlideShow(0, $('slideshow'), $$('#slideshow .slide'), 8);
	},
	BizCarousel: function () {
		var timeout = setTimeout(function () {
			toutshow = new SlideShow(0, $('carousel_tout'), $$('#carousel_tout .slide'), 6);
		}, 800);
	},
	BizFilingsAdvantage: function () {
		if ($('tooltip1')) {
			new PopUp($('tooltip1_trigger'), $('tooltip1'));
		}
		if ($('tooltip2')) {
			new PopUp($('tooltip2_trigger'), $('tooltip2'));
		}
		if ($('tooltip3')) {
			new PopUp($('tooltip3_trigger'), $('tooltip3'));
		}
	},
	BizGetStarted: function () {
		$('getstarted_select_button').observe('click', selectRedirect);
		function selectRedirect(event) {
			this.href = "";
			value = $('getstarted_business_types').value;
			if ( value == "" ) {
			}
			else {
				this.href = value;
			}
		}	
	}
}

/*--------------------------------------------------------------------------*/

document.observe('dom:loaded', function () {
	if ($('business_services')) {
		BizFilingsHomepage.BusinessTabs();
	}
	
	if ($('slideshow')) {
		BizFilingsHomepage.BizSlideShow();
	}
	
	if ($('carousel_tout')) {
		BizFilingsHomepage.BizCarousel();
	}
	
	if ($('bizfilings_advantage')) {
		BizFilingsHomepage.BizFilingsAdvantage();
	}

	if ($('getstarted_select_button')) {
		BizFilingsHomepage.BizGetStarted();	
	}
});
