
//Helper variables
var currentDeckIndex = 0; //index of visible slide deck
var currentSlide = 0; //index of visible slide withing the visible slide deck
var decks; //array of decks and number of slides in each
var slideWidth; //The width of the each slide
var timer;  //variable to hold the slideshow timer loop


/*-start
-------------------*/
$(function(){
	//stop if there are no slide decks
	if($('.slide-deck').size() === 0){
		return;
	}
	
	//initialize the slides
	init_slides();
	
	/* Start the timer only after the images have been loaded */
	
	if(!$('body').hasClass('portfolio')){
		$(window).load(function() {
			resetTimer();
		});
	}
});



function init_slides(){
	//get slide width
	slideWidth = $('.slide-deck').width();
	
	//set slide-deck height
	$('.slide-deck ul.slides').height($('.slide-deck:first').height());
	
	
	//if there is a hash id, show that deck first
	if(window.location.hash)
	{
		entryid = window.location.hash.slice(1);
		
		deck = $('.slide-deck#entry-'+entryid);
		
		if(deck.size()>0){
			//select current deck
			$('.slide-deck.current').removeClass('current');
			deck.addClass('current');
			
			//select current deck pager
			$('.deck-pager>li.current').removeClass('current');
			$('.deck-pager>li:eq('+deck.index()+')').addClass('current');
			
			//show correct description
			$('.project-overviews>.current').removeClass('current');
			$('.project-overviews>div:eq('+deck.index()+')').addClass('current');
		}
	}
	
	//Set current deck index
	currentDeckIndex = $('.slide-deck.current').index();
	
	
	//make an array to track the number of slides in each deck
	decks = new Array();
	$('.slide-deck').each(function(index) {
		//get slide count
	  decks[index] = $(this).find('.slides>li').size();
	
		//set with of sliding element, it needs to be +1 bigger to accomodate the infinite scroll
		$(this).find('.slides').width((decks[index] + 1) * slideWidth);
		
		//Bind mouse events
		$(this).hover(function() {
			// Stuff to do when the mouse enters the element;
			
		}, function() {	
			// Stuff to do when the mouse leaves the element;
			resetTimer();
		});
		
		createPagers(this);
	});
	
	//make the slides visible
	$('.slide-deck .slides>li').show();
	
	//give portfolio slide pagers the "loading" class
	$('.portfolio-entries .slide-pager li').addClass('loading');
	
	//bind deck pager
	$('.deck-pager li').click(function() {
		if(!$(this).hasClass('loaded'))
		{
			return false;
		}
	
		showDeck($(this).index());
		$(this).siblings('.current').removeClass('current');
		$(this).addClass('current');
		
		//reset the slide-timer
		resetTimer();
		
		//set url hash
		window.location.hash = $(this).children('a').attr('href');
		
		return false;
	});
	
	//bind loaded event for slide decks
	$('.portfolio-entries .slide-deck').bind('loaded', function(){
		window.log('Loaded Slide Deck: '+ $(this).index());
		
		//mark this as loaded
		$(this).addClass('loaded');
		
		//show the thumbnail for this deck
		loadThumbnail($('.deck-pager>li').eq($(this).index()));
		
		//show the images and pager in this deck
		$('img', $(this)).fadeIn();
		
		//load next slide-deck
		loadImages($('.slide-deck:not(.loaded)').first());
		
		
		//if this is the first one, reset the timer.
		if($(this).hasClass('current'))
		{
			resetTimer();
		}
		
	});
	
	
	
	//bind load event for thumbs
	$('.deck-pager img').load(function(){
		$(this).fadeIn();
		window.log('load thumb');
	});
	
	loadImages($('.portfolio-entries .slide-deck.current .slides'));
	
}

function loadThumbnail(context)
{
	$('img', context).attr('src', $('img', context).attr('dsrc'));
	context.addClass('loaded');
}

function loadImages(context){

	img = $('img:first', context);
	
	//set the image's source so it starts loading
	img.attr('src', img.attr('dsrc'));

	//apply the source attribute based on the dsrc attribute
	img.load(function(){
			
			//show this image
			$(this).fadeIn();
		
			//show the pager for this image
			$(this).closest('.slide-deck').find('.slide-pager li').eq($(this).parent().index()).removeClass('loading');
			
			
		
			//if this is the last image, bind it's load event
			if($(this).parent().is(":last-child"))
			{
				$(this).closest('.slide-deck').trigger('loaded');
			}else{
				//call this function again but use the next slide as the context
				loadImages($(this).parent().next());
			}
	});
}


function createPagers (slideDeck) {
	//make pager list
	pager = $('<ul class="slide-pager"></ul>');
	
	//append it to slidedeck
	$(slideDeck).append(pager);
	
	for (var i = decks[$(slideDeck).index()] - 1; i >= 0; i--){
		pager.append('<li><a href="#"></a></li>');
	};
	
	pager.find('a:first').addClass('current');
	
	//bind pager click events
	$('a', pager).click(function(){
		
		if($(this).hasClass('loading'))
		{
			return false;
		}
	
		//pause the slideshow until the mouse leaves
		killTimer();
		
		showSlide($(this).parent().index());
		return false;
	});
}

function showNextSlide () {	
	//if this is the last slide show the first one, else show the next one.
	showSlide(currentSlide+1);
}

function showSlide(index, speed)
{
	//set default speed
	if (!speed) { speed = 750;}
	
	//get offset value
	offset = index * slideWidth * -1;
	
	//setup Infinite scroll if the target index is greater than the last index
	if (index === decks[currentDeckIndex]) {
		window.log('looping-back');
		dupeFistSlide();
		
		//shenanigans done, make index 0
		index = 0;
	};
	
	//Select Pager
	selectPager(index);
	
	currentSlide = index;
	
	if (speed > 0) {
		//animate slides
		$('.slides', currentDeck()).animate({'left': offset + 'px'}, speed, function() {
			//remove duplicate after animation
			removeDupe();
		});
	}else{
		//just shift em
		$('.slides', currentDeck()).css('left', offset + 'px');
		removeDupe();
	}
}

function dupeFistSlide () {
	$('.slide:first', currentDeck())
			.clone()
			.appendTo($('.slides', currentDeck()));
}

function removeDupe () {
	lastSlide = decks[currentDeckIndex] - 1;
	$('.slide:gt('+ lastSlide +')', currentDeck()).remove();
	
	if (currentSlide === 0) {
		$('.slides', currentDeck()).css('left', '0px');
	}
}


function showDeck (index) {
	//Transition deck
	currentDeck().fadeOut().removeClass('current');
	$('.slide-deck').eq(index).fadeIn().addClass('current');
	
	//transition overview
	nextView = $('.project-overviews .overview').eq(index);
	$('.project-overviews .overview.current').css('position','absolute').removeClass('current').fadeOut();
	nextView.fadeIn().addClass('current');
	

	$('.project-overviews').animate({height: nextView.height()});
	
	currentDeckIndex = index;
	showSlide(0, 0);
}

function selectPager (index) {
	pager = $('.slide-pager', currentDeck());
	$('a.current', pager).removeClass('current');
	$('li', pager).eq(index).find('a').addClass('current');
}

function currentDeck() {
	return $('.slide-deck:eq('+ currentDeckIndex + ')');
}

function resetTimer () {

	clearInterval(timer);

	//create timer loop
	timer = setInterval ( "showNextSlide()", 7000);
}

function killTimer()
{
	clearInterval(timer);
}


