///////////////////////////////////////////////////////////////////////////////
//
//  gallery.js - Javascript functions for gallery
//
//  10-Nov-2008 pythonologist
//
//  Dependencies:
//    mootools core, mootools Assets
//
///////////////////////////////////////////////////////////////////////////////

function str_replace(haystack, needle, replacement) {
  var temp = haystack.split(needle);
  return temp.join(replacement);
}

var trans = 0;

function imageEnter(e) {
  //alert($type(e));
  if (this.src.indexOf('.t.') == -1) {
    return;
  }
  var divT = $('div' + trans);
  var imgT = divT.getElement('img');
  var parT = divT.getElements('p');
  var new_src = str_replace(this.src, '.t.', '.s.');
  var new_alt = this.alt;
  new Asset.image(new_src, {onload: function (imgNew) {
      imgNew.replaces(imgT);
      last_slash = new_src.lastIndexOf('/');
      last_dot_s = new_src.lastIndexOf('.s.');
      var title = unescape(new_src.substring(last_slash + 1, last_dot_s));
      title = title.replace(/ \d,/, ',')
      parT[0].set('text', title);
      parT[1].set('text', new_alt);
	  trans = (trans + 1) % 2;
	  divO = $('div' + trans);
      divT.fade(1);
      divO.fade(0);  
    }
  });
}

function imageLeave() {
}

function initialise_gallery() {
  //alert('initialising gallery');
  
  var imageTable = $('imageTable');
  imageTable.getElements('img').addEvents({
    'mouseenter': imageEnter,
    'mouseleave': imageLeave
    });
}

window.addEvent('domready', initialise_gallery);
