/**
 * @author justinrobinson
 */
var playSlideshow = true,
    global_timeout = 4000;

$(function() {
    //$('#images').css('overflow','hidden').css('height', '567px').children('ul').hide();
    $('#navigation').attr('class', $("#navigation ul.galleries li:first").attr("class") );
    //centreInterface();
    
    //$('#images ul:first li:first').show();
    /*
	$('#images ul').each(function() {
		$(this).children('li:first').addClass('first');
		$(this).children('li:last').addClass('last');
	});
	$('#navigation ul.directions a.next').click(function() {
        slideShow("stop");
        playSlideshow = false;
        $('#navigation ul.directions a.playPause').addClass( "paused" ).text( "play" );
		nextImage();
	});
	$('#navigation ul.directions a.back').click(function() {
        slideShow("stop");
        playSlideshow = false;
        $('#navigation ul.directions a.playPause').addClass( "paused" ).text( "play" );
		prevImage();
	});
	$('#navigation ul.directions a.playPause').click(function() {
		if($(this).hasClass('paused')) {
			$(this).removeClass('paused').text('pause');
            playSlideshow = true;
			slideShow('start', 100);
			console.log('play - slideshow = '+slideshow);
		}
		else {
			$(this).addClass('paused').text('play');
            playSlideshow = false;
			slideShow('stop');
			console.log('pause - slideshow = '+slideshow);
		}
	});
    
    */
   
    //$('#navigation ul.galleries li:first a').addClass( 'active' );
    //$('#navigation h3').text( $('#navigation ul.galleries li:first a').text() );
  
	/*
	$('#navigation ul.galleries a').click(function() {
		var gallery = $(this).parent('li').attr('class');
        var galleryId = $(this).parent('li').attr('class').replace( /^gallery_/, "" ); 
        
        if( !$(this).hasClass("active") && imagesLoaded >= totalImages ) {
            $('#images ul:visible').cycle('pause').hide();
            $('#images ul.'+gallery).cycle('resume').show();
            
            $('#navigation').attr('class', gallery);
    		$('#navigation h3').text( $(this).text() );
    		$('#navigation ul.galleries a').removeClass('active');
    		$(this).addClass('active');
            
            $('#navigation .directions').hide();
            $('#directions_'+galleryId).show();
            $('#navigation .directions:visible playPauseBtn').attr('class','playing');
            
            var galleryLength = $(this).parent('li').attr('gallerylength');
            var slideIndex = $('#images ul.'+gallery+' li:visible').attr('class').replace( /^image_/, "" );
            $('#navigation div.pages span.slideIndex').text( slideIndex );
            $('#navigation div.pages span.galleryLength').text( galleryLength );
        }
    });
	*/
   
	$("#images ul").each(function() {
        var galleryId = $( this ).attr( 'class' ).replace( /^gallery_/, "" ); 
        $(this).cycle({
            timeout: global_timeout,
            speed: 2000,
            next: '#directions_'+galleryId+' a.next',
            prev: '#directions_'+galleryId+' a.back',
            //pager: '#pager',
            before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
                var slideNumber = $(nextSlideElement).attr('class').replace( /^image_/, "" );
                $('#navigation div.pages span.slideIndex').text(slideNumber);
    		}
        });
    });
    
    $('a.playPause').click(function() {
        resumeSlideshow();    
    });
    
    $("ul.actions").show();
    
    var totalImages = $('#images li img').size();
    $('#navigation span.galleryLength').text( totalImages );
    
	/*
	$(window).resize(function() {
		centreInterface();
	});
    */
   /*
    //start the first gallery slideshow once the last image in the page is loaded
    
    var imagesToLoad = Math.round((totalImages/100)*70);
    var imagesLoaded = 0;
    console.log(imagesToLoad+' of '+totalImages+' to load');
    
    
    $('#loader span.total').text( totalImages );
    $('#loader span.index').text( imagesLoaded );
    
    
    $('#images img').each(function() {
        var imageSrc = $(this).attr('src');
        $(this).attr('src', imageSrc).load(function() { 
            imagesLoaded++;
            if( imagesLoaded >= imagesToLoad ) {
                $('#loader').fadeOut('normal', function() {
                    $('#navigation div.pages span.galleryLength').text( $('#navigation li a.active').parent('li').attr('galleryLength') );  
                    $('#images').show();
                    $('#images ul:first').fadeIn('normal', function() {
                        $("#images ul:first").cycle('resume');
                    });
                    
                    //$('#loader').hide();
                    
                    //$('#navigation ul.directions:first, div.pages').show();
                });
            } 
            else {
                $('#loader span.index').text( imagesLoaded );
            }
        }); 
    });
    */
});

