﻿var calloutCount = 0;
var counter = 0;
var previousIndex = 0;

var delayTimer;
var timeBetweenFades = 8000; // Amount of time (in Milli-seconds) the callouts are visible.
var fadeInSpeed = 1000;
var fadeOutSpeed = 1000;

$(function() 
{
    initMainCallout();
    
    $('#calloutControls .calloutIndicator').stop('true').click(function()
    {
        clearTimeout(delayTimer);
        var calloutIndex = $('#calloutControls .calloutIndicator').index($(this));
        calloutAnimate(calloutIndex);
    });
    
    $('#calloutContainer .calloutElement').hover(function()
    {
        clearTimeout(delayTimer);
    }, function()
    {
        initLoop();
    });
    
    $('.scrollToLocation').click(function()
    {
        var location = $(this).attr('href');
        location = location.replace('#', '');
        
        scrollToLocation(location);
        
        return false;
    });
    
    $('.backToTopLocation').click(function()
    {        
        scrollToLocation('topLocation');
        
        return false;
    });
});

function scrollToLocation(location)
{
    if(location != 0)
    {
        var locationPosition = $('#'+location).offset().top;
    }
    
    else
    {
        locationPosition = location;
    }
    
    $('html, body').animate(
    {
        scrollTop:locationPosition
    }, 500);
}

function initMainCallout()
{
    $('#calloutContainer .calloutElement').hide();
    $('#calloutContainer .calloutElement').eq(0).show();
    
    calloutCount = $('#calloutContainer .calloutElement').size();
    
    initCalloutIndicator();
}

function initCalloutIndicator()
{   
    for(var i = 1; i < calloutCount+1; i++)
    {
        $('#calloutControls').append("<div class='calloutIndicator'>"+ i +"</div>");
    }
    
    $('#calloutControls .calloutIndicator').eq(0).attr('id', 'active');
}

function getPreviousIndex(index)
{
    if(index == 0)
    {
       previousIndex = (calloutCount - 1); 
    }
    
    else
    {
        previousIndex = (index - 1);
    }
}

function animateCalloutIndicator(index)
{   
    $('#calloutControls .calloutIndicator').attr('id', '');
    $('#calloutControls .calloutIndicator').eq(index).attr('id', 'active');
}

function calloutAnimate(index)
{
	animateCalloutIndicator(index);
	getPreviousIndex(index);
	
	$('#calloutContainer .calloutElement').eq(previousIndex).fadeOut(fadeOutSpeed);
	$('#calloutContainer .calloutElement').eq(index).fadeIn(fadeInSpeed, function()
	{
		if(calloutCount == 1)
		{
			return false;
		}
		
		else
		{
			counter++;
			initLoop();
			return true;
		}
	});
}

///////////////////////////////////////////////////////////////////////////
//		initLoop() - performs the timeouts and looping through the items
///////////////////////////////////////////////////////////////////////////

function initLoop()
{
	if(calloutCount == 1)
	{
	    getPreviousIndex(index);    
		calloutAnimate(counter);
	}
	
	else
	{
		clearTimeout(delayTimer);

		if(counter < calloutCount)
		{
		    previousIndex = counter - 1;
			delayTimer = setTimeout('calloutAnimate("'+counter+'");', timeBetweenFades);
		}
		
		else
		{
		    previousIndex = counter;
			counter = 0;
			delayTimer = setTimeout('calloutAnimate("'+counter+'");', timeBetweenFades);
		}	
	}
}
