if (window['loadFirebugConsole']) 
{
	window.loadFirebugConsole();
} 
else 
{
	if (!window['console']) {
		window.console = {};
		window.console.info = function(s){};
		window.console.log = function(s){};
		window.console.warn = function(s){};
		window.console.error = function(s){};
	}
}

var theCarousel;
var switchImageObj = {};
var imagePadding = 60;
var keyScroll = 1;
var mouseScroll = 5;
var firstImageHeight;
var selectedIndex;
var Key = 
{
  LEFT:   37,
  UP:     38,
  RIGHT:  39,
  DOWN:   40
};

function carousel_click( elt, itemIndex, newImgSrc, desc )
{
  selectedIndex = itemIndex;
  switchImageObj['url'] = elt.getAttribute('href');
  switchImageObj['src'] = newImgSrc;
  switchImageObj['desc'] = desc;
  // alert( "Carousel Click - imageSrc is '"+newImgSrc+"', description: "+desc );
  jQuery('#full_size_image').fadeOut( 'slow', loadNewImage );
  jQuery('.description').fadeOut('slow');
  highlightCarouselItem( elt.getElementsByTagName('img')[0] );
}

function mycarousel_initCallback(carousel) 
{ 
  theCarousel = carousel;
  theCarousel.options.scroll = mouseScroll;
}

function highlightCarouselItem( elt )
{
  var thumbImages = document.getElementById('carousel_container').getElementsByTagName('img');
  for( var i in thumbImages )
  {
    if (thumbImages[i]==elt) $(elt).fadeTo( 'normal', 1 );
    else if (thumbImages[i].src) $(thumbImages[i]).fadeTo( 'normal', 0.75 );
  }
}

function loadNewImage()
{
  switchImageObj['img'] = new Image();
  switchImageObj['img'].onload = onNewImageLoadComplete;
  switchImageObj['img'].onerror = onNewImageLoadError;
  switchImageObj['img'].src = switchImageObj['src'];
  // console.log( "Image obj '"+newImg+"', .src = '"+newImg.src+"', onLoad = "+newImg.onload );
}

function onNewImageLoadComplete( e )
{
  // console.log( "onNewImageLoadComplete()" );
  var height = switchImageObj['img'].height;
  checkImageFit( height );
  $('#full_size_image').attr('src', switchImageObj['src']).fadeIn( 'slow' );
  $('.description').html(switchImageObj['desc']).fadeIn( 'slow' );
}

function onNewImageLoadError( e )
{
  // console.log( "onNewImageLoadError()" );
  window.location.href = switchImageObj['url'];
}

function checkImageFit( h )
{
  var availHeight = $('#image_target_cell').height() - imagePadding;
  // console.log( "checkImageFit: height = "+h+", available = "+availHeight );
  var newHeight = (availHeight < h) ? availHeight : h;
  $('#full_size_image').attr('height', newHeight);
  $('#full_size_image').removeAttr('width');
}

function checkFirstImageFit()
{
  // console.log( "checkFirstImageFit()" );
  firstImageHeight = $('#full_size_image').attr('height');
  checkImageFit( firstImageHeight );
}

function onWindowResize( e )
{
  // console.log( "onWindowResize()" );
  var height = (switchImageObj['img']) ? switchImageObj['img'].height : firstImageHeight;
  checkImageFit( height );
}

function onKeyPress( e )
{
  var keyCode = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
  // console.log( "Current selectedIndex = "+selectedIndex );
  theCarousel.options.scroll = keyScroll;
  if (keyCode==Key.LEFT) 
  {
    theCarousel.prev();
    nextItem = $('#galleryitem_'+(selectedIndex-1));
    nextItem.parent().click();
  }
  if (keyCode==Key.RIGHT) 
  {
    theCarousel.next();
    nextItem = $('#galleryitem_'+(selectedIndex+1));
    nextItem.parent().click();
  }
  theCarousel.options.scroll = mouseScroll;
}

jQuery(document).ready(function() 
{
  jQuery('.thumbnail_carousel').jcarousel({
    scroll: 1,
    initCallback: mycarousel_initCallback
  });

  if ($('#full_size_image').attr('height')) 
  {
    checkFirstImageFit();
    var preselectedItemId = $('.preselected').attr('id');
    selectedIndex = parseInt( preselectedItemId.split("_")[1], 10 );
    // console.log( "preselected index: "+selectedIndex );
    $(window).bind('keydown', onKeyPress);
  }
});

$(window).bind('resize', onWindowResize);