//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
$(document).ready(function() {

    //	Init share popup
	$(function() {
        $(".share-button").live('click', function(event) {
            $(".share-list").slideFadeToggle()
            return false;
        });

        $(".share-list-close").live('click', function() {
            $(".share-list").slideFadeToggle();
            return false;
        });
    
		$.fn.slideFadeToggle = function(easing, callback) {
        return this.animate({ opacity: 'toggle', height: 'toggle' }, "fast", easing, callback);
		};
	
	});
});

currentAnimationItem = 0;
stopAnimation = false;

function rotateAnimation()
{
	if (stopAnimation)
	{
		return;
	}

	//	Get the next testimonial
	currentAnimationItem++;
		
	//	Reset if we've hit the end
	if( currentAnimationItem > animationCount )
	{
		currentAnimationItem = 1;
	}

	setTimeout( "changeOpacity( 'slide-container', 10 );",  40 );
	setTimeout( "changeOpacity( 'slide-container', 9 );",  80 );
	setTimeout( "changeOpacity( 'slide-container', 8 );",  120 );
	setTimeout( "changeOpacity( 'slide-container', 7 );",  160 );
	setTimeout( "changeOpacity( 'slide-container', 6 );",  200 );
	setTimeout( "changeOpacity( 'slide-container', 5 );",  240 );
	setTimeout( "changeOpacity( 'slide-container', 4 );",  280 );
	setTimeout( "changeOpacity( 'slide-container', 3 );",  320 );
	setTimeout( "changeOpacity( 'slide-container', 2 );",  360 );
	setTimeout( "changeOpacity( 'slide-container', 1);",   400 );
	setTimeout( "changeOpacity( 'slide-container', 0);",  440 );
	
	//	Has the box been rendered yet? Place the new testimonial
	if (document.getElementById( 'slide-container' ))
	{
		setTimeout( "changeSlide( " + currentAnimationItem + " );",  440 );		
	}

	setTimeout( "changeOpacity( 'slide-container', 1 );", 900 );
	setTimeout( "changeOpacity( 'slide-container', 2 );",  920 );
	setTimeout( "changeOpacity( 'slide-container', 3 );",  940 );
	setTimeout( "changeOpacity( 'slide-container', 4 );",  960 );
	setTimeout( "changeOpacity( 'slide-container', 5 );",  980 );
	setTimeout( "changeOpacity( 'slide-container', 6 );",  1000 );
	setTimeout( "changeOpacity( 'slide-container', 7 );",  1020 );
	setTimeout( "changeOpacity( 'slide-container', 8 );",  1040 );
	setTimeout( "changeOpacity( 'slide-container', 9 );",  1060 );
	setTimeout( "changeOpacity( 'slide-container', 10);",   1080 );
	
	//	Set the slide to change
	setTimeout( "rotateAnimation()", animationTiming * 1000 );
}

function changeOpacity( id, op )
{
	document.getElementById( id ).style.opacity = op/10;
	document.getElementById( id ).style.filter = 'alpha(opacity=' +  (op*10) + ')';	
}

currentSlide = 1;

function changeSlide( slideId )
{
	document.getElementById('animation-circle-'+currentSlide ).src			= '/images/home-box-circle-empty.gif';
	document.getElementById('animation-circle-'+slideId).src	= '/images/home-box-circle.gif';
	
	document.getElementById( 'slide-container' ).innerHTML = document.getElementById( 'slide' + slideId ).innerHTML;

	currentSlide = slideId;
}

function animationPrevious()
{
	stopAnimation = true;

	currentAnimationItem--;

	if( currentAnimationItem == 0 )
	{
		currentAnimationItem = animationCount;
	}

	changeSlide( currentAnimationItem );
}

function animationNext()
{
	stopAnimation = true;

	currentAnimationItem++;

	if( currentAnimationItem > animationCount )
	{
		currentAnimationItem = 1;
	}

	changeSlide( currentAnimationItem );
}

function animationItem( idx )
{
	stopAnimation = true;

	currentAnimationItem = idx;

	if( currentAnimationItem > animationCount )
	{
		currentAnimationItem = 1;
	}

	changeSlide( currentAnimationItem );
}

function loadMailForm(title, link)
{
	$("#videobox").css('display','block');
	$('#videobox').load("/ajax/emailpage.php?title="+title+"&link="+link);
	$('#videobox').center(true);
};

function closeMailForm()
{
	$("#videobox").css('display','none');
};
