function theRotator() {
	//Make all images visable only to those who can see the unseen.
	$('div.rotate ul li:not(:first) > *').hide();
	//Show the first image
	$('div.rotate ul li:first').addClass('show');
	//Set it, and forget it!
	setInterval('rotate()', 10000);
}
function rotate() {
	//Get the first image
	var current = ($('div.rotate ul li.show') ?  $('div.rotate ul li.show') : $('div.rotate ul li:first'));
	
	//Get the next image, loop back to the beginning
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotate ul li:first') : current.next()) : $('div.rotate ul li:first'));

	//Hide the current image
	current
	.removeClass('show')
//	.find('*')
//	.fadeOut(1000)
	
	next.addClass('show').find('*').fadeOut(0)
	//Set the fade in effect for the next image, the show class has higher z-index
	next
	.addClass('show')
	.find('*')
	.fadeIn(1000);
}
function demSpecials() {
	$('.specials div').css({opacity: 0}).hide();
		
		// unhide, yes make visible, the first but only after 1.5 seconds
		setTimeout(function(){
				$('.specials div:first').css({opacity: 0}).show().addClass('cur').animate({opacity: 1}, 1000);
				setInterval('rotateDemSpecials()', 6000);
			}, 
				1500
		);
}
function rotateDemSpecials() {
	// Get the current or first special
	var cur = ($('.specials div.cur') ?  $('.specials div.cur') : $('.specials div:first'));
	
	// Get the next special or loop back to the beginning
	var nextSpecial = ((cur.next().length) ? ((cur.next().hasClass('cur')) ? $('.specials div:first') : cur.next()) : $('.specials div:first'));
	
	//Hide the current image
	cur.animate({opacity: 0}, 1000, function(){
		$(this).hide();
		
		//Set the fade in effect for the next image, the show class has higher z-index
		nextSpecial.css({opacity: 0})
			.show()
			.addClass('cur')
			.animate({opacity: 1}, 1000);
	})
	.removeClass('cur');
}




