$(document).ready(function() {

	var browserHeight = $(window).height();
		browserWidth = $(window).width();
		prevButton = $('nav .prev');
		nextButton = $('nav .next');
		images = $('.frame img');
		activeImg = 0;
		prevActiveImg = 0;
		dotMatrix = $('.dot-matrix');
		logo = $('.logo');
		about = $('.about')
		
	$('body').css({"height":browserHeight});
	
	images.css({"width":browserWidth})
	
	
	function resetGallery() {
		images = $('.frame img');
		images.css({"width":browserWidth});
		activeImg = 0;
		prevActiveImg = 0;
		$('.frame img:first').fadeIn(500);
		$('body').css({"background":"#000"});
	}
	
	$(window).resize(function() {
		browserWidth = $(window).width();
		browserHeight = $(window).height();
		images.css({"width":browserWidth});
	})

	
//Add Handlers
	nextButton.click(function() {
		swapImage('next');
	});
	prevButton.click(function() {
		swapImage('prev');
	})
	
//Actual Swaps
	function swapImage(direction) {
		//save the previous image location
		prevActiveImg = activeImg;
		
		//determine direction and set image index values
        if(direction == "next") {
            activeImg++
            if(activeImg == images.length) {
                activeImg = 0;
            }
        } else if(direction == "prev") {
            activeImg--
            if(activeImg < 0) {
                activeImg = images.length-1;
            }
        } 
	
		//animate the switch
		images.eq(prevActiveImg).fadeOut(300, function() {
			images.eq(activeImg).fadeIn(500);
		});
	}
	
//Gallery Swaps
	$('.gallery-nav ul li a').click(function(e) {
		e.preventDefault();
		var galleryLocation = $(this).attr('href');
		images.eq(activeImg).fadeOut(300);
		dotMatrix.hide();
		$('nav').hide();
		$('body').css({
			"background-image":"url(public/images/loading.gif)",
			"background-repeat":"no-repeat",
			"background-position":"center center"
		});
		$.ajax({
		   	type: "POST",
		   	url: "includes/"+galleryLocation+".php",
		   	success: function(html){
		   		$('.frame').html(html);
				$('nav').delay(300).fadeIn(500);
				$('.frame img:first').load(function() {
					dotMatrix.show();
			   		resetGallery();
			   	});
			}
	 	});
	});
	
//Logo Flip to About
	logo.click(function() {
		$(this).animate({"left":"-150px"}, 100, function() {
			about.animate({"left":0}, 200)
		})
	});
	$('.about .close').click(function() {
		about.animate({"left":"-280px"}, 100, function() {
			logo.animate({"left":0}, 200)
		})
	});
	
});



$(window).load(function() {
	$('body').css({"background":"#000"});
	$('.frame img:first').fadeIn(500, function() {
		logo.animate({"left" : "0"});
		$('nav').animate({"bottom" : "25px"});
		$('.gallery-nav').delay(300).animate({"left" : "30px"});
	});
})
