var currentProjType;
var filterProject;
var filterMatch;
var totalThumbs;
var currentThumb;
var includeContainer;
var excludeContainer;
var matchingThumbCount;
var scrollCall;
var panelCount;

function buildPanels(currentThumb) {
	if (matchingThumbCount==0) {
		includeContainer += '<div class="panel-wrapper">';
		includeContainer += '<div class="'+totalThumbs.eq(currentThumb).attr("class")+'">'+totalThumbs.eq(currentThumb).html()+'</div>';	
	}
	else if (matchingThumbCount % 9==0)
	{
		includeContainer += '</div><div class="panel-wrapper">';
		includeContainer += '<div class="'+totalThumbs.eq(currentThumb).attr("class")+'">'+totalThumbs.eq(currentThumb).html()+'</div>';
		panelCount++;
	}
	else
	{
		includeContainer += '<div class="'+totalThumbs.eq(currentThumb).attr("class")+'">'+totalThumbs.eq(currentThumb).html()+'</div>';	
	}
	matchingThumbCount++;
}//close buildPanels()

function callPlugins() {	
	$('.items').html(includeContainer);	
	$('#filteredThumbs').html(excludeContainer);
	scrollCall = $("div.scrollable").scrollable({size: 1, clickable:false, api: true});
	scrollCall.seekTo(0); 
	if (panelCount<1) {
		$('#coda-nav-right-1').addClass("disabled");
	}
		
	$(".panel-wrapper a[rel]").overlay({
		effect: 'apple',
		expose: {
			color: '#333',
			opacity: 0.7, 
			closeSpeed: 1250
		},
		onStart: function(event) { 
			if (jQuery.browser.msie) {
				if (jQuery.browser.version==6) {
					filterProject.css("display","none");
				}
			}
		},
		onClose: function(event) { 
			if (jQuery.browser.msie) {
				if (jQuery.browser.version==6) {
					filterProject.css("display","block");
				}
			}
		}
	});
} //close callPlugins()

function buildInterface() {
	totalThumbs = $('.panel-wrapper div, #filteredThumbs div');
	includeContainer="";
	excludeContainer="";
	matchingThumbCount=0;
	panelCount=0;
	currentProjType=filterProject.val();
	
	if (currentProjType == "all") {
		totalThumbs.each(function () {
			currentThumb = $(totalThumbs).index($(this));
			buildPanels(currentThumb);
		}); //close totalThumbs.each
		callPlugins();
	}
	else
	{
		totalThumbs.each(function (currentThumb) {	
			if ($(this).hasClass(currentProjType)) {
				currentThumb = $(totalThumbs).index($(this));
				buildPanels(currentThumb);
			}
			else
			{
				excludeContainer += '<div class="'+$(this).attr("class")+'">'+$(this).html()+'</div>';
			}
		}); //close totalThumbs.each
		includeContainer += '</div>';

		callPlugins();
	}
} //close buildInterface()

$(document).ready(function(){
	
$("#photosOverlay").appendTo("body");
filterProject = $('#filterProject');
filterProject.change(function () {
	buildInterface();
});

buildInterface();
	
}); //doc ready
