var Main = {
  position_comments: 0,
  
  generate: function(){
    Main.init_portal();
    Main.init_comments();
  },
  
  init_portal: function(){
    if($('portal')){
      Event.observe($('en'), 'mouseover', function(){Main.portal_show_en();});
      Event.observe($('zh'), 'mouseover', function(){Main.portal_show_zh();});
    }
  },
  
  portal_show_en: function(){
    $$('#portal .logo').first().setStyle({backgroundPosition:'left top'});
    $$('#portal .slogan').first().setStyle({backgroundPosition:'left top'});
    $('en').setStyle({backgroundPosition:'left bottom'});
    $('zh').setStyle({backgroundPosition:'left top'});
  },
  
  portal_show_zh: function(){
    $$('#portal .logo').first().setStyle({backgroundPosition:'left bottom'});
    $$('#portal .slogan').first().setStyle({backgroundPosition:'left bottom'});
    $('en').setStyle({backgroundPosition:'left top'});
    $('zh').setStyle({backgroundPosition:'left bottom'});
  },
    
  init_comments: function(){
    if($$('div.bubble-comments').first()){
      Event.observe($$('div.bubble-comments ul.navigation li.left').first(), 'click', function(){Main.previous_comment();});
      Event.observe($$('div.bubble-comments ul.navigation li.right').first(), 'click', function(){Main.next_comment();});      
    }  
  },
  
  previous_comment: function(){
    Main.move_comment(-1);
  },
  
  next_comment: function(){
    Main.move_comment(+1);
  },
  
  move_comment: function(move){
    var slider = $$('div.bubble-comments .comments .slider-comments').first();
    var position = Main.position_comments + move;
    var width_comment = 260;
    var total_comments = slider.select('div.comment').length;

    if(position < 0){
      position = total_comments-1;
    }else if(position >= total_comments){
      position = 0;
    }
    
    Main.position_comments = position;

    position *= width_comment;
    
    slider.setStyle({left:'-' + position + 'px'});
  }
  
}

Event.observe(window, 'load', Main.generate);