function handleEnlargeClick(evt) {
  var el = evt.target;

  if (el.tagName !== 'A') {
    el = $(el).parent('a.enlarge').get(0);
  }

  // get uri of image to enlarge
  var image_uri = el.href;

  // find title and date of image
  var link_title = $(el).parents('.image').find('.image_title').html();
  if (!link_title) {
    link_title = $(el).parents('.resultImg').find('.image_title').html();
  }
  if (!link_title) {
    link_title = $(el).parents('.otherView').find('.image_title').html();
  }
  if (!link_title) {
    link_title = el.title;
  }
  var matches = link_title.match(/\s*([^\[]+)\s*\[(.+)\]\s*/i);

  var title, date;
  if (matches) {
    title = matches[1];
    date = matches[2];
  } else {
    title = link_title;
  }

  // find width and height of fullsize image
  var width = 0;
  var height = 0;
  var link_class = $(el).attr('class');
  var size_matches = link_class.match(/fullsize_(\d+)_(\d+)/i);
  if (size_matches) {
    width = parseInt(size_matches[1]);
    height = parseInt(size_matches[2]);
  }

  // find uri to record
  var record_links =  $(el).parents('div.resultImg, div.image').find('a.btnViewRecord');

  var record_uri;
  if (record_links.length > 0) {
    var record_link = record_links.get(0);
    record_uri = record_link.href;
  }

  createPopover(image_uri, title, date, record_uri, width, height);
  return false;
}

function createPopover(image_uri, title, date, record_uri, width, height) {

  if (!date) date = '';

  // construct and embed scrim
  $('<div id="scrim"></div>').
    css('opacity', '0.9').
    click(closePopover).
    prependTo('body');

  // get scroll position
  var ypos = $(window).scrollTop();
  var xpos = 0;

  var win_height = $(window).height();
  var total_image_height = height + 8;
  var yoffset = 0;
  if (win_height > total_image_height) {
    yoffset = Math.floor((win_height - total_image_height) / 2);
  }

  if (title) {
    yoffset = yoffset - 50;
  } else if (record_uri) {
    yoffset = yoffset - 30;
  }

  if (yoffset < 0) yoffset = 0;
  ypos = ypos + yoffset;


  var win_width = $(window).width();
  var total_image_width = width + 16;
  var xpos = 0;
  if (win_width > total_image_width) {
    xpos = Math.floor((win_width - total_image_width) / 2);
  }
  if (xpos < 0) xpos = 0;



  // construct and embed overlay
  var overlay_style = '';
  var image_style = '';
  if (width) {
    overlay_style = ' style="width: ' + (width + 16) + 'px; top: ' + ypos + 'px; left: ' + xpos + 'px;"';
    image_style = ' style="width: ' + (width) + 'px;"';
  }

  var html = '<div id="overlay"' + overlay_style + '>';

  if (title) {
	html += '<h2>' + title + '</h2>';
    html += '<div class="date">' + date + '</div>';
  }

  html += '<div class="image"' + image_style + '>';

  if (record_uri) {
    html += '<a href="' + record_uri + '">';
  }

  html += '<img src="' + image_uri + '" width="' + width + '" height="' + height + '" alt="" />';

  if (record_uri) {
    html += '</a>';
  }


  if (record_uri) {
    html += '<a href="' + record_uri + '" class="btnViewRecordBig">' + 'View Record' + '</a>';
  }

  html += '<a href="#" class="btnViewRecordClose">Close</a>';
  html += '</div>';


  html += '</div>';

  $(html).prependTo('body');
  $(".btnViewRecordClose").click(closePopover);
}

function closePopover() {
  $('#scrim').add('#overlay').remove();
  return false;
}

// Send list of categories to highlight to the flash movie
// this is an array of numbers from 1 to 7, or an array containing -1 to
// deselect all items.
function setCategories( p_array ) {
	var type = "hightlight";
	var misc = "basic";
	toFlash( p_array, type, misc );
}

function toJava(target, type, last) { }

function toFlash(categories, type, misc) {
  var swf = getFlashMovie("swf");
  swf.toFlash(categories, type, misc);
}

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document.getElementById(movieName);
}

var categories_for_class = {
  theme_1_1: [0],
  theme_1_2: [3],
  theme_1_3: [3],
  theme_1_4: [6],
  theme_2_1: [0],
  theme_2_2: [1],
  theme_2_3: [2],
  theme_2_4: [3],
  theme_2_5: [4],
  theme_2_6: [5],
  theme_3_1: [0],
  theme_3_2: [2],
  theme_3_3: [2],
  theme_3_4: [3],
  theme_3_5: [4]
}

function handleVideoClick(event) {
  if (!event || !event.target || !event.target.href) {
    return false;
  }

  createVideoContainer();
  playMedia(event.target.href, 'video');
  return false;
}



function handleAudioClick(event) {
  if (!event || !event.target || !event.target.href) {
    return false;
  }
  if(createAudioContainer(event.target)) {
    playMedia(event.target.href, 'audio');
  }
  return false;
}

function createVideoContainer(){
  $('<div id="media_container" class="media_container_video">' +
    '<a href="#" class="btnCloseMedia">Close</a>' +
    '</div>').prependTo('body');
  $('<div id="scrim"></div>').prependTo('body').css('opacity', 0.9).click(destroyMediaContainer);
  $('.btnCloseMedia').click(destroyMediaContainer);
}

function createAudioContainer(target){
  destroyMediaContainer();
  $('<div id="media_container" class="media_container_audio"></div>').insertAfter(target);
  return true;
}

function destroyMediaContainer(event){
  $('#media_container').remove();
  $('#scrim').remove();
  return false;
}

function playMedia(file, type) {
  // IE seems to sometimes strip off the (required) initial slash
  //if (file.search(/^\//) === -1) {
  //  file = '/' + file;
  //}

  //create container for jwplayer
  $('#media_container').append('<div id="media_player"></div>');

  //calculated width, height, this is set per type using css
  var width, height = 400;
  switch( type ){
    case 'audio':
      width = '100%';
      height =  20;
      break;
    case 'video':
      width = 400;
      height = 256;
      break;
  }

  //setup jwplayer variables
  var flashvars = {
    file : file,
    bufferlength  : 5,
    autostart : true,
    skin:"../swf/simple.swf"
  };

  //setup flash render variables
  var flashparams = {
    bgcolor: "#000000",
    allowScriptAccess: "always",
    scale : "noscale",
    wmode: "opaque"
  };

  //setup html attributes
  var attributes = {
    id:"media_player",
    name:"media_player"
  };

  //embed jwplayer, the media file starts to play automatically
  swfobject.embedSWF("../swf/player.swf", "media_player", width, height, "9.0.115", "/swf/player.swf", flashvars, flashparams , attributes);
}

$(function() {
  $('.enlarge').click(handleEnlargeClick);

  $('.videoLink').click(handleVideoClick);
  $('.audioLink').click(handleAudioClick);


  $(".theme ul li a" ).mouseover(function(evt) {
      var link = evt.target;
      var link_class = $(link).attr('class');

      var categories = categories_for_class[link_class];
      setCategories( categories );

  });

  $(".theme ul li a" ).mouseout(function(evt) {
      setCategories( [-1] );
  });

  if ($.browser.msie && $.browser.version <= 7) {
    $('a').focus(function(event) { event.target.blur(); });
  }

});

