Landuse = function() {};

Landuse.menu = new Class({
	initialize: function(menu, props) {
		this.props = Object.extend({
			classes: ['toggler', 'expanded'],
			start: 5
		}, props || {});

		this.menu = $(menu);

		togglers = $ES('.' + this.props.classes[0], this.menu);
		togglers.each(function(a) {
			a.s = this.props.start;
			a.onclick = function() {
				ul = $E('ul', a.getParent());

				h = ul.scrollHeight + 'px';
				
				if (ul.getStyle('height').toInt() > 0) { // collapse
					e = 0;
					fx = { 'height': [h, '0px'] };
				}
				else { // expand
					e = 5;
					fx = { 'height': ['0px', h] };
				}

				ul.effects({transition: Fx.Transitions.quadInOut}).custom(fx);
				
				this.animate(a, e);				
			}.pass(a, this);
		}, this);
	},
	
	animate: function(a, e) {
		if (a.s == e && this.timeout) return $clear(this.timeout);
		
		if (e < a.s) a.s--;
		else a.s++
		
		a.setStyle('background-position', (a.s * -190 + 6) + 'px 3px');
		
		this.timeout = this.animate.delay(50, this, [a, e]);		
	}
});

Landuse.rating = new Class({
	initialize: function(input, props) {
		this.props = Object.extend({
			classes: ['star', 'set'],
			high: 5
		}, props || {});

		this.input = $(input);
		
		if (!this.input) return; // no form
		
		this.stars = new Array();

		for (i = 0; i < this.props.high; i++) {
			star = $(new Element('a'));
			star.addClass(this.props.classes[0]);
			star.onclick = (function (z) { return function() { this.set(z); }})(i).bind(this);
			star.injectAfter(this.input);

			this.stars[i] = star;
		}
		
		if (this.input.value.length == 0 || this.input.value == null) return; // don't set on first load in order to validate
		
		this.set(this.props.high - this.input.value);
	},

	set: function(n) {
		this.input.value = this.props.high - n;

		for (i = 0; i < this.props.high; i++) {
			star = $(this.stars[i]);
			
			if (i < n) star.removeClass(this.props.classes[1]);
			else if (i == n && star.hasClass(this.props.classes[1])) {
				star.removeClass(this.props.classes[1]);
				this.input.value = this.input.value - 1;
			}
			else star.addClass(this.props.classes[1]);
		}
	}
});