/*------------------------------------------------------------------
Project:		Patrick G. Withers
Author:			Ryan Dudek
Last change:	2010-01-07
-------------------------------------------------------------------*/

$(document).ready(function() {
	
	// Add the clear class to any textfield you want to auto clear
	$('.clear').focus(function() { clearInputValue(this); });
	
	// Add an odd class to the lists
	$('.oddRows li:odd').addClass('odd');
	
	// Add a first class to the first column
	$('.column:first').addClass('first');
	$('.column:even').css({'clear':'both'});
	$('.accordion dt:first').addClass('first');
	$('.calendar dt').next().addClass('first');
	
	
	$('.addLast li:last').addClass('last');
	$('#footer ul li:last').addClass('last');
	$('#footer ol li:last').addClass('last');


	/* Tab setup and functionality
	--------------------------------------------- */
	function setupTabs() {
		$('.tabbedContent li a').click(function(e) {
			e.preventDefault();
			makeActiveTab(this);
			showTabData(this);
		});
	}
	
	function makeActiveTab(tab) {
		$(tab).parent().parent().find('li').removeClass('active');
		$(tab).parent().addClass('active');
	}
	
	function showTabData(tab) {
		var tabToShow = "#" + $(tab).attr('rel');
		$(tab).parent().parent().parent().find('.tab').hide();
		$(tabToShow).show();
	}
	
	
	/* Accordion
	--------------------------------------------- */
	function setupAccordion() {
		$('dl.accordion dt').click(function(e) {
			e.preventDefault();
			if($(this).attr('class') == "active") {
				$(this).next().slideUp('fast');
				$(this).removeClass('active');
				return;
			}
			collapseAccordion();
			showTheDefintionFor(this);
		});
		collapseAccordion();
		showTheDefintionFor($('.accordion dt:first'));
	}
	function collapseAccordion() {
		$('dl.accordion dd').hide();
		$('dl.accordion dt').removeClass('active');
	}
	
	function showTheDefintionFor(item) {
		$(item).addClass('active');
		$(item).next().slideDown('fast');
	}
	
	
	// This function takes an input, clears it's value on focus
	// and sets it's value back if the user doesn't type anything
	function clearInputValue(input) 
	{
		var originalValue = $(input).val();
		$(input).val('');
		$(input).blur(function() {
			if($(this).val() == '') {
				$(this).val(originalValue);
			} else {
				$(this).unbind('blur');
			}
		})
	}

});



//rotator list function
// wait until list has loaded before starting cycle
$(window).load(function() {
	// front image rotator
	$('.rotator').cycle({ 
		fx:     'fade', 
		timeout:  5000, 
		delay:   -3750,
        before: onBefore
	});
});	

// hide all but the first lis when page loads
$(document).ready(function() {
    $('.rotator li:gt(0)').hide();
});

// callback fired when each slide transition begins
function onBefore(curr,next,opts) {
    var $slide = $(next);
    var w = $slide.outerWidth();
    var h = $slide.outerHeight();
    $slide.css({
        marginTop: (0),
        marginLeft: (0)
    });
};