/* RESEN */
/* Javascript Functions
----------------------------------------------------------------------------- */


	$(document).ready(function() {
	
		lightbox();
		actions();
		replaceSubmitButtons();
		registration();
		
	});
	
	$(window).load(function() {

		tabs();
		alerts();
		$('body').removeClass('loading');
					
	});
		

/* FUNCTION: LINK BEHAVIORS
----------------------------------------------------------------------------- */

	
	function actions() {

		
/* Back Link */


		$('a[href=#back]').click(function() {
		
			history.go(-1);
			return false;
		
		});
		
		
/* Social Bookmarks */
			

		if ($('ul.share').length > 0) {
		
			var itemTitle = $('ul.share').parent().children('h4').text();
		
			$('ul.share .facebook a').attr('href', 'http://www.facebook.com/sharer.php?u=' + location.href);
			$('ul.share .delicious a').attr('href', 'http://del.icio.us/post?v=4&noui&jump=close&url=' + location.href + '&title=' + itemTitle);
			$('ul.share .stumbleupon a').attr('href', 'http://www.stumbleupon.com/submit?url=' + location.href + '&title=' + itemTitle);
			$('ul.share .digg a').attr('href', 'http://digg.com/submit?phase=2&url=' + location.href + '&title=' + itemTitle);

		}
		
	}
	
	
/* FUNCTION: TABS
----------------------------------------------------------------------------- */


	function tabs() {
	
	
/* Remove Empty Tabs */


		$('.tabbed > ul.tabs').children('li').each(function() {
		
			var pane = $(this).find('a').attr('href');
			if ($(this).parent().siblings(pane).children().length == 0) { 
			
				$(this).parent().siblings(pane).remove();
				$(this).remove();
				
			}
	
		});
		
	
/* Initiate Tabs */


		$('.tabbed > ul.tabs').tabs({
			
			fxSlide: false
	
		});
		
		
/* Fix Tabbed Paging */

		
		$('.tabbed .pane ul.paging a, .tabbed .pane ul.pages a').each(function() { 
		
			var pane = $(this).closest('.pane').attr('id');
			$(this).attr('href', $(this).attr('href') + '#' + pane); 
	
		});
		
		
	}
	
	
/* FUNCTION: REPLACE SUBMIT BUTTONS
----------------------------------------------------------------------------- */


	function replaceSubmitButtons() {
	
	
/* Replace Buttons */

	
		$('input[type=submit]').each(function() {
					
			var language = $(this).attr('value');
			$(this).parent().addClass('submit');
			$(this).after('<a href="#submit">' + language + '</a>');

		});
		

/* Button Click */

	
		$('ul.actions .submit *').click(function() {
		
			$('label.error').removeClass('error');
		
			$(this).closest('form').find('label.required input, label.required select').each(function() {
			
				if ($(this).val() == "") $(this).closest('label').addClass('error');
				
			});

			if ($(this).closest('form').find('label.error').length > 0) {
			
				window.scrollTo(0,0);
				return false;
				
			} else {
			
				$(this).parents('form').submit();
				return false;
			
			}
			
		});
		
		
/* Form Submit */


		$('form:not(.cart form)').submit(function() {
			
			if ($(this).attr('action').match('actions')) {
			
				var action = window.location.href.replace(window.location.protocol + "//" + window.location.hostname, '').replace(':8888', '');
				$(this).attr('action', action);
				
			}
			
		});
		
	
	}
	
	
/* FUNCTION: ALERT
----------------------------------------------------------------------------- */


	function alerts() {
	
		$('div.alert').each(function() {
		
			$(this).prependTo('body').slideDown('fast');
		
		});
		
		$('div.alert').click(function() {
		
			$(this).slideUp('fast');
		
		});
			
	}
	
	
/* FUNCTION: REGISTRATION
----------------------------------------------------------------------------- */

	
	function registration() {
	
	
/* Markup Changes */


		if ($('#register').length > 0) {
		

		
		}
	
	
/* Classes */	
	
	
		$('#register .classes input[type=checkbox]').click(function() {
			
			// Adjust Price
			var overhead = 20;
			var classCost = $('#register .classes input:checked').length * 75;
			var amount = overhead + classCost;
			$(this).closest('form').children('input[name="amount"]').val(amount);
			
			// Adjust Discount
			var discount = $('#register .classes input:checked').length * 20;
			$(this).closest('form').children('input[name="discount_amount"]').val(discount);

			// Adjust Classes
			var classList = new Array();
			$('#register .classes input:checked').each(function() { classList.push($(this).attr('name')); });
			$(this).closest('form').children('input[name="os0"]').val(classList.toString());
		
		});

		$('#register form').submit(function() {


/* Validation */	


			if ($('#register .classes input:checked').length == 0) {
			
				alert('You must choose at least one class to race.');
				return false;
			
			}


/* Save to Local Database */


			var url = "/actions/custom-entry";
			var data = $(this).serialize();

			$.ajax({
			
				url: url,
				type: "POST",
				data: data,
				async: false,
				success: function(id) {
				
					$('#register input[name="invoice"]').val(id);
				
				}
			
			});
						
		});
	
	}
