$(document).ready(function () {
	if($('#headerimgs').length) {
		var loaded = 0;
		for(x in photos) {
			$('<img />').attr('src', '/i/'+photos[x].image).load(function() {
				if(loaded>1)
				{
					rotate();
				}
				loaded++;
				
			});
		}
	}
	
	if($('.article').height() > $(window).height()) {
		$('.backtotop').show();
	}
	
	$('a[href=#top]').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
	if(typeof(utm_source) != 'undefined') {
		$('a[href^=http://sa1.seatadvisor.com]').attr('href', function() {
			return this.href + '&utm_source=' + utm_source + '&utm_medium=' + utm_medium + '&utm_term=' + utm_term + '&utm_content=' + utm_content + '&utm_campaign=' + utm_campaign;
		});
	}
	if(typeof(mktg) != 'undefined') {
		$('a[href^=http://sa1.seatadvisor.com]').attr('href', function() {
			return this.href + '&mktg=' + mktg;
		});
	}
	
	$("a.register").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'titleShow'		:	false,
		'width'			:	835,
		'height'		:	595,
		'type'			:	'iframe',
		'padding'		:	3,
		'overlayOpacity':	0.75,
		'overlayColor'	:	'#000'
	});
	
	if($('div.hide').length) {
		$('div.hide').hide();
		$('div.hide').prev('h3').prepend('<b>+</b> ').append(' <span>click to expand</span>').addClass('expand').click(function() {
			$(this).next('div.hide').slideToggle();
			//$(this).children('b').html('-');
		});
		
		$('div.hide').prev('div').children('a.expand').prepend('<span>+ Click to view</span> ').click(function() {
			if($(this).parent().next('div.hide:visible').length) {
				$(this).parent().next('div.hide').slideUp();
				$(this).children('span').html('+ Click to view')
			}
			else {
				$(this).parent().next('div.hide').slideDown();
				$(this).children('span').html('- Click to hide');
			}
			//$(this).children('b').html('-');
		});
	}
	
	//http://maps.google.com/maps?f=d&source=s_d&saddr=10+Glover+St,+Salem,+MA+01970&daddr=Churchill+Downs,+Louisville,+KY
	$('form#directions').submit(function() {
		var saddr = $('#getdirections').val();
		window.open('http://maps.google.com/maps?f=d&source=s_d&saddr='+saddr+'&daddr=Churchill+Downs,+Louisville,+KY', 'map');
		return false;
	});
	
	$("a[href$=.flv], a[href$=.mov]").each(function() {
        $(this).attr('href', '/js/p.swf?movie=' + $(this).attr('href'));
    });

    $("a[href$=.flv], a[href$=.mov]").fancybox({
        'type': 'swf',
        'autoDimensions': false,
        'autoScale': false,
        'padding': 3,
        'width': 640,
        'height': 360 + 25,
        'swf': {
            'allowFullScreen': 'true',
            'FlashVars': 'autoload=on&autoplay=on&volume=70'
        },
		'overlayOpacity':	0.75,
		'overlayColor'	:	'#000'
    });
    
	$('form#registration').submit(function() {
    	
    	$('p.error').remove();
		$('.error').removeClass('error');
		
		var fail = false;
    	var reason = '';
    	
		if($('#first').val()=="")
		{
			$('#first').addClass('error');
			fail = true;
			reason += "Please enter your First Name.<br />";
		}
		
		if($('#last').val()=="")
		{
			$('#last').addClass('error');
			fail = true;
			reason += "Please enter your Last Name.<br />";
		}
		
		if($('#address').val()=="")
		{
			$('#address').addClass('error');
			fail = true;
			reason += "Please enter your Address.<br />";
		}
				
		if($('#city').val()=="")
		{
			$('#city').addClass('error');
			fail = true;
			reason += "Please enter your City.<br />";
		}
		
		if($('#state').val()=="")
		{
			$('#state').addClass('error');
			fail = true;
			reason += "Please enter your State.<br />";
		}
		
		if($('#zip').val()=="")
		{
			$('#zip').addClass('error');
			fail = true;
			reason += "Please enter your Zip.<br />";
		}
		
		if(!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/).test($('#email').val()))
		{
			$('#email').addClass('error');
			fail = true;
			reason += "Please enter your Email Address.<br />";
		}
		
		if($('#phone').val()=="")
		{
			$('#phone').addClass('error');
			fail = true;
			reason += "Please enter your Phone Number.<br />";
		}
		
		if($('#age:checked').length==0)
		{
			$('#age').next('label').addClass('error');
			fail = true;
			reason += "Please check the 'I am over 18 years of age' box<br />";
		}
		
		if(fail) {
			$(this).prepend('<p class="error">'+reason+'</p>');
			return false;
		}
	});

	$('form#claim').submit(function() {
    	
    	$('p.error').remove();
		$('.error').removeClass('error');
		
		var fail = false;
    	var reason = '';
    	
		if($('#first').val()=="")
		{
			$('#first').addClass('error');
			fail = true;
			reason += "Please enter your First Name.<br />";
		}
		
		if($('#last').val()=="")
		{
			$('#last').addClass('error');
			fail = true;
			reason += "Please enter your Last Name.<br />";
		}
		
		if($('#address').val()=="")
		{
			$('#address').addClass('error');
			fail = true;
			reason += "Please enter your Address.<br />";
		}
				
		if($('#city').val()=="")
		{
			$('#city').addClass('error');
			fail = true;
			reason += "Please enter your City.<br />";
		}
		
		if($('#state').val()=="")
		{
			$('#state').addClass('error');
			fail = true;
			reason += "Please enter your State.<br />";
		}
		
		if($('#zip').val()=="")
		{
			$('#zip').addClass('error');
			fail = true;
			reason += "Please enter your Zip.<br />";
		}
		
		if(!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/).test($('#email').val()))
		{
			$('#email').addClass('error');
			fail = true;
			reason += "Please enter your Email Address.<br />";
		}
		
		if($('#phone').val()=="")
		{
			$('#phone').addClass('error');
			fail = true;
			reason += "Please enter your Phone Number.<br />";
		}
				
		if($('#age:checked').length==0)
		{
			$('#age').next('label').addClass('error');
			fail = true;
			reason += "Please check the 'I am over 18 years of age' box<br />";
		}
		
		if(fail) {
			$(this).prepend('<p class="error">'+reason+'</p>');
			return false;
		}
	});
    
});

$.fn.smartBackgroundImage = function(url){
	var t = this;
  //create an img so the browser will download the image:
	$('<img />')
	 	.attr('src', url)
	 	.load(function(){ //attach onload to set background-image
	 		t.each(function(){ 
	 			$(this).css('backgroundImage', 'url('+url+')' );
	 		});
	 	});
	return this;
}
