$(document).ready(function(){
	//color dealer list
	$('.dealer:odd').css('background','url(images/input-bg.png)');

	$('.featured-switch-link a').click(function(){
		var product = $(this).attr('href');
		var bgImg = 'featured-' + product.slice(1) + '.jpg';

		$('.featured-switch-link a').css('color','#e5e5e5').find('img').css({'border-color':'transparent'});
		$(this).css('color','#ffbf00').find('img').animate({'border-color':'#ffbf00'});

		$('.featured-product').hide();
		$('.featured-image img').css('background', 'url(images/' + bgImg + ')');
		$(product).delay(100).fadeIn(500);

		return false;
	});

	$('#dealers-link a').click(function(){
		var dist = $('#all-products').offset().top;
		$('body').stop(true,true).animate({'scrollTop' : dist }, 'slow', function(){
			$('#all-products h3:first').animate({'color':'#fff'}, 100, function(){
				$(this).delay(1000).animate({'color':'#ffbf00'}, 5000);
			});
		});

		return false;
	});

	$('#contact-link a').click(function(){
		var dist = $('#contact').offset().top;
		$('body').stop(true,true).animate({'scrollTop' : dist }, 'slow', function(){
			$('#contact h3:first').animate({'color':'#fff'}, 100, function(){
				$(this).delay(1000).animate({'color':'#ffbf00'}, 5000);
			});
		});

		return false;
	});

	$('#dealers-link, #contact-link').mouseenter(function(){
		$(this).stop(true,true).animate({'margin-top':4}, 200);
	}).mouseleave(function(){
		$(this).animate({'margin-top':0}, 200);
	});

	$('input, textarea').focus(function(){
		if($(this).val() === ''){
			$(this).prev('label').animate({'left':312}, 'fast');
		}
	}).blur(function(){
		if($(this).val() === ''){
			$(this).prev('label').animate({'left':9}, 'fast');
		}
	});
	
	$('#contact-form').submit(function(){
	
		function isValidEmailAddress(emailAddress) {
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			return pattern.test(emailAddress);
		};
		
		var valid = true;
		
		if($('#name').val() == ''){
			$('#name').css('border-color','red');
			valid = false;
		}else{
			$('#name').css('border-color','#898989');
		}
		if(!isValidEmailAddress($('#email').val())){
			$('#email').css('border-color', 'red');
			valid = false;
		}else{
			$('#email').css('border-color','#898989');
		}
		if($('#subject').val() == ''){
			$('#subject').css('border-color','red');
			valid = false;
		}else{
			$('#subject').css('border-color','#898989');
		}
		if($('#message').val() == ''){
			$('#message').css('border-color','red');
			valid = false;
		}else{
			$('#message').css('border-color','#898989');
		}
		
		if(!valid){
			$('#req-field').css('color', 'red').text('Please fill in all field correctly to send message.');
		}else{
			$('#req-field').delay(5000).css('color','#7e7e7e').text('Sending message...');
			var formData = $('#contact-form').serialize();
			$.ajax({
				type: "POST",
				url: "send.php",
				data: formData,
				success: function(msg){
					$('#req-field').css('color','green').text('Thank you! Your message was sent successfully.');
				}
			});
		
		}/* end if(!valid) */
		return false;
	});

	
	$('.various').fancybox();

	$('.specs').toggle(
		function(){
			$(this).closest('.featured-product').find('.featured-description').hide()
			$(this).closest('.featured-product').find('.spec-list').fadeIn();
			$(this).css({'background-position-y':'-41px'}, 'fast');
		},
		function(){
			$(this).closest('.featured-product').find('.spec-list').hide();
			$(this).closest('.featured-product').find('.featured-description').fadeIn()
			$(this).css({'background-position-y':'0px'});
		}
		);

	$("#pro-more").click(function() {
		$.fancybox([
			{
				'href'	: 'images/slideshow/pro/01.jpg',
				'title'	: 'TM Pro - 30" Stroke, 5" Cylinder, 900 lbs'
			},
			{
				'href'	: 'images/slideshow/pro/03.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/04.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/05.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/06.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/07.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/08.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/09.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/10.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/11.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/12.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/13.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/14.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/15.jpg',
				'title'	: ''
			},
			{
				'href'	: 'images/slideshow/pro/16.jpg',
				'title'	: ''
			},
		], {
			padding			: 5,
			type              : 'image',
			helpers	: {
				thumbs	: {
					width	: 50,
					height	: 50
				}
		}
		});

		return false;
	});

	$("#warrior-more").click(function() {
			$.fancybox([
				{
					'href'	: 'images/slideshow/warrior/01.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/warrior/02.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/warrior/03.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/warrior/04.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/warrior/05.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/warrior/06.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/warrior/07.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/warrior/08.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/warrior/09.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/warrior/10.jpg',
					'title'	: ''
				},
			], {
				'padding'			: 0,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'              : 'image',
				'changeFade'        : 0
			});

			return false;
		});

		$("#hd-more").click(function() {
			$.fancybox([
				{
					'href'	: 'images/slideshow/hd/01.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/hd/02.jpg',
					'title'	: ''
				},
			], {
				'padding'			: 0,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'              : 'image',
				'changeFade'        : 0
			});

			return false;
		});

		$("#legend-more").click(function() {
			$.fancybox([
				{
					'href'	: 'images/slideshow/legend/01.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/legend/02.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/legend/03.jpg',
					'title'	: ''
				},
			], {
				'padding'			: 0,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'              : 'image',
				'changeFade'        : 0
			});

			return false;
		});

		$("#chef-more").click(function() {
			$.fancybox([
				{
					'href'	: 'images/slideshow/chef/01.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/chef/02.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/chef/03.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/chef/04.jpg',
					'title'	: ''
				},
				{
					'href'	: 'images/slideshow/chef/05.jpg',
					'title'	: ''
				},
			], {
				'padding'			: 0,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'              : 'image',
				'changeFade'        : 0
			});

			return false;
		});
});