// Begin the jQuery
$(document).ready(function() {
  
  jQuery('#beside-the-ball a[href=\'/contactus\']').before(jQuery("#class-hover"));
  jQuery(".class-schedule-button").wrap('<li class="last" id="schedule-btn"></li>');
  jQuery('#beside-the-ball #class-hover').append(jQuery("#schedule-btn"));
	//Anything inside a <div class="rotate"> in a list format will rotate. Boom.
		theRotator();
	
	// Rotate the home page slides start by hiding all the slides so they aren't visible... you know hidden
		demSpecials();
	
	// Set up a hover effect for class lists
	$('#class-hover > li, .week-class').hover(
		function(){
			$(this).find('div.class-details').show();
		},
		function(){
			$(this).find('div.class-details').hide();
		}
	);
	
	// Initialize dialogs, they're all the same anyway...
	$('#renew-dialog, #address-dialog, #join-dialog, #rent-dialog, #league-dialog, #trainer-dialog').dialog({
		autoOpen: false,
		width: 564,
		draggable: false,
		resizable: false,
		modal: true,
		buttons: { 
			'Send': function(){
				$(this).find('form').submit();
			}
		}
	});
	
	// Open dialogs
	$('#renew, #address, #join, #rent, #trainer, .league').click(function(e){

		e.preventDefault();
		var id = $(this).attr('id');
		
		// BECAUSE LEAGUE IS A CLASS... DUH!
		if (!id) {
			id = 'league';
		}
		
		// Need some additonal dialog options for rent dialog, like dragging, and no modal, and a button with a different title...
		if ( id == 'rent' ) { 
			$('#rent-dialog')
				.dialog('option', 'draggable', true)
				.dialog('option', 'modal', false).dialog('open')
				.next('div').find('button').text('Send Request');
			$('.ui-dialog-titlebar').css('cursor', 'move');
		}
		// Need some additional dialog options for leagues... also show cost in the dialog for emphasis
		if ( id == 'league' ) {
			var leagueName = $(this).find('.league-name').text();
			$('#league-dialog').dialog('option', 'title', leagueName).dialog('open')
				.next('div').find('button').text('Send Request');
			$('#ContactLeague').val(leagueName);
			
			$('.dialog-cost > *:not(.title)').remove();
			var cost = $(this).find('.cost').html();
			$('.dialog-cost').append(cost).find('b').remove();
		}
		// additional information processing for trainer forms
		if ( id == 'trainer' ) {
			var trainer = $(this).parent().prev('span').text();
			$('#ContactTrainer').val(trainer);
			
			$('#trainer-dialog').dialog('option', 'title', 'Request Personal Trainer: '+trainer).dialog('open')
				.next('div').find('button').text('Send Request');
		}
		else {
			$('#'+id+'-dialog').dialog('open');
		}
	});
	
	// change of address form, automatically fill out state / zip information
	$('#ContactZip').blur(function(){
		var place = $('#place');
		var zip = $(this);
		
		if ( zip.val().length == 5 ) {
			place.html('');
			$.getJSON(
				'http://ws.geonames.org/postalCodeLookupJSON?postalcode='+zip.val()+'&country=US&maxRows=1',
				function(data){
					if ( data.postalcodes.length != 0 ) { 
						$('#ContactState').val(data.postalcodes[0].adminCode1);
						$('#ContactCity').val(data.postalcodes[0].placeName);
						place.html('<img width="10px" height="10px" src="/img/img_tick.png" />');
						zip.css('background', '#FFF');
					}
					else { 
						zip.css('background', '#FF7F7F');
						place.html('Invalid U.S. Zip');
					}
				}
			)
		}
		else {
			zip.css('background', '#FF7F7F');
			place.html('Incomplete U.S. Zip');
		}
	});
	
	
	// Contact forms submit via ajax
	$('#RenewForm, #AddressForm, #RentForm, #LeagueForm, #TrainerForm').submit(function(){
		var a = $(this);
		a.ajaxSubmit({
			beforeSubmit: function(formData){
				if ( a.attr('id') == 'RentForm' || a.attr('id') == 'LeagueForm' || a.attr('id') == 'TrainerForm' ) {
					var b = '';
					a.find('*:input').each(function(){
						if ( $(this).attr('required') == 'required' && !$(this).val()) {
							$(this).css('background', '#FF7F7F');
							b = 'set';
						}
					});
					
					if ( b != '' )
						return false;
				}
				
				a.fadeOut().parent().next('div').find('button').fadeOut();
				a.after('<p class="center"><img src="/img/img_smallloading.gif" height="16px" width="16px"></p>').parent().animate({'height': 27}, 500);
			},
			success: function(){
				a.next('p').fadeOut(function(){
					a.after('<p class="received">Your message has been received. Thank you for contacting us.</p>').next('p').fadeIn();
				}).remove();
			}
		});
		return false;
	}).find('*:input').focus(function(){
		$(this).css('background', '#FFF');
	});
	
	$('#JoinForm').submit(function(){
		var a = $(this);
		
		$(this).ajaxSubmit({
			beforeSubmit: function(){
				var c = '';
				
				a.find('*:input').each(function(){
					var b = $(this);
					
					if ( !b.val() && b.attr('id') != 'ContactPhone' ){
						b.css('background', '#FF7F7F');
						c = 'set';
					}
				});
				
				if ( c != '' ) { 
					return false;
				}
				else {
					a.fadeOut().parent().next('div').find('button').fadeOut();
					a.after('<p class="center"><img src="/img/img_smallloading.gif" height="16px" width="16px"></p>').parent().animate({'height': 27}, 500);
				}
			},
			success: function(){
				a.next('p').fadeOut(function(){
					a.after('<p class="received">A download will start shortly with further offer details. Thank you for your interest in 1on1 Athletic Club.</p>').next('p').fadeIn();
					setTimeout(
						function(){
							var replace = $('#ContactReplace').val();
							window.location.replace(replace);
						},
						2500
					);
				}).remove();
			}
		});
		return false;
	});
});
