// Default JS Content - Do Not Delete


var buffer = false;
function loadGallery(){
$(function(){
	$('div.slideshow').height(totalH);
    $.ajax({
        url: path+'myimages.xml',
        dataType: 'xml',
        success: function(data){
            var dataArray = [];
            $(data).find('data').each(function(){
                dataArray.push({img:$(this).attr('img'),imgcaption:$(this).attr('imgcaption')});
            });
            $('.slideshow').append(
                '<img sr'+'c="'+path
                +dataArray[0].img
                +'" imagelabel="'
                +dataArray[0].imgcaption
                +'" style="position: absolute;display: none; ">'
            );
            dataArray.shift();
			var loaded = false;
            /* As soon as the first image is loaded, we may begin */
            $('.slideshow img:first').load(function(){
				if(loaded) return false;
				loaded = true;
                for(var i in dataArray){
                    $('.slideshow').append(
                        '<img sr'+'c="'+path
                        +dataArray[i].img
                        +'" imagelabel="'
                        +dataArray[i].imgcaption
                        +'" style="position: absolute;display: none; ">'
                    );
                }
                /* Variables */
                var transitionTime = 5000;
                
                /* Show First Image / Gallery / Hide Play Button */
                var n = $('.slideshow img:first');
                $('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
                n.show();
                if(buffer){
                    $('.buffer').css({
                        'height':n.height(),
                        'width':n.width(),
                        'margin-left' : (totalW-n.width())/2,
                        'margin-top' : (totalH-n.height())/2
                    }).fadeOut(1000,function(){
                        n.css('z-index','5');
                        $('.buffer').show();
                    });
                    n.css({
                        position : 'absolute',
                        'margin-left' : (totalW-$(this).width())/2,
                        'margin-top' : (totalH-$(this).height())/2
                    });
                }
                
                $('.play').hide();
                var animating = false;
                var nextSlide = function(next){
                    /* Make sure we're not already in transition */
                    if(animating) return false;
                    animating = true;
                    var f = $('.slideshow img:visible');
                    if(next){
                        var n = $('.slideshow img:visible').prev('img');
                        if(n.length==0) n = $('.slideshow img:last');
                    }else{
                        var n = $('.slideshow img:visible').next('img');
                        if(n.length==0) n = $('.slideshow img:first');
                    }
                    /* Make sure the next image is loaded before proceeding (don't worry, the loop event will re-fire) */
                    if(!n[0].complete) return false;
                    
                    /* Buffered Animation */
                    if(buffer){
                        $('.buffer').css({
                            'z-index':'4',
                            'height':f.height(),
                            'width':f.width(),
                            'margin-left' : (totalW-f.width())/2,
                            'margin-top' : (totalH-f.height())/2
                        });
                        f.css('z-index','5').fadeOut(600,function(){
                            $('.buffer').animate({
                                'height':n.height(),
                                'width':n.width(),
                                'margin-left' : (totalW-n.width())/2,
                                'margin-top' : (totalH-n.height())/2
                            },300,'swing',function(){
                                n.css({
                                    position : 'absolute',
                                    'margin-left' : (totalW-$(this).width())/2,
                                    'margin-top' : (totalH-$(this).height())/2,
                                    'z-index' : 3
                                }).show();
                                animating=false;
                                /* Alter Text */
                                $('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
                                
                                $('.buffer').fadeOut(1000,function(){
                                    n.css('z-index','5');
                                    $('.buffer').show();
                                });
                            });
                        });
                    }else{
                        /* Non Buffered Animation */
                        f.css('z-index',5);
                        n.css('z-index',4).show();
                        f.fadeOut(1500,function(){
                            animating=false;
                            /* Alter Text */
                            $('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
                        },'swing');
                    }
                };
                var looping=true;
                var t = setTimeout(function(){beginLoop();},transitionTime);
                var beginLoop = function(){
                    nextSlide();
                    t = setTimeout(function(){beginLoop();},transitionTime);
                };
                var play = function(){
                    clearTimeout(t);
                    $('.play').hide();
                    $('.pause').show();
                    beginLoop();
                };
                var pause = function(){
                    clearTimeout(t);
                    $('.play').show();
                    $('.pause').hide();
                };
                $('.play').click(play);
                $('.pause').click(pause);
                $('.back').click(function(){nextSlide(true);play();});
                $('.next').click(function(){nextSlide();play();});
            /* If the image is loaded from cache, the load event doesn't fire, this checks if it's already loaded and the load event missed */
            }).each(function(){
                if(this.complete) $(this).trigger("load");
            });
        }
    });
});
}
