var portfolio_gallery_widget = {
  gallery: null,
  entries: [],
  init: function(root_elm) {
    if (!root_elm) return;
    
    this.gallery = cssQuery('div.gallery', root_elm)[0];
    
    if (!this.gallery) return;
    
    this.link = cssQuery('div.link a', root_elm)[0];
    
    if (this.link)
      connect(this.link, 'onclick', function(e) {
        e.stopPropagation();
        e.preventDefault();
        
        window.open(getNodeAttribute(e.src(), "href"), "bd-portfolio");
        
        return false;
        
      });
    
    var body = cssQuery('div.body', root_elm)[0];
    
    setStyle(body, {'marginTop': (getElementDimensions(this.gallery).h + 60) + "px"});
    
    this.entries = cssQuery('li', this.gallery);
    
    this.entries.gotoPage = function(i) {
      if (!this[i]) return;
      
      if (typeof(this.current_idx) != 'undefined')
        removeElementClass(this[this.current_idx], "highlight");
      
      this.current_idx = i;
      addElementClass(this[this.current_idx], "highlight");
      
      window.setTimeout("portfolio_gallery_widget.entries.nextPage();", 2500);
      
    }
    
    this.entries.nextPage = function() {
      if (typeof(this.current_idx) == 'undefined') {
        this.gotoPage(0);
        
        return;
        
      }
      
      if (this.current_idx == this.length - 1) {
        this.gotoPage(0);
        
        return;
        
      }
      
      this.gotoPage(this.current_idx + 1);
      
    }
    
    this.entries.gotoPage(0);
    
  }
};

connect(window, 'onload', function(e) {
  portfolio_gallery_widget.init(getElement('content'));
});
