// Content Switcher
// The tabs on the trucks.
var ContentSwitcher = function (elm) {
	this.jElm = $(elm);
	this.controls = this.jElm.find('.controls a');
	this.slides = this.jElm.find('.slide');
	
	if (this.controls.length !== this.slides.length) {
		alert('ContentSwitcher: There is an unequal amount of slides and controls.');
	} else {
		this.init();
	}
};
ContentSwitcher.prototype.init = function () {
	this.show_slide(0);
	this.apply_listeners();
};
ContentSwitcher.prototype.show_slide = function (index) {
	this.slides.hide();
	this.slides.eq(index).show();
	this.controls.removeClass('active');
	this.controls.eq(index).addClass('active');
};
ContentSwitcher.prototype.apply_listeners = function (index) {
	var that = this;
	this.controls.click(function () {
		that.show_slide(that.controls.index($(this)));
		return false;
	});
};



// Contact Form
// Sends off an email via ajax php script.
var email_manager = {
	name_field: '',
	email_field: '',
	msg_field: '',
	address_field: '',
	city_field: '',
	state_field: '',
	zip_field: '',
	errors: [],
	
	init: function () {
		
		var that = this;
		
		$('#send_email').bind('click', function () {

// Clear error array.
			that.errors = [];

// Set field properties.
			that.name_field = $('input[name="contact_name"]').val();
			that.email_field = $('input[name="contact_email"]').val();
			that.msg_field = $('textarea[name="contact_message"]').val();
			that.address_field = $('input[name="contact_address"]').val();
			that.city_field = $('input[name="contact_city"]').val();
			that.state_field = $('input[name="contact_state"]').val();
			that.zip_field = $('input[name="contact_zip"]').val();
			
// Handle validation.
			that.do_validation();
			
// Process.
			that.process();
			
			return false;
		});
	},
	
	do_validation: function () {
// Check email.
		if ( ! this.is_validate_email(this.email_field)) {
			this.errors.push('Please enter a valid email.');
		}
		if (this.name_field.length < 1) {
			this.errors.push('Please enter you\'re name.');
		}
		if (this.address_field.length < 1) {
			this.errors.push('Please enter your address.');
		}
		if (this.city_field.length < 1) {
			this.errors.push('Please enter your city.');
		}
		if (this.state_field.length < 1) {
			this.errors.push('Please enter your state.');
		}
		if (this.zip_field.length < 1) {
			this.errors.push('Please enter your zip code.');
		}
		if (this.msg_field.length < 1) {
			this.errors.push('You must have something to say!');
		}
	},
	
	process: function () {
		if (this.errors.length > 0) {
			this.display_errors();
		} else {
			this.send_mail();
		}
	},
	
	display_errors: function () {
		var msg = 'There were some issues with submitting the form:\n',
			i = 0;
		for (error in this.errors) {
			msg += i = i + 1;
			msg += '. ' + this.errors[error] + "\n";
		}
		alert(msg);
	},
	
	send_mail: function () {
		var that = this,
			params = {
				"name": that.name_field,
				"email": that.email_field,
				"msg": that.msg_field,
				"address": that.address_field,
				"city": that.city_field,
				"state": that.state_field,
				"zip": that.zip_field
			};
		
		$.ajax({
			type: "POST",
			url: "/ajax.send_email.php",
			data: params,
			success: function (msg) {
				if (msg == 'success') {
					that.success();
				} else {
					that.errors.push('Sorry, there was a server error. Please try again.');
					that.display_errors();
				}
			}
		 });
	},
	
	is_validate_email: function (email) {
		var reg = new RegExp("^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");
		return reg.test(email);
	},
	
	success: function () {
		$('input[name="contact_name"]').val('');
		$('input[name="contact_email"]').val('');
		$('textarea[name="contact_message"]').val('');
		$('input[name="contact_address"]').val('');
		$('input[name="contact_city"]').val('');
		$('input[name="contact_state"]').val('');
		$('input[name="contact_zip"]').val('');
		alert('Your message was sent!');
	}
};

