var group = 1;
var total = 1;
var currentPic = 0;

function CreateGroups()
{
	total = Math.ceil($("#thumbGallery li").length/16);
	var numstring = "";
	for(i=0;i<total;i++)
	{
		if(numstring != "")
		{ numstring += ',';}
		numstring += '<a href="#" class="num" name="'+(i+1)+'">'+(i+1)+'</a>';
		for(e=0;e<16;e++)
		{
			if($("#thumbGallery li")[e + i*16])
			{
				$("#thumbGallery li")[e+(i*16)].className="a"+(i+1);
			}
		}
	}
	$("#numbers")[0].innerHTML = numstring;
}

function switchGroup()
{
	//keep group within boundaries
	if(group>total)
	{	group = 1;	}
	if(group<1)
	{	group = total; }
	
	//define default and dynamic classes
	var allclasses = ".a1";
	var dclass = ".a"+group;
	
	//grab all class names to add to all classes variable
	for (i=0;i<total;i++)
	{
		if(i!=0)
		{
			allclasses += ", .a"+(i+1);
		}
	}
	//hide all elements with default class except for the ones with the dynamic class
	$(allclasses).css("display","none");
	//$(dclass).css("display","list-item");
	$(dclass).css("display","block");
	
	var lnks = document.getElementById("numbers").getElementsByTagName("a");
	$('.num').removeClass("sel");
	for(i=0;i<lnks.length;i++)
	{
		if(lnks[i].name==group.toString())
		{
			lnks[i].className = "num sel";
		}
	}
}

function switchImg(u)
{
	$('#featuredImage img').attr({
		src: ""
	});
	var long = $('#thumbGallery li a').length-1;
	//make sure it loops through available photos
	if(u>long){u -= long;}
	if(u<0){u += long;}
	var title = $('#thumbGallery li a')[u].title;
	var hRef = $('#thumbGallery li a')[u].href;
	$('#featuredImage img').attr({
		src: hRef
	});
	//Caption's removed for Targhee Landing
	//$('#featuredImage .caption')[0].innerHTML = title;
}

$(document).ready(function(){
	CreateGroups()
	switchGroup();
	if(total>10) {
		total =10;
	}
	//Gallery Control Listeners
	$('#galleryControls .nextBtn').click(function() {
		group++;
		switchGroup();
		return false;
	})
	$('#galleryControls .prevBtn').click(function() {
		group--;
		switchGroup();
		return false;
	})
	$('#galleryControls .num').click(function() {
		group=$(this).attr('name');
		switchGroup();
		return false;
	})
	
	$('#thumbGallery a').click(function() {
		for(i=0;i<$('#thumbGallery li').length;i++)
		{
			if($('#thumbGallery li a')[i]==this)
			{	currentPic = i;	}
		}
		switchImg(currentPic);
		$('#featuredImage').fadeIn(400);
		$('#galleryControls div').fadeOut(400);
		return false;
	})
	
	$("#featuredImage a.closeBtn").click(function() {
		$('#featuredImage').fadeOut(400);
		$('#galleryControls div').fadeIn(400);
		return false;
	});
	
	$("#featuredImage .prevBtn a").click(function() {
		currentPic --;
		switchImg(currentPic);
		return false;
	});
	$("#featuredImage .nextBtn a").click(function() {
		currentPic ++;
		switchImg(currentPic);
		return false;
	});
});
