window.addEvent('domready', function() {

	$$('label.phantom').each( function(el){
		new PhantomLabels(el);
	});
	
	fixIE6Nav();

	var ticker = new BudgetCounter('ticker');

	var featured = new FeaturedSlide();

});


function fixIE6Nav(){
	if (Browser.Engine.trident && Browser.Engine.version <= 4) {
		$$('#header li')
		.addEvent('mouseenter', function(){ this.addClass('hover'); })
		.addEvent('mouseleave', function(){ if (this.hasClass('hover')) this.removeClass('hover'); });
	}
}

var BudgetCounter = new Class({

	initialize: function(el){
		this.ticker = $(el);

		var newdate = new Date();
		var year = newdate.getFullYear();
		var month = newdate.getMonth();

		if (month <= 5) {
			var fyear = (year - 1);
		} else {
			var fyear = year;
		}
		switch (fyear) {
			case 2008:
				this.start = 1214902799;
				this.increment = 0.658939173837;
				break;
			case 2009:
				this.start = 1246438799;
				this.increment = 0.329469590089;
				break;
			default:
				this.start = 1246438799;
				this.increment = 0.329469590089;
		}
		this.updateTicker();
	},

	updateTicker: function() {
	  if (!this.ticker) return;
	  var now = ((new Date()).getTime()/1000);
	  var amount = this.format( (now - this.start) * this.increment);
	  this.ticker.innerHTML = amount;
		setTimeout(this.updateTicker.bind(this), 50);
	},

	format: function(num) {
		num = num.toString().replace(/$|\,/g,'');
		if (isNaN(num)) num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100000+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if (cents<10) cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	}

});

var PhantomLabels = new Class({

	options: {
		className: 'hideme'
	},

	initialize: function(el){
		this.label = $(el);
		var element = this.label.get('for');
		this.input = $(element);
		if (this.input) {
			this.input.addEvent('focus', this.hideLabel.bind(this)).addEvent('blur', this.checkLabel.bind(this));
			this.checkLabel();
		}
	},

	hideLabel: function(){
		this.label.addClass(this.options.className);
	},

	checkLabel: function() {
		if (this.input.value === '') {
			if (this.label.hasClass(this.options.className))
				this.label.removeClass(this.options.className);
		} else {
			this.label.addClass(this.options.className);
		}
	}

});

var FeaturedSlide = new Class({

	options: {
		frameId: 'featuredframe',
		slideClass: 'slide',
		prevSel: '#prev',
		nextSel: '#next',
		waitTime: 5000
	},

	initialize: function(el) {
		this.frame = $(this.options.frameId);
		if (!this.frame) return false;
		if (this.options.changeHeight) {
			this.frame.set('morph', {duration: 'long'});
		}
		this.slides = this.frame.getElements('.'+this.options.slideClass);
		this.count = this.slides.length;
		this.prevlink = $$(this.options.prevSel);
		this.nextlink = $$(this.options.nextSel);
		this.auto = true;
		// don't show the arrows if there's only one image
		if (this.count > 1) {
			// assume there is only one previous and next, if at all
			this.prevlink.setStyle('display', 'block').addEvent('click', this.handleEvent.bindWithEvent(this, [false]));
			this.nextlink.setStyle('display', 'block').addEvent('click', this.handleEvent.bindWithEvent(this, [true]));
		} else {
			this.prevlink.remove();
			this.nextlink.remove();
			return false;
		}

		// Going to find the tallest slide. Starting at 0.
		this.tallestHeight = 0;
		this.slides.each( function(el) {
			var slideHeight = parseInt(el.getStyle('height'));
			if (slideHeight > this.tallestHeight) {
				this.tallestHeight = slideHeight;
			}
			if (el.hasClass('active')) {
				el.set({
					opacity: 1,
					styles: {
						position: 'absolute'
					},
					morph: {duration: 'long'}
				});
			} else {
				el.set({
					opacity: 0,
					morph: {duration: 'long'}
				});
			}
		}, this);
		if (this.options.changeHeight) {
			this.frame.setStyle('height', this.slides[0].getStyle('height'));
		} else {
			// Set the entire frame to the height of the tallest one.
			this.frame.setStyle('height', this.tallestHeight);
		}
		this.active = 0;
		this.current = this.slides[0];
		this.shifter = window.setTimeout(this.figureSlide.bind(this, [true]), this.options.waitTime);
	},

	handleEvent: function(e, next) {
		e.stop();
		this.auto = false;
		this.figureSlide(next);
	},

	figureSlide: function(next) {

		var old = this.active;

		// find the direction based on whether true or false was passed in
		if (next) {
			this.active++;
		} else {
			this.active--;
		}

		if (this.active < 0) this.active = (this.count - 1);
		if (this.active >= this.count) this.active = 0;
		if (this.options.changeHeight) {
			this.frame.morph({height: this.slides[this.active].getStyle('height')});
		}
		this.slides[old].morph({opacity: 0});
		this.slides[this.active].morph({opacity: 1});

		window.clearTimeout(this.shifter);
		if (this.auto) {
			this.shifter = window.setTimeout( this.figureSlide.bind(this, [true]), this.options.waitTime );
		}
	}

});