//Function for enabling placeholder
//attribute for IE and older FF.
var activatePlaceholders = function() {
	var detect = navigator.userAgent.toLowerCase();
	if (detect.indexOf("safari") > 0) return false;
	var inputs = document.getElementsByTagName("input");
	for (var i=0;i<inputs.length;i++) {
		if (inputs[i].getAttribute("type") == "text") {
			if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
				inputs[i].value = inputs[i].getAttribute("placeholder");
				inputs[i].onclick = function() {
					if (this.value == this.getAttribute("placeholder")) {
						this.value = "";
					}
					return false;
				};
				inputs[i].onblur = function() {
					if (this.value.length < 1) {
						this.value = this.getAttribute("placeholder");
					}
				};
			}
		}
	}
};


// Main Navigation
var main_navigation = function () {
	var checkpoints = [226, 3261, 9905, 13903, 21394, 25705, 28743];
	var nav_li = $('nav li');
	
	var moveto = function (elm) {
		var cp          = $(elm).attr('href').replace('#checkpoint', '') - 1, 
			windowWidth = $(window).width(),
			offset      = scroll_manager.x_position,
			scrollPos   = scrollPos = checkpoints[cp] - 50,
			scrollSpeed = 0;
		
		
		/*
		if (scrollPos > offset) {
			scrollSpeed = scrollPos - offset;
		} else {
			scrollSpeed = offset - scrollPos;
		}
		
		$('html,body').stop().animate({
			scrollLeft: scrollPos
		}, scrollSpeed);
		*/
		$(window).scrollLeft(scrollPos);
		
		return false;
	};
	
	var change_nav = function () {
		var offset = scroll_manager.x_position,
			range = 200,
			current = 0,
			to, from;
		
		if (offset < checkpoints[1] - 1000) {
			nav_li.removeClass('active');
		} else {
			for (var i = 0, l = checkpoints.length; l > i; i++) {
				to = checkpoints[i] + range;
				from = checkpoints[i] - 150;
				if (offset > from && offset < to) {
					nav_li.removeClass('active');
					nav_li.eq(i-1).addClass('active');
					break;
				}
			}
		}
	};
	
	// Scrolling functionality
	$('a[href*="#checkpoint"]').click(function () {
		return moveto(this);
	});
	$(window).scroll(change_nav);
	$(window).trigger('scroll');
};

function toggle_sidescroll() {
	if(window.sidescroll == 'off') {
		$(document).mousewheel(scroll_manager.on_mousewheel);
		window.sidescroll = 'on';
	}
	else {
		$(document).unbind('mousewheel');
		window.sidescroll = 'off';
	}
}

$(function () {
	
	// Activate main nav.
	main_navigation();
	
	// Activate bird effect.
	// new Bird();
	

	// Content Switchers (Toggle Navigations)
	new ContentSwitcher('#checkpoint3');
	new ContentSwitcher('#checkpoint5');

	// Contact Form
	email_manager.init();
	
	// Placeholders for form
	activatePlaceholders();

	// Link hovers
	$('.more').hover(function () {
		$(this).find('img').attr('src', '/assets/images/link-active.png');
	},
	function () {
		$(this).find('img').attr('src', '/assets/images/link.png');
	});

	$('.dl').hover(function () {
		$(this).find('img').attr('src', '/assets/images/download-active.png');
	},
	function () {
		$(this).find('img').attr('src', '/assets/images/download.png');
	});

	// Media center popup
	$('#media-center').click(function () {
		
		$.fancybox.showActivity();
		$.ajax({
			type: "GET",
			url: "/media-center.php",
			success: function(data) {
				$.fancybox(data, {
					'autoDimensions'	: false,
					'overlayShow'       : false,
					'width'         	: 850,
					'height'        	: 600,
					'centerOnScroll'    : true,
					'onStart'			: toggle_sidescroll,
					'onClosed'			: toggle_sidescroll,
					'onComplete' 		: 	function() {$("#fancybox-wrap").unbind('mousewheel.fb');}
				});
			}
		});
		return false;
	});
});
