$(document).ready(function() {

	/**
	* The ever awesome Exists funciton
	**/
	jQuery.fn.exists = function(){return this.length>0;}
	
	if($('#deliveries-slider').exists()){

		$('#deliveries-slider').bxSlider({
	
			nextText: '',
			prevText: '',
			auto: true,
			speed: 600,
			pause: 5000
	
		});	
	
	}


	/* == Listing Images =========== */

	if($('#images').exists()){
                    	
		$('#images').after('<div id="slide_nav">').cycle({ 
			fx:     'scrollHorz', 
			speed:  450, 
			startingSlide: 0,
			timeout: 5000,
			pagerEvent: 'click',
			pauseOnPagerHover: true,
			pager:  '#slide_nav',
			next: '#images'
		});
	
		$('#images img').hover(
	
			function(){
				$('#images').cycle('pause');
			},
			function(){
				$('#images').cycle('resume');
			}
	
		);
	
	}
	
		
	/* == How We Helped ============ */
	
	if($('#services-container').exists()){
	
		$('#services-container').cycle({
			fx:		'scrollVert',
			speed: 	'fast',
			timeout: 0
		});
	
		$('.how-we-helped li').first().addClass("active-service");

		$('.how-we-helped li').each(
			
			function(i) {	
				
				$(this).click(function() {
					$(".active-service").removeClass("active-service");				
					$('#services-container').cycle(i);
					$(this).addClass("active-service");
					return false;
				});	
			
		});
		
	}
	
	
	/* == Reference Drop Down Arrow ===== */
	
	if($('#reference-heading').exists()){
		
		$('#reference-heading').click(
			
			function() {
		
					if($("#reference-heading").hasClass("swap-arrow")){
						$("#reference-heading").removeClass("swap-arrow");
					} else {					
						$("#reference-heading").addClass("swap-arrow");
					}
										
					$("#delivery-reference").slideToggle();
					
			}
			
		);
	}
	
	/* == Listings Thumbnail Rollover ======= */
	
	if($('ul.listings').exists()){
	
		$('.listings li').hover(
	
		function() {
		
			var myListings = $(this).find(".listing-details");
			
			if(myListings.hasClass("listing-details-on")){ return false; }
			
			var t = setTimeout(function() {
				$(myListings).slideDown();
			}, 115);
			$(this).data('timeout', t);
		
		},
		
		function() {
			
			clearTimeout($(this).data('timeout'));
			
			var myListings = $(this).find(".listing-details");
			
			if(myListings.hasClass("listing-details-on")){ return false; }
			
			$(myListings).slideUp();
	
		}
	
		);
	
	}
	
	
	/* == Contact Form ============ */
	
	if($('form#contact').exists()){
	
		$("form#contact").validate({
			rules: {
				myname: "required",
				email: {
					required: true,
					email: true
				},
				squad: "required",
				comments: "required"
			},
			messages: {
				myname: "Please enter your name",
				email: "Please enter your email",
				squad: "Please enter your the name of your squad/dept.",
				comments: "Please enter your comments"
			},
			errorPlacement: function(error, element) {
	       		error.fadeIn().appendTo(element.parent());
	    	}
		});
	
	}
	
	
	/* == Listing Page View Truck Specs ==== */
	
	if($('a#truck-specs-link').exists()){
	
		$('a#truck-specs-link').facebox();
	
	}
	
	/* == Stryker Signup Form ============= */
	
	if($('form#strykersignup').exists()){
	
		$("form#strykersignup").validate({
		
			rules: {
				fname: "required",
				lname: "required",
				email: {
					required: true,
					email: true
				},
				dept: "required",
				phone: "required",
				address: "required",
				city: "required",
				state: "required",
				zip: "required",
				country: "required",
				howyouheard: "required",
				youwoulduse: "required",
				wereyoufamiliar: "required",
				subscribe: "required",
				terms: "required"
			
				},
			messages: {
				fname: "Please enter your name",
				lname: "Please enter your name",
				email: "Email address required",
				dept: "Required",
				phone: "Please provide your phone #",
				address: "Please provide your address",
				city: "City is Required",
				state: "Req.",
				zip: "Required",
				country: "Please provide your country",
				howyouheard: "Please select one",
				youwoulduse: "Tell your story",
				wereyoufamiliar: "Please select one",
				subscribe: "This is required to enter our giveaway. Click the link above to read why we require this field.",
				terms: "You must agree to our terms & conditions in order to register (click link above to read the terms)"
				},
			errorPlacement: function(error, element) {
	      		error.fadeIn().appendTo(element.parent());
	   		}
		});
		
			
		$('a#strykerOptIn').facebox();
	
		$('a#termsConditions').facebox();
		
	}

	
	if($('#newsletter-signup').exists()){
	
		$("#newsletter-signup .emailaddress").click(
	
			function() {
				
				if( $(this).val() == $(this)[0].defaultValue){
			
					$(this).val("");
			
				}
			}
		);
	
	
		$("form#newsletter-signup").validate({
	
			rules: {
				emailaddress: {
					required: true,
					email: true
				}
			},
			messages: {
				emailaddress: "Invalid email address"
			},
			errorPlacement: function(error, element) {
	      		error.fadeIn().appendTo(element.parent());
	    	}
	
		});
	
	}
	
	
	
	
	
	

});
