var currentIndex = 0;
var actualIndex = 0;
var noImages = 0;
var incrementValue = 0;
var timer = false;

var animActivity = false;
var animState = false;
var animTime = 800;
var animTimeout = 5000;
var animEasing = 'linear';

var currentPos = 0;
var overLimit = false;

$(document).ready(function(){
	$('.mobile').css('position', 'absolute');
	$('#lform .field .input input[name="password"]').focus();
	initGalleryCarousel();
	if($('#content img').size() == 0)
	{
		onWindowResize();
	}
	else
	{
		$('#content img').load(function(){$(this).unbind('load'); onWindowResize();});
	}
});

$(window).resize(function(){
	onWindowResize();
});


$(function() {
	$('.gallery a').lightBox();
});

function onWindowResize()
{
	if($('.mobile').size() == 0)
	{
		var wh = $(window).height();
		var ch = wh - 264;
		if($('#content').height() < ch)
		{
			$('#content').height(ch);
		}
		
		//process backgrounds
		var ih = $('#bgwrap #bg #bgl img').height();
		var dh = wh-ih;
		
		$('#bgwrap, #bgwrap #bg').css('height', '');
		$('#bgwrap #bg div img').css('margin-top', '');
		$('#bgwrap, #bgwrap #bg').height(wh);
		//document.title = "ih:"+ih+", wh:"+wh+", dh:"+dh;
		$('#bgwrap #bg div img').css('margin-top', dh);
	}
}

function initGalleryCarousel()
{
	stopTimer();
	
	//calculate number of items
	noImages = $('#galleryholder .slider-holder li').size();
	if(noImages  > 1)
	{
		animActivity = true;
		
		//make sure gallery is relative
		$("#galleryholder").css('position', 'relative');
		
		//the increment value
		incrementValue = $('#galleryholder').width();
		
		//wrap gallery
		var list = $("#galleryholder").html();
		$("#galleryholder").html('<div class="slider-wrap">'+list+'</div>');
		$("#galleryholder .slider-holder").css('overflow', 'visible');
		$("#galleryholder .slider-holder").css('position', 'absolute');
		$("#galleryholder .slider-holder").css('left', 0);
		$("#galleryholder .slider-holder").width(incrementValue*(noImages+1));
		currentIndex = 0;

		//update navigation
		//addButtons();
		
		startTimer();
	}
	
	updateNavigation();
}

/////////////////////////////
// TIMER FUNC
/////////////////////////////

function startTimer()
{
	animState = "pause";
	timer = setTimeout("doTimerSlide()", animTimeout);
}

function stopTimer()
{
	currentPos = 0;
	animState = "stopped";
	animActivity = false;
	clearTimeout(timer);
}

function doTimerSlide()
{
	incrementSlide(1);
}

/////////////////////////////
//SLIDE FUNC
/////////////////////////////

function onSlideComplete()
{
	if(overLimit)
	{
		if(actualIndex == noImages)
		{
			currentIndex = 0;
			currentPos = 0;
			$('#galleryholder .slider-holder li:last').remove();
			
		}
		else if(actualIndex < 0)
		{
			currentIndex = noImages-1;
			currentPos = -currentIndex*incrementValue;
			$('#galleryholder .slider-holder li:first').remove();
		}
		$('#galleryholder .slider-holder').css('left', currentPos);
		overLimit = false;
	}

	updateNavigation();
	startTimer();
}

function doSlide()
{
	animState = "playing";
	$('#galleryholder .slider-holder').animate({"left":currentPos+"px"}, animTime, animEasing, onSlideComplete);
}

/////////////////////////////
//PROCESSING FUNC
/////////////////////////////

function incrementSlide(value)
{
	if(animState == "pause")
	{
		clearTimeout(timer);
		currentIndex+=value;
		actualIndex = currentIndex;
		
		//process new position
		currentPos-= incrementValue*value;
		
		if(currentIndex == noImages)
		{
			currentIndex = 0;
			overLimit = true;
			$('#galleryholder .slider-holder li:first').clone().appendTo('#galleryholder .slider-holder');
		}
		else if(currentIndex < 0)
		{
			currentIndex = (noImages-1);
			overLimit = true;
			$('#galleryholder .slider-holder li:last').clone().prependTo('#galleryholder .slider-holder');
			$('#galleryholder .slider-holder').css('left', incrementValue*value);
			currentPos = 0;
		}
		doSlide();
	}
}

/////////////////////////////
//NAVIGATION FUNC
/////////////////////////////

/////////////////////////////
//NAVIGATION FUNC
/////////////////////////////

function updateNavigation()
{
	$('#gallerynav a').unbind('click');
	if(noImages > 1)
	{
		$('#gallerynav').show();
		$('#gallerynav a').click(function(){processCarouselNavigationClick($(this)); return false;});
		$('#gallerynav .optionBtn').toggleClass('selected', false);
		$('#gallerynav .optionBtn:eq('+currentIndex+')').toggleClass('selected', true);
	}
}

function processCarouselNavigationClick(atag)
{
	var inc = 0;
	if(atag.find('.optionBtn').size() > 0)
	{
		inc = (atag.parent().index()-1)-currentIndex;
	}
	else if(atag.find('.prevBtn').size() > 0)
	{
		inc = -1;
	}
	else if(atag.find('.nextBtn').size() > 0)
	{
		inc = 1;
	}
	incrementSlide(inc);
}

function addButtons()
{
	if(!$("#galleryholder .slideshow-back-next").size())
	{
		$("#galleryholder").append('<ul class="slideshow-back-next"><li class="back-button"><a href="javascript:;">&nbsp;</a></li><li class="next-button"><a href="javascript:;">&nbsp;</a></li></ul>');
		$("#galleryholder .back-button").click(function(){incrementSlide(-1);});
		$("#galleryholder .next-button").click(function(){incrementSlide(1);});
	}
}

