window.addEvent('domready', function() {
  $$('#menu li').each(function(item) {
    if(!item.hasClass('akvaario')) {
      new ScrollHover(item.getElement('a'));
    }
  });
  
  $$('.scrollHover').each(function(hover) {
    new ScrollHover(hover);
  });
  
  
  var muokkaa_perustietoja = $('muokkaa_perustietoja');
  if(muokkaa_perustietoja) {
    muokkaa_perustietoja.addEvent('click', function() {
      this.getParent().getNext().toggleClass('groupDataEdit');
    });
  }
  
  var muokkaa_lisatietoja = $('muokkaa_lisatietoja');
  if(muokkaa_lisatietoja) {
    muokkaa_lisatietoja.addEvent('click', function() {
      this.getParent().getNext().toggleClass('groupDataEdit');
    });
  }
  
  var vaihda_salasanaa = $('vaihda_salasanaa');
  if(vaihda_salasanaa) {
    vaihda_salasanaa.addEvent('click', function() {
      $('overlayer').setStyle('display', 'block');
      $('passwordChanger').setStyle('display', 'block');
      $('editPhoto').setStyle('display', 'none');
    });
  }
  
  var vaihda_kuvaa = $('vaihda_kuvaa');
  if(vaihda_kuvaa) {
    vaihda_kuvaa.addEvent('click', function() {
      $('overlayer').setStyle('display', 'block');
      $('passwordChanger').setStyle('display', 'none');
      $('editPhoto').setStyle('display', 'block');
    });
  }
  
  
  /* Autoresize for textareas */
  
  $$('textarea.autosize').each(function(textarea) {
    var size = textarea.get('rows');
    
    var calculate = function() {
      var lines = (textarea.value.length / 0.17 / textarea.getSize().x) + textarea.value.split(/\n/).length;
      
      if(Browser.Engine.trident) {
        lines += 2;
      }
      
      textarea.set('rows', lines.round().limit(size, 20));
    }
    
    textarea.addEvent('keyup', calculate);
    
    calculate();
  });
  
  
  /* Blockquote hide */
  
  $$('#forum blockquote').each(function(b) {
    var y = b.getSize().y - 20;
    
    if(y/17 > 6) {
      b.setStyles({
        'max-height': 17*4,
        'overflow': 'hidden'
      });
      
      var a = new Element('a', {
        'class': 'read-more',
        'text': '(Näytä lisää)'
      }).inject(b, 'after');
      
      a.addEvent('click', function() {
        b.erase('style');
        this.destroy();
      });
    }
  });
  
  
  /* Quote */
  
  if($('addYours') && !Browser.Engine.trident) {
    $$('table.discussion').each(function(table) {
      var q;
      
      table.getElements('td')[1].addEvent('mouseup', function() {
        var sel = document.getSelection().trim();
        
        $$('a.quote-this').destroy();
        
        if(sel.length > 0) {
          q = new Element('a', {'text': 'Lainaa viestiä', 'class': 'quote-this'}).inject(table.getParent());
          q.addEvent('click', function() {
            var t = $('add').getElement('textarea');
            
            if(t.value.length > 0) {
              t.value += "\n";
            }
            
            t.value += '<blockquote>'+sel+'</blockquote>'+"\n";
            
            document.location = '#addYours';
            
            t.focus();
            t.selectionStart = t.value.length;
            t.scrollTop = 10000000;
            
            t.fireEvent('keyup');
            
            this.destroy();
          });
        }
      });
    });
  }
});


var ScrollHover = new Class({
  options: {
    height: 0,
    direction: 'up',
    duration: 500,
    transition: Fx.Transitions.Back.easeOut
  },
  initialize: function(element, options) {
    this.element = $(element);
    this.setOptions(options);
    
    if(this.options.height === 0) {
      this.options.height = this.element.getHeight();
    }
    
    this.fx = new Fx.Morph(this.element, {
      duration: this.options.duration,
      link: 'cancel',
      transition: this.options.transition
    }).set({'background-position': '0 0'});
    
    var self = this;
    
    this.element.addEvents({
      'mouseover': function() {
        self.fx.start({'background-position': '0 '+ self.options.height + 'px'});
      },
      'mouseout': function() {
        self.fx.start({'background-position': '0 0'});
      }
    });
  }
});

ScrollHover.implement(new Options);


function bbs_lukitse(id) {
  if(confirm("Haluatko varmasti lukita keskustelun?")) {
    document.location = '/foorumi/admin/lukitse/'+id;
  }
  else {
    return(false);
  }
}

function bbs_avaa(id) {
  if(confirm("Haluatko varmasti avata keskustelun?")) {
    document.location = '/foorumi/admin/avaa/'+id;
  }
  else {
    return(false);
  }
}

function bbs_poista_keskustelu(id) {
  if(confirm("Haluatko varmasti poistaa koko keskustelun?\n\nHuom! Kaikki viestit menetetään") && confirm('Aivan varmasti?')) {
    document.location = '/foorumi/admin/poista_keskustelu/'+id;
  }
  else {
    return(false);
  }
}

function bbs_poista_viesti(id, element) {
  if(confirm('Haluatko varmasti poistaa viestin?')) {
    new Request({
      url: '/foorumi/admin/poista_viesti/'+id,
      method: 'get'
    }).send();
    
    element.getParents('table.discussion').getParent().destroy();
  }
  
  return(false);
}


function openChat() {
	chatWindow = window.open("/chat/","chat","toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=no,width=800,height=550,left=25,top=25");
	chat.focus();
	return false;
}