/*
 $ slideshow for Lazyest Gallery
 copyright 2009-2010 Marcel Brinkkemper
 */

function lg_js_slideshow() {
    $('.lg_loading').each( function() {
      $(this).hide();
    });
    $('.lg_slideshow').each( function() { 
       $(this).children('a').css({opacity: 0.0});
      $(this).children('a').css({visibility: 'visible'});
      var maxWidth = 0; 
      var maxHeight = 0;  
      $(this).children('a').each( function(index, object) {
        var imgWidth = parseInt($(object).find('img').width());
        maxWidth = (imgWidth > maxWidth)? imgWidth : maxWidth;
        var imgHeight = parseInt($(object).find('img').height());
        maxHeight = (imgHeight > maxHeight)? imgHeight : maxHeight;
      })
      $(this).css({width:maxWidth+'px', height:maxHeight+'px'});     
      $(this).children('a:first').css({opacity: 1.0}); 
      $(this).children('a:first').addClass('show');      
      var leftPad = ( maxWidth - parseInt( $(this).children('a:first').find('img').width() ) ) / 2;
      var bottomPad = ( maxHeight - parseInt( $(this).children('a:first').find('img').height() ) ) / 2;
      $(this).children('a:first').css({left: leftPad+'px', bottom:bottomPad+'px'});
      if ( $(this).attr('id').match('lg_slideshow') && ( $(this).children('a:first').find('img').attr('rel').length > 0 )  ) {
        $(this).children('.sscaption').css({width: $(this).children('a:first').find('img').width(), left:leftPad+'px', bottom:bottomPad+'px'});        
        $(this).children('.sscaption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
        $(this).children('.sscaption').animate({opacity: 0.7},100 ).animate({height: '100px'},800 );
        $(this).children('.sscaption').html($(this).children('a:first').find('img').attr('rel'));     
        $(this).children('.sscaption').animate({height: '100px'},1800 );
        $(this).children('.sscaption').animate({height: '0px'},800 );
        $(this).children('.sscaption').css({opacity: 0.0});
      } 
    });
  	setInterval('lg_js_gallery_show()', lgSlideDuration );	
}

function lg_js_gallery_show() {
  $('.lg_slideshow').each( function() {    
    var current = $(this).children('a.show');
  	var next = ((current.next().length) ? ((current.next().hasClass('sscaption'))? $(this).children('a:first') :current.next()) : $(this).children('a:first'));	
  	var nextImg = next.find('img');
    var leftPad = ( parseInt( $(this).width() ) - parseInt( nextImg.width() ) ) / 2;
    var bottomPad = ( parseInt( $(this).height() ) - parseInt( nextImg.height() ) ) / 2;
    var caption = nextImg.attr('rel');  
  	next.css({opacity: 0.0})
  	.addClass('show')
  	.animate({opacity: 1.0}, 1000)
    .css({left: leftPad+'px', bottom:bottomPad+'px'});
  	current.animate({opacity: 0.0}, 1000 )
  	.removeClass('show');    
    if ( $(this).attr('id').match('lg_slideshow') && ( caption.length > 0 ) ) {
      $(this).children('.sscaption').css({ width: nextImg.width(), left: leftPad+'px', bottom:bottomPad+'px' });
    	$(this).children('.sscaption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
    	$(this).children('.sscaption').animate({opacity: 0.7},100 ).animate({height: '100px'},800 );
    	$(this).children('.sscaption').html(caption);      
      $(this).children('.sscaption').animate({height: '100px'},1800 );
      $(this).children('.sscaption').animate({height: '0px'},800 );
      $(this).children('.sscaption').css({opacity: 0.0});
    }	
  });
}

var $ = jQuery.noConflict();
$(window).load(function() {  
  if ( $('.lg_slideshow').length ) {  
    lg_js_slideshow();
  }
}) ;
