rf = {};
rf.contentSwitcher = {
	init : function(selector, destination, content){
		$(selector).find("a").click(function(e){
			$this = $(this);
			var name = $this.attr("id");
			$.ajax({
				url : '../view',
				data : {
					view : content[name]
				},
				dataType : 'html',
				type : "POST",
				success : function(data){
					$(destination).empty().append(data);
					$this.closest("ul").find("li").removeClass("selected");
					$this.parent().addClass("selected");
				}
			});
			return false;
		});
		
		var hash = window.location.hash;
		if(hash == '#security'){
			$(hash).trigger('click');
		}else if(hash == '#roadside'){
			console.log('here');
			$(hash).trigger('click');
		}else if(hash == '#response'){
			$(hash).trigger('click');
		}
	}
};
rf.form_contact = {
	init : function(){
		var $form_contact = $('#form-contact');
		$form_contact.submit(function(e){
			var $this = $(this);
			var $empty = $this.find(":input:enabled[value='']");
			if($empty.length == 0){
				var bool = rf.validate.Form($this, function(errors){
					var e = errors.join("<br/>");
					$this.find(".error").append(e);
				});
				
				return bool;
			}else{
				$this.find(".error").append("All fields are required!");
			}
			return false;
		});
		$form_contact.find("[name='type']").change(function(){
			var $this = $(this);
			var $company = $this.closest('form').find('[name="company"]');
			console.log($company);
			if($(this).val() == 'commercial'){
				$company.removeAttr("disabled").parent().slideDown();
			}else{
				$company.attr("disabled", "disabled").val('').parent().slideUp();
			}
		});
	}
};
rf.init = function(){
	$('#form-contact textarea').autoResize({
	    animateDuration : 100
	});
	
	$('#QapTcha').QapTcha({});
	
	$("#rotator").cycle({ 
		fx: 'fade',
		timeout: 5000,
		speed: 1000,
		pause: 1,
		fit: 1
	});
	
	$("#top").click(function(){
		$("body").animate({scrollTop : 0},'slow');
		return false;
	});
};
rf.validate = {
	Form : function($form, onError){
		var errors = [];
		var email = $form.find('.email');
		for(i = 0; i < email.length; i++){
			if(!this.types.isEmail(email[i].value) && email[i].value != ""){
				errors.push("Value for " + $(email[i]).siblings("label").html() + " must be a valid email address");
			}
		}
		var number = $form.find('.number');
		for(i = 0; i < number.length; i++){
			if(!this.types.isPhoneNumber(number[i].value) && number[i].value != ""){
				errors.push("Value for " + $(number[i]).siblings("label").html() + " must be in the format 555-5555");
			}
		}
		if(errors.length > 0){
			if(onError) onError(errors);
			return false;
		}
		
		return true;
		
	},
	types : {
		isEmail : function(email){
			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(email);
		},
		isPhoneNumber : function(number){
			return number.match(/^\d\d\d-\d\d\d\d$/);
		}
	}
};
$(document).ready(function(){
	rf.form_contact.init();
	
	rf.contentSwitcher.init(".content-switch", "#switch-container", {
		fleet : "products/fleet-management.php",
		security : "products/vehicle-security.php"
	});
	
	rf.init();
});
