Element.implement({
	toggle: function() {
		if (this.getStyle('display') != 'none') this.setStyle('display','none');
		else this.setStyle('display','');
	},	
	toggleVis: function() {
		if (this.getStyle('visibility') != 'hidden') this.setStyle('visibility','hidden');
		else this.setStyle('visibility','visible');
	},	
	hide: function() {
		this.setStyle('display', 'none');
	},	
	show: function() {
		this.setStyle('display', '');
	},
	isShowing: function() {
		return this.getStyle('display') != 'none';
	},
	moveToBottom: function() {
		var w = this.clone(true, true);
		this.destroy();
		$(document.body).adopt(w);
		return w;
	},
	disable: function(txt)	{
		if( this.toString().indexOf('HTMLInput') > 0 ) {
			if(txt) this.value = txt;
			this.disabled = true;
			return;
		}
		
		$$('#' + this.id + ' select, #' + this.id + ' input').setProperty('disabled', true);
		
		if( $(this.id + '_disable') ) {
			$(this.id + '_disable').destroy();
		}
		
		this.setStyle('position', 'relative');
		if( !$(this.id + '_disable') ) {
			var coords = this.getSize();
			this.adopt(
				new Element('div', {
					'id': this.id + '_disable',
					'styles': {
						'background-color': !txt ? '#fff' : '#' + txt,
						'width': coords.x,
						'height': coords.y,
						'top': '0',
						'left': '0',
						'position': 'absolute',
						'opacity': 0.7
					}
				})
			);
		}
	},
	
	enable: function(txt) {
		if( this.toString().indexOf('HTMLInput') > 0 ) {
			this.disabled = false;
			if(txt) this.value = txt;
			return;
		}
		
		$$('#' + this.id + ' select, #' + this.id + ' input').setProperty('disabled', false);
		
		this.setStyle('position', '');
		if( $(this.id + '_disable') ) {
			$(this.id + '_disable').destroy();
		}
	},
	
	setWidth: function(num) {
		this.setStyle('width', num.toString() + 'px');
	},
	
	setHeight: function(num) {
		this.setStyle('height', num.toString() + 'px');
	},
	
	getWidth: function() {
		return this.isShowing() ? this.getSize().x : 0;
	},
	
	getHeight: function() {
		return this.isShowing() ? this.getSize().y : 0;
	},
	
	getX: function() {
		return this.getPosition().x;
	},
	
	setX: function(num) {
		this.setStyle('left', Math.round(num.toInt()) + 'px');
	},
	
	getY: function() {
		return this.getPosition().y;
	},
	
	setY: function(num) {
		this.setStyle('top', Math.round(num.toInt()) + 'px');
	},
	
	setPosition: function(x, y) {
		this.setX(x);
		this.setY(y);
	},
	
	getBottomY: function() {
		return this.getHeight() + this.getPosition().y;
	}
});

var Utils = 
{
	urlencode: function(obj)
	{
		query = '';
		for (var name in obj) {
			query += '&' + name + '=' + obj[name];
	    };
	
		return query;
	}
};

/* TODO
var Ajax = new Class({
	
	request: false,
	
	initialize: function()
	{
		this.request = 
	},
	
	
});*/

var Poll = new Class({
	options: {
		bind: false, 
		interval: 541, 
		allow_empty: false
	}, 
	
	initialize: function (fields, fn, options) {
		this.setOptions(options);
		this.fields = [];
		this.is_polling = false;
		
		if ($type(fields) == "array") {
			fields.each(function (item, index) {
				this.fields.push($(item));
			}, this);
		} else {
			this.fields = [$(fields)];
		}
		
		if (!this.fields[0]) {
			return false;
		}
		
		this.onChange = this.options.bind ? fn.bind(this.options.bind) : fn;this.cur_text = this.getValue();this.start();
	}, 
	
	stop: function() {
		this.is_polling = false;
		$clear(this.period);
	}, 
	
	start: function() {
		if (!this.is_polling) {
			this.is_polling = true;
			this.period = this.poll.periodical(this.options.interval, this);
		}
	}, 
	
	resetTime: function() {
		this.stop();this.start();
	}, 
	
	getValue: function() {
		var text = "";this.fields.each(function (item, index) {text += item.value;});return text;
	}, 
	
	poll: function() {var text = this.getValue();var actual_text = this.fields[0].value;if ((this.options.allow_empty || actual_text != "") && this.cur_text != text) {this.cur_text = text;this.onChange(actual_text, this.options);}}
	
});

Poll.implement(new Options);
