var iCurrent = 0;
var iPrev = 0;
var aPics = new Array();
var iFadeTime = 500; //Time fading between images when big image clicked, in ms
var i;

	
function setPic(iPic)
	{
	if ( iPic != iCurrent )
	
		{
		iPrev = iCurrent;
		iCurrent = iPic;
		$(aPics[iPrev]).fadeOut(iFadeTime);
		$(aPics[iCurrent]).fadeIn(iFadeTime);	
		}
	}

function nextPic()
	{
	iPrev = iCurrent;
	iCurrent = (iCurrent + 1) % aPics.length;
	$(aPics[iPrev]).fadeOut(iFadeTime);
	$(aPics[iCurrent]).fadeIn(iFadeTime);	
	}
	
function initPics( )

	{
/*		
	$('#primary-nav > li >  a').mouseover( function(){	$(this).animate({color: '#FFFFFF', backgroundColor: '#3c3c3c', borderTopColor: '#FFFFFF', borderBottomColor: '#FFFFFF'}, 300); });
	$('#primary-nav > li > a').mouseout( function(){	$(this).animate({color: '#BBBBBB', backgroundColor: '#1a1a1a', borderTopColor: '#9C9C9C', borderBottomColor: '#9C9C9C'}, 150); }); */
	
	aPics = $('#fadepics img');
	
	iPrev = aPics.length - 1;

//Set the Z index of each image in turn
	z= aPics.length;
	$(aPics).each(function() { 
		$(this).css('z-index', z); 
		z--; 
		});

	//Make all the images visible
	$(aPics).each( function() { $(this).css('display', 'block'); $(this).hide(); } );
	
	$(aPics[0]).show();
	
	//Set nextPic to be called on clicking the main pic
	$(aPics).click(nextPic);
	
	$('#thumbnails > img').click( function(event)
		{ 
		setPic( $('#thumbnails > img').index(event.target)	);
		} );

	$('#thumbnails > img').hover( function(event)
		{ 
		setPic( $('#thumbnails > img').index(event.target)	);
		} );

	
	}

// $(document).ready(initPics);

		
var iShopLinkFadeTime = 300;
var bShopLinkShown = false;
var bShopLinkHovering = false;
var iShopLinkShows = 0;
var iShopLinkTotalShows = 9;
var shopLinkTimeoutID;
var iShopLinkShowTime = 800;

function animateShopLink () {
	if ( iShopLinkShows < iShopLinkTotalShows )	
		{
		if ( bShopLinkShown )
			$('#shopHover').fadeOut(iShopLinkFadeTime);	
		else
			$('#shopHover').fadeIn(iShopLinkFadeTime);	
		bShopLinkShown = ! bShopLinkShown;
		iShopLinkShows++;
		shopLinkTimeoutID = window.setTimeout( animateShopLink, iShopLinkShowTime);
		}
}

function mOver() {
	if ( ! bShopLinkHovering ) {	//This should stop the fadeIn() being called twice, but doesn;t
		bShopLinkHovering = true;
		iShopLinkNoShows = iShopLinkTotalShows;
		$('#shopHover').stop(true, true); //Cancel previous call
		$('#shopHover').fadeIn(iFadeTime);		
	}
}

function mOut() {
	iShopLinkShows = iShopLinkTotalShows;
	bShopLinkHovering = false;
	$('#shopHover').fadeOut(iShopLinkFadeTime);		
}


function initShopLink() {
	$('#shopLinkWrapper').mouseover( mOver);
	$('#shopLinkWrapper').mouseout( mOut);
	animateShopLink();
}

$(document).ready( function () {
							 initPics();
							 initShopLink();});

