var McArtzt = {
  Gallery : {
    // Initialize the gallery DIVs
    init : function() {
      
      // get each gallery div
      $$('div.bb_gallery').each(function(item) {

        //get the anchors and put an observe click event
        var anchors = item.select('a');
        anchors.each(function(a){a.observe('click', McArtzt.Gallery.click);});

        //push in the divs and stuff for the show setup
        var imgCurrent = Builder.node('img',{src:"images/spacer.gif",className:"current"});
        var bbShow = Builder.node('div',{className:"bb_show"},[
            Builder.node('div',{className:'caption'}),
            Builder.node('div',{className:'last'},Builder.node('img',{src:"images/spacer.gif",className:"last"})),
            Builder.node('div',{className:'current'},imgCurrent)
          ]);
        item.insert({top: bbShow});
        
        // Add the event handler for the gallery image
        $(imgCurrent).observe('load',McArtzt.Gallery.loaded.bind(imgCurrent));
         
        // Push the topmost UL element into the gallery
        var first = anchors[0];
        if(!first)
          return;
          
        McArtzt.Gallery.insert($(bbShow), first);
      });
    },
    
    // handle a click
    click : function(event) {
      // get the anchor and stop the event chain 
      var src = event.findElement('a');
      if(!src)
        return false;
      event.stop();
      
      // make sure we're not reloading the same image
      var cur = event.findElement('div.bb_gallery').select('img.current');
      if(cur[0] && cur[0].src==src.href)
        return false;
      
      // add the new image 
      var show = event.findElement('div.bb_gallery').select('div.bb_show')[0];

      McArtzt.Gallery.insert(show, src);
      
      return false;
    },
    
    // Swap in the image with the specified "src" 
    insert : function(dest, src) {
      var caption_text = src.nextSibling.innerHTML;
      
      var caption = dest.select('div.caption')[0];
      var current = dest.select('img.current')[0];
      var last = dest.select('img.last')[0];
      
      caption.innerHTML = caption_text;
      last.src = current.src;
      
      current.style.display = 'none';
      current.src = src;
    },
    
    // Fade in the image once loaded
    loaded : function(event) {
      new Effect.Appear(this,{duration:1.0});
    }
  },
  Search : {
    prompt : "Search...",
    search : function() {
      var find = $F('q');
      if(find==McArtzt.search_tag||find=='')
      {
        $('results').innerHTML = '';
        Element.hide('loading');
        return true;
      }
      Element.show('loading');
      new Ajax.Updater('results','/live/search',{parameters:'q='+encodeURIComponent($F('q')),onComplete:"Element.hide('loading')"});
      return true;
    },
    leave : function() {
      if($F('q')=='') 
        $('q').value='Search...';
      return true;
    },
    enter : function() {
      if($F('q')=='Search...') 
        $('q').value='';
      return true;
    }
  }
}

document.observe("dom:loaded", function() {
  McArtzt.Gallery.init();
});
