$(document).ready(function() {
	
	// Bind the prev/next click events.
	$('.prev_btn').click(slideRight);
	$('.next_btn').click(slideLeft);
	
	// Button spamming prevention.
	$('.next_btn').dblclick(function() {
	//	alert('Look mate, I know our portfolio is beautiful and you\'re obviously eager to see it, but spamming the next button is doing us no favors. Feel free to close this box when you\'re feeling sensible.');
	});
	$('.prev_btn').dblclick(function() {
	//	alert('Look mate, I know our portfolio is beautiful and you\'re obviously eager to see it, but spamming the previous button is doing us no favors. Feel free to close this box when you\'re feeling sensible.');
	});
	
	// Set all the overlay boxes to hidden.
	$('#home_slider > ul > li > ul > li > a > span').css('opacity', '0.0');
	
	// Fancybox.
	$('.fancybox').fancybox({
		cyclic: true,
		titleFormat: formatTitle,
		overlayColor: '#000',
		overlayOpacity: '0.9',
		onStart: function() {
			SliderInUse = 1;
		},
		onClose: function() {
			SliderInUse = 0;
		}
	});
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		return '<div id="imagetitle">- ' + title + ' -</div>';
	}
	
	$('.fancybox').click(function() {
		$('#fancybox-wrap').prepend('<div class="top_overlay_background"></div>');
		$('#fancybox-wrap').append('<div class="bottom_overlay_background"></div>');
	});
	
	// Add the current amount of control buttons.
	var ControlAmount = $('#home_slider > ul > li').length;
	for(var i = 0; i < ControlAmount; i++) {
		if (i == 0) {
   			$('#controls').append('<li class="active"><a href="#" onclick="return false;">' + i + '</a></li>');
		} else {
   			$('#controls').append('<li><a href="#" onclick="return false;">' + i + '</a></li>');
   		}
	}	
	
	// Hover toggle to show/hide overlay boxes.
	$('#home_slider > ul > li > ul > li > a').hover(function() {
		HoverKill = 1;
		$(this).children('span').stop(true, true).animate({'opacity':1.0}, 300);
	}, function() {
		HoverKill = 0;
		$(this).children('span').stop(true, true).animate({'opacity':0.0}, 300);
	});
	
});


var SliderInUse = 0;
var HoverKill = 0;
var PermaKill = 0;
setInterval('autoSlideLeft()', 5000);

function autoSlideLeft() {		
	
	// Check if the slider has recently been manually moved.
	if(SliderInUse == 1 || HoverKill == 1 || PermaKill == 1) {
		SliderInUse = 0;
	} 
	
	// If it hasnt.
	else {
		SliderInUse = 1;
		
		// Change active control
		if($('#controls li.active').next('li').length > 0) {
			$('#controls li.active').next('li').addClass('active');
			$('#controls li.active:first').removeClass('active');
		} else {
			$('#controls li.active').removeClass('active');
			$('#controls li:first').addClass('active');			
		}
		
		// Slide it left.
		$('#home_slider > ul').stop(true, true).animate({'marginLeft':'-318px'}, 1000, function() {
			$('#home_slider > ul').children('li:first').appendTo('#home_slider > ul');
			$('#home_slider > ul').css('margin-left', '0px');
			SliderInUse = 0;
		});
	}
}

function slideLeft() {
	if(SliderInUse == 0) {
		// Do the animation.
		SliderInUse = 1;
		
		// Change active control
		if($('#controls li.active').next('li').length > 0) {
			$('#controls li.active').next('li').addClass('active');
			$('#controls li.active:first').removeClass('active');
		} else {
			$('#controls li.active').removeClass('active');
			$('#controls li:first').addClass('active');
		}
		
		$('#home_slider > ul').stop(true, true).animate({'marginLeft':'-318px'}, 500, function() {
			// Move the first (now hidden slide) to the end.
			$('#home_slider > ul').children('li:first').appendTo('#home_slider > ul');
			// Reset the slider position.
			$('#home_slider > ul').css('margin-left', '0px');
			// Set the slider to not in use.
			SliderInUse = 0;
			PermaKill = 1;
		});
	}
	return false;
}

function slideRight() {
	if(SliderInUse == 0) {
	
		if($('#controls li.active').prev('li').length > 0) {
			$('#controls li.active').prev('li').addClass('active');
			$('#controls li.active:last').removeClass('active');
		} else {
			$('#controls li.active').removeClass('active');
			$('#controls li:last').addClass('active');
		}
	
		SliderInUse = 1;
		// Move the last item to the start.
		$('#home_slider > ul').children('li:last').prependTo('#home_slider > ul');
		// Push the slider back so the first element cannot be seen.
		$('#home_slider > ul').css('margin-left', '-320px');
		// Slide into the first element.
		$('#home_slider > ul').stop(true, true).animate({'marginLeft':'0px'}, 500);
		SliderInUse = 0;
		PermaKill = 1;
	}
	
	return false;
}

var KeyboardKill = 0;

// Keyboard navigation
$(document).keydown(function(e){
    if (e.keyCode == 37) {
 		console.log(KeyboardKill);
    	if(KeyboardKill == 0) {
    		KeyboardKill = 1;
    		slideRight();
    		setTimeout(function() {
    			KeyboardKill = 0;
    		}, 500)
    	}
        return false;
    }
    
    if (e.keyCode == 39) {
 		console.log(KeyboardKill);
    	if(KeyboardKill == 0) {
    		KeyboardKill = 1;
    		slideLeft();
    		setTimeout(function() {
    			KeyboardKill = 0;
    		}, 500)
    	}
        return false;
    }
});
