var HomeTabs = Behavior.create({
  initialize:function() {
    this.links = $$('#home_tabs li a');
    this.fixLinksHeight();
    this.contents = $$('#home_maintext div.content');
    this.hideContents();
    if ($$('#home_tabs li.on a').first()) {
        this.switchTo($$('#home_tabs li.on a').first().id.replace('home_tabs_link_',''));
    } else {
        this.switchTo(1);
    }
    
  },
  
  fixLinksHeight:function() {
    var maxHeight=0;
    this.links.each(function(element){
        maxHeight=Math.max(maxHeight,element.getHeight());
    });
    this.links.each(function(element){
        if (element.getHeight()<maxHeight) {
            pixels=maxHeight-element.getHeight();
            plusPaddingTop=parseInt(pixels/2);
            plusPaddingBottom=parseInt(pixels/2);
            if (pixels%2!=0) plusPaddingTop++;
            element.setStyle('padding-top: '+(plusPaddingTop+parseInt(element.getStyle('padding-top')))+'px');
            element.setStyle('padding-bottom: '+(plusPaddingBottom+parseInt(element.getStyle('padding-bottom')))+'px');
        }
    });
  },
  
  
  hideContents:function() {
    this.contents.each(function(element){ element.hide() });
  },
  
  clearLiOn:function() {
      this.links.each(function(element){ element.up('li').removeClassName('on') });
  },
  
  onclick:function(e) {
    var element = Event.element(e);
    if (element.id != null) {
      Event.stop(e);
      element.blur();
      this.switchTo(element.id.replace('home_tabs_link_',''));
    }
  },
  
  current:function() {
    return $$('#home_maintext div.current').first();
  },
  
  switchTo:function(index) {
    var li=$('home_tabs_link_'+index).up('li');
    this.clearLiOn();
    li.addClassName('on');
    
    this.killCurrent();
    
    var element=$('home_tabs_content_'+index);
    element.addClassName('current');
    typeof(Effect)!='undefined'?Effect.Appear(element,{duration:0.5,queue:'end'}):element.show();
  },
  
  killCurrent:function() {
    var current = this.current();
    if (current) {
      current.removeClassName('current');
      typeof(Effect)!='undefined'?Effect.Fade(current,{duration:0.5, queue:'end'}):current.hide();
    }    
  }
  
});

Event.addBehavior({'#home_tabs':HomeTabs});