

var Nav = Class.create();

Nav.prototype = {
	initialize: function() {
		this.initialBackgorundPosition = $('navbarHolder').getStyle('backgroundPosition');
		if(this.initialBackgorundPosition.indexOf('px') == -1)
		{
			var tmp = this.initialBackgorundPosition.split(' ');
			tmp = tmp[0] + 'px ' + tmp[1] + 'px';
			this.initialBackgorundPosition = tmp;
		}
		Event.observe('navHome', 'mouseover', this.hover.bindAsEventListener(this,'navHome'));
		Event.observe('navHowItWorks', 'mouseover', this.hover.bindAsEventListener(this,'navHowItWorks'));
		Event.observe('navPurchase', 'mouseover', this.hover.bindAsEventListener(this,'navPurchase'));
		Event.observe('navCommunities', 'mouseover', this.hover.bindAsEventListener(this,'navCommunities'));
		Event.observe('navSignup', 'mouseover', this.hover.bindAsEventListener(this,'navSignup'));
		Event.observe('navAboutUs', 'mouseover', this.hover.bindAsEventListener(this,'navAboutUs'));
		
		Event.observe('navbar', 'mouseout', this.out.bindAsEventListener(this));
		this.hoverTimes    = 0;
		this.oldHoverTimes = 0;
	},
	
	hover: function(e, navItem) {
		this.hoverTimes++;
		var theNav = $('navbarHolder');
		switch(navItem)
		{
			case 'navHome' :
				theNav.style.backgroundPosition = '0px 0px';
				break;
			case 'navHowItWorks' :
				theNav.style.backgroundPosition = '0px -30px';
				break;
			case 'navPurchase' :
				theNav.style.backgroundPosition = '0px -60px';
				break;
			case 'navCommunities' :
				theNav.style.backgroundPosition = '0px -90px';
				break;
			case 'navSignup' :
				theNav.style.backgroundPosition = '0px -120px';
				break;
			case 'navAboutUs' :
				theNav.style.backgroundPosition = '0px -150px';
				break;
		}
	},

	out: function(e) {
		// console.log(this.hoverTimes);
		this.oldHoverTimes = this.hoverTimes;
		// console.log(this.oldHoverTimes);
		window.setTimeout(this.out_callback.bindAsEventListener(this), 100);
		// $('navbarHolder').style.backgroundPosition = this.initialBackgorundPosition;
	},
	
	out_callback: function() {
		if(this.oldHoverTimes == this.hoverTimes)
			$('navbarHolder').style.backgroundPosition = this.initialBackgorundPosition;
	}
};



var Signup = Class.create();

Signup.prototype = {
	initialize: function() {
		
	},
	
	submit: function(e) {
		Event.stop(e);
		if(Form.check('purchaseForm'))
			$('purchaseForm').submit();
	},
	
	averageUsageUpdate: function() {
		var multiplier = (Form.getRadioValue('purchaseForm', 'billing_type') == 'monthly') ? $F('kwh_multiplier') : 11 * $F('kwh_multiplier');
		if(!parseInt($F('average_usage')))
			$('totalBill').update('Invalid value.');
		else
		{
			var value = Math.round($F('average_usage') * multiplier);	
			$('totalBill').update('US$ ' + value);
		}
	},
	
	showLogin: function() {
		new Effect.BlindDown('loginContainer', {duration:0.4});
	}
};

var signup = new Signup();

