$(document).ready(function(){
    $('a[rel=lightbox]').fancybox({
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'titlePosition' : 'over'
    });
    if($('.slideshow_m_fade').length) slideShow(0.6);
});

function slideShow(op){
    galleryInt = {};
    $('.slideshow_m_fade').each(function(k,i){
        var sl = $(this);
        sl.find('div.photo_container').css({opacity: 0.0});
        sl.find('div:first').css({opacity: 1.0});
        sl.find('.caption').css({opacity: op});
        sl.find('.caption').css({width: sl.find('div').find('img').css('width')});
        sl.find('.content').html(sl.find('div:first').find('img').attr('alt'));
        sl.find('.content').animate({opacity: 1.0}, 400);
        if(sl.find('div.photo_container').length > 1){
            if(sl.find('div.buttons').length){
                for(var i = 0;i < sl.find('div.photo_container').length;i++){
                    sl.find('div.buttons').append('<div class="button_' + i + '"></div>');
                }
                sl.find('div.buttons div').first().addClass('active');
                sl.find('div.buttons div').click(function(){
                    if(!$(this).hasClass('active')){
                        clearInterval(galleryInt[k]);
                        $(sl.find('div.photo_container')[sl.find('div.buttons div').index(this)]).addClass('next');
                        gallery(op);
                        galleryInt[k] = setInterval(function(){gallery(sl, op);},6000);
                    }
                });
            }
            galleryInt[k] = setInterval(function(){gallery(sl, op);},6000);
        }
    });
}

function gallery(sl, op){
	var current = (sl.find('div.show').length ?  sl.find('div.show') : sl.find('div:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? sl.find('div:first') :current.next()) : sl.find('div:first'));
	var caption = next.find('img').attr('alt');

	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);

	current.animate({opacity: 0.0}, 1000).removeClass('show');

	sl.find('.caption').animate({opacity: 0.0}, {queue:false, duration:50}).animate({top: '-' + sl.find('.caption').height() + 'px'}, {queue:true, duration:300, complete:function(){sl.find('.content').html(caption);}});

	sl.find('.caption').animate({opacity: op},100 ).animate({top: '0px'},500 );
    if(sl.find('div.buttons').length){
        sl.find('div.buttons div.active').removeClass('active');
        $(sl.find('div.buttons div')[sl.find('div.photo_container').index(next)]).addClass('active');
    }
}

