$(document).ready(function(){
	
	$('.msgok').each(coolReveal);
	$('.msgerror').each(coolReveal);
	
	$('A[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});

});

function startSplashFades()
{
	var iSlideshowFadeSpeed = 6000;
	var iSlideshowTimeout = 10;
	
	$('.splashcontent_slides01').cycle({ 
		fx: 'fade', 
		speed: iSlideshowFadeSpeed,
		timeout: iSlideshowTimeout,
		delay:   -6000
	});
	
	$('.splashcontent_slides02').cycle({ 
		fx: 'fade', 
		speed: iSlideshowFadeSpeed,
		timeout: iSlideshowTimeout
	});
}

function coolReveal()
{
	$(this).hide();
	$(this).slideDown('slow'); 
}









var iSpeedInterval = 30;
var iSpeedPXNatural = 7;
var iSpeedPXFasterClick = 470;
var iSpeedPX = iSpeedPXNatural;
var iTotalWidthOfStrip = 5000;
var iMinTotalWidth = 570;
var iMaxRight = 0;
var iMaxLeft = (iTotalWidthOfStrip - iMinTotalWidth) * -1;
var bMoveLeft = false;
var bMoveRight = false;
var sCurrentYear = '2008';
var t;

function initializeGalleries()
{
	$('.thumbwrapper_inner a').lightBox();
	
	$('.arrowleft').hover(
		function(){
			determineCurrentYear(this);
			bMoveLeft = true;
		},
		function(){
			determineCurrentYear(this);
			bMoveLeft = false;
		}
	);

	$('.arrowright').hover(
		function(){
			determineCurrentYear(this);
			bMoveRight = true;
		},
		function(){
			determineCurrentYear(this);
			bMoveRight = false;
		}
	);
	
	$('.arrowleft').click(
		function(){
			determineCurrentYear(this);
			iSpeedPX = iSpeedPXFasterClick;				
		}
	);
	
	$('.arrowright').click(
		function(){
			determineCurrentYear(this);
			iSpeedPX = iSpeedPXFasterClick;	
		}
	);
	
	setTimeout(setOfficialWidths, 1000);
			
	t = setTimeout(MoverChecker, 500);
}

function determineCurrentYear(oThis)
{
	jQueryThis = $(oThis);
	sCurrentYear = jQueryThis.parent().find('h4').text();
}

function getCurLeft()
{
	var iCurCssLeft = $('#thumbwrapper_inner' + sCurrentYear).css('left');
	
	// strip off "px"
	iCurCssLeft = String(iCurCssLeft).substring(0,String(iCurCssLeft).length-2);
	
	// properly handle negative numbers
	iCurCssLeft = parseInt(iCurCssLeft);
	
	return iCurCssLeft;
}

function MoverChecker()
{	
	oContainer = $('#thumbwrapper_inner' + sCurrentYear);
	iTotalWidthOfStrip = oContainer.width() - 10;
	iMaxLeft = (iTotalWidthOfStrip - iMinTotalWidth) * -1;
	
	if (bMoveLeft)
	{
		if (iTotalWidthOfStrip > iMinTotalWidth)
		{
			var iCurCssLeft = getCurLeft();
			if (iCurCssLeft < iMaxRight)
			{
				var iNewPos = iCurCssLeft+iSpeedPX;
				if (iNewPos > iMaxRight)
				{
					iNewPos = iMaxRight;
				}
				oContainer.css('left', (iNewPos)+'px');
			}
		}				
	}
	else if (bMoveRight)
	{
		if (iTotalWidthOfStrip > iMinTotalWidth)
		{
			var iCurCssLeft = getCurLeft();
			if (iCurCssLeft > iMaxLeft)
			{
				var iNewPos = iCurCssLeft-iSpeedPX;
				if (iNewPos < iMaxLeft)
				{
					iNewPos = iMaxLeft;
				}					
				oContainer.css('left', (iNewPos)+'px');
			}								
		}			
	}
	
	if (iSpeedPX != iSpeedPXNatural)
	{
		iSpeedPX = iSpeedPXNatural;
	}
	
	t = setTimeout(MoverChecker, iSpeedInterval);
}

function setOfficialWidths()
{
	$('.thumbwrapper_inner').each(function(){
		iTotalWidth = 0;
		oThisContainer = $(this);
		oThisContainer.find('img').each(function(){
			oThisImg = $(this);
			iImgWidth = oThisImg.width();
			iTotalWidth = iTotalWidth + iImgWidth + 2 + 10;	
		});
		oThisContainer.width(iTotalWidth);
	});
	
	setTimeout(setOfficialWidths, 2000);
}


		

