/* ON/OFF EFFECTS FOR THE NAV BAR */

Event.observe(window, 'load', function () {
	$$('#nav a').each(function (link) {
		var image = link.down('img');
		
		var normalUrl = image.src; 
		var activeUrl = image.src.replace(/(\..{3,4})$/, '-over.gif\1');
		
		var loadImage = new Image();
		loadImage.src = activeUrl;
		
		Event.observe(link, 'mouseover', function (e) {
			image.src = activeUrl;
		});
		
		Event.observe(link, 'mouseout', function (e) {
			image.src = normalUrl;
		});
	});
});

/* ON/OFF EFFECTS FOR THE CAREER NAV BAR */

Event.observe(window, 'load', function () {
	$$('#careerNav a','#careerNav2 a').each(function (link) {
		var image = link.down('img');
		
		var normalUrl = image.src; 
		var activeUrl = image.src.replace(/(\..{3,4})$/, '-over.jpg\1');
		
		var loadImage = new Image();
		loadImage.src = activeUrl;
		
		Event.observe(link, 'mouseover', function (e) {
			image.src = activeUrl;
		});
		
		Event.observe(link, 'mouseout', function (e) {
			image.src = normalUrl;
		});
	});
});

/* SEARCH BAR */

Event.observe(window, 'load', function () {
	$$('input.search').each(function (input) {
		Event.observe(input, 'click', function () {
			if(input.value == 'Search') input.value = '';
		});
		Event.observe(input, 'blur', function () {
			if(input.value == '') input.value = 'Search';
		});
	});
});

/* HANDLES PHYSICS FOR THE VARIOUS SLIDING/EXPANDING ELEMENTS */

var NumberSlider = Class.create();
NumberSlider.prototype = {

	initialize: function (options) {
		this.options = Object.extend({
			startValue:		0,
			resolution:		3
		}, options || {});
		this.value = this.options.startValue;
		this.target = this.value;
		this.lastChange = 0;
		this.moving = false;
	},
	
	slideTo: function (target) {
		this.target = target;
		this.lastChange = 0;
		this.moving = true;
	},
	
	jumpTo: function (target) {
		this.target = target;
		this.moving = false;
		this.value = target;
	},
	
	step: function () {
		var change = (this.target - this.value) / this.options.resolution;
		if (Math.abs(change) < 1 && change != 0)
			change = this.target - this.value;
		change = parseInt(change);
		
		this.value += change;
		this.lastChange = change;
		this.moving = (this.value != this.target);
		
		return !this.moving;
	}

};

/* IMAGE TOGGLE CONTROL */
var ImageToggle = Class.create();
ImageToggle.prototype = {

	initialize: function (element, field, value) {
		this.element = $(element);
		this.field = $(field);
		this.value = value;
		this.on = false;
		
		this.element.addClassName('imageToggle');
		if (this.values().include(this.value)) {
			this.on = true;
			this.element.addClassName('imageToggleOn');
		}
		
		Event.observe(this.element, 'click', this.toggle.bind(this));
		Event.observe(this.element, 'mouseover', this.mouseover.bind(this));
		Event.observe(this.element, 'mouseout', this.mouseout.bind(this));
	},
	
	mouseout: function () {
		this.element.removeClassName('imageToggleHover');
	},
	
	mouseover: function () {
		this.element.addClassName('imageToggleHover');
	},
	
	toggle: function () {
		var values = this.values();
		if (this.on) {
			this.on = false;
			values = values.without(this.value);
			this.element.removeClassName('imageToggleOn');
		} else {
			this.on = true;
			values.push(this.value);
			this.element.addClassName('imageToggleOn');
		}
		this.field.value = values.join(', ');
	},
	
	values: function () {
		return this.field.value ? this.field.value.split(', ') : [];
	}
};

/* EXPANDING/CONTRACTING FOR THE MENU */

var ExpandingMenu = Class.create();
ExpandingMenu.prototype = {
	
	initialize: function (element) {
		this.element = $(element);
		this.headerElement = this.element.down('strong');
		
		this.expandedHeight = this.element.getHeight();
		this.collapsedHeight = this.headerElement.getHeight();
		
		this.element.style.overflow = 'hidden';
		
		this.urls = $A(this.element.getElementsByTagName('a')).collect(function (elem) { return elem.href; });
		
		var active = this.urls.include(window.location.href);
		if (active)
			this.element.addClassName('expanded');
		
		this.height = new NumberSlider({startValue:
			(this.persistant() || active) ?
			this.expandedHeight :
			this.collapsedHeight});
		
		this.updateHeight();
		
		ExpandingMenu.menus.push(this);	
		
		Event.observe(this.element, 'mouseover', this.expand.bind(this));
	},
	
	expand: function () {
		var skip = this;
		ExpandingMenu.menus.each(function (menu) {
			if (skip != menu)
				menu.collapse();
		});
		this.element.addClassName('expanded');
		this.height.slideTo(this.expandedHeight);
		ExpandingMenu.setTimer();
	},
	
	collapse: function () {
		this.element.removeClassName('expanded');
		if (!this.persistant()) {
			this.height.slideTo(this.collapsedHeight);
			ExpandingMenu.setTimer();
		}
	},
	
	tick: function () {
		this.height.step();
		this.updateHeight();
		return this.height.moving;
	},
	
	updateHeight: function () {
		this.element.style.height = this.height.value + 'px';
	},
	
	persistant: function () {
		return this.element.hasClassName('persistant');
	}

};

ExpandingMenu.menus = [];

ExpandingMenu.setTimer = function () {
	if (!ExpandingMenu.timer)
		ExpandingMenu.timer = setTimeout(ExpandingMenu.tick, 50);
};

ExpandingMenu.tick = function () {
	ExpandingMenu.timer = null;
	var finished = true;
	ExpandingMenu.menus.each(function (menu) {
		if (menu.tick())
			finished = false; 
	});
	if (!finished)
		ExpandingMenu.setTimer();
};

Event.observe(window, 'load', function () {
	ExpandingMenu.currentUrl = location.href.replace(/.*\//, '');
	$$('#subnav .section').each(function (element) {
		new ExpandingMenu(element);
	});
});
