// global variables

var quoteClass = 1;
var originalList;
var pathName;
var isInTransition = false;

$().ready(function(){
	
	// get page
	pathName = window.location.pathname.substring(window.location.pathname.lastIndexOf("/") + 1);
	
	// Set up click events for the images
	$("#partnerThumbs img").click(function(){
		if (!isInTransition)
		{
			isInTransition = true;
			var partner = $(this).attr('src').split('-thumb')[0].split('images/')[1];
			SwitchPartners(partner);
			SwitchThumbnails($(this));
		}
	});
	
	setInterval(function() {
      // Do something every 8 seconds
	  if ($('#partnerThumbs img.active').next().length)
	  {
	  	$('#partnerThumbs img.active').next().click();
	  }
	  else
	  {
		if (quoteClass==1)
		{
			quoteClass=2;	
		}
		else {
			quoteClass=1;	
		}
		$('#partnerThumbs img:first').click();	  
	  }
	}, 8000);
	
	// store the list of team members for use later
	originalList = $('#brazosTeamList').children('li').get();
	
	// sort Team List alphabetically
	$('a#sortAlpha').click(function(){		
		if ($(this).text() == "Sort Alphabetically")
		{
			SortMyList('#brazosTeamList', 'alpha');
			$("#rightBar a").each(function(){
				var href = $(this).attr('href');
				$(this).attr('href',href + "#alpha");					   
			});
		}
		else {
			SortMyList('#brazosTeamList', 'original');
			$("#rightBar a").each(function(){
				if (window.location.hash.length > 0)
				{
					var href = $(this).attr('href');
					$(this).attr('href',href.substring(0,href.indexOf('#')));		
				}
			});
		}
		return false;
	});
	
	$("#rightBar a[href=" + pathName + "]").parent().addClass("active");
	
	if (window.location.hash == "#alpha")
	{
		SortMyList('#brazosTeamList', 'alpha');
		$("#rightBar a").each(function(){
			var href = $(this).attr('href');
			$(this).attr('href',href + "#alpha");					   
		});
	}
});

function SwitchPartners(newPartner) {

	
	$('.partnerQuote.active').fadeOut(1500, function(){
		
		//change quote between first and second
		
		if (quoteClass==2)
		{
			$('div.quote p.second').show().addClass("active");	
			$('div.quote p.first').hide().removeClass("active");
			
		}
		else {
			$('div.quote p.second').hide().removeClass("active");
			$('div.quote p.first').show().addClass("active");
	
		}
		
		$(this).removeClass('active');
		$('.partnerQuote.'+newPartner).fadeIn(1500,function(){														  
			$(this).addClass('active');
			isInTransition = false;
			});
	});
}

function SwitchThumbnails(newImage) {
	var currentSRC = $('#partnerThumbs img.active').attr('src');
	$('#partnerThumbs img.active').attr('src', (currentSRC.split('-on')[0] + '.jpg')).removeClass('active');
	var newImagesrc = newImage.attr('src').split('.jpg')[0] + '-on.jpg';
	newImage.attr('src',newImagesrc).addClass('active');
}

function SortMyList(myList, sortBy){
	if (typeof myList == 'string'){
		myList = $(myList);
	}
	if (sortBy == "alpha")
	{
		var listitems = myList.children('li').get();
		listitems.sort(function(a, b) {
								
		   var compA = $(a).children().attr("rel").toUpperCase();
		   var compB = $(b).children().attr("rel").toUpperCase();
		   return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
		})
		$("a#sortAlpha").text("Sort By Title");
	}
	else {
		listitems = originalList;	
		$("a#sortAlpha").text("Sort Alphabetically");
	}
	$.each(listitems, function(idx, itm) { myList.append(itm); });	
	
}
