/** 
* @projectDescription	Product-specific javascript
*
* @id	landing.js
*/

var TabStateSelect = Class.create({
    initialize: function(container) {
        this.container = $(container);
        this.container.select('button').invoke('observe', 'click', this.handler.bind(this));
    },
    handler: function(ev) {
        if(ev)
            ev.stop();
        
        var new_url = "";
        el = ev.element();
        // get the dropdown value
        state = el.up().down('select').getValue();
        // CHeck the active class
        if ($('options_nav_llc').hasClassName("active"))
            new_url = "/limited-liability-company-package.aspx?state=" + state;
        else if ($('options_nav_scorp').hasClassName("active"))
            new_url = "/s-corporation-package.aspx?state=" + state;
        else if ($('options_nav_ccorp').hasClassName("active"))
            new_url = "/c-corporation-package.aspx?state=" + state;
        else if ($('options_nav_nonprofit').hasClassName("active"))
            new_url = "/nonprofit-package.aspx?state=" + state;

        window.location = new_url;
    }
});

/*--------------------------------------------------------------------------*/

var OverviewTabs = Class.create({
	initialize: function (container, options) {
		this.container  = $(container);
		this.togglers = this.container.select('.tabs li');
		this.tabs = this.container.select('.tab');
		this.extraContent = this.container.down('.col_content');
		this.hasSlid = false;
		
		this.options = Object.extend({
			closeSelector: '.close'
		}, options || {});
		
		if (navigator.appVersion.indexOf("Mac")!=-1) { 
			if (Prototype.Browser.WebKit) {
				this.container.addClassName('safari'); 
			}
			else {
				this.container.addClassName('mac'); 
			}
		}
		
		this.setup();
	},
	setup: function () {
		this.togglers.each(function (el, i) {
			el.observe('click', this.dispatch.bindAsEventListener(this, i));
		}.bind(this));
		this.container.select(this.options.closeSelector).each(function (el, i) {
			el.observe('click', this.close.bindAsEventListener(this, i));
		}.bind(this));
		if (Prototype.Browser.IE6 || Prototype.Browser.IE7) { this.toggleTout(); }
	},
	dispatch: function (ev, i) {
		if (!this.hasSlid) {
			this.slide(ev, i);
			this.hasSlid = true;
		}
		else {
			this.show(ev, i);
		}
	},
	slide: function (ev, i) {
		this.togglers.invoke('removeClassName', 'active');
		this.togglers[i].addClassName('active');
		this.tabs.invoke('hide');
		this.tabs.invoke('removeClassName', 'active');
		this.tabs[i].addClassName('active');
		
		Effect.SlideDown(this.tabs[i], { duration: 0.5 });
		this.hasSlid = true;
		
		if (ev) {
			ev.stop();
		}
	},
	show: function (ev, i) {	
		this.togglers.invoke('removeClassName', 'active');
		this.togglers[i].addClassName('active');
		this.tabs.invoke('hide');
		this.tabs.invoke('removeClassName', 'active');
		this.tabs[i].addClassName('active').show();

		if (ev) {
			ev.stop();
		}
	},
	close: function (ev, i) {
		this.tabs.each(function (el) {
			if (el.hasClassName('active')) {
				Effect.SlideUp(el, { 
					duration: 0.5
				});
			}
			else {
				el.hide();
			}

			this.togglers.invoke('removeClassName', 'active');
			//this.tabs.invoke('removeClassName', 'active');
		}.bind(this));
		
		setTimeout(function () {

		}.bind(this), 10);
		
		this.hasSlid = false;
		
		if (ev) {
			ev.stop();
		}
	},
	moveExtra: function (el, y_coord) {
		if (this.hasSlid) {
			el.setStyle({ top: y_coord + 'px' });
		}
		else {
			new Effect.Morph(el, {
				style: 'top:' + y_coord + 'px;',
				duration: 0.5
			});
		}
	},
	toggleTout: function () {
		/* ie6 only */
		var selectbox = $('main').down('.selectbox');
		if ($('questions_tout')) {
			var tout = $('questions_tout');
			
			document.observe('pop:active', function () {
				tout.setStyle({ 'visibility': 'hidden' });
			});
			document.observe('pop:inactive', function () {
				tout.setStyle({ 'visibility': 'visible' });
			});	
		};
	}
});

/*--------------------------------------------------------------------------*/

document.observe('dom:loaded', function () {
	var incorporationOptionsInner = $('incorporation_landing_inner');
	var bizfilingsServicesInner = $('bizfilings_services_inner');
	var incorporationLanding = $('incorporation_landing_inner');
	
	if (incorporationOptionsInner) {
		tabsService = new AdvancedTabs(incorporationOptionsInner);
		tabDropdowns = new TabStateSelect(incorporationOptionsInner);
	}
	if (bizfilingsServicesInner) {
		tabsManaging = new AdvancedTabs(bizfilingsServicesInner);
	}
	
	/* incorporation overview */
	if (incorporationLanding) {
		new OverviewTabs(incorporationLanding);
		new PopUp($('business_types'), $('business_types_menu'));
	}
	
	$('landing_product_submit').observe('click', function() {
		var product_url = $('landing_product_select').getValue();
		if (product_url == "") {
			// show error?
			$('landing_product_select').addClassName('error');
		}
		else {
			window.location.href = '/' + product_url;
		}
	}.bindAsEventListener());
	
	$('landing_state_select').observe('change', function() {
		var state_url = $('landing_state_select').getValue();
		if (state_url != "") {
			window.location.href = state_url;
		}
		else {
			// show error
		}
	}.bindAsEventListener());

	/* tooltips */
	new GlobalToolTip($('main').down('.col_content'));
	
	
});
