var ResizingTextArea = Class.create();

ResizingTextArea.prototype = {
    defaultRows: 1,

    initialize: function(field)
    {
        this.defaultRows = Math.max(field.rows, 1);
        this.resizeNeeded = this.resizeNeeded.bindAsEventListener(this);
        Event.observe(field, "click", this.resizeNeeded);
        Event.observe(field, "keyup", this.resizeNeeded);
    },

    resizeNeeded: function(event)
    {
        var t = Event.element(event);
        var lines = t.value.split('\n');
        var newRows = lines.length + 1;
        var oldRows = t.rows;
        for (var i = 0; i < lines.length; i++)
        {
            var line = lines[i];
            if (line.length >= t.cols) newRows += Math.floor(line.length / t.cols);
        }
        if (newRows > t.rows) t.rows = newRows;
        if (newRows < t.rows) t.rows = Math.max(this.defaultRows, newRows);
    }
}

var Selectable = Class.create({
  initialize: function(selectId)
  {
    this.select = $(selectId);
  },
  getSelectedValue: function()
  {
    return this.select.selectedIndex;
  }
});

var SelectBlockType = Class.create(Selectable, {
  change: function()
  {
    for(i = 0; i < this.select.length; i++)
    {
      if(i == this.select.selectedIndex) $($('blockType' + this.select.options[i].value)).show();
      else $($('blockType' + this.select.options[i].value)).hide();
    }
  }
});

var LoginForm = Class.create({
  form: null,
  usernameDefault: 'username',
  passwordDefault: '********',
  initialize: function(formId)
  {
    this.form = $(formId);
    Event.observe('UserEmail', 'focus', this.focusUsername);
    Event.observe('UserEmail', 'blur', this.blurUsername);
    Event.observe('UserPassword', 'focus', this.focusPassword);
    Event.observe('UserPassword', 'blur', this.blurPassword);
    Event.observe(window, 'load', function(){Form.focusFirstElement(formId);});
    Event.observe('UserEmail', 'keypress', function(event){if(event.keyCode == Event.KEY_RETURN)$(formId).submit();});
    Event.observe('UserPassword', 'keypress', function(event){if(event.keyCode == Event.KEY_RETURN)$(formId).submit();});
  },
  focusUsername: function(event)
  {
    username = Event.element(event);
    if(username.value == 'username') username.value = '';
  },
  blurUsername: function(event)
  {
    username = Event.element(event);
    if(username.value == '') username.value = 'username';
  },
  focusPassword: function(event)
  {
    password = Event.element(event);
    if(password.value == '********') password.value = '';
  },
  blurPassword: function(event)
  {
    password = Event.element(event);
    if(password.value == '') password.value = '********';
  }
});

var Togglable = Class.create({
  t: null,
  textA: null,
  textB: null,
  classT: null,
  classSelector: null,
  initialize: function(tId)
  {
    this.t = tId;
    Event.observe(this.t, 'click', this.toggle);
  },
  toggleText: function(element,a,b)
  {
    e.innerHTML == a ? e.update(b) : e.update(a);
  },
  toggleClass: function(elements,className)
  {
    $A($$(elements)).map(function(e){e.toggleClassName(className);});
  }
});

var EditModeButton = Class.create(Togglable, {
  initialize: function(tId,textA,textB,classT,classSelector)
  {
    this.t = tId;
    this.textA = textA;
    this.textB = textB;
    this.classT = classT;
    this.classSelector = classSelector;
    Event.observe(this.t, 'click', this.toggle.bindAsEventListener(this));
  },
  toggle: function(event)
  {
    e = Event.element(event);
    this.toggleText(e, this.textB, this.textA);

    $A($(this.classSelector).select('div.newsBlock')).map(function(z){
      if(z.hasClassName('block')){ z.removeClassName('block'); z.addClassName('previewBlock'); }
      else{ z.removeClassName('previewBlock'); z.addClassName('block'); }
    });
  }
});

var URLValidator = Class.create({
  form: null,
  initialize: function(formId)
  {
    this.form = $(formId);
    Event.observe(window, 'load', this.refresh.bindAsEventListener(this));
    Event.observe('URL', 'focus', this.refresh.bindAsEventListener(this));
    Event.observe('URL', 'blur', this.refresh.bindAsEventListener(this));
    Event.observe('URL', 'change', this.refresh.bindAsEventListener(this));
    Event.observe('URL', 'keyup', this.refresh.bindAsEventListener(this));
  },
  refresh: function(event)
  {
  	e = Event.element(event);
  	if(this.checkURL(e.value))
  	{
  		if($($('URL').hasClassName('urlCheckFailed'))) $($('URL').removeClassName('urlCheckFailed'));
  		$($('LinkBlockFormSubmit')).enable();
  	}
  	else
  	{
  		$($('URL')).addClassName('urlCheckFailed');
  		$($('LinkBlockFormSubmit')).disable();
  	}
  },
  checkURL: function(url)
  {
	return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
  }
});

