var Ticker = new Class({
	setOptions: function(options) {
		this.options = Object.extend({
			speed: 1000,
			delay: 4000,
			direction: 'vertical',
			onComplete: Class.empty,
			onStart: Class.empty
		}, options || {});
	},
	initialize: function(el,options){
		this.setOptions(options);
		this.el = $(el);
		this.items = this.el.getElements('li');
		var w = 0;
		var h = 0;
		if(this.options.direction.toLowerCase()=='horizontal') {
			h = this.el.getSize().size.y;
			this.items.each(function(li,index) {
				w += (li.getSize().size.x + 20);
			});
		} else {
			w = this.el.getSize().size.x;
			this.items.each(function(li,index) {
				h += li.getSize().size.y;
			});
		}
		this.el.setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			width: w,
			height: h
		});
		this.fx = new Fx.Styles(this.el,{duration:this.options.speed,onComplete:function() {
			var i = (this.current==0)?this.items.length:this.current;
			this.items[i-1].injectInside(this.el);
			this.el.setStyles({
				left:0,
				top:0
			});
		}.bind(this)});
		this.current = 0;
    this.next.bind(this).delay(this.options.delay+this.options.speed);
	},
	next: function() {
		this.current++;
		if (this.current >= this.items.length) this.current = 0;
		var pos = this.items[this.current];
		this.fx.start({
			top: -pos.offsetTop,
			left: -pos.offsetLeft
		});
		this.next.bind(this).delay(this.options.delay+this.options.speed);
	}
});


window.addEvent('domready', function(){
  new Ticker('tweets',{speed:1000,delay:4000,direction:'horizontal'});
  $('shrink_form').onsubmit = shrink_form_submit;
  $ES('form')[1].onsubmit = validate_length;
  max_length = 140;
  counter = $('counter');
  text_field = $('shrunken_text_field');
  text_field.onkeyup = function(event){ check_length() };
  text_field.onblur = function(event){ check_length() };
  // setInterval ( "get_new_tweets()", 20000 );
});

function shrink_form_submit() {
  new Ajax('/shrink', {method: 'post', data: 'text=' + $('text').value, onComplete: update_div.bindAsEventListener(this)}).request();
  return false;
}

function get_new_tweets() {
  new Ajax('/tweets', {method: 'post', evalScripts: true, evalResponse: true, onComplete: update_tweets.bindAsEventListener(this)}).request();
}

function update_div(resp) {
  var json = eval('('+resp+')');
  text_field.value = json.text;
  $('shrunken_text_difference').innerHTML = json.difference;
  text_field.focus();
  text_field.select();
  check_length();
  $('shrunken_text_wrapper').setStyle('opacity', 1);
}

function update_tweets(partial) {
  $('tweets').innerHTML = partial;
  new Ticker('tweets',{speed:1000,delay:4000,direction:'horizontal'});
}

function check_length() {
  var length = text_field.value.length;
  counter.innerHTML = (max_length - length);
  
  if (length > max_length && !counter.className.match(/red/)) {
    counter.className = 'red';
    $('twitter_submit').disabled = true;
    return false;
  } else if (length <= max_length && counter.className.match(/red/)) {
    counter.className = 'green';
    $('twitter_submit').disabled = false;
    return true;
  }
}

function validate_length() {
  if (text_field.value.length < 1) {
    return false; 
  }
}
