/*
               _ _                   _         _ _   
 __      _____| | | __ _  __ _  __ _(_)_ __   | | |_ 
 \ \ /\ / / _ \ | |/ _` |/ _` |/ _` | | '_ \  | | __|
  \ V  V /  __/ | | (_| | (_| | (_| | | | | |_| | |_ 
   \_/\_/ \___|_|_|\__,_|\__, |\__,_|_|_| |_(_)_|\__|
                         |___/                       

@author: gadas.eu
@author: wellagain.lt
*/
var currentSelection = null;
var frontSelection = null;
var NAV_LINK_NEXT = '#navLinkNext';
var NAV_LINK_PREV = '#navLinkPrev';
var ANIMATE_STOP = '#stopPreview';
var ANIMATE_PREVIEW = '#animatePreview';
var ANIMATE_PREVIEW_ID = "animatePreview()";
var animateTimeoutID = null;
var slideShowCycleExecuted = false;

var DELAY_LINK_FADE_IN = 1000;
var DELAY_SCROLL = 700;
var DELAY_SLIDESHOW = 2000;

$(document).ready(function() {
	$('li[id*=photoNav]').click(function() {
		stopSlideShow();
		currentSelection = $(this);
		movePhoto();	
  	});

	initControls();
	initKeyBindings();
});

function initControls(){
	frontSelection = currentSelection = $('li[id=photoNav0]');

	$(NAV_LINK_NEXT).click( nextPhotoLink );
	$(NAV_LINK_NEXT).fadeTo(DELAY_LINK_FADE_IN, 1);

	$(NAV_LINK_PREV).click( prevPhotoLink );
	$(NAV_LINK_PREV).fadeTo(DELAY_LINK_FADE_IN, 1);

	$(ANIMATE_PREVIEW).click( animatePreview );
	$(ANIMATE_PREVIEW).fadeTo(DELAY_LINK_FADE_IN, 1);
		
	$(ANIMATE_STOP).click( stopPreview );
}

function initKeyBindings(){
	$(document).keydown(stopSlideShow);

	$(document).keydown(function(event) {
		event.preventDefault();
		// keypad-right; space
  		if (event.keyCode == '39' || event.keyCode == '32') {
     		nextPhoto();		
		// keypad-left
   		} else if( event.keyCode == '37' ){
			prevPhoto();
		// home button
		} else if( event.keyCode == '36' ){
			resetNavigation();
		}
   	});
}

function resetNavigation(){
	currentSelection = frontSelection;
	$('html,body').animate({scrollLeft: 0}, DELAY_SCROLL);
}

function stopSlideShow(){
	if( animateTimeoutID != null ){
		clearTimeout(animateTimeoutID);
		animateTimeoutID = null;

		$(ANIMATE_PREVIEW).fadeTo(500,1);
		$(ANIMATE_STOP).hide();
	}
}

function stopPreview(){
	stopSlideShow();
}

function nextPhotoLink(){
	stopSlideShow();
	nextPhoto();
}

function prevPhotoLink(){
	stopSlideShow();
	prevPhoto();
}

function movePhoto(){
	var offsetLeft = currentSelection.offset().left;
	offsetLeft -= ($('html,body').width() - currentSelection.width())/2;
    	$('html,body').animate({scrollLeft: offsetLeft}, DELAY_SCROLL);
}

function nextPhoto(){
	if( slideShowCycleExecuted ){
		resetNavigation();
		slideShowCycleExecuted = false;
	}

	var nextSel = currentSelection.next();
	if( nextSel.is("li") ){
		currentSelection = nextSel;
		movePhoto();
	} else {
		slideShowCycleExecuted = true;
		stopSlideShow();
	}
}

function prevPhoto(){
	var prevSel = currentSelection.prev();
	if( prevSel.is("li") ){
        	currentSelection = prevSel;
		movePhoto();
	}
}

function animatePreview(){
	animateTimeoutID = setTimeout(ANIMATE_PREVIEW_ID, DELAY_SLIDESHOW);
	nextPhoto();

	if( animateTimeoutID != null ){
		$(ANIMATE_PREVIEW).hide();
		$(ANIMATE_STOP).fadeTo(1000, 1);
	}
}

function fireCompleted(img, fullSizePic){
	if( fullSizePic != img.src ){
		img.src=fullSizePic;
	}
}

