// jQuery.noConflict(); // gives back the '$'

(function($) { // takes back the dollar sign in a 'safe zone'
	
	$(function() { // document ready
		
		if(window['HomeSlideShowImages']){
			var images = window['HomeSlideShowImages'];
			if(images.length < 2) return;
			var $HomeSlideShow = $('#HomeSlideShow');
			// preload images
			$.each(images,function(){
				var image = new Image();
				image.src = this;
			});
			images.push(images.shift()); // we started with the first image, shuffle the pack once
			var rotate = function(){
				var image = images.shift();
				images.push(image);
				var IMG = new Image();
				IMG.src = image;
				var testCompleteTimer = setInterval(function(){
					if(IMG.complete){
						clearInterval(testCompleteTimer);
						var $image = $('<img src="' + image + '" alt="" />').hide();
						$HomeSlideShow.append( 
							$image.fadeIn(1000, function(){
								$HomeSlideShow.find('img').not(':last').remove();
								setTimeout(rotate,3000);
							})
						);
					}
				},50);
			};
			setTimeout(rotate,3000);
		}
		
		$('#Gallery')
			.find('a')
				.bind('click',function(e){
					e.preventDefault();
					$('#Gallery').find('> img').fadeOut(250,function(){
						$(this).remove();
					});
					var image = new Image();
					image.src = this.getAttribute('href');
					p = $(this).position();
					top_position = 0;
					
					if(p.top > 450)
					{
						top_position = p.top - 450;
					}
					
					$('#Gallery').append(
						
						$('<img src="' + this.getAttribute('href') + '" alt="" title="Click to close" />')
							.css({position:'absolute',top: top_position,left:'50%',marginLeft:-image.width/2,cursor:'pointer',zIndex:25})
							.hide()
							.fadeIn(500)
							.bind('click',function(){
								$(this).fadeOut(250,function(){
									$(this).remove();
								});
							})
					);
				})
				.each(function(){
					// preloading full images
					var image = new Image();
					image.src = this.getAttribute('href');
				});
		
		$('#RegistrationForm')
			.bind('submit', function(e){
				$(this)
					.find('input[type=text]')
						.removeClass('error')
						.filter(':visible')
						.each(function(){
							if( $.trim(this.value) == '' ){
								e.preventDefault();
								$(this).addClass('error');
							}
						});
			})
			.find('input.player')
				.bind('change keyup blur', function(){
					var players = 0;
					var $price = $('#RegistrationPrice');
					$('#RegistrationForm').find('input.player').each(function(){
						if($.trim(this.value) != ''){
							players++;
						}
					});
					if(players < 1){
						players = 1;
					}
					$price.html( Number( 125.00 * players).toFixed(2) );
				})
				.trigger('change')
			.end()
			.find('#TeamCaptain, #INeedATeam').bind('change',function(){
				$('#TeamCaptain, #INeedATeam').closest('label').removeClass('checked');
				$(this).closest('label').addClass('checked');
				if( $('#TeamCaptain:checked')[0] ){
					$('#TeamPlayers').fadeIn(250);
					return;
				}
				if( $('#INeedATeam:checked')[0] ){
					$('#TeamPlayers').fadeOut(250).find('input').val('').change();
					return;
				}
			})
				.filter(':checked').trigger('change');
		
	});
	
})(jQuery);


