 // prevent errors in browsers without console
if (typeof console == "undefined") {
    this.console = {log: function() {}};
}

$(function() {
	
	/* members login */
	$('#members-login-toggle').click(function(event) {
		event.preventDefault();
		$('#members-login').slideToggle();
	});
	
	$('#cancel-members-login').click(function(event) {
		event.preventDefault();	
		$('#members-login').slideUp();
	});
	
	/* homeowner's login */
	$('#homeowner-login-toggle').click(function(event) {
		event.preventDefault();
		$('#homeowner-login').slideToggle();
	});
	
	$('#cancel-homeowner-login').click(function(event) {
		event.preventDefault();	
		$('#homeowner-login').slideUp();
	});	
	
	
	/* show/hide arrow on hover */
	$('ul.adxm li').hover(function() {
		$(this).addClass('nav-arrow');
	}, function() { 
		$(this).removeClass('nav-arrow');
	});

	/* remove last dotted image separator in nav */
	$('ul.menu li:last-child').css('backgroundImage', 'none');
	
	/* remove last dotted image separator in footer */
	$('#footer .inner a:last-child').css('background', 'none');
		
	/* carousel */
	$('#carousel li:lt(2)').clone().appendTo('#carousel'); // put the first two items at the end so we can loop around
	
	var indicatorsWidth = $('#carousel-indicators').width();
	var newLeft = (620 / 2) - (indicatorsWidth / 2) + 6;
	$('#carousel-indicators').attr('style', 'left:'+ newLeft + 'px');
	
	var carouselTimeout = null;
	var itemsPerFrame = 2;
	var currentFrame = 0; // zero indexed
	var frameWidth = 640;
	var numberOfItems = $('#carousel li').length;
	var numberOfFrames = Math.ceil(numberOfItems / itemsPerFrame);
	var animating = false;
	
	if(window.addEventListener) {
		window.addEventListener('focus', function() { startAutoPlay(); }, false);
		window.addEventListener('blur', function() { stopAutoPlay(); }, false);
	}
	
	function stopAutoPlay()	{
		clearTimeout(carouselTimeout);
	}
	
	function startAutoPlay() {		
		clearTimeout(carouselTimeout);
		carouselTimeout = setTimeout(function() { doAnimation(); }, 5000);
	}
	
	$('div#carousel-wrapper').hover(function() {
		stopAutoPlay();
	}, function() {
		startAutoPlay();
	});	
	
	function doAnimation() {

		// don't do anything if there's one frame or less
		if(numberOfFrames <= 1) { return; }

		animating = true;

		// jump back to the start if at the last frame
		if(currentFrame == (numberOfFrames-1)) {
			$('#carousel').css('left', '0px');
			currentFrame = 0;
		}

		// increment the frame
		if(currentFrame == (numberOfFrames-1)) {
			currentFrame = 0;
		} else {
			currentFrame++;
		}

		updateIndicators();

		// move the carousel ul
		$('#carousel').animate({
			left: '-=' + frameWidth
		}, { duration: 700, queue: false, easing: 'easeInOutSine', complete: function() {
			clearTimeout(carouselTimeout);
			carouselTimeout = setTimeout(function() { doAnimation(); }, 5000);
			animating = false;
		}});
	}
	
	// start the carousel
	carouselTimeout = setTimeout(function() { doAnimation(); }, 3000);
	
	function updateIndicators()	{
		
		// change the indicator image
		var indicatorToTurnOn = currentFrame+1;
		if((currentFrame+1) == numberOfFrames) { // currentFrame + 1 because it's zero indexed
			indicatorToTurnOn = 1;
		}

		// update indicator
		$('#carousel-indicators li a').removeClass('on');
		$('#carousel-indicators li:nth-child('+indicatorToTurnOn+') a').addClass('on');
	}
	
	$('.carousel-arrow-prev').click(function(event) {
		event.preventDefault();
		
		if(animating) { return; } else { animating = true }
		
		clearTimeout(carouselTimeout);
		var frameSelected = currentFrame--;

		// go to the end and go back one
		if(frameSelected==0) { 
			$('#carousel').css('left', '-' + ((numberOfFrames - 1) * frameWidth) + 'px');
			currentFrame = numberOfFrames - 2;
		}
		
		updateIndicators();
		
		$('#carousel').animate({
			left: '+=640px'
		}, { duration: 700, queue: false, easing: 'easeInOutSine', complete: function() {
			animating = false;
		}});
		
	});
	
	$('.carousel-arrow-next').click(function(event) {
		event.preventDefault();
		
		if(animating) { return; } else { animating = true; }
		
		clearTimeout(carouselTimeout);
		var frameSelected = currentFrame++;

		// go to the end and go back one
		if(frameSelected==numberOfFrames-1) { 
			$('#carousel').css('left', '0px');
			currentFrame = 1;
		}
		
		updateIndicators();
		
		$('#carousel').animate({
			left: '-=640px'
		}, { duration: 700, queue: false, easing: 'easeInOutSine', complete: function() {
			animating = false;
		}});
		
	});	
	
	$('#carousel-indicators li a').click(function(event) { 
		event.preventDefault();
		
		if(animating) { return; } else { animating = true; }
		
		clearTimeout(carouselTimeout);
		
		var frameSelected = $(this).parent().index();
		var requestedLeft = (frameWidth * frameSelected) * -1;
		currentFrame = frameSelected;

		updateIndicators();
		
		$('#carousel').animate({
			left: requestedLeft + 'px'
		}, { duration: 700, queue: false, easing: 'easeInOutSine', complete: function() {
			animating = false;
		}})
		
	});
	
});


function showAllEvents() { $('.sub-news').show() }
function hideAllEvents() { $('.sub-news').hide() }
function showEventsWithClass(cssClass) { $(cssClass).show(); }

$('select').change(function() { alert('!'); });

function filterCategoriesBySelection() {
	
	var selectedOptionValueForEvents = $('select#category option:selected').val();
	var selectedOptionValueForDate = $('select#date option:selected').val();
	var classToShow;

	if(selectedOptionValueForEvents == 'all' && selectedOptionValueForDate == 'all') {
		showAllEvents();
		return;
	}
	
	if(selectedOptionValueForEvents != 'all') {
		hideAllEvents();		
		classToShow = '.' + selectedOptionValueForEvents;
		showEventsWithClass(classToShow);
		return;
	}
	
	if(selectedOptionValueForDate != 'all') {
		hideAllEvents();		
		classToShow = '.' + selectedOptionValueForDate;
		showEventsWithClass(classToShow);
	}
}