function pauseSlideshow() {
    $('a.playPause:visible').removeClass('playing').addClass('paused');
    $('#images ul:visible').cycle('pause');
    //$('.slideshowStatus').show();
}

function resumeSlideshow() {
    if($('a.playPause:visible').hasClass('paused')) {
        $('a.playPause:visible').removeClass('paused').addClass('playing').attr('title','pause slideshow');
        $('#images ul:visible').cycle('resume');
    }
    else {
        $('a.playPause:visible').removeClass('playing').addClass('paused').attr('title','play slideshow');
        $('#images ul:visible').cycle('pause');
    }
}

function getImages() {
    $.ajax({
        type: "GET",
        url: "getImages.php",
        dataType: "json",
        success: function(msg){
            buildGalleries(msg);
        },
        error: function(msg) {
            console.log(msg);
        }
    });

}

function buildGalleries( galleries ) {
    var navigation = "",
        images = "";
        
    $.each(galleries, function(g_i, g_v) {
        navigation += '<li class="gallery_' + g_i + '"><a class="flash-title">' + g_v.title + '</a></li>';
        
        images += '<ul class="gallery_' + g_i + '">';
        $.each(g_v.images, function(i_i, i_v) {
            images += '<li><img src="img/photos/' + i_v + '" /></li>';
        });
        images += '</ul>';
    });
    $("#navigation ul.galleries").html( navigation );
    $("#images").html( images );
    
    $("#images ul").each(function() {
        $(this).children("li:first").addClass("first");
        $(this).children("li:last") .addClass("last");
    }); 
    
}

function centreInterface() {
    var wrapperMargin = $('#wrapper').css('margin-top');
	var height = $('#wrapper').height();
	var winHeight = $(window).height();
	var top = (( winHeight-height ) / 2 ) - parseInt( wrapperMargin );
	$('#wrapper').css('top', top);
}

function slideShow(action, interval) {
    if(action === 'start' && playSlideshow === true) {
		slideshow = setTimeout("nextImage()", interval);
	}
	else {
		clearTimeout(slideshow);
	}
}

function nextImage() {
    var currentGallery = $('#images ul:visible');
	var currentImage = $('#images ul:visible li:visible');
    
    if($(currentImage).hasClass('last')) {
		var nextImage = $(currentGallery).children('li.first');
	}
	else {
		var nextImage = $(currentImage).next();
        console.log("currentImage = "+$(currentImage).children('img').attr('src')+" - "+$(nextImage).children('img').attr('src'));
	}
    
	$(currentImage).fadeOut("slow", function() {
        $(nextImage).fadeIn("slow");
    });
    
	if(playSlideshow === true) {
		slideshow = setTimeout("nextImage()", 3000);
	}
}

function prevImage() {
    var currentGallery = $('#images ul:visible');
	var currentImage = $('#images ul:visible li:visible');
	if($(currentImage).hasClass('first')) {
		var nextImage = $(currentGallery).children('li.last');
	}
	else {
		var nextImage = $(currentImage).prev();
	}
	
    $(currentImage).fadeOut("fast", function() {
        $(nextImage).fadeIn("slow");
    });
}
