$(document).ready( function() {
  var css_url = '/vb-theme/css/fancybox.css';
  if(document.createStyleSheet) {
    document.createStyleSheet(css_url);
  } else {
    var newSS=document.createElement('link');
    newSS.rel='stylesheet';
    newSS.href=escape(css_url);
    newSS.type='text/css';
    document.getElementsByTagName("head")[0].appendChild(newSS);
  }

  $('table tr:odd').addClass('odd');
  $('table tr:even').addClass('even');

  $('table a[href]').filter( function(index){
    /* filter links to images, case insensitive */
    return (/(?:jpg|png|gif)$/i).test( $(this).attr('href') );
  })
    .addClass('fancybox')
    .attr('rel', 'FB_gallery')
    .each( function() {
      this.title=this.text;
      if ( (/(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})/).test( $(this).attr('href') ) ) {
	this.title = RegExp.$4 + ':' + RegExp.$5;
	if ( (/_(.{2})\./).test( $(this).attr('href') ) ) {
	  if (RegExp.$1 == 'vp') {
	    this.title += ', Viewpoint';
	    $(this).attr('rel', 'FB_gallery_VP');
	  } else {
	    this.title += ', Pic ' + RegExp.$1;
	    $(this).attr('rel', 'FB_gallery_' + RegExp.$1);
	  }
	}
	$(this).text(this.title);
      }
    })
    .fancybox({
      'imageScale'     : true,
      'overlayOpacity' : 0.9
    });
  
  var host = '<a href="/">' + location.hostname.replace(/\..*$/, '').replace(/[-_]/g, ' ') + '</a>';
  host && $('table').before('<h1>' + host + '</h1>');

  var date_array = location.pathname
    .replace(/(?:^\/|\/$)/g, '')
    .split('/')
    .reverse() ;
  date_array = jQuery.map( date_array, function(element, index) {
    var counter = index;
    var uri = '';
    var title = '-';
    while (counter--) {
      uri += '../';
      title = '<' + title;
    }
    return '<a title="' + title + '" href="' + uri + '">' + element + '</a>';
  });
  var date = date_array.join('.');
  date && $('table').before('<h2>' + date + '</h2>');
});

