var totalSliders = 9;
var SliderShortWidth = 48, SliderShortHeight = 317;
var SliderLargeWidth = 548;
var BorderWidth = 2;

var autoScrollTimeout = 5000, autoTimer = null;

var turnOffTimer = null;
function offTimer()
{
    if( turnOffTimer )
	{
	clearTimeout( turnOffTimer );
	turnOffTimer = null;
	}
}

function clearMenu()
{
    offTimer();
    $('.mmi').removeClass('ammi');
    $('.smd').hide();
}

function turnSM( e )
{
    $(this).toggleClass('smdia'); 
}

function init()
{
    $('.smib').fadeTo('slow',0.5);
    $('.smi img').click( switchSlides );
    
    $('.mmi a').each( function(i,c) { $(this).parent().click( function() { window.location = c.href; } ); } );
    $('.smdi a').each( function(i,c) { $(this).parent().click( function() { window.location = c.href; } ); } );

    autoScrollIt();
}

var currSlide = 1; var targetSlide = 1, animated = false;
function switchSlides( e )
{
	e.preventDefault();
	if( animated )
		return;
	targetSlide = $(this).parent().parent().parent().attr('id').substr(2);
	if( targetSlide == currSlide )
		return;
	
	clearAutoScroll();
	autoScrollTimeout = 20000;
	animateSlides();
}

function autoScroll()
{
	clearAutoScroll();
	autoScrollTimeout = 5000;
	targetSlide = Math.floor(currSlide)+1;
	if( targetSlide > totalSliders )
		targetSlide = 1;
	animateSlides();
}

function animateSlides()
{
	animated = true;
	for( i=1; i<=totalSliders; i++ )
		{
		opt = { left: SliderShortWidth*(i-1)+(i>1?BorderWidth:0)*(i-1)+(i>targetSlide?SliderLargeWidth-SliderShortWidth:0), width: (targetSlide==i?SliderLargeWidth:SliderShortWidth) };
		if( i!=totalSliders || $.support.opacity )
			$('#sl'+i).animate( opt, 'slow' );
		else
			$('#sl'+i).animate( opt, 'slow', swapSmall );
		}
	if( $.support.opacity )
		{
		$('#sl'+currSlide+' .smc').show('slow');
		$('#sl'+targetSlide+' .smc').hide('slow', swapSmall );
		}
	else
		$('#sl'+targetSlide+' .smc').hide();
}

function autoScrollIt()
{
	if( autoTimer )
		clearTimeout( autoTimer );
	autoTimer = setTimeout( 'autoScroll()', autoScrollTimeout );
}

function clearAutoScroll()
{
	if( autoTimer )
		{
		clearTimeout( autoTimer );
		autoTimer = null;
		}
}

function swapSmall()
{
	$('#sl'+currSlide+' .smc').show();
	currSlide = targetSlide; 
	animated = false;

	autoScrollIt();
}


