/*

 JQuery slideshow for Lazyest Gallery

 copyright 2009 Marcel Brinkkemper

 */



function lg_js_slideshow() {

    jQuery('.lg_loading').each( function() {

      jQuery(this).hide();

    });

    jQuery('.lg_slideshow').each( function() { 

       jQuery(this).children('a').css({opacity: 0.0});

      jQuery(this).children('a').css({visibility: 'visible'});

      var maxWidth = 0; 

      var maxHeight = 0;  

      jQuery(this).children('a').each( function(index, object) {

        var imgWidth = parseInt(jQuery(object).find('img').width());

        maxWidth = (imgWidth > maxWidth)? imgWidth : maxWidth;

        var imgHeight = parseInt(jQuery(object).find('img').height());

        maxHeight = (imgHeight > maxHeight)? imgHeight : maxHeight;

      })

      jQuery(this).css({width:maxWidth+'px', height:maxHeight+'px'});     

      jQuery(this).children('a:first').css({opacity: 1.0}); 

      jQuery(this).children('a:first').addClass('show');      

      var leftPad = ( maxWidth - parseInt( jQuery(this).children('a:first').find('img').width() ) ) / 2;

      var bottomPad = ( maxHeight - parseInt( jQuery(this).children('a:first').find('img').height() ) ) / 2;

      jQuery(this).children('a:first').css({left: leftPad+'px', bottom:bottomPad+'px'});

      if ( jQuery(this).attr('id').match('lg_slideshow') && ( jQuery(this).children('a:first').find('img').attr('rel').length > 0 )  ) {

        jQuery(this).children('.sscaption').css({width: jQuery(this).children('a:first').find('img').width(), left:leftPad+'px', bottom:bottomPad+'px'});        

        jQuery(this).children('.sscaption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	

        jQuery(this).children('.sscaption').animate({opacity: 0.7},100 ).animate({height: '100px'},800 );

        jQuery(this).children('.sscaption').html(jQuery(this).children('a:first').find('img').attr('rel'));     

        jQuery(this).children('.sscaption').animate({height: '100px'},1800 );

        jQuery(this).children('.sscaption').animate({height: '0px'},800 );

        jQuery(this).children('.sscaption').css({opacity: 0.0});

      } 

    });

  	setInterval('lg_js_gallery_show()', lgSlideDuration );	

}



function lg_js_gallery_show() {

  jQuery('.lg_slideshow').each( function() {    

    var current = jQuery(this).children('a.show');

  	var next = ((current.next().length) ? ((current.next().hasClass('sscaption'))? jQuery(this).children('a:first') :current.next()) : jQuery(this).children('a:first'));	

  	var nextImg = next.find('img');

    var leftPad = ( parseInt( jQuery(this).width() ) - parseInt( nextImg.width() ) ) / 2;

    var bottomPad = ( parseInt( jQuery(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 ( jQuery(this).attr('id').match('lg_slideshow') && ( caption.length > 0 ) ) {

      jQuery(this).children('.sscaption').css({ width: nextImg.width(), left: leftPad+'px', bottom:bottomPad+'px' });

    	jQuery(this).children('.sscaption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	

    	jQuery(this).children('.sscaption').animate({opacity: 0.7},100 ).animate({height: '100px'},800 );

    	jQuery(this).children('.sscaption').html(caption);      

      jQuery(this).children('.sscaption').animate({height: '100px'},1800 );

      jQuery(this).children('.sscaption').animate({height: '0px'},800 );

      jQuery(this).children('.sscaption').css({opacity: 0.0});

    }	

  });

}



jQuery(window).load(function() {  

  if ( jQuery('.lg_slideshow').length ) {  

    lg_js_slideshow();

  }

}) ;

